Law of large numbers and central limit theorem: why averages behave so predictably
The sample mean, the law of large numbers and the central limit theorem with animations — why the normal distribution is everywhere, where the ±3% polling error comes from and why an estimate twice as precise costs four times as much data.
Series AI basics Part 18 of 52
- Map of artificial intelligence: what is what and how it all fits together
- A brief history of artificial intelligence: from Turing to language models
- Data, model, training, inference: the basic terms on a single example
- How to read mathematical notation: symbols, indices, sums and quantifiers
- Sets and relations: the building blocks of definitions in AI
- Functions and mappings: why every AI model is a function
- Logic and proofs: the rules AI reasons by
- Graphs and trees: the structure that both search and neural networks stand on
- Linear algebra for machine learning: vectors, matrices and why AI mostly multiplies
- Derivatives, the gradient and the chain rule: how a model finds out which way to learn
- Gradient descent: how a model learns
- What probability is: from rolling dice to the Kolmogorov axioms
- Conditional probability and Bayes’ theorem: how to update your estimate when you learn something new
- Random variables and the cumulative distribution function: one function that tells you everything about a distribution
- Probability density and probability mass functions: why a continuous variable has zero probability at a point
- Expected value, variance and covariance: where a distribution lies, how wide it is and what goes with what
- Overview of probability distributions: Bernoulli, binomial, Poisson, uniform, normal and exponential
- Law of large numbers and central limit theorem: why averages behave so predictably
- Statistics and parameter estimation: maximum likelihood, MAP and confidence intervals
- Information theory: entropy, cross-entropy and KL divergence — where loss functions come from
- State-space search: BFS, DFS, UCS and A* with animations
- Games and adversaries: minimax, alpha-beta pruning and Monte Carlo tree search
- Constraint satisfaction: backtracking, constraint propagation and AC-3 on sudoku and a map of Czech regions
- Logic and knowledge systems: forward and backward chaining, resolution and expert systems
- Planning: STRIPS, the blocks world, heuristics from relaxation and planning in AI agents
- Local search and simulated annealing: a travelling salesman through the Czech regional capitals
- Genetic algorithms: population, crossover, mutation and selection on a backpack for a hike
- Evolution strategies and genetic programming: from the one-fifth rule to Kepler’s law
- Ant colony optimization: how a pheromone trail finds a short path
- Particle swarm optimisation: inertia, personal experience and the swarm’s experience
- Neuroevolution: evolving the weights and shape of neural networks, and the NEAT algorithm
- Fuzzy sets and membership functions: how to write down “somewhat warm”
- Fuzzy logic and probability: why “somewhat warm” does not mean “probably warm”
- Fuzzy logic: t-norms, implications and IF–THEN rules
- Fuzzy control: Mamdani and Sugeno from a fan to a pole on a cart
- Fuzzy clustering: the fuzzy c-means algorithm and points between clusters
- Neuro-fuzzy systems (ANFIS): fuzzy rules that learn from data
- Markov chains: the transition matrix, the stationary distribution and PageRank
- Hidden Markov models: the Viterbi algorithm and the dishonest casino
- Bayesian networks: why the grass is wet and what it means for the sprinkler
- The Kalman filter: how to estimate where a car is and how fast it goes from imprecise measurements
- Types of machine learning: supervised, unsupervised, semi-supervised and more
- Prompts, context and RAG: how to give a model the right information
- Variational autoencoders (VAE): new images from a continuous latent space
- Generative adversarial networks (GAN): the forger versus the detective
- Diffusion models: how pure noise becomes an image
- Markov decision processes: states, actions, rewards and the Bellman equations
- Q-learning and DQN: learning the value of actions, from a table to a neural network
- Policy gradient and PPO: learning the policy directly
- How an AI agent works: a language model in a loop with tools
- Programming with AI agents: the task, tests and review
- Evaluating and securing AI systems: how to measure and how to keep watch
Full series syllabus 52 of 83 topics published
- Why the sample mean approaches the expected value and why its fluctuation shrinks only with the square root of the amount of data.
- What the central limit theorem says and why the average of many variables is almost always normally distributed.
- How to use it to calculate the error of an estimate — in polls, in model evaluation and in gradient descent.
Useful to know first: Overview of probability distributions: Bernoulli, binomial, Poisson, uniform, normal and exponential
A single roll of a die is pure chance. But the average of ten thousand rolls is almost certainly 3.5 — and with a precision you can calculate in advance. This part is about two theorems that turn the chaos of individual trials into predictability: the law of large numbers tells you where the average is heading, and the central limit theorem tells you how it fluctuates around that target. Opinion polls, quality control, A/B tests and the training of neural networks all rest on them.
It builds on the parts Expected value and variance and Overview of distributions.
1. Sample mean
Definition — independent and identically distributed variables
Let (Ω, 𝓕, P) be a probability space and X1, X2, … : Ω → ℝ a finite or infinite sequence of random variables. For x ∈ ℝ, let FXi(x) = P(Xi ≤ x) denote the distribution function of Xi (more on it in the section on convergence below). The variables are independent and identically distributed (i.i.d.) if and only if both conditions hold:
- independence: for every n ≥ 1 (at most the length of the sequence) and all x1, …, xn ∈ ℝ, P(X1 ≤ x1 ∧ … ∧ Xn ≤ xn) = P(X1 ≤ x1) · … · P(Xn ≤ xn);
- identical distribution: for all indices i, j and every x ∈ ℝ, FXi(x) = FXj(x).
The notation P(X1 ≤ x1 ∧ … ∧ Xn ≤ xn) is short for P({ω ∈ Ω : X1(ω) ≤ x1 ∧ … ∧ Xn(ω) ≤ xn}). Condition 1 thus says that knowing the values of some of the variables does not change the probabilities of the values of the others.
You repeat the same experiment over and over under the same conditions, and one result does not influence another: rolls of the same die, answers from randomly chosen people, measurements with the same instrument.
Definition — sample mean
Let (Ω, 𝓕, P) be a probability space, n ∈ ℕ, n ≥ 1, and X1, …, Xn : Ω → ℝ random variables. Their sample mean is the random variable X̄n : Ω → ℝ,
X̄n(ω) = X1(ω) + … + Xn(ω)/n = 1/n ∑i=1n Xi(ω), ω ∈ Ω.
For every outcome ω it is thus the ordinary arithmetic mean of the numbers X1(ω), …, Xn(ω).
The ordinary average of the measured values. Mind the difference: the expected value μ is a property of the distribution, one fixed number. The sample mean is calculated from data, and because the data are random, it is a random variable too — it comes out a little differently with every new experiment.
Theorem — expected value and variance of the mean
Let n ∈ ℕ, n ≥ 1, and let X1, …, Xn be independent and identically distributed random variables with finite expected value E[Xi] = μ ∈ ℝ and finite variance Var(Xi) = σ² (σ ≥ 0). Then
E[X̄n] = μ, Var(X̄n) = σ²/n, and the standard deviation of X̄n is σ/√n.
The average aims at the right target, and its fluctuation shrinks as you add data — but only with the square root. A single die has σ ≈ 1.71, the average of 100 dice only 0.171. To make an estimate twice as precise, you need four times as much data. The theorem follows directly from linearity and from adding the variances of independent variables, both from the previous parts.
Proof and example — the variance of the average of dice
E[X̄n] = 1/n · (E[X1] + … + E[Xn]) = 1/n · nμ = μ (linearity)
Var(X̄n) = (1/n)2 · (Var X1 + … + Var Xn) = nσ2/n2 = σ2/n (a constant comes out of the variance squared, the variances of independent variables add up)
A single die: μ = 1 + 2 + … + 6/6 = 3.5, E[X2] = 1 + 4 + 9 + 16 + 25 + 36/6 = 91/6
σ2 = 91/6 − 3.52 = 35/12 ≈ 2.917, σ ≈ 1.708
The average of 100 dice: σ/√100 = 1.708/10 ≈ 0.171
The whole theorem rests on the fact that the factor 1/n gets squared to 1/n2 in the variance, whereas the sum of the variances grows only like n. For dependent variables the covariances would be added to the sum and the fluctuation would shrink more slowly.
2. Law of large numbers
Theorem — (weak) law of large numbers
Let (Ω, 𝓕, P) be a probability space and X1, X2, … : Ω → ℝ independent and identically distributed random variables with expected value E[Xi] = μ ∈ ℝ and finite variance Var(Xi) = σ² < ∞. Let X̄n be the sample mean of the first n of them. Then
∀ε > 0 : limn→∞ P(|X̄n − μ| ≥ ε) = 0.
(We say that X̄n converges to μ in probability. The statement holds even without the assumption of finite variance — Khinchin’s law of large numbers — but the proof is more involved.)
However small a tolerance ε you choose, with enough data the probability that the average lies farther than ε from the true expected value becomes vanishingly small. The average of the data is a good estimate of the expected value — and the more data, the better.
The proof fits on one line: by Chebyshev's inequality, P(|X̄n − μ| ≥ ε) ≤ σ²/nε², and that goes to zero as n grows. For a die, a tolerance of 0.1 and 10,000 rolls, the bound is 2.917/10,000 · 0.1² = 2.917/100 ≈ 0.029 (colours as in the formula below).
P(|X̄n − μ| ≥ ε) ≤ σ²/n · ε²
- |X̄n − μ| ≥ ε
- “the average missed” — it missed the true expected value by at least the chosen tolerance
- σ²
- the variance of a single trial — the wilder the individual data, the larger the chance of a miss we have to allow for
- n
- the number of data points — the variance of the average is σ²/n, which is why the bound falls to zero as n grows; that is the whole law of large numbers
- ε²
- the tolerance squared — Chebyshev compares the variance (a “squared” quantity) with the square of the distance; a stricter tolerance costs quadratically more data
The bound can also be used the other way round — how many rolls are enough for the average to miss by less than 0.1 with probability at least 95%? It suffices that 2.917/n · 0.01 ≤ 0.05, i.e. n ≥ 2.917/0.0005 ≈ 5833.3 — 5834 rolls will do. But Chebyshev is cautious: it holds for every distribution, and so the number actually needed is usually much smaller. Exactly how much smaller is what the central limit theorem tells you.
After ten rolls the averages of the four runs lie between 3.4 and 4.1; after 10,000 rolls, between 3.496 and 3.502. The funnel μ ± 2σ/√n narrows exactly as the theorem on the variance of the mean predicts — and the lines stray outside it only rarely. Why 2σ in particular, and why “only rarely”, is what the next theorem explains.
The law of large numbers does not say that chance “evens out”. When a coin lands heads five times in a row, tails is not “due” — the coin has no memory. The average approaches 0.5 not because the surplus of heads gets cancelled out, but because the next thousand tosses dilute it.
3. Convergence in distribution
Definition — distribution function
Let (Ω, 𝓕, P) be a probability space and Y : Ω → ℝ a random variable. The distribution function of Y is the function FY : ℝ → [0, 1],
FY(x) = P(Y ≤ x) = P({ω ∈ Ω : Y(ω) ≤ x}), x ∈ ℝ.
Every distribution function
- is non-decreasing: x ≤ y ⇒ FY(x) ≤ FY(y);
- is right-continuous: limt→x+ FY(t) = FY(x) for every x ∈ ℝ;
- satisfies limx→−∞ FY(x) = 0 and limx→+∞ FY(x) = 1,
and it determines the distribution of Y completely. The distribution function of the standard normal distribution N(0, 1) is denoted Φ.
Example: for a single die F(3) = 3/6 = 0.5 and F(3.7) is also 0.5 — between whole numbers F is flat, and at each of the points 1, …, 6 it jumps by 1/6.
Definition — convergence in distribution
Let Y, Y1, Y2, … be random variables with distribution functions FY, FY1, FY2, … (they need not be defined on the same probability space — only their distributions matter). Denote
C(FY) = {x ∈ ℝ : FY is continuous at the point x}.
The sequence Y1, Y2, … converges in distribution to the variable Y, written Yn →d Y, if and only if
∀x ∈ C(FY) : limn→∞ FYn(x) = FY(x).
If Y has the distribution N(0, 1), we also write Yn →d N(0, 1); since Φ is continuous everywhere, this means limn→∞ FYn(x) = Φ(x) for every x ∈ ℝ.
It is not that the values Yn approach some number, but that the shape of their distribution approaches the shape of another distribution. This is how the binomial distribution approached the Poisson distribution in the previous part.
Example — why points of discontinuity are exempt
Let Yn be equal to 1/n with certainty (i.e. Yn(ω) = 1/n for every ω ∈ Ω) and Y equal to 0 with certainty. The values clearly approach zero, so we would like Yn to converge to Y. But
FYn(0) = P(1/n ≤ 0) = 0 for every n, while FY(0) = P(0 ≤ 0) = 1.
For every x ≠ 0, FYn(x) and FY(x) agree from some n on (both 0 for x < 0, both 1 for x > 0 and n > 1/x).
The only problematic point is 0 — and that is exactly where FY has a jump. That is why the definition leaves points of discontinuity out.
4. Central limit theorem
Theorem — central limit theorem
Let (Ω, 𝓕, P) be a probability space and X1, X2, … : Ω → ℝ independent and identically distributed random variables with expected value E[Xi] = μ ∈ ℝ and variance Var(Xi) = σ², where 0 < σ² < ∞. For n ≥ 1 let X̄n be the sample mean of the first n of them and
Zn = X̄n − μ/σ/√n.
Then Zn →d N(0, 1), i.e.
∀z ∈ ℝ : limn→∞ P(Zn ≤ z) = Φ(z).
Whatever the distribution of the individual trials — uniform, skewed, with several peaks — the average of many of them is approximately normally distributed, with expected value μ and standard deviation σ/√n. That is why the bell curve is everywhere: whenever many small independent effects add up. The Galton board from the previous part is exactly this.
X̄n − μ/σ/√n →d N(0, 1)
- X̄n − μ
- the error of the average — it shifts the distribution so that its centre is at zero; X̄n on its own would, by the law of large numbers, shrink to the point μ and no shape would be left
- σ/√n
- the standard deviation of the average — dividing by it “magnifies” the error by exactly as much as a large n shrank it, so the variance always stays 1 and the shape is visible
- →d
- convergence in distribution — what approaches is the shape of the distribution, not the values
- N(0, 1)
- the target is always the same, regardless of the distribution of the Xi — of the original distribution only μ and σ survive, and we have already subtracted and divided them out
The distribution of a single die is flat — no bell at all. The average of two dice is a triangle, the average of five already resembles a bell, and the average of thirty is almost impossible to tell apart from a normal distribution. The bars are the exact distribution, computed by enumerating every possibility; the dashed line is the normal approximation:
It works even for a strongly skewed distribution — such as waiting for an e-mail, which follows an exponential distribution. A single wait is very asymmetric (skewness 2), the average of five waits less so, and the average of thirty is almost symmetric (skewness 0.37):
Definition — skewness
Let X be a random variable with finite expected value E[X] = μ, standard deviation σ, where 0 < σ < ∞, and finite third moment E[|X|3] < ∞. The skewness of X is the number
γ = E[(X − μ)3]/σ3.
The third power keeps the sign of the deviation, so for a symmetric distribution the deviations up and down cancel out and γ = 0. A long tail to the right gives γ > 0. Dividing by σ3 makes skewness a unitless number — it does not matter whether you measure the wait in minutes or in seconds.
Exp(λ): γ = 2 for every λ. The average of n independent copies: γn = γ/√n
n = 5: 2/√5 ≈ 0.89, n = 30: 2/√30 ≈ 2/5.477 ≈ 0.37
Skewness thus vanishes only with the square root of n — with strongly skewed data, the central limit theorem needs more observations than with symmetric data.
How good is the approximation? Take a concrete question: what is the probability that the average of 30 die rolls is greater than 4? The exact calculation over all possibilities gives 0.0488. The central limit theorem says: 4 is 4 − 3.5/1.708/√30 ≈ 1.60 standard deviations above the mean (colours as in the breakdown of the theorem above), so P ≈ 1 − Φ(1.60) ≈ 0.0544. Without a single sum over 630 possibilities, and with an error below six thousandths.
Example — the average of 30 dice above 4, step by step
σ/√30 = 1.708/5.477 ≈ 0.3118
z = 4 − 3.5/0.3118 ≈ 1.604, P(X̄30 > 4) ≈ 1 − Φ(1.604) ≈ 1 − 0.9456 = 0.0544
Exactly: an average above 4 means a sum of at least 121, and adding up the probabilities of the sums 121 to 180 gives 0.0488.
Most of the difference is caused by the fact that the sum of dice is a whole number, whereas the normal distribution is continuous: it assigns different probabilities to “above 120” and “at least 121”, even though they are the same event. The usual continuity correction puts the boundary halfway between 120 and 121, i.e. at the average 120.5/30 ≈ 4.017:
z = 120.5/30 − 3.5/0.3118 = 0.5167/0.3118 ≈ 1.657, 1 − Φ(1.657) ≈ 0.0488
The colours match the terms of the breakdown of the central limit theorem above. With the correction, the approximation agrees with the exact result to four decimal places.
5. What it is good for: the error of an estimate
The central limit theorem says that an estimate based on the average lies within 1.96 · σ/√n of the true value in 95% of cases. This gives the most widely used formula in statistics:
m = 1.96 · √p(1 − p)/n
- 1.96
- how many standard deviations to include so that 95% lies inside: Φ(1.96) ≈ 0.975, so 2.5% is left above and 2.5% below; for 99% it would be 2.58
- p(1 − p)
- the variance of a single yes/no answer — the Bernoulli variance from the previous part; it is largest, 0.25, for p = 0.5
- n
- the number of respondents or test examples — under the square root, which is why half the error costs four times as much data
- An opinion poll with 1,000 respondents and support of around 50% has an error of 1.96 · √0.25/1,000 ≈ ±3.1 percentage points (colours as in the formula above). That is why polls ask about a thousand people — to halve the error, they would need four thousand.
- Model accuracy measured as 90% on 1,000 test examples is in reality 90% ± 1.9 percentage points. A model scoring 91% on a test set of the same size is therefore not necessarily better.
Example — the error of a poll, the number of people needed and comparing two models
Poll: 1.96 · √0.5 · 0.5/1,000 = 1.96 · √0.00025 = 1.96 · 0.01581 ≈ 0.031
Model: 1.96 · √0.9 · 0.1/1,000 = 1.96 · 0.00949 ≈ 0.019
The inverted formula n = 1.962 · p(1 − p)/m2: for m = 0.03 it gives 3.8416 · 0.25/0.0009 ≈ 1067.1, i.e. 1068 people
The difference between two models (90% and 91%, each on a different set of 1,000 examples): the variances of independent estimates add up, 1.96 · √0.09/1,000 + 0.0819/1,000 ≈ 0.026
The colours match the terms of the formula for m above. The measured difference of 1 percentage point is smaller than the 2.6 points that chance alone can produce — hence “not necessarily better”. On the same test set the errors of the two models are dependent and the calculation is different (a paired test), but the conclusion tends to be similar.
Where statistics gets σ from when it does not know it, and what exactly that interval means, is the topic of the next part, on statistics and estimation.
6. When the theorems do not hold
- Infinite variance or no expected value. The Cauchy distribution has such heavy tails that it has no expected value at all — and the average of n Cauchy variables has exactly the same distribution as a single one. More data does not help in the slightest. City sizes, fortunes and stock market crashes tend to be similarly wild. (The Cauchy distribution has the density f(x) = 1/π(1 + x2), x ∈ ℝ; the integral ∫−∞∞ |x| f(x) dx diverges, because |x|/π(1 + x2) falls only like 1/π|x| — and so E[X] does not exist.)
- Dependent data. A thousand answers from people living on one street, or a thousand frames of the same video, are not a thousand independent observations, and σ/√n badly underestimates the error.
- Little data and a skewed distribution. The theorem is a limit theorem — for the exponential wait, the distribution of the average is still noticeably asymmetric after five observations.
7. Limit theorems in artificial intelligence
- Stochastic gradient descent. A gradient computed from a minibatch of B examples is a sample mean. On average it points the same way as the gradient over the full data (law of large numbers), but it fluctuates with a standard deviation ∝ 1/√B. A batch four times larger means only half the noise — which is why it does not pay to keep growing batches indefinitely.
- Monte Carlo methods. When something cannot be computed exactly — the expected reward in reinforcement learning, an integral in high dimensions — you average a large number of random samples. The law of large numbers guarantees the right result; the central limit theorem tells you how precise it is.
- Model evaluation and A/B tests rest on the error 1.96 · σ/√n: without it, you cannot tell whether a new model is really better or just got lucky.
- Network initialization. The input of a neuron is a sum of many weights times activations — by the central limit theorem, it is approximately normally distributed. This is the basis of the Xavier and He initialization schemes, which choose the variance of the weights so that the signal neither blows up nor dies out as it passes through the network.
gB = 1/B ∑i ∈ batch ∇ℓi(w), standard deviation of gB = σ∇/√B
- gB
- the gradient from the batch — the estimate a step is taken by; computing the exact gradient over millions of examples would be too expensive for every step
- 1/B ∑, B
- the sample mean over B randomly chosen examples — thanks to it, the estimate points exactly right on average (E[gB] = the true gradient) and fluctuates only like 1/√B
- ∇ℓi(w)
- the gradient of the loss on a single example — a random variable, because the example is chosen at random
- σ∇
- how much the gradients of individual examples differ — a property of the data and the model, which the batch size cannot change
A batch of 32 examples has noise σ∇/√32 ≈ 0.177 σ∇, a batch of 128 examples σ∇/√128 ≈ 0.088 σ∇ — four times the computation for half the noise (colours as in the formula above).
Algorithm — a Monte Carlo estimate with its error
Input: a function h : ℝ → ℝ, a procedure vzorek() that on every call returns a new value of the random variable X, independent of the previous ones, and a number of samples n ∈ ℕ, n ≥ 2. We assume E[h(X)2] < ∞ (finite variance of h(X)).
Output: an estimate ȳ ∈ ℝ of the expected value E[h(X)] and an approximate 95% interval ȳ ± 1.96 · s/√n.
Steps:
- For i = 1, …, n: generate an independent sample xi of X and compute yi = h(xi).
- Estimate: ȳ = 1/n ∑i=1n yi (law of large numbers: ȳ → E[h(X)]).
- Sample standard deviation: s = √1/n − 1 ∑i=1n (yi − ȳ)2 (an estimate of σ).
- Return ȳ and the interval ȳ ± 1.96 · s/√n (central limit theorem).
Termination: after a fixed number n of samples; if a given precision is wanted, n is chosen in advance (or sampling continues until 1.96 · s/√n drops below the required bound).
monte_carlo(h, vzorek, n): # estimate of E[h(X)]
for i = 1 … n:
x ← vzorek() # independent sample of X
y[i] ← h(x)
odhad ← mean(y) # law of large numbers: → E[h(X)]
s ← sample standard deviation(y) # estimate of σ
return odhad ± 1.96 · s / √n # central limit theorem: 95% interval
The error shrinks like 1/√n regardless of the dimension of the problem — which is why Monte Carlo is often the only feasible route for integrals over hundreds of variables, where a grid of points would have an astronomical number of nodes.
Example — the variance of the weights when initializing a network
Simplified: a neuron adds up z = ∑j=1n wj xj, the weights and the inputs are independent, and the weights have expected value 0. The variances of independent terms add up:
Var(z) = n · Var(w) · E[x2]
For the signal to neither weaken nor strengthen, we want Var(z) ≈ E[x2] ⇒ Var(w) = 1/n
For n = 256 inputs: Var(w) = 1/256 ≈ 0.0039, standard deviation of the weights √1/256 = 1/16 = 0.0625
ReLU zeroes out roughly half of the inputs of the next neuron, which is why He et al. (2015) choose Var(w) = 2/n (for n = 256 a standard deviation ≈ 0.088). Glorot and Bengio (2010, “Xavier”) take a compromise between the forward and the backward pass, Var(w) = 2/nin + nout. The central limit theorem adds that z is approximately normal, so it is enough to watch the variance.
8. Summary
- The sample mean is a random variable with expected value μ and standard deviation σ/√n.
- Law of large numbers: as n grows, the average almost certainly approaches μ.
- Central limit theorem: the average of many independent variables is approximately normally distributed — no matter how the individual variables are distributed.
- The error of an estimate based on the average is roughly ±1.96 · σ/√n; an estimate twice as precise costs four times as much data.
- Without finite variance or without independence, the theorems do not hold.
Do you see it differently? Write to support@coden-app.com.