手动计算置信区间
面试中关于置信区间常见的两类问题是:请您用通俗的话解释,或详细说明其计算方式,并可能要求您实现一个。在本练习中,您将练习后一种方式,手动构造一个置信区间,除已为您导入的包外不使用任何其他包。
我们已经为 95% 置信区间预先指定了合适的 z 分数,并将其与样本均值分别赋给了 z_score 和 sample_mean 变量,以便简化操作。
本练习是课程的一部分
用 Python 练习统计学面试题
练习说明
- 使用为您导入的
sem()函数和z_score变量,计算标准误和误差边界。 - 使用为您导入的
sample_mean变量,计算并打印置信区间的下界。 - 使用为您导入的
sample_mean变量,计算并打印置信区间的上界。
交互式实操练习
通过完成这段示例代码来试试这个练习。
from scipy.stats import sem, t
data = [1, 2, 3, 4, 5]
confidence = 0.95
# Compute the standard error and margin of error
std_err = sem(____)
margin_error = std_err * ____
# Compute and print the lower threshold
lower = sample_mean - ____
print(____)
# Compute and print the upper threshold
upper = sample_mean + ____
print(____)