开始使用免费开始使用

功效分析——第 II 部分

之前,我们模拟了实验的一次实例并生成了一个 p 值。现在将用这一框架来计算统计功效。实验的功效是指:如果处理组与对照组之间确实存在差异,实验能检测出这一差异的能力。良好的统计实践通常以 80% 的功效为目标。

对于我们的网站,假设我们想知道每个版本需要有多少人访问,才能以 80% 的功效检测到停留时间提升 10%。为此,我们从较小的样本量(50)开始,模拟多次该实验并检查功效。如果达到 80% 的功效就停止;否则就增大样本量并重试。

本练习是课程的一部分

Python 中的统计模拟

查看课程

练习说明

  • time_spent 随机变量,将 size 设为元组,使其形状为 sample_size × sims
  • 将小于 0.05 的 p 值比例计算为 power(具有统计显著性)。
  • 如果 power 大于或等于 80%,则从 while 循环中 break。否则,将 sample_size 以 10 递增并继续。

交互式实操练习

通过完成这段示例代码来试试这个练习。

sample_size = 50

# Keep incrementing sample size by 10 till we reach required power
while 1:
    control_time_spent = np.random.normal(loc=control_mean, scale=control_sd, size=(____,____)))
    treatment_time_spent = np.random.normal(loc=control_mean*(1+effect_size), scale=control_sd, size=(____,____))
    t, p = st.ttest_ind(treatment_time_spent, control_time_spent)
    
    # Power is the fraction of times in the simulation when the p-value was less than 0.05
    power = (p < 0.05).sum()/____
    if ____: 
        ____
    else: 
        ____ += ____
print("For 80% power, sample size required = {}".format(sample_size))
编辑并运行代码