单个正态总体均值的检验
(1)sigma已知的u检验(Z检验)
#######Z检验'''Z检验条件1.样本量大于302.数据之间彼此独立3.数据正常分布4.样本量应该相等'''#单正态总体,方差已知from scipy import statsfrom statsmodels.stats import weightstats as stestsdef test_z_single(group1,value):ztest,pval=stests.ztest(group1,value=value)print(float(ztest))print(float(pval))if pval<0.05:print("reject null hypothesis")else:print("accept null hypothesis")
######单边检验'''若为 `larger’,备选假设 H1 大于 value 值若为 `smaller’,备选假设 H1 小于 value 值'''ztest,pval=stests.ztest(group1,value=value,alternative="smaller")ztest,pval=stests.ztest(group1,value=value,alternative="larger")
#双样本Z检验,方差相等,未知def test_z_double(group1,group2,alternative):ztest,pval=stests.ztest(group1,group2,value=0,alternative=alternative)print(float(ztest))print(float(pval))if pval<0.05:print("reject null hypothesis")else:print("accept null hypothesis")#test_z_double(group1,group2,'two-sided')
2.sigma 未知的t检验
对于总体标准差未知的情况,可以把总体标准差sigma替换为样本标准差s,形成t检验统计量
from scipy.stats import ttest_1samp,ttest_rel,ttest_inddef test_t_single(var_list,mean):#方差未知ttest,pval=ttest_1samp(var_list,mean)print(ttest)if pval<0.05:print("Reject the Null Hypothesis.")else:print("Accept the Null Hypothesis.")#test_t_single(group1,15)
##独立样本t检验#前提条件正态分布,方差齐性def test_t_ind(group1,group2):ttest,pval=ttest_ind(group1,group2)print(ttest)if pval<0.05:print("Reject the Null Hypothesis.")else:print("Accept the Null Hypothesis.")#test_t_ind(group1,group2)
##配对样本t检验def test_t_double(var1,var2):#两总体方差未知ttest,pval=ttest_rel(var1,var2)print(ttest)if pval<0.05:print("Reject the Null Hypothesis.")else:print("Accept the Null Hypothesis.")#test_t_double(group1,group2)##样本大小必须相同