def pearson_r(x, y): assert len(x) == len(y) x_bar = average(x) y_bar = average(y) s_x = sqrt(s_2(x)) s_y = sqrt(s_2(y)) tmp = 0.0 for i in range(0, len(x)): tmp += ((x[i] - x_bar) * (y[i] - y_bar) / s_x / s_y) return tmp / (len(x) - 1)
def infer_by_chi(l1, l2, alpha=0.01): ss1 = s_2(l1) ss2 = s_2(l2) fv = ss1 / ss2 from scipy.stats import f res = f.ppf(1 - alpha, len(l1) - 1, len(l2) - 1) print('res {} fv {}'.format(res, fv)) if res > fv: print('sigma1 <= sigma2') else: print('sigma1 > sigma2')
def infer_by_t(l1, l2, alpha=0.01, sigma=0): n1 = len(l1) n2 = len(l2) x_aver = average(l1) y_aver = average(l2) s_1sq = s_2(l1) s_2sq = s_2(l2) s_w2 = ((n1 - 1) * s_1sq + (n2 - 1) * s_2sq) / (n1 + n2 - 2) tv = ((x_aver - y_aver) - sigma) / sqrt(s_w2 * (1 / n1 + 1 / n2)) t_a = t.ppf(alpha, n1 + n2 - 2) print('tv {} t_a {}'.format(tv, t_a)) if tv < t_a: print('mu1 < mu2') else: print('mu1 >= mu2')
def t_check(l, mu, alpha=0.05): aver = average(l) n = len(l) ss = s_2(l) tt = (aver - mu) * sqrt(n) / sqrt(ss) l, r = t.interval(1 - alpha, n - 1) if tt > r or tt < l: print('reject') else: print('accept')
import numpy as np from numpy import var, std from scipy.stats import stats from statistic.check.util import s_2, sigma_2 __author__ = 'zzt' if __name__ == '__main__': l = [420, 500, 920, 1380, 1510, 1650, 1760, 2100, 2300, 2350] print(stats.kstest(l, 'expon', [1500.0])) x = np.linspace(-15, 15, 9) print(std(x)**2) print(var(x)) print(s_2(x)) print(sigma_2(x)) print(stats.kstest(x, 'norm', [0, 9]))