Visualization of the Central Limit Theorem
Visualization of the Central Limit Theorem
By repeatedly sampling the mean of a uniform distribution that is not normally distributed, observe how the mean distribution approaches a bell shape.
After studying this case, you will understand: why the Gaussian distribution is ubiquitous in AI—it is the inevitable result of the superposition of numerous independent factors.
Introduction to life
**Why is the average height of the entire class always bell-shaped?
Measure the height of 50 students in the class. The height of an individual is affected by hundreds of factors: genes, nutrition, exercise, sleep... Each factor has a large or small impact, and a positive or negative effect.
When many independent small factors accumulate, the distribution of the final result is bell-shaped—this is a mathematical theorem, not a coincidence. The Central Limit Theorem states that the sum (or mean) of a large number of independent random variables tends towards a normal distribution.
Intuitive understanding
We sample from a uniform distribution U(0,1)—this distribution is completely flat and has nothing to do with a bell shape. But when we calculate the mean of multiple samples (e.g., 30) and repeat this operation 5000 times—the histogram of these 5000 means starts to look bell-shaped. The larger n is, the closer it gets to a perfect normal distribution.
Mathematical definition
[ \frac{\bar{X}_n - \mu}{\sigma / \sqrt{n}} \xrightarrow{d} \mathcal{N}(0, 1) ]
Where (\bar{X}_n = \frac{1}{n}\sum X_i$). The standard deviation of the mean shrinks at a rate of (1/\sqrt{n}).
Python hands-on practice
Example
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
}
(
"RUNOOB Central Limit Theorem verification:\n"
)
(
"Original distribution: Uniform distribution U(0,1) (perfectly flat)"
)
(
"Theory: standard deviation of the mean = 1/sqrt(12n)\n"
)
(
f
"{'n':<6} {'MeanSD':<12} {'TheoreticalValue':<12} {'Match'}"
)
(
"-"
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}"
)
Normality test when n=30
m30
=
results
[
30
]
skew
=
np.
mean
(
(
(
m30-m30.
mean
(
)
)
/m30.
std
(
)
)
**
3
)
kurt
=
np.
mean
(
(
(
m30-m30.
mean
(
)
)
/m30.
std
(
)
)
**
4
)
3
(
f
"\nRUNOOB n=30: skewness={skew:.3f} (should be ~0), excess kurtosis={kurt:.3f} (should be ~0)"
)
Original prompt (Chinese, left untranslated)
RUNOOB Central Limit Theorem Verification:
原始分布: 均匀分布 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)
Application scenarios in AI
| Scenarios | Connection with CLT |
|---|---|
| Batch Normalization | Assume that the activation values within each mini-batch are approximately normally distributed, then subtract the mean and divide by the standard deviation |
| Weight Initialization | Xavier/He initialization samples initial weights from a normal distribution |
| Error Modeling | In regression problems, assume that the error follows a normal distribution—the error is the result of the superposition of multiple unmodeled factors |