02.中心极限定理验证
""" 中心极限定理指的是给定一个任意分布的总体。 我每次从这些总体中随机抽取 n 个抽样,一共抽 m 次。 然后把这 m 组抽样分别求出平均值/和。 这些平均值/和的分布接近正态分布。 """ import numpy as np import matplotlib.pyplot as plt # 1 给定任意分布的总体 population = np.random.randint(0, 100, 100000) # 2 随机抽取m个样本,抽取n次 def sample_choice(population, sample_num, sample_size): samples = [] for i in range(sample_num): sample = [] for j in range(sample_size): unit = np.random.choice(population) sample.append(unit) samples.append(sample) return samples # 3 计算样本均值 def samples_mean(samples): sample_mean = [] for i in range(len(samples)): sample_mean.append(np.mean(samples[i])) return sample_mean # 4 中心定理结果 def central_limit_theorem(): # 1.任意分布总体 population = np.random.randint(0, 100, 100000) # 2.抽样 samples = sample_choice(population, 1000, 50) # 3.求样本均值 sample_mean = samples_mean(samples) plt.hist(sample_mean, bins=30) plt.show() if __name__ == "__main__": central_limit_theorem()