【发布时间】:2019-11-01 18:28:12
【问题描述】:
均值的置信区间有如下解析解:
假设我的数据集是正态分布的,并且我不知道总体标准差,我可以使用 t-score 来计算均值的 CI。所以我做了:
from scipy import stats
import numpy as np
arr = np.array([4, 4, 1, 6, 6, 8, 1, 2, 3, 2, 2, 3, 4, 7, 6, 8, 0, 2, 8, 6, 5])
alpha = 0.05 # significance level = 5%
df = len(arr) - 1 # degress of freedom = 20
t = stats.t.ppf(1 - alpha/2, df) # 95% confidence t-score = 2.086
s = np.std(arr, ddof=1) # sample standard deviation = 2.502
n = len(arr)
lower = np.mean(arr) - (t * s / np.sqrt(n))
upper = np.mean(arr) + (t * s / np.sqrt(n))
print((lower, upper))
>>> (3.0514065531195387, 5.329545827832843)
print(stats.t.interval(1 - alpha/2, df, loc=np.mean(arr), scale=s / np.sqrt(n)))
>>> (2.8672993716475763, 5.513653009304806)
而且我使用公式手动计算的间隔与 CI 的 scipy 实现不一致。这个错误是从哪里来的?
【问题讨论】:
标签: scipy statistics confidence-interval