我正在学习假设检验,并通过以下示例:
一家大型电力公司的首席执行官声称,他的 1,000,000 名客户中有 80% 对他们获得的服务非常满意。为了验证这一说法,本地报纸使用简单随机抽样调查了 100 名顾客。在抽样客户中,73%的人表示非常满意。基于这些发现,我们是否可以拒绝 CEO 的 80% 的客户非常满意的假设?使用 0.05 的显着性水平。
与 python 中的自举方法相比,使用单样本 z 检验计算 p 值时得到不同的结果。
Z 检验方法:
σ = 开方 [(0.8 * 0.2)/100] = 开方 (0.0016) = 0.04 z = (p - P)/σ = (.73 - .80)/0.04 = -1.75
双尾检验 P(z < -1.75) = 0.04,P(z > 1.75) = 0.04。
因此,P 值 = 0.04 + 0.04 = 0.08。
引导方法(Python):
一般方法是从总体(1,000,000)中随机抽取大小为 100 的样本,其中 80% 是满意的
repeat 5000 times:
take random sample of size 100 from population (1,000,000, 80% of which are satisfied)
count the number of satisfied customers in sample, and append count to list satisfied_counts
calculate number of times that a value of 73 or more extreme (<73) occurs. Divide this by the number of items in satisfied_counts
Since it's a two-tailed test, double the result to get the p-value.
使用此方法,p 值为 0.11。
代码如下:
population = np.array(['satisfied']*800000+['not satisfied']*200000) # 80% satisfied (1M population)
num_runs = 5000
sample_size = 100
satisfied_counts = []
for i in range(num_runs):
sample = np.random.choice(population, size=sample_size, replace = False)
hist = pd.Series(sample).value_counts()
satisfied_counts.append(hist['satisfied'])
p_val = sum(i <= 73 for i in satisfied_counts) / len(satisfied_counts) * 2
为什么两个结果不同?任何指向正确方向的帮助/点表示赞赏!
最佳答案
区别在于栅栏柱/舍入误差的一种形式。
正态近似表示得到 0.73 的几率大约是相应正态分布在 0.725 和 0.735 之间的几率。因此,您应该使用 0.735 作为截止值。这将使这两个数字更接近。
关于statistics - 比例测试 : Z-test vs bootstrap/permutation - different results,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54949353/