python - 正态分布样本的置信区间

标签 python matplotlib jupyter statsmodels confidence-interval

我想找出服从正态分布的样本的置信区间。

为了测试代码,我首先创建一个示例,并尝试在 Jupyter Notebook[python 内核] 中绘制置信区间的图片

%matplotlib notebook

import pandas as pd
import numpy as np
import statsmodels.stats.api as sms
import matplotlib.pyplot as plt

s= np.random.normal(0,1,2000)
# s= range(10,14)                   <---this sample has the right CI
# s = (0,0,1,1,1,1,1,2)             <---this sample has the right CI

# confidence interval
# I think this is the fucniton I misunderstand
ci=sms.DescrStatsW(s).tconfint_mean()

plt.figure()
_ = plt.hist(s,  bins=100)

# cnfidence interval left line
one_x12, one_y12 = [ci[0], ci[0]], [0, 20]
# cnfidence interval right line
two_x12, two_y12 = [ci[1], ci[1]], [0, 20]

plt.plot(one_x12, one_y12, two_x12, two_y12, marker = 'o')

绿线和黄线应该是置信区间。但他们没有处于正确的位置。

我可能会误解这个函数:

sms.DescrStatsW(s).tconfint_mean()

但是文档说这个函数将返回置信区间。

enter image description here

这是我期望的数字:

%matplotlib notebook

import pandas as pd
import numpy as np
import statsmodels.stats.api as sms
import matplotlib.pyplot as plt

s= np.random.normal(0,1,2000)


plt.figure()
_ = plt.hist(s,  bins=100)
# cnfidence interval left line
one_x12, one_y12 = [np.std(s, axis=0) * -1.96, np.std(s, axis=0) * -1.96], [0, 20]
# cnfidence interval right line
two_x12, two_y12 = [np.std(s, axis=0) * 1.96, np.std(s, axis=0) * 1.96], [0, 20]

plt.plot(one_x12, one_y12, two_x12, two_y12, marker = 'o')

enter image description here

最佳答案

问题看起来像“有什么函数可以计算置信区间”。

由于给定的数据服从正态分布,因此可以简单地通过以下方式完成

ci = scipy.stats.norm.interval(0.95, loc=0, scale=1)

0.95 是 alpha 值,它指定 95 个百分位点,因为公式中给出了相应的平均值的 1.96 个标准差。 (https://en.wikipedia.org/wiki/1.96)

loc=0 指定平均值,scale=1 指定西格玛。 (https://en.wikipedia.org/wiki/68%E2%80%9395%E2%80%9399.7_rule)

您可以查看@bogatron的回答以获取有关Compute a confidence interval from sample data的更多详细信息。


以下代码生成您想要的图。为了再现性,我播种了随机数。

import pandas as pd
import numpy as np
import statsmodels.stats.api as sms
import matplotlib.pyplot as plt
import scipy

s = np.random.seed(100)
s= np.random.normal(0,1,2000)

plt.figure()
_ = plt.hist(s,  bins=100)

sigma=1
mean=0
ci = scipy.stats.norm.interval(0.95, loc=mean, scale=sigma)
print(ci)

# cnfidence interval left line
one_x12, one_y12 = [ci[0],ci[0]], [0, 20]
# cnfidence interval right line
two_x12, two_y12 = [ci[1],ci[1]], [0, 20]

plt.plot(one_x12, one_y12, two_x12, two_y12, marker = 'o')

ci 返回

(-1.959963984540054, 1.959963984540054)

这是情节。

enter image description here

关于python - 正态分布样本的置信区间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49769546/

相关文章:

python - matplotlib 堆叠条形图 AssertionError : incompatible sizes: argument 'bottom' must be length 3 or scalar

python - 如何获取正在使用的 ipython 笔记本的文件路径? (相当于 __file__)

javascript - 使用javascript提交一个django表单

python - PyUSB/Python 2.7 的类似 USB 功能

python - matplotlib 中刻度线的方向

python - 如何在 mac 10.7.5 上正确安装 matplotlib?还有为什么Anaconda安装成功后找不到命令 "conda"呢?

python - 根据日期时间列创建一天中的小时和日期列

python - 如何删除 Robobrowser 中的 AttributeError

python - @jupyterlab/vega6-extension 在哪里?

python - Jupyter Notebook : Import . ipynb 文件并在其他 .ipynb 文件中访问它的方法给出错误