中心极限定理的可视化
对一个完全不是正态分布的均匀分布反复抽样求均值,观察均值分布如何趋近钟形。
学完本案例你将理解:为什么高斯分布在 AI 中无处不在——它是大量独立因素叠加的必然结果。
生活引入
全班平均身高为什么总是钟形?
测量全班 50 人的身高。单个人的身高受几百个因素影响:基因、营养、运动、睡眠……每个因素的影响有大有小、有正有负。
当很多独立的小因素叠加在一起时,最终结果的分布就是钟形——这是数学定理,不是巧合。中心极限定理说的就是:大量独立随机变量的和(或均值),趋向于正态分布。
直观理解
我们从均匀分布 U(0,1) 中抽样——这个分布完全是平的,和钟形毫无关系。但当每次抽多个样本(如 30 个)求一次均值,重复这个操作 5000 次——这 5000 个均值画出来的直方图,就开始像钟形了。n 越大,越接近完美的正态分布。
数学定义
\[ \frac{\bar{X}_n - \mu}{\sigma / \sqrt{n}} \xrightarrow{d} \mathcal{N}(0, 1) \]其中 \(\bar{X}_n = \frac{1}{n}\sum X_i\)。均值的标准差以 \(1/\sqrt{n}\) 的速度缩小。
Python 动手实践
实例
import numpy as np
np.random.seed(2)
def sample_raw(size):
return np.random.uniform(0, 1, size=size)
def clt_experiment(n, repeat=5000):
means = [sample_raw(n).mean() for _ in range(repeat)]
return np.array(means)
sample_sizes = [1, 2, 5, 30]
results = {n: clt_experiment(n) for n in sample_sizes}
print("RUNOOB 中心极限定理验证:\n")
print("原始分布: 均匀分布 U(0,1)(完全平的)")
print("理论: 均值的标准差 = 1/sqrt(12n)\n")
print(f"{'n':<6} {'均值SD':<12} {'理论值':<12} {'匹配'}")
print("-" * 42)
for n, means in results.items():
theo = 1 / np.sqrt(12 * n)
ok = "Yes" if abs(means.std() - theo) < 0.01 else "No"
print(f"{n:<6} {means.std():<12.4f} {theo:<12.4f} {ok}")
# n=30 时的正态性检验
m30 = results[30]
skew = np.mean(((m30-m30.mean())/m30.std())**3)
kurt = np.mean(((m30-m30.mean())/m30.std())**4) - 3
print(f"\nRUNOOB n=30 时:偏度={skew:.3f} (应~0), 超峰度={kurt:.3f} (应~0)")
np.random.seed(2)
def sample_raw(size):
return np.random.uniform(0, 1, size=size)
def clt_experiment(n, repeat=5000):
means = [sample_raw(n).mean() for _ in range(repeat)]
return np.array(means)
sample_sizes = [1, 2, 5, 30]
results = {n: clt_experiment(n) for n in sample_sizes}
print("RUNOOB 中心极限定理验证:\n")
print("原始分布: 均匀分布 U(0,1)(完全平的)")
print("理论: 均值的标准差 = 1/sqrt(12n)\n")
print(f"{'n':<6} {'均值SD':<12} {'理论值':<12} {'匹配'}")
print("-" * 42)
for n, means in results.items():
theo = 1 / np.sqrt(12 * n)
ok = "Yes" if abs(means.std() - theo) < 0.01 else "No"
print(f"{n:<6} {means.std():<12.4f} {theo:<12.4f} {ok}")
# n=30 时的正态性检验
m30 = results[30]
skew = np.mean(((m30-m30.mean())/m30.std())**3)
kurt = np.mean(((m30-m30.mean())/m30.std())**4) - 3
print(f"\nRUNOOB n=30 时:偏度={skew:.3f} (应~0), 超峰度={kurt:.3f} (应~0)")
RUNOOB 中心极限定理验证: 原始分布: 均匀分布 U(0,1)(完全平的) 理论: 均值的标准差 = 1/sqrt(12n) n 均值SD 理论值 匹配 ------------------------------------------ 1 0.2891 0.2887 Yes 2 0.2043 0.2041 Yes 5 0.1292 0.1291 Yes 30 0.0526 0.0527 Yes RUNOOB n=30 时:偏度=-0.023 (应~0), 超峰度=-0.005 (应~0)
AI 中的应用场景
| 场景 | 与 CLT 的联系 |
|---|---|
| Batch Normalization | 假设每个 mini-batch 内的激活值近似正态分布,做减均值除标准差 |
| 权重初始化 | Xavier/He 初始化从正态分布中采样初始权重 |
| 误差建模 | 回归问题中假设误差服从正态分布——误差是多个未建模因素叠加的结果 |
