95% 置信区间的含义

从 2,000 家企业中抽取 30 家、重复 100 次调查;未覆盖平均气候风险得分的 95% 置信区间以红色显示。

95% 置信区间的含义

“95% 置信”是方法的性质,而不是某一个区间的性质。这里 2,000 家企业构成的总体完全已知,因此其平均得分就是已知的真值。同样的小型调查——随机、不放回地抽取 30 家企业——重复 100 次,每条水平线是围绕样本均值得到的 95% t 区间。其中约 95 个区间覆盖真实均值(虚线);未覆盖的少数几个以红色绘制。区间宽度也各不相同,因为每个样本都要估计自己的标准差。

背景为 Kouloukoui、de Marcellis-Warin、Armellini、Warin 和 Andrade Torres (2021),Journal of Cleaner Production,研究碳密集型行业企业如何感知自身面临的气候风险。感知得分(0–100,右偏)的总体由 numpy(固定种子)模拟,仅作示例,并非该论文的数据或结果。区间使用 scipy.stats.t.interval。由于得分分布偏斜,均值较低的样本往往离散程度也较小,因此未覆盖的区间大多落在真实均值之下;此外,一轮 100 次调查仅因偶然就可能比 95% 高或低几个百分点。

代码及图中文字均为英文。

confidence-intervals.py · 35 行
下载 GitHub
import matplotlib.pyplot as plt
import numpy as np
from scipy import stats

# Simulated data, set in the context of Kouloukoui et al. (2021) [kouloukoui2021]
# Population: climate-risk perception scores (0–100) of 2,000 firms; 100 surveys of n = 30
rng = np.random.default_rng(2021)
score = 100 * rng.beta(2, 4.5, 2000)
mu, n, reps = score.mean(), 30, 100
samples = np.array([rng.choice(score, n, replace=False) for _ in range(reps)])
means = samples.mean(axis=1)
se = samples.std(axis=1, ddof=1) / np.sqrt(n)
lo, hi = stats.t.interval(0.95, df=n - 1, loc=means, scale=se)
miss = (lo > mu) | (hi < mu)

fig, ax = plt.subplots(figsize=(8, 6.6))
y = np.arange(1, reps + 1)
colors = np.where(miss, "#FF3B30", "#7FB8FF")
ax.hlines(y, lo, hi, color=colors, lw=np.where(miss, 2.2, 1.2))
ax.scatter(means, y, s=np.where(miss, 16, 9), color=colors, zorder=3, lw=0)
ax.vlines(mu, 0, reps + 1.5, color="#1D1D1F", ls="--", lw=1.2, zorder=4)
ax.text(mu, reps + 2.5, f"population mean score μ = {mu:.1f}", ha="center", va="bottom", fontsize=9.5,
        color="#1D1D1F")

ax.set_ylim(0, reps + 6)
ax.set_yticks([1, 25, 50, 75, 100])
ax.set_ylabel("Survey (random sample of firms)")
ax.set_xlabel("Mean climate-risk perception score with 95% CI (0–100)")
ax.grid(axis="y", visible=False); ax.grid(axis="x")
ax.margins(x=0.08)
ax.set_title(f"{reps - miss.sum()} of {reps} surveys' intervals capture the true mean score", pad=26)
ax.text(0, 1.02, f"Observed coverage {100 * (1 - miss.mean()):.0f}% (nominal 95%) · "
        f"each survey n = {n} of {len(score):,} firms (simulated) · t-intervals, {miss.sum()} misses in red",
        transform=ax.transAxes, fontsize=9.5, color="#6E6E73")
plt.show()

图表使用了可选的 PyStatlas 样式:添加 plt.style.use("pystatlas.mplstyle") 即可完全还原其外观。

数据来源

  • Simulated data. Simulated data with a fixed random seed, created for this example.
    No real respondents, documents or measurements: the numbers only illustrate how the chart and its statistics work.
  • Kouloukoui et al. (2021), perceived exposure to climate risks. Kouloukoui, D., de Marcellis-Warin, N., Armellini, F., Warin, T., & Andrade Torres, E. (2021). Factors influencing the perception of exposure to climate risks: Evidence from the world's largest carbon-intensive industries. Journal of Cleaner Production, 306, 127160. https://doi.org/10.1016/j.jclepro.2021.127160
    The example is set in the context of this paper; unless its description says otherwise, the numbers are simulated and are not the paper's data or results.

这些示例用于说明统计方法。除条目另有说明外,其数值均为模拟数据,图表标题描述的是该图在这些模拟数据中呈现的内容——既不是所引论文的结论,也不是作者对实质问题的立场。关于结果、背景和数据,请以此处引用的论文为准。

如何引用

PyStatlas 由 Thierry Warin 创建并维护。如果它对你的工作有所帮助,敬请引用。

Warin, T. (2026). What a 95% confidence interval means. In PyStatlas: A Python Gallery of Statistical Graphics for the Social Sciences (Version 1.1.0) [Computer software]. https://warin.ca/pystatlas/inference-uncertainty/confidence-intervals/

@misc{warin2026statlas,
  author       = {Warin, Thierry},
  title        = {PyStatlas: A Python Gallery of Statistical Graphics for the Social Sciences},
  year         = {2026},
  version      = {1.1.0},
  howpublished = {\url{https://warin.ca/pystatlas/}},
  note         = {Source code: https://github.com/warint/pystatlas}
}

更多推断与不确定性图表