无偏估计与自由度

不记得当初是怎么学概率论和数理统计的了。最近总是遇到一个小问题,想不通为什么样本方差的无偏估计量是要除以N-1的。

上Wiki找了一下,

Estimating variance

Suppose X1, ..., Xn are independent and identically distributed random variables with expectation μ and variance σ? Let

\overline{X}=(X_1+\cdots+X_n)/n

be the "sample average", and let

S^2=\frac{1}{n}\sum_{i=1}^n(X_i-\overline{X}\,)^2

be a "sample variance". Then S?is a "biased estimator" of σ?because

\operatorname{E}(S^2)=\frac{n-1}{n}\sigma^2\neq\sigma^2.

Note that when a transformation is applied to an unbiased estimator, the result is not necessarily itself an unbiased estimate of its corresponding population statistic. That is, for a non-linear functionf and an unbiased estimator U of a parameter pf(U) is usually not an unbiased estimator of f(p). For example the square root of the unbiased estimator of the population variance is not an unbiased estimator of the population standard deviation.

Bias is not the only consideration when choosing a statistic, however. Bias refers to the central tendency of the sampling distribution of a statistic, but the variance of the sampling distribution can also be an important consideration. Specifically, statistics with smaller sampling variances will yield greater statistical power. For example, while S?above is more biased than the traditional sample calculation

S_\mathrm{sample}^2=\frac{1}{n-1}\sum_{i=1}^n(X_i-\overline{X}\,)^2,

S?has a lower estimation variability than S?sub>sample because the denominator dividing the sum of squares is larger in the calculation of S? resulting in a smaller scale of final values, and therefore lower estimation variability, than that of S?sub>sample. Practically, this demonstrates that for some applications (where the amount of bias can be equated between groups/conditions) it is possible that a biased estimator can prove to be a more powerful, and therefore useful, statistic.

自由度(degree of freedom, df)是指当以样本的统计量来估计总体的参数时,样本中独立或能自由变化的数据的个数称为该统计量的自由度。

例如,在估计总体的平均数时,样本中的n个数全部加起来,其中任何一个数都和其他数据相独立,从其中抽出任何一个数都不影响其他数据(这也是随机抽样所要求的)。因此一组数据中每一个数据都是独立的,所以自由度就是估计总体参数时独立数据的数目,而平均数是根据n个独立数据来估计的,因此自由度为n。

但是为什么用样本估计总体的方差时,方差的自由度就是(n-1)?

s2= å(X-m)2/n

从此公式我们可以看出总体的方差是由各数据与总体平均数的差值求出来的,因此必须将m固定后才可以求总体的方差。因此,由于m被固定,它就不能独立自由变化,也就是方差受到总体平均数的限制,少了一个自由变化的机会,因此要从n里减掉一个。

假设一个样本有两个数值,X1=10,X2=20,我们现在要用这个样本估计总体的方差,则样本的平均数是:

Xm=å X/n=(10+20)/2=15

现在假设我们已知Xm=15,X1=10,根据公式Xm=å X/n,则有:

X2=2Xm-X1=2×15-10=20

由此我们可以知道在有两个数据样本中,当平均数的值和其中一个数据的值已知时,另一个数据的值就不能自由变化了,因此这个样本的自由度就减少一个,变成了(n-1)。依此类推:在一组数据中,当其平均数和前面的数据都已知时,最后一个数据就被固定而不能独立变化了,因此这个样本能够独立自由变化的数目就是(n-1)个.

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在Python中,可以使用SciPy库中的t分布函数来计算自由度为10时的置信区间。具体步骤如下: 1. 导入相关库: ```python import numpy as np from scipy.stats import t import matplotlib.pyplot as plt ``` 2. 生成数据: ```python data = np.random.randn(100) # 生成100个随机数 ``` 3. 计算置信区间: ```python n = len(data) # 样本数量 m = np.mean(data) # 样本均值 s = np.std(data, ddof=1) # 样本标准差 alpha = 0.05 # 置信水平 df = n - 1 # 自由度 t_value = t.ppf(1 - alpha/2, df) # t分布值 lower_bound = m - t_value * s / np.sqrt(n) upper_bound = m + t_value * s / np.sqrt(n) ``` 其中,`ddof`参数指定计算样本标准差时使用无估计,即除以`n-1`而非`n`。 4. 绘制置信区间图: ```python plt.hist(data, bins=20, alpha=0.5, density=True) # 绘制直方图 x = np.linspace(m - 4*s, m + 4*s, 1000) # 生成x轴数据 y = t.pdf(x, df) # 计算t分布的概率密度函数值 plt.plot(x, y, 'r-', label='t distribution') # 绘制t分布曲线 plt.axvline(lower_bound, color='g', linestyle='--', label='lower bound') # 绘制置信区间下限 plt.axvline(upper_bound, color='g', linestyle='--', label='upper bound') # 绘制置信区间上限 plt.legend() plt.show() ``` 完整代码如下: ```python import numpy as np from scipy.stats import t import matplotlib.pyplot as plt data = np.random.randn(100) # 生成100个随机数 n = len(data) # 样本数量 m = np.mean(data) # 样本均值 s = np.std(data, ddof=1) # 样本标准差 alpha = 0.05 # 置信水平 df = n - 1 # 自由度 t_value = t.ppf(1 - alpha/2, df) # t分布值 lower_bound = m - t_value * s / np.sqrt(n) upper_bound = m + t_value * s / np.sqrt(n) plt.hist(data, bins=20, alpha=0.5, density=True) # 绘制直方图 x = np.linspace(m - 4*s, m + 4*s, 1000) # 生成x轴数据 y = t.pdf(x, df) # 计算t分布的概率密度函数值 plt.plot(x, y, 'r-', label='t distribution') # 绘制t分布曲线 plt.axvline(lower_bound, color='g', linestyle='--', label='lower bound') # 绘制置信区间下限 plt.axvline(upper_bound, color='g', linestyle='--', label='upper bound') # 绘制置信区间上限 plt.legend() plt.show() ``` 运行代码可以得到一个类似于下图的置信区间图: ![置信区间图](https://i.imgur.com/TGJzX4Z.png)

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值