[英]Sequential Sampling
To sample from N(1,2) with sample size 100 and calculating the mean of this sample we can do this:要从样本大小为 100 的 N(1,2) 中采样并计算该样本的平均值,我们可以这样做:
import numpy as np
s = np.random.normal(1, 2, 100)
mean = np.mean(s)
Now if we want to produce 10000 samples and save mean of each of them we can do:现在,如果我们想生成 10000 个样本并保存每个样本的平均值,我们可以这样做:
sample_means = []
for x in range(10000):
sample = np.random.normal(1, 2, 100)
sample_means.append (sample.mean())
How can I do it when we want to sample sequentially from N(1,2) and estimate the distribution mean sequentially?当我们想从 N(1,2) 顺序采样并顺序估计分布均值时,我该怎么做?
IIUC you meant accumulative IIUC 你的意思是累积
sample = np.random.normal(1,2,(10000, 100))
sample_mean = []
for i,_ in enumerate(sample):
sample_mean.append(sample[:i+1,:].ravel().mean())
Then sample_mean
contains the accumulative samples 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]
Maybe list comprehension?也许列表理解?
sample_means = [np.random.normal(1, 2, 100).mean() for i in range(10000)]
TIP Use lower case to name variables in Python提示在 Python 中使用小写来命名变量
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.