---
title: The law of large numbers
heading: Why averages settle, and how many samples you need
eyebrow: A11 · Lesson 29 · The law of large numbers
xp: 40
---

::: hook
To measure a bit error rate of one in a million, how many bits must you send? To estimate a noise voltage's mean to within 1 mV, how many readings? Lesson 1 defined probability as a long-run frequency, trusting that averages settle down. The law of large numbers proves they do, and two simple inequalities, Markov's and Chebyshev's, say how fast, with no assumptions about the distribution's shape. Turned around, they tell you how much data a measurement needs.
:::

::: play
- Each draw is the average of $n$ dice. The histogram shows how those averages spread around the true mean, 3.5.
- Raise $n$ and watch the spread shrink as $1/\sqrt n$. The readout measures how often an average misses 3.5 by more than $\varepsilon$.
- Chebyshev's inequality gives a guaranteed upper limit on that chance. How cautious is it compared with what you see?
:::
:::

```sim plot
view: random
x: m = 1..6
slider: n = 1..400 = 10 log
slider: e = 0.05..1 step 0.05 = 0.5
sample: dice(n)/n | sd: S
trials: 20000
bins: 100
measure: step(abs(m - 3.5) - e) | name: P
readout: standard deviation of the average = S | digits: 3
readout: square root of (35/12)/n = sqrt(35/12/n) | digits: 3
readout: chance of missing by more than ε, measured = P | digits: 4
readout: Chebyshev's limit = (35/12)/(n*e^2) | digits: 4
```

::: predict
## You want to measure a bit error rate near $10^{-6}$ to within about 10%. Roughly how many bits must you send?

- [ ] $10^6$
- [ ] $10^7$
- [x] $10^8$
- [ ] $10^{12}$

::: reveal
About $10^8$. The error count is close to Poisson, so if you expect $k$ errors the count wobbles by $\sqrt k$, a relative error of $\frac{1}{\sqrt k}$. For 10% you need $k = 100$ errors, and at one per million bits that's $10^8$ bits. Sending only $10^6$ bits would show about one error, which says almost nothing.
:::
:::

::: nameit
## Two inequalities

**Markov.** For a variable that's never negative, the chance of being large is limited by the mean:

$$P(X \ge a) \le \frac{E[X]}{a}$$

**Chebyshev.** For any variable with mean $\mu$ and standard deviation $\sigma$,

$$P(|X - \mu| \ge k\sigma) \le \frac{1}{k^2}$$

It follows from Markov applied to $(X - \mu)^2$. These hold for every distribution, so they're cautious: Chebyshev allows a 25% chance of a 2σ miss where a Gaussian has 4.6%. Use them when you don't know the shape, and the Gaussian when you do.

## The sample mean

The average $M_n = \frac{1}{n}(X_1 + \cdots + X_n)$ of $n$ independent draws, each with mean $\mu$ and variance $\sigma^2$, has

$$E[M_n] = \mu \qquad \sigma_{M_n} = \frac{\sigma}{\sqrt n}$$

## The weak law of large numbers

Chebyshev applied to $M_n$:

$$P(|M_n - \mu| \ge \varepsilon) \le \frac{\sigma^2}{n\varepsilon^2} \to 0$$

The average converges to the mean. Long-run frequency is the special case where each $X_i$ is 1 when an event happens and 0 otherwise, so $M_n$ is the fraction of trials where it happened and $\mu = p$.

## How many samples

- **Guaranteed (Chebyshev):** to miss by more than $\varepsilon$ with probability at most $\delta$, take $n \ge \frac{\sigma^2}{\varepsilon^2\delta}$.
- **Typical (Gaussian, by the central limit theorem):** the average is within $\pm z\frac{\sigma}{\sqrt n}$ with the usual Gaussian odds, so $n \approx \left(\frac{z\sigma}{\varepsilon}\right)^2$; $z = 2$ for about 95%.
- **Rare events:** an estimated probability $\hat p = \frac{k}{n}$ has relative error about $\frac{1}{\sqrt k}$, set by the number of events seen, not the number of trials. Plan for enough events.
:::

::: example
## A die is thrown 100 times. Bound the chance that the average misses 3.5 by 0.5 or more, then estimate it with the Gaussian.

::: solution
1. **One die's variance.**
   $$\begin{aligned} \sigma^2 &= \frac{35}{12} \\ &= 2.92 \end{aligned}$$
2. **Chebyshev's bound.**
   $$\begin{aligned} P &\le \frac{2.92}{100 \times 0.25} \\ &= 0.117 \end{aligned}$$
3. **Spread of the average.**
   $$\begin{aligned} \sigma &= \sqrt{2.92/100} \\ &= 0.171 \end{aligned}$$
   so 0.5 is $2.93\sigma$.
4. **Gaussian estimate.**
   $$2Q(2.93) = 0.0034$$
5. **Compare.** The guarantee is about 30 times more cautious than the reality.

**Answer:** Chebyshev bounds the chance by 0.117, and the Gaussian estimate is 0.0034.
:::
:::

::: example
## How many bits must a test send to measure a BER near $10^{-5}$ with a 1σ error of 20%?

::: solution
1. **Errors needed.** A relative error of
   $$\frac{1}{\sqrt k} = 0.2$$
   needs $k = 25$ errors.
2. **Bits needed.** At $10^{-5}$ per bit that's
   $$\frac{25}{10^{-5}} = 2.5 \times 10^6\ \text{bits}$$

**Answer:** the test must send $2.5 \times 10^6$ bits.
:::
:::

::: example
## A Monte Carlo run estimates a failure probability near 0.01 with $10^4$ trials. How accurate is it?

::: solution
1. **Standard deviation of the estimate.** The estimate is an average of $N$ yes/no outcomes, so
   $$\begin{aligned} \sigma &= \sqrt{\frac{p(1 - p)}{N}} \\ &= \sqrt{\frac{0.0099}{10^4}} \\ &= 0.00099 \end{aligned}$$
   about 10% of $p$.
2. **More trials.** Halving that error takes four times as many trials.
3. **Rare events.** Rare events need many trials, roughly $\frac{100}{p}$ for 10% accuracy.

**Answer:** the estimate has a standard deviation of 0.00099, about 10% of $p$.
:::
:::

::: check
given: m = 2 | 5; a = 10 | 20
answer: m/a
tolerance: 0.5%

## A current is never negative and averages {m} mA. What upper limit does Markov's inequality put on the chance it exceeds {a} mA?

::: right
Right. $\frac{E[X]}{a}$ = {answer}.
:::

::: misses
- [a/m] Upside down.
- [any] Not yet. $P(X \ge a) \le E[X]/a$.
:::
:::

::: check
given: k = 2 | 3 | 4
answer: 1/k^2
tolerance: 0.5%

## For any distribution, what is the most the chance of landing {k} or more standard deviations from the mean can be?

::: right
Right. Chebyshev: $\frac{1}{k^2}$ = {answer}.
:::

::: misses
- [1/k] Chebyshev's bound falls as $1/k^2$.
- [2*Q(k)] That's the Gaussian value. For an unknown distribution the guarantee is weaker.
- [any] Not yet. Chebyshev's inequality.
:::
:::

::: check
given: s = 4 | 10; n = 16 | 100
answer: s/sqrt(n)
unit: mV
tolerance: 0.5%

## Readings have noise {s} mV RMS, independently. What is the standard deviation of the average of {n} readings?

::: right
Right. $\frac{\sigma}{\sqrt n}$ = {answer}.
:::

::: misses
- [s/n] Variances shrink as $1/n$, so standard deviations shrink as $1/\sqrt n$.
- [any] Not yet. $\sigma/\sqrt n$.
:::
:::

::: check
given: s = 1 | 2; e = 0.1 | 0.2; d = 0.05 | 0.1
answer: s^2/(e^2*d)
tolerance: 0.5%

## Readings have standard deviation {s}. By Chebyshev, how many are needed so their average misses the mean by {e} or more with probability at most {d}?

::: right
Right. $n = \frac{\sigma^2}{\varepsilon^2\delta}$ = {answer}.
:::

::: misses
- [s^2/(e^2)] Include $\delta$: a smaller allowed miss rate needs more samples.
- [s/(e*d)] Square $\sigma$ and $\varepsilon$.
- [any] Not yet. Solve $\frac{\sigma^2}{n\varepsilon^2} = \delta$.
:::
:::

::: check
given: p = 0.1 | 0.5; n = 100 | 10000
answer: sqrt(p*(1 - p)/n)
tolerance: 0.5%

## A simulation runs {n} independent trials of an event with probability {p}. What is the standard deviation of the estimated probability $k/n$?

::: right
Right. $\sqrt{\frac{p(1 - p)}{n}}$ = {answer}.
:::

::: misses
- [sqrt(n*p*(1 - p))] That's the spread of the count $k$; divide by $n$.
- [any] Not yet. A Bernoulli variable has variance $p(1 - p)$; average $n$ of them.
:::
:::

::: check
given: r = 0.1 | 0.2; b = 1e-6 | 1e-4
answer: 1/(r^2*b)
tolerance: 0.5%

## About how many bits must be sent to measure a BER near {b} with a relative error (1σ) of {r}?

::: right
Right. You need $k = \frac{1}{r^2}$ errors, so $\frac{1}{r^2b}$ = {answer} bits.
:::

::: misses
- [1/b] That gives about one error, a 100% uncertainty.
- [1/(r*b)] The relative error is $\frac{1}{\sqrt k}$, so $k = 1/r^2$.
- [any] Not yet. Errors needed: $1/r^2$.
:::
:::

::: check
kind: parts
given: p = 0.001 | 0.01; N = 10000 | 100000

## A Monte Carlo run estimates a probability near {p} with {N} trials.

::: part
answer: sqrt(p*(1 - p)/N)
tolerance: 0.5%

### What is the estimate's standard deviation?

::: misses
- [p/sqrt(N)] Use $\sqrt{p(1 - p)/N}$.
- [any] The standard error of a proportion.
:::
:::

::: part
answer: sqrt((1 - p)/(p*N))
tolerance: 0.5%

### What is that relative to $p$?

::: misses
- [any] Divide by $p$.
:::
:::
:::

::: check
kind: derivation
vars: s = 0.1..5, n = 1..1000

## Averaging $n$ independent readings, each with standard deviation $s$.

::: step
answer: s/sqrt(n)

### What is the standard deviation of the average?
:::

::: right
Right. The law of large numbers in numbers: the spread falls as $1/\sqrt n$.
:::
:::

::: explain
## Why does the average of many independent readings settle down, while a single reading never does?

::: model
Each reading carries its own random error. In a sum of $n$ readings the errors are independent, so their variances add: the sum's spread grows only as $\sqrt n$ while the sum itself grows as $n$. Dividing by $n$ to form the average leaves a spread of $\sigma/\sqrt n$, which shrinks as more readings come in. Positive and negative errors increasingly cancel. A single reading has no partner errors to cancel with, so its spread stays $\sigma$.
:::
:::

::: practice

::: check
given: s = 3 | 5; e = 1 | 0.5
answer: (2*s/e)^2
tolerance: 0.5%

## Readings have noise σ = {s} mV. Using the Gaussian rule with $z = 2$, how many are needed to pin the mean to within ±{e} mV?

::: misses
- [2*s/e] Square it: $n = (z\sigma/\varepsilon)^2$.
- [any] $n = (z\sigma/\varepsilon)^2$.
:::
:::

::: check
given: k = 25 | 100 | 400
answer: 1/sqrt(k)
tolerance: 0.5%

## An experiment saw {k} errors. Roughly what is the relative uncertainty of the measured error rate?

::: misses
- [1/k] It's $1/\sqrt k$.
- [any] Poisson counts vary by $\sqrt k$.
:::
:::

::: check
given: k = 1.5 | 2 | 2.5 | 3 | 4 | 5
answer: 1/k^2
tolerance: 0.5%

## What is Chebyshev's limit on the chance of a miss of {k} standard deviations or more?

::: misses
- [any] $1/k^2$.
:::
:::
:::
