【Python】python中p-value的实现

案例:

tt = (sm-m)/np.sqrt(sv/float(n))  # t-statistic for mean
pval = stats.t.sf(np.abs(tt), n-1)*2  # two-sided pvalue = Prob(abs(t)>tt)
print 't-statistic = %6.3f pvalue = %6.4f' % (tt, pval)
t-statistic =  0.391 pvalue = 0.6955

链接:

https://stackoverflow.com/questions/17559897/python-p-value-from-t-statistic

http://docs.scipy.org/doc/scipy/reference/tutorial/stats.html

可执行代码

# coding: utf-8
from __future__ import division
import numpy as np
from scipy import stats

means = [0.0539, 4,8,3,6,9,1]
stds = [5,4,8,3,6,7,9]
mu = [0, 4.1, 7, 2, 5, 8, 0]
n = 20
output = []
for sm, std, m in zip(means, stds, mu):
    # print("value:", sm, std)
    tt = (sm-m)/(std/np.sqrt(float(n)))      # t-statistic for mean
    pval = stats.t.sf(np.abs(tt), n-1)*2  # two-sided pvalue = Prob(abs(t)>tt)
    # print('t-statistic = %6.3f pvalue = %6.4f' % (tt, pval))
    output.append(format(pval))
print("\t".join(output))

 

发布了392 篇原创文章 · 获赞 492 · 访问量 241万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览