样本均值的变异性
根据中心极限定理,大随机样本均值的概率分布大致为正态。钟形曲线以总体均值为中心。一些样本均值更高,一些更低,但偏离总体均值的偏差在两侧大致对称,正如我们反复看到的。形式上,概率理论表明样本均值是总体均值的无偏估计。
在我们的模拟中,我们还注意到较大样本的均值往往比较小样本的均值更紧密地聚集在总体均值周围。在本节中,我们将量化样本均值的变异性,并建立变异性与样本量之间的关系。
from datascience import *
import numpy as np
path_data = '../../../assets/data/'
%matplotlib inline
import matplotlib.pyplot as plots
plots.style.use('fivethirtyeight')
让我们从我们的航班延误表格开始。平均延误约为16.7分钟,延误分布向右偏斜。
united = Table.read_table(path_data + 'united_summer2015.csv')
delay = united.select('Delay')
pop_mean = np.mean(delay.column('Delay'))
pop_mean
16.658155515370705delay.hist(bins=np.arange(-20, 300, 10))
plots.scatter(pop_mean, -0.0008, marker='^', color='darkblue', s=60)
plots.ylim(-0.004, 0.04);
Histogram with 'Delay' in the x-axis and 'Percent per unit' on the y-axis. The histogram has its tallest bar just to the left of x-0, then a quick decrease with bars visible until about x=200, however the x-axis extends out to x=300. There is a triangle just to the right of x=0 under the histogram.现在让我们抽取随机样本,并观察样本均值的概率分布。和往常一样,我们将使用模拟来获得该分布的经验近似。
我们将定义一个函数 simulate_sample_mean 来实现这一点,因为我们之后会改变样本量。参数包括:表格名称、包含变量的列标签、样本量和模拟次数。
"""Empirical distribution of random sample means"""
def simulate_sample_mean(table, label, sample_size, repetitions):
means = make_array()
for i in range(repetitions):
new_sample = table.sample(sample_size)
new_sample_mean = np.mean(new_sample.column(label))
means = np.append(means, new_sample_mean)
sample_means = Table().with_column('Sample Means', means)
# Display empirical histogram and print all relevant quantities
sample_means.hist(bins=20)
plots.xlabel('Sample Means')
plots.title('Sample Size ' + str(sample_size))
print("Sample size: ", sample_size)
print("Population mean:", np.mean(table.column(label)))
print("Average of sample means: ", np.mean(means))
print("Population SD:", np.std(table.column(label)))
print("SD of sample means:", np.std(means))
让我们模拟100个延误的随机样本的均值,然后是400个延误,最后是625个延误的均值。我们将对每个过程执行10,000次重复。xlim 和 ylim 行设置所有图形中一致的坐标轴,以便于比较。你可以忽略每个单元格中的这两行代码。
simulate_sample_mean(delay, 'Delay', 100, 10000)
plots.xlim(5, 35)
plots.ylim(0, 0.25);
Sample size: 100
Population mean: 16.658155515370705
Average of sample means: 16.672836
Population SD: 39.480199851609314
SD of sample means: 3.92467202924066
Histogram titled 'Sample Size 100' with 'Sample Means' on the x-axis and 'Percent per unit' on the y-axis. The histogram is roughly symmetric about approx. x=16 and extends from about x=5 to x=27. The x-axis continues until x=35.simulate_sample_mean(delay, 'Delay', 400, 10000)
plots.xlim(5, 35)
plots.ylim(0, 0.25);
Sample size: 400
Population mean: 16.658155515370705
Average of sample means: 16.678091499999997
Population SD: 39.480199851609314
SD of sample means: 1.9474592014668113
Histogram titled 'Sample Size 400' with 'Sample Means' on the x-axis and 'Percent per unit' on the y-axis. The height of the peak is about twice as tall as in the previous histogram and centered around x=17. The histogram ranges from about x=10 to x=23.simulate_sample_mean(delay, 'Delay', 625, 10000)
plots.xlim(5, 35)
plots.ylim(0, 0.25);
Sample size: 625
Population mean: 16.658155515370705
Average of sample means: 16.649224
Population SD: 39.480199851609314
SD of sample means: 1.5883338034053167
Histogram with 'Sample Size 625' and 'Sample Means' on the x-axis and 'Percent per unit' on the y-axis. The height of the peak of the histogram at approx x=17 is even taller than the previous histogram and it is again less wide, from about x=12 to x=22. 你可以看到中心极限定理在起作用——样本均值的直方图大致为正态,尽管延误本身的直方图远非正态。
你还可以看到,三个样本均值直方图中的每一个都中心在非常接近总体均值的位置。在每种情况下,“样本均值的平均值”都非常接近16.66分钟,即总体均值。两个值都在每个直方图上方的打印输出中提供。正如预期,样本均值是总体均值的无偏估计。
所有样本均值的标准差
你还可以看到,随着样本量的增加,直方图变得更窄,因此更高。我们以前见过这种情况,但现在我们将更仔细地关注分散程度的度量。
所有延误的总体的SD约为40分钟。
pop_sd = np.std(delay.column('Delay'))
pop_sd
39.480199851609314看看上面样本均值直方图中的SD。在三个直方图中,延误总体的SD都是约40分钟,因为所有样本均来自同一个总体。
现在看看当样本量为100时,所有10,000个样本均值的SD。该SD约为总体SD的十分之一。当样本量为400时,所有样本均值的SD约为总体SD的二十分之一。当样本量为625时,样本均值的SD约为总体SD的二十五分之一。
将样本均值经验分布的SD与“总体SD除以样本量的平方根”这个量进行比较,似乎是个好主意。
以下是数值。对于第一列中的每个样本量,抽取了10,000个该大小的随机样本,并计算了10,000个样本均值。第二列包含那10,000个样本均值的SD。第三列包含计算“总体SD除以样本量的平方根”的结果。
该单元格需要一段时间运行,因为它是一个大型模拟。但你很快就会看到等待是值得的。
repetitions = 10000
sample_sizes = np.arange(25, 626, 25)
sd_means = make_array()
for n in sample_sizes:
means = make_array()
for i in np.arange(repetitions):
means = np.append(means, np.mean(delay.sample(n).column('Delay')))
sd_means = np.append(sd_means, np.std(means))
sd_comparison = Table().with_columns(
'Sample Size n', sample_sizes,
'SD of 10,000 Sample Means', sd_means,
'pop_sd/sqrt(n)', pop_sd/np.sqrt(sample_sizes)
)
sd_comparison
Sample Size n | SD of 10,000 Sample Means | pop_sd/sqrt(n)
25 | 7.94482 | 7.89604
50 | 5.6131 | 5.58334
75 | 4.57417 | 4.55878
100 | 3.98687 | 3.94802
125 | 3.49769 | 3.53122
150 | 3.22776 | 3.22354
175 | 3.00675 | 2.98442
200 | 2.77764 | 2.79167
225 | 2.64268 | 2.63201
250 | 2.49447 | 2.49695
... (15 rows omitted)第二列和第三列中的值非常接近。如果我们将这些列中的每一列以样本量为横轴绘制,两条图线基本上无法区分。
sd_comparison.plot('Sample Size n')
A line graph with 'Sample Size n' on the x-axis. Two lines are labeled, but are overlapping enough that you can't really see a difference between them. In dark blue is 'SD of 10,000 Sample Means' and in gold is 'pop_sd/sqrt(n).' The line decreases rapidly before beginning to even out, but still decreasing.确实有两条曲线。但它们彼此如此接近,看起来仿佛只有一条。
我们看到的是一个普遍结果的一个实例。请记住,上图是基于每个样本量的10,000次重复。但每个大小的样本远不止10,000个。样本均值的概率分布基于固定大小的所有可能样本的均值。
固定样本量。 如果样本是从总体中有放回随机抽取的,那么
$$ {\mbox{所有可能样本均值的SD}} ~=~ \frac{\mbox{总体SD}}{\sqrt{\mbox{样本量}}} $$
这是所有可能被抽取的样本平均值的标准差。它大致衡量了样本均值与总体均值的偏离程度。
样本均值的中心极限定理
如果你从总体中有放回地抽取一个大随机样本,那么无论总体的分布如何,样本均值的概率分布大致为正态,以总体均值为中心,SD等于总体SD除以样本量的平方根。
样本均值的准确性
所有可能样本均值的SD衡量了样本均值的变异性。因此,它被视为样本均值作为总体均值估计的准确性的度量。SD越小,估计越准确。
该公式表明: - 总体大小不影响样本均值的准确性。公式中任何地方都没有出现总体大小。 - 总体SD是一个常数;对于从总体中抽取的每个样本,它是相同的。样本量可以变化。因为样本量出现在分母中,样本均值的变异性随着样本量的增加而减小,因此准确性提高。
平方根定律
从SD比较表中可以看出,25个航班延误的随机样本的均值的SD约为8分钟。如果你将样本量乘以4,就会得到100的样本量。所有这些样本均值的SD约为4分钟。这比8分钟小,但不是4倍小;它只有2倍小。这是因为分母中的样本量有一个平方根。样本量增加了4倍,但SD下降了 $2 = \sqrt{4}$ 倍。换句话说,准确性提高了 $2 = \sqrt{4}$ 倍。
一般来说,当你将样本量乘以某个倍数时,样本均值的准确性会提高该倍数的平方根倍。
因此,要将准确性提高10倍,你必须将样本量乘以100倍。准确性来之不易!