【发布时间】:2018-09-21 00:18:12
【问题描述】:
我想找出服从正态分布的样本的置信区间。
为了测试代码,我首先创建了一个示例,并尝试在 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()
但是文档说这个函数会返回置信区间。
这是我期望的数字:
%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')
【问题讨论】:
-
tconfint_mean返回估计平均参数的置信区间,而不是单个观察值。 -
@user333700 哦!这就是我误解的地方。谢谢你的指出。
标签: python matplotlib jupyter statsmodels confidence-interval