功效分析 - 第 I 部分
现在我们来进行功效分析。通常,您希望确保任何实验或 A/B 测试的统计功效至少为 80%。一种方法是计算达到 80% 功效所需的样本量。
假设您负责一个新闻媒体网站,并希望提高用户在网站上的停留时间。当前,用户停留时间服从正态分布,平均值为 1 分钟,标准差为 0.5 分钟。您计划上线一个更快加载页面的功能,想知道为了检测网站停留时间提升 5% 所需的样本量。
在本练习中,我们将搭建运行一次模拟的框架,运行一次t 检验,并计算 p 值。
本练习是课程的一部分
Python 中的统计模拟
练习说明
- 将
effect_size初始化为 5%,control_mean为 1,control_sd为 0.5。 - 使用
np.random.normal(),基于您初始化的值各模拟一次control_time_spent和treatment_time_spent。 - 使用
st.ttest_ind()(其中st为已导入的scipy.stats)对treatment_time_spent和control_time_spent进行 t 检验。 - 若
p_value小于 0.05,则统计显著性stat_sig应为True,否则为False。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Initialize effect_size, control_mean, control_sd
effect_size, sample_size, control_mean, control_sd = ____, 50, ____, ____
# Simulate control_time_spent and treatment_time_spent, assuming equal variance
control_time_spent = np.random.normal(loc=control_mean, scale=____, size=sample_size)
treatment_time_spent = np.random.normal(loc=____*(1+effect_size), scale=control_sd, size=____)
# Run the t-test and get the p_value
t_stat, p_value = st.ttest_ind(____, ____)
stat_sig = p_value < ____
print("P-value: {}, Statistically Significant? {}".format(p_value, stat_sig))