计算 python 中平均值的 95% 置信区间
Calculating 95 % confidence interval for the mean in python
我几乎不需要帮助。如果我有 30 个随机样本,均值为 52,方差为 30,那么我如何计算估计方差和真实方差为 30 的均值的 95% 置信区间。
在这里您可以结合 numpy and statsmodels 的力量来开始:
要生成均值为 52、方差为 30 的正态分布浮点数,您可以使用 numpy.random.normal 和 numbers = np.random.normal(loc=52, scale=30, size=30)
,其中参数为:
Parameters
----------
loc : float
Mean ("centre") of the distribution.
scale : float
Standard deviation (spread or "width") of the distribution.
size : int or tuple of ints, optional
Output shape. If the given shape is, e.g., ``(m, n, k)``, then
``m * n * k`` samples are drawn. Default is None, in which case a
single value is returned.
这里是均值的 95% 置信区间 DescrStatsW.tconfint_mean:
import statsmodels.stats.api as sms
conf = sms.DescrStatsW(numbers).tconfint_mean()
conf
# output
# (36.27, 56.43)
编辑 - 1
但这不是全部……根据您的样本量,您应该使用 Z 分数而不是 sms.DescrStatsW(numbers).tconfint_mean()
此处使用的 t 分数。而且我有一种感觉,经验法则阈值是 30 并且您在问题中有 30 个观察结果并非巧合。 Z vs t
还取决于您是否 知道 总体标准偏差或必须依赖样本的估计值。这些计算方式也不同。看看here。如果这是您希望我进一步解释和演示的内容,我很乐意在周末再看一遍。
我几乎不需要帮助。如果我有 30 个随机样本,均值为 52,方差为 30,那么我如何计算估计方差和真实方差为 30 的均值的 95% 置信区间。
在这里您可以结合 numpy and statsmodels 的力量来开始:
要生成均值为 52、方差为 30 的正态分布浮点数,您可以使用 numpy.random.normal 和 numbers = np.random.normal(loc=52, scale=30, size=30)
,其中参数为:
Parameters ---------- loc : float Mean ("centre") of the distribution. scale : float Standard deviation (spread or "width") of the distribution. size : int or tuple of ints, optional Output shape. If the given shape is, e.g., ``(m, n, k)``, then ``m * n * k`` samples are drawn. Default is None, in which case a single value is returned.
这里是均值的 95% 置信区间 DescrStatsW.tconfint_mean:
import statsmodels.stats.api as sms
conf = sms.DescrStatsW(numbers).tconfint_mean()
conf
# output
# (36.27, 56.43)
编辑 - 1
但这不是全部……根据您的样本量,您应该使用 Z 分数而不是 sms.DescrStatsW(numbers).tconfint_mean()
此处使用的 t 分数。而且我有一种感觉,经验法则阈值是 30 并且您在问题中有 30 个观察结果并非巧合。 Z vs t
还取决于您是否 知道 总体标准偏差或必须依赖样本的估计值。这些计算方式也不同。看看here。如果这是您希望我进一步解释和演示的内容,我很乐意在周末再看一遍。