Skip to content

Instantly share code, notes, and snippets.

@amankharwal
Created November 11, 2020 07:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amankharwal/19e0584d3aa11482b1f4263b273edafc to your computer and use it in GitHub Desktop.
Save amankharwal/19e0584d3aa11482b1f4263b273edafc to your computer and use it in GitHub Desktop.
import numpy as np
import scipy.stats as stats
x = np.array([12,13,14,19,21,23])
y = np.array([12,13,14,19,21,23,45])
def grubbs_test(x):
n = len(x)
mean_x = np.mean(x)
sd_x = np.std(x)
numerator = max(abs(x-mean_x))
g_calculated = numerator/sd_x
print("Grubbs Calculated Value:",g_calculated)
t_value = stats.t.ppf(1 - 0.05 / (2 * n), n - 2)
g_critical = ((n - 1) * np.sqrt(np.square(t_value))) / (np.sqrt(n) * np.sqrt(n - 2 + np.square(t_value)))
print("Grubbs Critical Value:",g_critical)
if g_critical > g_calculated:
print("From grubbs_test we observe that calculated value is lesser than critical value, Accept null hypothesis and conclude that there is no outliers\n")
else:
print("From grubbs_test we observe that calculated value is greater than critical value, Reject null hypothesis and conclude that there is an outliers\n")
grubbs_test(x)
grubbs_test(y)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment