Python实现平均数抽样分布
时间: 2024-10-29 22:04:09 浏览: 19
Python中实现平均数抽样分布通常涉及到统计学中的中心极限定理,它指出当样本量足够大时,样本均值的分布近似于正态分布,无论总体分布是什么样的。你可以使用numpy库中的random模块来模拟这个过程。
首先,你需要导入必要的库,然后创建一个函数来生成大量的样本均值。这里是一个简单的示例:
```python
import numpy as np
def sample_mean_distribution(n_samples, n_trials, population_mean, population_std_dev):
sample_means = []
for _ in range(n_trials):
# 从正态分布中随机抽取n_samples个样本
samples = np.random.normal(population_mean, population_std_dev, size=n_samples)
# 计算样本均值并添加到列表中
sample_means.append(np.mean(samples))
# 将样本均值转换为数组并计算频率分布或直方图
histogram, bin_edges = np.histogram(sample_means, bins='auto')
return sample_means, histogram
# 示例参数设置
population_mean = 0
population_std_dev = 1
n_samples = 50
n_trials = 10000
sample_means, distribution = sample_mean_distribution(n_samples, n_trials, population_mean, population_std_dev)
mean_of_sample_means = np.mean(sample_means)
print(f"Sample means and their frequency:")
print(distribution)
print(f"\nMean of the sample means: {mean_of_sample_means}")
```
在这个例子中,`sample_mean_distribution`函数会返回每次试验的样本均值以及它们的频数分布。你可以通过改变`n_trials`来观察随着更多样本的增加,均值分布是如何接近正态分布的。
阅读全文