对正态总体参数的单侧假设检验,可以用如下的p值法进行。设显著水平为,考虑假设的右侧检验。首先,注意到检验统计量的分布对应显著水平的右分位点,实际上就是其残存函数()在该点处的函数值恰为,即。我们知道在假设的右侧检验中,的拒绝域为。若检验统计量观测值落在拒绝域内,则必有。而若检验统计量观测值落在非拒绝域内,则应有,如下图所示。
相仿地,对假设的左侧检验而言,设检验统计量的分布对应显著水平的右侧分位点为,则分布函数。若检验统计量观测值满足,则落在的非拒绝域内,而若检验统计量观测值满足,则落在的拒绝域内,如下图所示。
已知正态总体方差的情况下,对总体均值作显著水平为的假设检验,采用Z检验法。单侧检验的计算步骤为
1.构造检验统计量观测值;
2.计算对应于的累积分布函数值(左侧检验)或残存函数值(右侧检验);
3.比较结果为真,则接受假设,否则拒绝假设。
将此算法编写如下计算总体均值单侧假设检验的Python函数。
from scipy.stats import norm #导入norm
def ztestL(z, alpha): #左侧检验函数
p=norm.cdf(z) #F(z)
return p>=alpha #检验
def ztestR(z, alpha): #右侧检验函数
p=norm.sf(z) #S(z)
return p>=alpha #检验
程序的第2~4行定义Z方法左侧检验函数ztestL,第5~7行定义右侧检验函数ztestR。两个函数函数的参数z和alpha分别表示检测统计量观测值和显著水平。返回的布尔值或为True,则接受假设(或),否则拒绝。
例1 公司从生产商购买牛奶。公司怀疑生产商在牛奶中掺水以牟利。通过测定牛奶的冰点,可以检验出牛奶是否掺水。天然牛奶的冰点温度近似服从,牛奶掺水可使冰点温度升高而接近于水的冰点温度(C)。测得生产商提交的5批牛奶的冰点温度,其均值为C,问是否可以认为生产商在牛奶中掺了水()?
解: 按题意,需对假设,即牛奶未掺水进行右侧检验。下列代码完成本例计算。
import numpy as np #导入numpy
xmean=-0.535 #样本均值
s0=0.008 #总体均方差
mu0=-0.545 #总体均值假设值
n=5 #样本容量
alpha=0.05 #显著水平
z=(xmean-mu0)/(s0/np.sqrt(n)) #检验统计量
accept=ztestR(z, alpha) #右侧检验
print('mu<=%.3f is %s.'%(mu0, accept))
第2~6行设置各项数据。第7行计算检验统计量观测值,第8行调用函数ztestR对假设作右侧检验。运行程序,输出
mu<=-0.545 is False.
表示拒绝假设,即拒绝假设:牛奶未掺水。
例2 要求一种元件平均使用寿命不得低于1000h。生产者从一批这种元件中随机抽取25件,测得其寿命的平均值为950h。已知该种元件寿命服从标准差h的正态分布。试在显著水平下判断这批元件是否合格。
解: 按题意,需要对假设,即寿命不低于1000小时进行左侧检验。下列代码完成本例计算。
import numpy as np
xmean=950
sigma=100
mu0=1000
n=25
alpha=0.05
z=(xmean-mu0)/(s0/np.sqrt(n))
accept=ztestL(z, alpha)
print('mu>=%d is %s.'%(mu0, accept))
本例计算的是对假设的左侧检验,故第8行调用函数ztestL执行计算。运行程序,输出
mu>=1000 is False.
即拒绝假设,认为本批次产品不合格。
写博不易,敬请支持:
如果阅读本文于您有所获,敬请点赞、评论、收藏,谢谢大家的支持!
代码诚可贵,原理价更高。若为AI学,读正版书好。