<p>从正态分布的68%置信区间
平均mu和标准偏差sigma为</p>
<pre><code>stats.norm.interval(0.68, loc=mu, scale=sigma)
</code></pre>
<p>N的平均值的68%置信区间来自正态分布
平均mu和标准偏差sigma为</p>
<pre><code>stats.norm.interval(0.68, loc=mu, scale=sigma/sqrt(N))
</code></pre>
<p>直觉上,这些公式是有道理的,因为如果你举起一罐果冻豆,让很多人猜猜果冻豆的数量,每一个个体都可能有很大的偏离——同样的标准差<code>sigma</code>——但是猜测的平均值在估计实际数量方面会做得非常好,这反映在平均收缩的标准差乘以系数<code>1/sqrt(N)</code>上。</p>
<hr/>
<p>如果一次抽签有方差<code>sigma**2</code>,那么通过<a href="http://en.wikipedia.org/wiki/Variance#Sum_of_uncorrelated_variables_.28Bienaym.C3.A9_formula.29" rel="noreferrer">Bienaymé formula</a>,不相关的<code>N</code><em>抽签的和有方差<code>N*sigma**2</code>。</p>
<p>平均值等于和除以N。当你用一个随机变量(如和)乘以一个常数时,方差乘以常数的平方。那就是</p>
<pre><code>Var(cX) = c**2 * Var(X)
</code></pre>
<p>所以均值的方差等于</p>
<pre><code>(variance of the sum)/N**2 = N * sigma**2 / N**2 = sigma**2 / N
</code></pre>
<p>所以平均值的标准差(方差的平方根)等于</p>
<pre><code>sigma/sqrt(N).
</code></pre>
<p>这是分母中<code>sqrt(N)</code>的原点。</p>
<hr/>
<p>下面是一些基于Tom代码的示例代码,它演示了上述声明:</p>
<pre><code>import numpy as np
from scipy import stats
N = 10000
a = np.random.normal(0, 1, N)
mean, sigma = a.mean(), a.std(ddof=1)
conf_int_a = stats.norm.interval(0.68, loc=mean, scale=sigma)
print('{:0.2%} of the single draws are in conf_int_a'
.format(((a >= conf_int_a[0]) & (a < conf_int_a[1])).sum() / float(N)))
M = 1000
b = np.random.normal(0, 1, (N, M)).mean(axis=1)
conf_int_b = stats.norm.interval(0.68, loc=0, scale=1 / np.sqrt(M))
print('{:0.2%} of the means are in conf_int_b'
.format(((b >= conf_int_b[0]) & (b < conf_int_b[1])).sum() / float(N)))
</code></pre>
<p>印刷品</p>
<pre><code>68.03% of the single draws are in conf_int_a
67.78% of the means are in conf_int_b
</code></pre>
<p>注意,如果使用<code>mean</code>和<code>sigma</code>的估计值定义<code>conf_int_b</code>
基于样本<code>a</code>,平均值可能不会在<code>conf_int_b</code>中下降
频率。</p>
<hr/>
<p>如果从分布中抽取样本并计算
样本平均值和标准偏差</p>
<pre><code>mean, sigma = a.mean(), a.std()
</code></pre>
<p>注意不能保证这些
等于<em>总体</em>平均值和标准差,我们假设</em>
人口是正态分布的——这些不是自动给予的!</p>
<p>如果你要取一个样本,并想估计人口的平均值和标准值
偏差,你应该使用</p>
<pre><code>mean, sigma = a.mean(), a.std(ddof=1)
</code></pre>
<p>因为sigma的这个值是总体标准差的<a href="http://en.wikipedia.org/wiki/Bias_of_an_estimator" rel="noreferrer">unbiased estimator</a>。</p>