Settings

Theme
Lite Mode (saves mobile data)

In Lite Mode, animations and videos don't play until you tap them, and their size is shown first. Interactive widgets still work.

Text size

Back up or move your progress →

Part 1: Math FoundationsProbability and Statistics

Law of large numbers and the Central Limit Theorem

P1.D845 min●●○ IntermediateReviewed Dec 1, 2026

Why this matters for machine learning

Almost every number a model reports is an average: the loss over a mini-batch, the accuracy over a test set, an estimate from random samples. The law of large numbers says such averages settle on the truth, and the central limit theorem says how fast and with what shape, so that any average can carry an error bar.

  • A mini-batch gradient is an average of per-example gradients, so its noise has standard deviation σ/B\sigma/\sqrt B: quadrupling the batch size halves the noise. (P1.F4: Stochastic and mini-batch gradient descent, coming soon)
  • Test-set accuracy is the average of correct (1) and wrong (0), so it has a standard error p(1−p)/n\sqrt{p(1-p)/n} that tells you how many test examples you need before you can tell a model with 90%90\% from one with 91%91\%. (P3.A9: Evaluation metrics, coming soon)
  • A Monte Carlo estimate averages a function of random samples. The law says it converges, and the CLT says its error shrinks like 1/n1/\sqrt n. (P1.D9: Sampling and Monte Carlo)
  • Measurement noise is often the sum of many small independent effects, which the CLT makes roughly Gaussian. This is the usual reason for the Gaussian noise assumption in regression. (P3.A2: Linear regression, coming soon)
  • A neuron’s pre-activation is a sum of many weighted inputs, so its spread grows like d\sqrt d with the number of inputs dd. Weight initializations are scaled to cancel that. (P4.12: Weight initialization, coming soon)

Before this: Probability basics, Common distributions, Expectation and variance, Joint, marginal and conditional probability

After this lesson you can

  • Write the sample mean Xˉn\bar X_n of nn i.i.d. draws, say that it has mean μ\mu and standard deviation σ/n\sigma/\sqrt n, and state and prove Markov’s and Chebyshev’s inequalities with a numeric example
  • Prove the weak law of large numbers from Chebyshev’s inequality, and say what it does not promise: nothing about a short run, no payback after a streak, and a gap in counts that grows even as the gap in frequency shrinks
  • State the central limit theorem for the standardized sum, use Xˉn≈N(μ,σ2/n)\bar X_n\approx N(\mu,\sigma^2/n) to compute probabilities (with the continuity correction for counts), and explain how a Galton board turns coin steps into a bell curve
  • Say when the bell shape arrives slowly or not at all (skewness, far tails, dependence, infinite variance, a Cauchy population), and use a standard error to put an error bar on a mini-batch average or a test-set accuracy

Flip a fair coin a hundred times. The count of heads is usually close to 5050 but rarely exactly 5050, and the fraction of heads is usually close to 0.50.5. Flip it ten thousand times and the fraction is much closer to 0.50.5, even though the count of heads is now usually further from 50005000 than before. Two questions follow. Why do averages settle at all? And when they wobble around the settled value, what shape does the wobble have?

The first question is the law of large numbers. The second is the central limit theorem. Earlier lessons promised both: P1.D1 said relative frequencies settle and that the law would be proved here, P1.D3 said the Gaussian appears wherever many small independent effects add up, and P1.D4 computed how big the wobble is. This lesson delivers all three.

The average of draws is itself random

Draw X1,X2,…,XnX_1,X_2,\dots,X_n independently from the same distribution. As P1.D5 put it, they are i.i.d. (independent and identically distributed). Call the mean of the distribution μ\mu and its standard deviation σ\sigma. The sample mean is the ordinary average of the draws,

Xˉn=X1+⋯+Xnn.\bar X_n=\frac{X_1+\dots+X_n}{n}.

The bar means “average of”, and the subscript nn says how many draws went into it. The sum Sn=X1+⋯+XnS_n=X_1+\dots+X_n is used too, with Xˉn=Sn/n\bar X_n=S_n/n.

Draw the numbers again and you get a different average, so Xˉn\bar X_n is a random variable, and P1.D4 gave its two key numbers. By linearity of expectation, E[Xˉn]=μ\mathbb{E}[\bar X_n]=\mu. Because the draws are independent, variances add, so Var(Sn)=nσ2\mathrm{Var}(S_n)=n\sigma^2 and Var(Xˉn)=nσ2/n2=σ2/n\mathrm{Var}(\bar X_n)=n\sigma^2/n^2=\sigma^2/n. Hence

E[Xˉn]=μ,sd(Xˉn)=σn.\mathbb{E}[\bar X_n]=\mu,\qquad \mathrm{sd}(\bar X_n)=\frac{\sigma}{\sqrt n}.

The second number is called the standard error of the mean. A fair die has μ=3.5\mu=3.5 and σ=35/12≈1.708\sigma=\sqrt{35/12}\approx1.708. The average of 2525 rolls is centred on 3.53.5 with standard deviation 1.708/5≈0.3421.708/5\approx0.342.

The mean and spread alone do not say how likely the average is to land far away, or what its histogram looks like. The next section answers the first with a bound that needs no knowledge of the shape, and the rest of the lesson answers the second.

How far can a random variable stray? Markov and Chebyshev

We want a guarantee that works for any distribution. Start with the simplest. Markov’s inequality says that for a variable YY that is never negative, and any a>0a\gt0,

P(Y≥a)≤E[Y]a.\mathbb{P}(Y\ge a)\le\frac{\mathbb{E}[Y]}{a}.

The proof takes two lines. Compare YY with the variable that equals aa when Y≥aY\ge a and 00 otherwise. The first is never smaller, so E[Y]≥a P(Y≥a)\mathbb{E}[Y]\ge a\,\mathbb{P}(Y\ge a), and dividing by aa gives the claim. For example, if waiting times are never negative and average 22 minutes, then at most 2/10=20%2/10=20\% of the waits can be 1010 minutes or longer. (For an exponential waiting time with mean 22 the true share is e−5≈0.0067e^{-5}\approx0.0067, much smaller. Markov’s bound is safe, not sharp.)

Now apply Markov to the variable Y=(X−μ)2Y=(X-\mu)^2, which is never negative, with a=k2σ2a=k^2\sigma^2. The event (X−μ)2≥k2σ2(X-\mu)^2\ge k^2\sigma^2 is the event ∣X−μ∣≥kσ|X-\mu|\ge k\sigma, and E[(X−μ)2]=σ2\mathbb{E}[(X-\mu)^2]=\sigma^2 by the definition of variance. So

P(∣X−μ∣≥kσ)≤σ2k2σ2=1k2.\mathbb{P}\bigl(|X-\mu|\ge k\sigma\bigr)\le\frac{\sigma^2}{k^2\sigma^2}=\frac1{k^2}.

This is Chebyshev’s inequality, named after the 19th-century Russian mathematician Pafnuty Chebyshev. It needs only a finite variance: the distribution can have any shape. It says that at most 25%25\% of the probability lies 22 standard deviations or more from the mean, and at most 11.1%11.1\% lies 33 or more. Written for a distance ε\varepsilon instead of kσk\sigma:

P(∣X−μ∣≥ε)≤σ2ε2.\mathbb{P}\bigl(|X-\mu|\ge\varepsilon\bigr)\le\frac{\sigma^2}{\varepsilon^2}.

How loose is it? For a Gaussian the true probabilities of being at least 22 and 33 standard deviations away are 0.04550.0455 and 0.00270.0027, far below 0.250.25 and 0.1110.111. The price of covering every shape is a weak bound for any one shape. It cannot be improved for all distributions at once: a variable that is ±2σ\pm2\sigma with probability 18\frac18 each and 00 with probability 34\frac34 has variance exactly σ2\sigma^2 and puts exactly 14\frac14 at distance 2σ2\sigma. But for a coin, a die or a bell curve it is pessimistic, and the central limit theorem below gives much sharper answers.

The weak law of large numbers

Apply Chebyshev’s inequality to the sample mean, with the mean μ\mu and the standard deviation σ/n\sigma/\sqrt n from above:

P(∣Xˉn−μ∣≥ε)≤σ2/nε2=σ2n ε2.\mathbb{P}\bigl(|\bar X_n-\mu|\ge\varepsilon\bigr)\le\frac{\sigma^2/n}{\varepsilon^2}=\frac{\sigma^2}{n\,\varepsilon^2}.

The right side goes to 00 as nn grows, for any fixed tolerance ε>0\varepsilon\gt0. That is the weak law of large numbers: for i.i.d. draws with finite variance, the chance that the average is farther than ε\varepsilon from μ\mu tends to 00. In symbols, Xˉn→μ\bar X_n\to\mu “in probability”. The proof above is the entire proof under these conditions.

Revisiting P1.D1. There, a fair coin’s frequency of heads was within 0.10.1 of 0.50.5 with probability 0.9650.965 for 100100 flips, within 0.050.05 with probability 0.7290.729 for 100100 flips, and within 0.050.05 with probability 0.99860.9986 for 10001000 flips. A flip has σ2=14\sigma^2=\frac14, so Chebyshev gives 14/(nε2)\frac14/(n\varepsilon^2) for the chance of being ε\varepsilon or more away. For n=100n=100 and ε=0.1\varepsilon=0.1 the bound is 0.250.25, so “within 0.10.1” has probability at least 0.750.75 (the exact value is 0.9650.965). For n=1000n=1000 and ε=0.05\varepsilon=0.05 the bound is 0.10.1 (the exact chance of being farther than 0.050.05 is 0.00140.0014). For n=100n=100 and ε=0.05\varepsilon=0.05 the bound equals 11 and says nothing, while the truth is a 27%27\% chance of being farther than 0.050.05. The law is true in all three cases. Chebyshev is a proof tool, not a calculator.

How many rolls? How many rolls of a fair die make the average within 0.10.1 of 3.53.5 with probability at least 0.950.95? With σ2=35/12\sigma^2=35/12, Chebyshev guarantees it once 35/12n⋅0.01≤0.05\frac{35/12}{n\cdot0.01}\le0.05, that is n≥5834n\ge5834. The Gaussian estimate from later in this lesson says n≈(1.96×1.708/0.1)2≈1121n\approx(1.96\times1.708/0.1)^2\approx1121. The exact value at n=1121n=1121 is 0.94990.9499, a little below 0.950.95 (the exact value wobbles around 0.950.95 near here, because the sum is a whole number: n=1120n=1120 gives 0.95100.9510, counting an average exactly 0.10.1 away as within). The Gaussian estimate is very close, and Chebyshev’s guarantee is about five times too cautious.

What the law does and does not say

  • It is about the long run. For a short run it promises nothing. Ten flips have exactly 55 heads only 24.6%24.6\% of the time (P1.D1).
  • It does not pay back a surplus. It dilutes it. After 100100 fair flips with 5555 heads, the next 99009900 flips are expected to bring 49504950 heads, so the expected frequency at 10,00010{,}000 flips is 5005/10000=0.50055005/10000=0.5005. The surplus of 55 is still there. It just matters less. The belief that tails is “due” after a run of heads is the gambler’s fallacy (P1.D1): each flip is independent of the past (P1.D5).
  • The count wanders further while the frequency settles. The number of heads in nn flips has standard deviation 12n\frac12\sqrt n, and the frequency has 12n\frac1{2\sqrt n} (P1.D4). P1.D4’s “typical gap of about 55” for 100100 flips is the standard deviation. The table also shows the average gap, ignoring sign, which is about 0.80.8 of the standard deviation (for a Gaussian it is 2/π≈0.798\sqrt{2/\pi}\approx0.798 of it).
Flips nnsd of the count 12n\frac12\sqrt naverage gap of the countsd of the frequency 12n\frac1{2\sqrt n}
10010055about 440.050.05
10,00010{,}0005050about 40400.0050.005
1,000,0001{,}000{,}000500500about 4004000.00050.0005

The popular “law of averages” mixes these up. Averages settle. Totals do not: they keep drifting by about n\sqrt n.

  • It needs a mean. The proof used a finite variance, and the standard laws need a finite mean. For the Cauchy distribution (P1.D4) the mean does not exist, and a short calculation shows that the average of nn independent standard Cauchy draws has exactly the same distribution as a single draw, whatever nn is. Averaging does not help at all: half of all averages lie between −1-1 and 11 for n=1n=1 and for n=100,000n=100{,}000. You will see this in the second widget.
  • It needs the draws to bring in new information. If every XiX_i were a copy of the same XX, the average would be XX itself and would never settle. Independence (or at least weak dependence) is the part of “i.i.d.” that does this work.
Go deeper The strong law of large numbers, and how little the law needs

The weak law says that for each fixed ε\varepsilon, the probability of a gap larger than ε\varepsilon shrinks. The strong law says something about the whole sequence of averages Xˉ1,Xˉ2,Xˉ3,…\bar X_1,\bar X_2,\bar X_3,\dots at once: for i.i.d. draws with a finite mean μ\mu, with probability 11 the sequence converges to μ\mu. This is a stronger statement, called almost-sure convergence. It is a theorem of Andrey Kolmogorov, and its proof is harder than the one above. “With probability 11” does not mean “for every possible sequence”: a sequence of all heads is possible, it just has probability 00.

Both laws hold with only a finite mean, without the finite variance used in the proof above. A heavy-tailed population such as a Pareto distribution with finite mean but infinite variance still has averages that settle, but they settle slowly and their shape is not Gaussian. The first law of this kind was proved by Jacob Bernoulli for repeated yes-or-no trials (his book was published in 1713, after his death).

The Galton board: steps that add up to a bell

The law says the average settles. It does not say what the wobble around the settled value looks like. A Galton board, devised by Francis Galton in the 19th century, shows it. Balls fall through rows of pegs. At each peg a ball goes left or right, here with probability pp of going right, independently of the pegs above. After nn rows its bin is the number of right moves, kk. That is a sum of nn independent Bernoulli steps, so the bin has the binomial distribution of P1.D3: mean npnp, standard deviation np(1−p)\sqrt{np(1-p)}.

Galton board

No balls landed yet. The dots and curve show the theory for 12 rows and p = 0.5. np = 6 and n(1−p) = 6: both are below about 10, so the Gaussian curve is only a rough fit.
00.10.20.30123456789101112bin k = number of right moves

observed relative frequency exact binomial probability Gaussian, mean np, variance np(1−p)

balls landed = 0 largest bar vs dot gap = — mean bin = — np = 6 sd of bins = — √(np(1−p)) = 1.73
Counts for every bin
bin kcountfreq.exact P
00—0.0002
10—0.0029
20—0.0161
30—0.0537
40—0.1208
50—0.1934
60—0.2256
70—0.1934
80—0.1208
90—0.0537
100—0.0161
110—0.0029
120—0.0002
A Galton board. Balls fall through n rows of pegs; at each peg a ball goes right with probability p, otherwise left. Bars show the relative frequency of the balls that have landed in each bin (the bin number k is the number of right moves). Pink dots are the exact binomial probabilities, and the dark curve is the Gaussian with mean np and variance np(1−p). Drop 1 or 10 balls to watch them fall, or drop 100 or 1000 to see the bars settle onto the dots. The sliders change the number of rows and p, and start a new board. In Lite Mode or with reduced motion the balls land instantly. The table below the readouts lists every count. The random numbers come from a seeded pseudo-random generator that gets a new seed at each reset.

What to try:

  • Twelve rows, p=0.5p=0.5. Drop 10001000 balls. The bars follow the dots closely, and the dots follow the curve. The middle bin k=6k=6 has exact probability 924/4096=0.2256924/4096=0.2256. The Gaussian density at 66 is 0.23030.2303, and the Gaussian area over the bin (from 5.55.5 to 6.56.5, the continuity correction of P1.D3) is 0.22720.2272.
  • Drop only a few balls. Ten balls give a ragged histogram. The law of large numbers is at work: bars settle onto the dots as the balls pile up, and the “largest bar vs dot gap” in the readouts shrinks.
  • More rows. At 2020 rows the bell is smoother. The curve follows the dots even more closely.
  • Change pp to 0.10.1 with 1212 rows. The pile leans against the left wall. Here np=1.2np=1.2 is small, the binomial is lopsided, and the Gaussian is a poor fit, because it ignores the wall. The common rule of thumb of P1.D3, that npnp and n(1−p)n(1-p) should both be at least about 1010, is a warning about exactly this.

The numbers from P1.D3. For n=100n=100 flips of a fair coin, P(X≤55)=0.8644\mathbb{P}(X\le55)=0.8644 exactly. The Gaussian with the continuity correction gives Φ(55.5−505)=Φ(1.1)=0.8643\Phi\bigl(\frac{55.5-50}{5}\bigr)=\Phi(1.1)=0.8643, and without it Φ(1)=0.8413\Phi(1)=0.8413. With 1212 rows the same idea gives P(5≤k≤7)=0.6123\mathbb{P}(5\le k\le7)=0.6123 exactly and 0.61350.6135 with the correction, but only 0.43630.4363 if you integrate from 55 to 77 instead of 4.54.5 to 7.57.5. Counts are whole numbers, so each count kk owns the interval from k−12k-\frac12 to k+12k+\frac12.

The statement that the standardized binomial approaches the Gaussian is the oldest case of the central limit theorem. Abraham de Moivre found it for p=12p=\frac12 in a pamphlet dated 1733 (printed in English in the 1738 edition of his book The Doctrine of Chances), and Laplace extended it in the early 19th century. It is called the de Moivre–Laplace theorem.

The central limit theorem

A binomial is a sum of Bernoulli steps. The same picture appears for sums of almost any kind of step. The central limit theorem (CLT) says so. To compare sums for different nn on one scale, standardize: subtract the mean and divide by the standard deviation, both known from above,

Zn=Sn−nμσn=Xˉn−μσ/n.Z_n=\frac{S_n-n\mu}{\sigma\sqrt n}=\frac{\bar X_n-\mu}{\sigma/\sqrt n}.

By the rules of P1.D4, ZnZ_n has mean 00 and standard deviation 11 for every nn. The theorem adds the shape.

“In distribution” means that the CDF of ZnZ_n (P1.D2) approaches the CDF Φ\Phi at every point. It does not claim that any single ZnZ_n equals a Gaussian, only that probabilities of intervals approach Gaussian ones. For large nn this licenses the working approximations

Sn≈N(nμ, nσ2)Xˉn≈N ⁣(μ, σ2n)\begin{aligned} S_n&\approx N(n\mu,\,n\sigma^2)\\ \bar X_n&\approx N\!\left(\mu,\,\tfrac{\sigma^2}{n}\right) \end{aligned}

The first gives the Galton board as the case Xi∈{0,1}X_i\in\{0,1\}, μ=p\mu=p, σ2=p(1−p)\sigma^2=p(1-p). The second is the form used most in machine learning: an average is roughly Gaussian, centred on μ\mu, with standard deviation σ/n\sigma/\sqrt n. The standard deviation of the average was already known. The new content is the shape.

An example with dice. The average of 3030 dice has mean 3.53.5 and standard deviation 1.708/30=0.3121.708/\sqrt{30}=0.312. How likely is an average of at least 44, that is, a sum of at least 120120? With the continuity correction (the sum is a whole number) the Gaussian gives 1−Φ(119.5−1059.354)=1−Φ(1.55)=0.06061-\Phi\bigl(\frac{119.5-105}{9.354}\bigr)=1-\Phi(1.55)=0.0606. The exact value, by adding up the probabilities of all sums, is 0.06070.0607. Without the correction the answer is 0.05440.0544, about 10%10\% too small.

Watch it happen

Sample-mean histogram

Population (where each draw comes from)

Sample size n (draws in each mean)

Exponential, n = 1: the means centre near μ = 1 with spread σ/√n = 1 (measured 1.034). Each “mean” is one draw, so the histogram is the population itself, and the curve is the Gaussian with the same μ and σ.
00.51−2024mean of 1 draw

histogram of 2000 means Gaussian N(μ, σ²/n) μ

22 of the 2000 means fall outside the axis and are not drawn.

mean of the means = 0.974 μ = 1 sd of the means = 1.034 σ/√n = 1 share within μ ± 1.96σ/√n = 0.949 Gaussian says 0.95, a poor guide at this n skewness of the means = 2.46 predicted skewness γ/√n = 2

Each draw uses a seeded pseudo-random generator. The measured numbers change a little with every new draw, the predicted ones do not. Skewness measures lean: 0 is symmetric, positive leans right.

The sample-mean histogram. Choose a population and a sample size n. The widget draws 2000 samples of n values, takes the mean of each sample, and shows a histogram of those 2000 means with the Gaussian N(μ, σ²/n) that the central limit theorem predicts (pink curve), with μ marked by a dashed line. The readouts compare the measured mean, spread, share within 1.96 standard errors and skewness of the 2000 means with the predictions. For the Cauchy population no mean or standard deviation exists, so no Gaussian is drawn and the pink dashed curve is the density of a single Cauchy draw. The axis can be zoomed to the means or fixed to a range that fits one draw. The draws come from a seeded pseudo-random generator, and the button picks a new seed.

What to try:

  1. Uniform, then n=1,2,5n=1,2,5. One draw is flat. The mean of two is a triangle, and the mean of five is already close to a bell. Averaging smooths the shape.
  2. Exponential. At n=1n=1 the histogram is the skewed population, and the Gaussian is a clear mismatch. At n=10n=10 it still leans right. At n=30n=30 the lean is slight. At n=100n=100 it is close. The readout “predicted skewness γ/n\gamma/\sqrt n” falls as 2/n2/\sqrt n.
  3. Fair coin. The means can only be the values k/nk/n, so the histogram is a row of bars, and it follows the Galton board.
  4. Two humps. One draw has two bumps. The mean of two draws has three bumps (both low, one of each, both high). At n=5n=5 and n=10n=10 it is still lumpy, with several small peaks, because the means sit on a lattice of spacing 4/n4/n. By about n=30n=30 it is one smooth bump.
  5. Rare event (heads probability 0.10.1). It is skewed like the exponential, but worse, because npnp is small. At n=30n=30 we have np=3np=3 and the bars still lean right.
  6. Cauchy. No matter what nn is, the histogram of means stays as wide as one draw, and the middle half stays near −1-1 to 11. Many means fall outside the axis. The average of Cauchy draws never becomes a bell.
  7. Fixed axis. Switch to “fixed range” and step through nn for the uniform population. The bell does not move: it narrows like 1/n1/\sqrt n, which is the law of large numbers. The zoomed axis shows the shape, which is the CLT. Both are the same fact seen two ways: the mean and spread of Xˉn\bar X_n are μ\mu and σ/n\sigma/\sqrt n (the law), and in addition the shape is Gaussian (the theorem).

A 95% interval for an average

For a Gaussian, P(∣Z∣≤1.96)=0.95\mathbb{P}(|Z|\le1.96)=0.95. So for large nn, about 95%95\% of sample means land within 1.96 σ/n1.96\,\sigma/\sqrt n of μ\mu. The readout “share within μ±1.96σ/n\mu\pm1.96\sigma/\sqrt n” checks this. Turned around, it gives the usual recipe xˉ±1.96 σ/n\bar x\pm1.96\,\sigma/\sqrt n as an interval likely to contain μ\mu. In practice σ\sigma is not known and is estimated from the data, which makes the interval slightly wider for small nn. How to read such an interval correctly is the subject of P1.D13.

What the CLT does not say

  • The data do not become Gaussian. A single draw keeps its own distribution, however many you collect. It is the average (or sum) that becomes bell-shaped. A pile of skewed incomes is still skewed.
  • n=30n=30 is a rule of thumb, not a theorem. The speed depends on the population. A symmetric, light-tailed population such as the uniform is close to Gaussian after a handful of draws, and a strongly skewed one needs far more (see the Go-deeper box below).
  • The bell is a good fit in the middle and a worse one far out. The theorem says the CDFs approach each other, but the relative error in a far tail stays large for a long time.
  • Strong dependence can break it. If every XiX_i is a copy of the same variable, the sum is nXnX and the standardized sum has the shape of XX. Positive correlation between neighbours makes the naive σ/n\sigma/\sqrt n too small.
  • Heavy tails break it. Without a finite variance (the Cauchy distribution is the standard example) the theorem does not apply. For tails that fall like 1/xα1/x^{\alpha} with α<2\alpha\lt2 the limit, after a different scaling, is not Gaussian.
Go deeper How fast the bell arrives: skewness and the exponential

Take the exponential population with μ=σ=1\mu=\sigma=1 (the one in the widget). The average of nn draws can be computed exactly, so we can compare the chance that the average lands more than 1.961.96 standard errors above μ\mu, or below, with the Gaussian value 0.0250.025 for each side.

nnskewness 2/n2/\sqrt nabove μ+1.96σ/n\mu+1.96\sigma/\sqrt nbelow μ−1.96σ/n\mu-1.96\sigma/\sqrt n
10100.630.630.03930.03930.00580.0058
30300.370.370.03400.03400.01400.0140
1001000.200.200.03020.03020.01920.0192
100010000.060.060.02670.02670.02320.0232

At n=30n=30 the upper tail is 36%36\% too big and the lower tail 44%44\% too small, and the two errors nearly cancel in the sum, 0.04810.0481 against 0.050.05. A two-sided interval looks fine while each side is off. Farther out it is worse: the chance of being more than 44 standard errors above μ\mu at n=30n=30 is 0.000390.00039, about 1212 times the Gaussian value 0.00003170.0000317.

The general statement is the Berry–Esseen theorem: with a finite third moment, the largest gap between the CDF of ZnZ_n and Φ\Phi is at most a constant times ρ/(σ3n)\rho/(\sigma^3\sqrt n), where ρ=E∣X−μ∣3\rho=\mathbb{E}|X-\mu|^3. The error shrinks like 1/n1/\sqrt n, and a skewed population has a large ρ\rho. For the exponential the largest CDF gap (found by computing it) is 0.1590.159 at n=1n=1, 0.0600.060 at n=5n=5, 0.0420.042 at n=10n=10, 0.0240.024 at n=30n=30 and 0.0130.013 at n=100n=100. For the uniform population it is 0.0570.057 at n=1n=1, 0.0160.016 at n=2n=2, 0.0060.006 at n=5n=5 and 0.0030.003 at n=10n=10.

Go deeper When the central limit theorem fails: dependence and infinite variance

The CLT has hypotheses, and in machine learning they are sometimes violated.

Dependent draws. A sum of copies of one variable has variance n2σ2n^2\sigma^2, not nσ2n\sigma^2, and the standardized sum is just the original variable. Weakly dependent draws, such as values of a time series that forget the distant past, can still obey a version of the CLT, but when neighbouring draws are positively correlated the variance of the average is larger than σ2/n\sigma^2/n, so the naive standard error is too small. The i.i.d. assumption of P1.D5 matters here: for example, consecutive frames of one video are not independent examples.

Infinite variance. Suppose the tail probability P(∣X∣>x)\mathbb{P}(|X|\gt x) falls like 1/xα1/x^{\alpha} for large xx. For 1<α<21\lt\alpha\lt2 the mean exists but the variance does not. The averages still settle (the law holds), but their fluctuations shrink more slowly than 1/n1/\sqrt n, and their limit shape is not Gaussian (it belongs to the family of “stable” distributions). For α≤1\alpha\le1 not even the mean exists. The Cauchy distribution, with tail probability falling like 1/x1/x, is the case α=1\alpha=1, and its averages do not settle at all.

Independent but not identical. The i.i.d. assumption can be relaxed. For independent pieces with different distributions a bell shape still appears when no single piece dominates the total variance (the Lindeberg and Lyapunov conditions). This is the precise version of the “many small effects, none dominating” description of P1.D3.

Go deeper A short history

Jacob Bernoulli proved the first law of large numbers for repeated yes-or-no trials in a book published in 1713. Abraham de Moivre showed in 1733 that fair-coin counts follow a bell curve for large nn (the work reached a wide audience in the 1738 edition of The Doctrine of Chances), and Pierre-Simon Laplace extended it to other sums in the early 1800s. The name “central limit theorem” is credited to George Pólya, in the early 20th century. The modern conditions for it to hold came in work from the late 19th and early 20th centuries. The word “central” is usually read as meaning that the theorem is central to probability. Francis Galton’s board was a way to show the effect to an audience.

Putting it to work: an error bar for every average

The CLT turns “this is an average of nn things” into “it has a standard error σ/n\sigma/\sqrt n and a bell shape”. Four uses follow, all previews of lessons to come.

Mini-batch gradients (P1.F4). A mini-batch gradient is the average of BB per-example gradients. If the examples are drawn independently and each gradient has spread σg\sigma_g, the average has spread σg/B\sigma_g/\sqrt B. Batch sizes 1,4,16,64,2561,4,16,64,256 give 1,12,14,18,1161,\frac12,\frac14,\frac18,\frac1{16} of the single-example noise. To halve the noise you need four times the batch, so large batches have diminishing returns.

Test-set accuracy (P3.A9). Each test example is correct (11) or wrong (00), so accuracy is an average of Bernoulli draws with spread p(1−p)\sqrt{p(1-p)}, and its standard error is p(1−p)/n\sqrt{p(1-p)/n}. Suppose a model’s true accuracy is 0.900.90 and you test on n=1000n=1000 fresh examples. The standard error is 0.9×0.1/1000=0.0095\sqrt{0.9\times0.1/1000}=0.0095, so the measured accuracy is likely (about 95%95\%) to lie within ±0.019\pm0.019 of 0.900.90, between 0.8810.881 and 0.9190.919. The value 0.910.91 lies inside that range, so a measured 0.910.91 fits a true 0.900.90 well, and 10001000 examples cannot reliably tell 90%90\% from 91%91\%. To pin one accuracy near 0.900.90 to ±0.01\pm0.01 needs n≥1.962×0.09/0.012≈3458n\ge1.96^2\times0.09/0.01^2\approx3458 examples. To make a one-point gap between two models, measured on separate test sets, as large as 1.961.96 standard errors takes about 66006600 examples each, because the two variances add: n=1.962(0.09+0.0819)/0.012≈6604n=1.96^2(0.09+0.0819)/0.01^2\approx6604. Even then, when the true gap equals 1.961.96 standard errors you would detect it only about half the time. (Comparing two models on the same test set can need fewer examples, a point for P1.D13.) All of this assumes the test examples are independent draws from the data you care about.

Monte Carlo (P1.D9). To estimate E[f(X)]\mathbb{E}[f(X)], average ff over random samples. The law says the estimate converges, and the standard error σf/n\sigma_f/\sqrt n says how fast: ten times more precision costs a hundred times more samples.

Why Gaussian noise, and why weight scales. A measurement error made of many small independent effects is roughly Gaussian (P3.A2). A neuron’s pre-activation is a sum of dd terms, and with independent zero-mean weights its variance grows in proportion to dd (for inputs of a fixed size), so initial weights are scaled by about 1/d1/\sqrt d to keep it steady (P4.12).

Intuition

An average is a vote of many draws. A lucky draw is often cancelled by an unlucky one, so the average is steadier than a single draw, and the more draws, the steadier, in proportion to 1/n1/\sqrt n rather than 1/n1/n because the cancellation is partial. That is the law of large numbers.

Adding independent things is a repeated smoothing of the picture of the distribution (the distribution of a sum is a blend of shifted copies of the others). Each blend rounds off corners: a two-valued coin becomes a binomial staircase, a flat uniform becomes a triangle, then a hump. Whatever the starting shape, if no single piece dominates and the variance is finite, the repeated blending drifts to one shape, the bell. That is the central limit theorem. The Galton board is the picture of this blending with the simplest possible piece.

Math

The sample mean, and its mean and spread:

Xˉn=1n∑i=1nXi\bar X_n=\frac1n\sum_{i=1}^nX_iE[Xˉn]=μsd(Xˉn)=σn\begin{aligned} \mathbb{E}[\bar X_n]&=\mu\\ \mathrm{sd}(\bar X_n)&=\frac{\sigma}{\sqrt n} \end{aligned}

Markov, for Y≥0Y\ge0, and Chebyshev:

P(Y≥a)≤E[Y]aP(∣X−μ∣≥ε)≤σ2ε2\begin{aligned} \mathbb{P}(Y\ge a)&\le\frac{\mathbb{E}[Y]}{a}\\ \mathbb{P}(|X-\mu|\ge\varepsilon)&\le\frac{\sigma^2}{\varepsilon^2} \end{aligned}

The weak law:

P(∣Xˉn−μ∣≥ε)≤σ2nε2→0\begin{aligned} \mathbb{P}(|\bar X_n-\mu|\ge\varepsilon)&\le\frac{\sigma^2}{n\varepsilon^2}\\ &\to0 \end{aligned}

The central limit theorem:

Zn=Xˉn−μσ/nP(Zn≤z)→Φ(z)\begin{aligned} Z_n&=\frac{\bar X_n-\mu}{\sigma/\sqrt n}\\ \mathbb{P}(Z_n\le z)&\to\Phi(z) \end{aligned}

The binomial case, with X∼Bin(n,p)X\sim\mathrm{Bin}(n,p):

X−npnp(1−p)→N(0,1)\frac{X-np}{\sqrt{np(1-p)}}\to N(0,1)

Code

Simulate batches of nn rolls with NumPy and compare the spread of the averages with σ/n\sigma/\sqrt n.

import numpy as np

rng = np.random.default_rng(0)
means = rng.integers(1, 7, size=(5000, 100)).mean(axis=1)  # 5000 batches of 100 rolls
print(means.mean(), means.std(), np.sqrt(35 / 12) / 10)    # near 3.5, near 0.171, 0.171

Try it: laws and limits in code

The first cell rolls a die in batches of nn and compares the spread of the averages with σ/n\sigma/\sqrt n, the share of averages within 0.10.1 of 3.53.5, and the Chebyshev bound on the share that are not. It then repeats the idea with Cauchy draws. The second cell is exact (no random numbers): for a fair coin, it compares the true chance that the frequency is more than ε\varepsilon from 0.50.5 with the Chebyshev bound and the Gaussian estimate, then checks the exponential table from the Go-deeper box. Predict first: will Chebyshev’s bound be above or below the exact value every time?

Try it: averages of dice settle, averages of Cauchy draws do not

Runs real Python in your browser. First run downloads about 9 MB (once; your browser keeps it).

The cell prints a spread of the averages that tracks σ/n\sigma/\sqrt n (about 1.7081.708, 0.5400.540, 0.1710.171 and 0.0540.054), a share within 0.10.1 that rises toward 11, and a Chebyshev bound that is capped at 11 (no information) for n=1,10,100n=1,10,100 and equals 0.2920.292 for n=1000n=1000. For the Cauchy draws the middle half stays near −1-1 to 11 for every nn, up to random variation. Challenge: change the tolerance 0.10.1 to 0.50.5 and find the smallest nn in the list for which the Chebyshev bound is below 0.50.5.

Try it: Chebyshev against the exact value and the Gaussian estimate

Runs real Python in your browser. First run downloads about 6 MB (once; your browser keeps it).

The first table shows the exact chance of being farther than ε\varepsilon (0.03520.0352, 0.27130.2713 and 0.00140.0014), the Gaussian estimate (0.04550.0455, 0.31730.3173 and 0.00160.0016, much nearer than Chebyshev, though a little off for n=100n=100 because it ignores the continuity correction) and the Chebyshev bound (0.250.25, 11 and 0.10.1). The second table reproduces the exponential tails: the upper tail shrinks from 0.03930.0393 toward 0.0250.025 and the lower tail grows from 0.00580.0058 toward it. Challenge: add the pair (10,0.1)(10, 0.1) to the coin list and check that the Chebyshev bound is then capped at 11.

Common mistakes

Check your understanding

  1. A variable has mean 5050 and standard deviation 55, and nothing else is known about its shape. What does Chebyshev’s inequality say about P(∣X−50∣≥15)\mathbb{P}(|X-50|\ge15)?

  2. You average n=400n=400 independent draws from a population with standard deviation σ=10\sigma=10. What is the standard deviation of the average?

  3. A fair coin gives 6060 heads in its first 100100 flips. It is flipped 900900 more times. What is the expected frequency of heads over all 10001000 flips?

  4. Which statement about the central limit theorem is correct?

  5. A model gets 80%80\% of 400400 test examples right. Treating the examples as independent, what is the standard error of its measured accuracy?

Exercises

  1. A waiting time is never negative and averages 22 minutes. Use Markov’s inequality to bound the chance that it is at least 1010 minutes.
    Show answerP(Y≥10)≤2/10=0.2\mathbb{P}(Y\ge10)\le2/10=0.2.
  2. A variable has standard deviation σ\sigma. What does Chebyshev’s inequality say about the chance that it lies strictly within 2σ2\sigma of its mean?
    Show answerThe chance of being 2σ2\sigma or more away is at most 14\frac14, so the chance of lying strictly within 2σ2\sigma is at least 34=0.75\frac34=0.75.
  3. A fair coin is flipped 400400 times. Find the standard deviation of the count of heads and of the frequency, Chebyshev’s bound on P(∣frequency−0.5∣≥0.05)\mathbb{P}(|\text{frequency}-0.5|\ge0.05), and the Gaussian estimate of the same probability.
    Show answerThe count has 12400=10\frac12\sqrt{400}=10 and the frequency has 12400=0.025\frac{1}{2\sqrt{400}}=0.025. The distance 0.050.05 is 22 standard deviations. Chebyshev gives 0.25/(400×0.052)=0.250.25/(400\times0.05^2)=0.25, and the Gaussian gives 2(1−Φ(2))≈0.04552(1-\Phi(2))\approx0.0455.
  4. How many rolls of a fair die make the average lie within 0.050.05 of 3.53.5 with probability at least 0.950.95, by Chebyshev and by the Gaussian estimate?
    Show answerChebyshev: 35/12n⋅0.0025≤0.05\frac{35/12}{n\cdot0.0025}\le0.05 gives n≥23,334n\ge23{,}334. Gaussian: n≈(1.96×1.708/0.05)2≈4482n\approx(1.96\times1.708/0.05)^2\approx4482. Halving the tolerance multiplies both by 44, compared with the 0.10.1 case in the text (58345834 and 11211121).
  5. A model scores 95%95\% on 25002500 independent test examples. Give the standard error of the accuracy and an approximate 95%95\% interval.
    Show answer0.95×0.05/2500≈0.0044\sqrt{0.95\times0.05/2500}\approx0.0044, and ±1.96×0.0044≈±0.0085\pm1.96\times0.0044\approx\pm0.0085, so about 0.94150.9415 to 0.95850.9585.
  6. Use the Gaussian approximation with the continuity correction to estimate the chance of at least 6060 heads in 100100 fair flips. The exact value is 0.028440.02844.
    Show answerThe mean is 5050 and the standard deviation is 55. “At least 6060” starts at 59.559.5, so 1−Φ(59.5−505)=1−Φ(1.9)≈0.02871-\Phi\bigl(\frac{59.5-50}5\bigr)=1-\Phi(1.9)\approx0.0287. Without the correction, 1−Φ(2)=0.02281-\Phi(2)=0.0228.
  7. Why can the law of large numbers not be used to argue that a coin which has shown 1010 heads in a row is “due” for tails?
    Show answerThe flips are independent, so the next flip is heads with probability 12\frac12 whatever came before. The law says the frequency approaches 12\frac12 because later flips dilute the surplus, not because they compensate for it.
  8. Suppose X1=X2=⋯=XnX_1=X_2=\dots=X_n, all the same draw XX, with variance σ2\sigma^2. What are Xˉn\bar X_n and its variance? Which part of the law’s proof fails?
    Show answerXˉn=X\bar X_n=X and its variance is σ2\sigma^2, which does not shrink with nn. The proof used Var(Sn)=nσ2\mathrm{Var}(S_n)=n\sigma^2, which needs the draws to be independent (or at least uncorrelated).
  9. One example’s gradient has noise σg\sigma_g. Which batch size BB reduces the noise of the mini-batch average to a tenth of that, assuming independent examples?
    Show answerσg/B=σg/10\sigma_g/\sqrt B=\sigma_g/10 gives B=100B=100.
  10. An estimator has mean μ\mu and variance 4/n4/n. By Chebyshev, how large must nn be so that it is within 0.10.1 of μ\mu with probability at least 0.990.99?
    Show answer4/n0.12≤0.01\frac{4/n}{0.1^2}\le0.01 gives n≥4/(0.01×0.01)=40,000n\ge4/(0.01\times0.01)=40{,}000.
  11. What skewness is predicted for the average of 3030 exponential draws, given that one exponential draw has skewness 22?
    Show answerThe skewness of an average of nn i.i.d. draws is the skewness of one draw divided by n\sqrt n, so 2/30≈0.3652/\sqrt{30}\approx0.365. (This is the formula used by the widget’s “predicted skewness” readout.)
  12. What is the middle half of the average of 10001000 standard Cauchy draws?
    Show answerThe same as for one draw, since the average is again standard Cauchy: from −1-1 to 11.

Next: P1.D9 turns the law and the CLT into a tool: estimating quantities by averaging random samples (Monte Carlo), such as π\pi by throwing darts at a square. P1.D10 uses the i.i.d. product to fit parameters by maximum likelihood, and P1.D13 turns xˉ±1.96 σ/n\bar x\pm1.96\,\sigma/\sqrt n into confidence intervals and fair comparisons of models. P1.F4 uses the 1/B1/\sqrt B noise of mini-batches.

Key takeaways

  • The sample mean of nn i.i.d. draws with mean μ\mu and standard deviation σ\sigma is Xˉn=1n∑iXi\bar X_n=\frac1n\sum_i X_i. It has mean μ\mu and standard deviation σ/n\sigma/\sqrt n (the standard error), so a hundred times the data buys ten times less noise.
  • Chebyshev’s inequality, P(∣X−μ∣≥kσ)≤1/k2\mathbb{P}(|X-\mu|\ge k\sigma)\le1/k^2, holds for every distribution with finite variance. It is proved by applying Markov’s inequality to (X−μ)2(X-\mu)^2. It is safe but loose: for a Gaussian and k=2k=2 it gives 0.250.25 where the truth is 0.04550.0455.
  • The weak law of large numbers: for i.i.d. draws with finite variance, P(∣Xˉn−μ∣≥ε)≤σ2/(nε2)→0\mathbb{P}(|\bar X_n-\mu|\ge\varepsilon)\le\sigma^2/(n\varepsilon^2)\to0. The average gets close to μ\mu with high probability. It says nothing sure about a short run, and it does not pay back a surplus, it dilutes it: the count of heads strays further from n/2n/2 (like n\sqrt n) while the frequency strays less (like 1/n1/\sqrt n). The strong law says the sequence of averages converges with probability 11.
  • The law needs a mean. For the Cauchy distribution the average of nn draws is exactly as spread out as one draw, for every nn, so the averages never settle.
  • The central limit theorem: for i.i.d. draws with finite variance, the standardized sum (Sn−nμ)/(σn)(S_n-n\mu)/(\sigma\sqrt n) has a CDF that approaches the standard Gaussian CDF Φ\Phi. So Xˉn≈N(μ,σ2/n)\bar X_n\approx N(\mu,\sigma^2/n) for large nn. The Galton board is the special case of coin steps: a binomial becomes a Gaussian (de Moivre–Laplace).
  • The CLT is about averages and sums, not about the data: single draws keep their shape. It is slow for skewed populations and far tails, so n=30n=30 is only a rule of thumb. Strong dependence can break it, and positive correlation makes the naive σ/n\sigma/\sqrt n too small. Infinite variance breaks it too. A 95% interval for an average is about xˉ±1.96 σ/n\bar x\pm1.96\,\sigma/\sqrt n (P1.D13).
Found a mistake? Tell us