均值的置信区间具有以下解析解:
假设我的数据集是正态分布的,并且我不知道总体标准差,我可以使用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)
并且使用该方程手动计算的间隔I与CI的scipy实现不一致。这个错误来自哪里?
发布于 2019-06-19 06:25:15
你的显著性水平是0.05,所以置信水平是0.95。将该值传递给stats.t.interval
。不要除以2;函数会帮你除以2:
In [62]: print(stats.t.interval(1 - alpha, df, loc=np.mean(arr), scale=s / np.sqrt(n)))
(3.0514065531195387, 5.329545827832843)
https://stackoverflow.com/questions/56660487
复制相似问题