顺序采样

Sequential Sampling

要从样本大小为 100 的 N(1,2) 中抽样并计算该样本的平均值,我们可以这样做:

import numpy as np

s = np.random.normal(1, 2, 100)
mean = np.mean(s)

现在如果我们想要生成 10000 个样本并保存每个样本的平均值,我们可以这样做:

sample_means = []
for x in range(10000):
    sample = np.random.normal(1, 2, 100)
    sample_means.append (sample.mean())

当我们想从 N(1,2) 中顺序采样并顺序估计分布均值时,我该怎么办?

IIUC 你说的是累计

sample = np.random.normal(1,2,(10000, 100))
sample_mean = []
for i,_ in enumerate(sample):
    sample_mean.append(sample[:i+1,:].ravel().mean())

sample_mean包含累计样本均值

sample_mean[:10]
[1.1185342714036368,
 1.3270808654923423,
 1.3266440422140355,
 1.2542028664103761,
 1.179358517854582,
 1.1224645540064788,
 1.1416887857272255,
 1.1156887336750463,
 1.0894328800573165,
 1.0878896099712452]

也许列表理解?

sample_means = [np.random.normal(1, 2, 100).mean() for i in range(10000)]

TIP Python

中使用小写命名变量