Created
March 21, 2012 08:46
-
-
Save JaHIY/2145652 to your computer and use it in GitHub Desktop.
Python: Grubbs, 4d, Q
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python | |
# -*- coding: utf-8 -*- | |
''' | |
1.Grubbs | |
python svalues.py 1.25 1.27 1.31 1.40 1.40 0.05 'Grubbs' | |
python svalues.py data(0) data(1) data(2) ... data(n) (n>=2) 可疑值x 显著性水平a(可用值为 0.05/0.025/0.01) 'Grubbs' | |
2.4d | |
python svalues.py 1.25 1.27 1.31 1.40 1.40 '4d' | |
python svalues.py data(0) data(1) data(2) ... data(n) (n>=2) 可疑值x '4d' | |
3.Q | |
python svalues.py 1.25 1.27 1.31 1.40 1.40 0.90 'Q' | |
python svalues.py data(0) data(1) data(2) ... data(n) (n>=2) 可疑值x 置信度t(可用值为 0.90/0.96/0.99) 'Q' | |
''' | |
import math | |
def grubbs(*args): | |
a = args[-2] | |
x = args[-3] | |
args = list(args[:-3]) | |
args.sort() | |
n = len(args) | |
#print(n) | |
#print(args[0]) | |
avg = round(float(sum(args))/float(n), 5) | |
s = round(float(math.sqrt(sum(pow(arg-avg, 2) for arg in args)/float(n-1))), 5) | |
t = round((x-avg)/s, 5) | |
t = (x == args[0]) and (-1)*t | |
T_list = ((1.15, 1.15, 1.15), (1.46, 1.48, 1.49), (1.67, 1.71, 1.75), (1.82, 1.89, 1.94), (1.94, 2.02, 2.10), (2.03, 2.13, 2.22), (2.11, 2.21, 2.32), (2.18, 2.29, 2.41), (2.23, 2.36, 2.48), (2.29, 2.41, 2.55), (2.33, 2.46, 2.61), (2.37, 2.51, 2.63), (2.41, 2.55, 2.71), (2.56, 2.71, 2.88)) | |
A_dict = { | |
0.05: 0, | |
0.025: 1, | |
0.01: 2 | |
} | |
A = A_dict[a] | |
T = T_list[n-3][A] | |
#print(T) | |
j = "Delete" if t > T else "Retain" | |
return "Arithmetic Mean: %.5f, Standard Deviation: %.5f, T=%.5f, You should %s %.5f (Grubbs)" % (avg, s, t, j, x) | |
def _4d(*args): | |
x = args[-2] | |
args = list(args[:-2]) | |
#print(args) | |
args.remove(x) | |
n = len(args) | |
avg = round(float(sum(args))/float(n), 5) | |
s = round(float(math.sqrt(sum(pow(arg-avg, 2) for arg in args)/float(n-1))), 5) | |
d = abs(x-avg) | |
#print(args) | |
#print(avg) | |
#print(d) | |
j = "Delete" if d > 4*s else "Retain" | |
return "Delta = %.5f, You should %s %.5f (4d)" % (d, j, x) | |
def _q(*args): | |
t = args[-2] | |
x = args[-3] | |
args = list(args[:-3]) | |
n = len(args) | |
args.sort() | |
i = args.index(x) | |
q = round(float(args[1]-args[0]/float((args[-1]-args[0])), 5) if i == 0 else round(float((args[i]-args[i-1])/float(args[-1]-args[0])), 5) | |
Q_list = ((0.94, 0.98, 0.99), (0.76, 0.85, 0.93), (0.64, 0.73, 0.82), (0.56, 0.64, 0.74), (0.51, 0.59, 0.68), (0.47, 0.54, 0.63), (0.44, 0.51, 0.60), (0.41, 0.48, 0.57)) | |
T_dict = { | |
0.90: 0, | |
0.96: 1, | |
0.99: 2 | |
} | |
T = T_dict[t] | |
Q = Q_list[n-3][T] | |
j = "Delete" if q > Q else "Remain" | |
return "Q = %.5f, You should %s %.5f (Q)" % (q, j, x) | |
def solve(*args): | |
method = args[-1] | |
#print(method) | |
if method == "Grubbs": | |
result = grubbs(*args) | |
elif method == "4d": | |
result = _4d(*args) | |
elif method == "Q": | |
result = _q(*args) | |
return result | |
if __name__ == '__main__': | |
import sys | |
#print(sys.argv[1:]) | |
argv = [float(arg) for arg in sys.argv[1:-1]] | |
argv.append(sys.argv[-1]) | |
print(argv) | |
result = solve(*argv) | |
print(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment