- 單個正態總體均值的檢驗
(1)sigma已知的u檢驗(Z檢驗)
#######Z檢驗
'''Z檢驗條件
1.樣本量大於30
2.數據之間彼此獨立
3.數據正常分佈
4.樣本量應該相等
'''
#單正態總體,方差已知
from scipy import stats
from statsmodels.stats import weightstats as stests
def 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_ind
def 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)##樣本大小必須相同