【问题标题】:Confidence interval of mean - scipy implementation doesn't agree with mathematic formula均值的置信区间 - scipy 实现与数学公式不符
【发布时间】: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


    【解决方案1】:

    您的显着性水平为 0.05,因此置信水平为 0.95。将该值传递给stats.t.interval。不要除以 2;该功能会为您执行此操作:

    In [62]: print(stats.t.interval(1 - alpha, df, loc=np.mean(arr), scale=s / np.sqrt(n)))              
    (3.0514065531195387, 5.329545827832843)
    

    【讨论】:

      猜你喜欢
      • 2018-01-22
      • 1970-01-01
      • 2019-06-05
      • 1970-01-01
      • 1970-01-01
      • 2019-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多