The Normal Density
The bell curve appears whenever many small independent effects add up: measurement errors, heights, test scores, the sum of many dice. The central limit theorem explains why. This lesson covers its density, its standardization and the rules of thumb for its tails. Reading normal tables and z-scores is covered in the Statistics course.
The density
The standard normal has density
with mean 0 and variance 1. A normal variable with mean and standard deviation is , with density . The constant comes from the Gaussian integral .
The CDF has no formula in elementary functions, but Python computes it through the error function: .
import math
Phi = lambda z: 0.5 * (1 + math.erf(z / math.sqrt(2)))
for k in [1, 2, 3, 4, 6]:
inside = Phi(k) - Phi(-k)
print(f"within {k} sd: {inside:.6f} outside: {1 - inside:.2e}")
print("97.5% quantile ~", 1.96, "->", round(Phi(1.96), 4))within 1 sd: 0.682689 outside: 3.17e-01
within 2 sd: 0.954500 outside: 4.55e-02
within 3 sd: 0.997300 outside: 2.70e-03
within 4 sd: 0.999937 outside: 6.33e-05
within 6 sd: 1.000000 outside: 1.97e-09
97.5% quantile ~ 1.96 -> 0.975
This is the 68–95–99.7 rule: about 68% of values lie within one standard deviation, 95% within two, 99.7% within three. "Six sigma" corresponds to about 2 in a billion outside.
Standardization
To find probabilities for with mean and standard deviation , convert to . If response times are normal with mean 120 ms and standard deviation 15 ms, the fraction above 150 ms is .
Sums of normals
If and are independent normals, is normal with mean and variance (see Sums of Continuous Variables). Standard deviations do not add; variances do.
import numpy as np
rng = np.random.default_rng(1)
x = rng.normal(10, 3, 500_000)
y = rng.normal(5, 4, 500_000)
s = x + y
print("mean of sum:", round(s.mean(), 3), " sd of sum:", round(s.std(), 3), " predicted sd:", np.sqrt(3 ** 2 + 4 ** 2))
print("fraction within 1 sd:", round((np.abs(s - 15) < 5).mean(), 4))mean of sum: 15.001 sd of sum: 4.996 predicted sd: 5.0
fraction within 1 sd: 0.6832
Generating normal samples
The Box–Muller transform turns two independent uniforms into two independent standard normals:
It works because, in polar coordinates, two independent normals have an exponentially distributed squared radius and a uniform angle.
import math
import random
rng = random.Random(5)
zs = []
for _ in range(100_000):
u1, u2 = 1 - rng.random(), rng.random()
r = math.sqrt(-2 * math.log(u1))
zs += [r * math.cos(2 * math.pi * u2), r * math.sin(2 * math.pi * u2)]
m = sum(zs) / len(zs)
v = sum((z - m) ** 2 for z in zs) / len(zs)
print("mean", round(m, 4), " variance", round(v, 4))
print("within 1, 2, 3 sd:", [round(sum(abs(z) < k for z in zs) / len(zs), 4) for k in (1, 2, 3)])mean 0.0007 variance 1.0035
within 1, 2, 3 sd: [0.6818, 0.9543, 0.9974]
In software
Neural network weights are initialised from scaled normals (Xavier/He initialization), diffusion models add and remove Gaussian noise, and Gaussian processes model unknown functions. Monitoring uses "3 sigma" alert thresholds, which only make sense if the metric is roughly normal; latencies usually are not (they have long right tails), which is why percentiles are preferred.
Exercises
Exercise 1: Response times
Response times are normal with mean 120 ms and standard deviation 15 ms. What fraction lies between 105 and 150 ms?
Answer
About .
Solution
from to : .
Exercise 2: Sum of two parts
Two parts have independent normal lengths with means 40 mm and 60 mm and standard deviations 0.3 mm and 0.4 mm. What is the standard deviation of the assembled length?
Answer
0.5 mm.
Solution
Variances add: , so the standard deviation is .
Exercise 3: Quantile by bisection (code)
Find with by bisection using math.erf.
Answer
.
Solution
is increasing, so bisection on converges.
import math
Phi = lambda z: 0.5 * (1 + math.erf(z / math.sqrt(2)))
lo, hi = 0.0, 5.0
for _ in range(60):
mid = (lo + hi) / 2
if Phi(mid) < 0.99:
lo = mid
else:
hi = mid
print(round(lo, 4))2.3263
Test yourself
About what fraction of a normal distribution lies within 2 standard deviations of the mean?
- 68%
- 90%
- 95%
- 99.7%
If and are independent normals with standard deviations 3 and 4, the standard deviation of is…
- 7
- 5
- 12
- 1