mineti.dev

The Feynman experience · Part 1

The law of large numbers

evergreen · Jan 4, 2019

Introduction

Everyone who studies probability runs into at least two limit theorems: the law of large numbers and the central limit theorem. The law of large numbers describes the result of performing the same experiment a large number of times. It is pervasive in probability and shows up everywhere from gambling to risk management.

Examples

Let’s simulate the two usual suspects of any probability example: a coin and a die.

Coin toss

Let’s track the frequency of heads over 1,000 coin tosses. As throughout this series, we work from scratch in plain Python with NumPy:

import numpy as np

np.random.seed(51)

# 1,000 coin tosses, each 0 (tails) or 1 (heads)
coin = np.random.randint(0, 2, 1000)

# Running frequency of heads after each toss
heads_mean = [np.mean(coin[:i + 1]) for i in range(len(coin))]
Running frequency of heads over 1,000 coin tosses: it swings widely at first, then settles onto the expected 0.5.
Over 1,000 tosses the observed frequency of heads settles onto the expected 0.5 (dashed). The early swings are large; they shrink as trials accumulate.

The frequency lurches around early on, then closes in on the expected 0.5 as the tosses pile up.

Throwing dice

The same idea, this time with a variable that isn’t just 0 or 1. A frequency is itself an average — of a variable worth 1 when the event happens and 0 otherwise — so the coin example is a special case. Let’s instead track the running mean of the face values, which should approach the expected value of a fair die, (1+2+⋯+6)/6=3.5(1 + 2 + \dots + 6)/6 = 3.5:

np.random.seed(51)

# 1,000 dice throws, each 1 to 6
dice = np.random.randint(1, 7, 1000)

# Running mean of the face values
dice_mean = [np.mean(dice[:i + 1]) for i in range(len(dice))]
Running mean of the face values over 1,000 dice throws, settling onto the expected 3.5.
The running mean of the face values over 1,000 throws settles onto the expected 3.5 (dashed). The law is about averages of any random variable, not just frequencies of events.

A common confusion

Sometimes you will hear that the law of large numbers means the counts of heads and tails grow closer together as the number of trials increases. That is not what it says — and we can show it. Here is the running difference between the number of heads and the number of tails:

np.random.seed(51)
coin = np.random.randint(0, 2, 1000)

# Cumulative (number of heads) minus (number of tails)
diff = [2 * np.sum(coin[:i + 1]) - (i + 1) for i in range(len(coin))]
The running difference between the number of heads and tails over 1,000 tosses, wandering up and down without settling.
The difference between the number of heads and tails wanders rather than converging. The average converges; the raw counts need not.

And it isn’t just that our sample happens to look unsettled: the difference is a simple random walk, which converges to nothing — its typical distance from zero grows like n\sqrt{n} as trials accumulate.1 The average converges even as the raw count gap tends to grow. A related mistake is to imagine the coin has some kind of memory, so that a long run of tails makes heads more likely; that is the gambler’s fallacy.

Mathematical treatment

Convergence in probability

A sequence of random variables X1,X2,…X_1, X_2, \dots converges in probability to bb if, for every ϵ>0\epsilon > 0,2

lim⁡n→∞P(∣Xn−b∣<ϵ)=1\lim_{n \to \infty} P(|X_n - b| < \epsilon) = 1

also written

Xn→pbX_n \overset{p}{\to} b

Now let X1,X2,…X_1, X_2, \dots be independent and identically distributed (i.i.d.) random variables with expected value E(Xi)=μE(X_i) = \mu, and let Xˉn=1n(X1+⋯+Xn)\bar{X}_n = \frac{1}{n}(X_1 + \dots + X_n) be the average of the first nn of them. The law of large numbers states that this sample average converges in probability to the expected value:3

Xˉn→pμ\bar{X}_n \overset{p}{\to} \mu

as n→∞n \to \infty. This is the weak law of large numbers. Convergence in probability is just one of the ways to define the convergence of a sequence of random variables.4 The strong law of large numbers makes a stronger claim — the sample average converges almost surely to the expected value3 — and its proof is beyond our scope here; Terence Tao has a nice exposition on his blog.5

Proof of the weak law

Assume additionally that every XiX_i has finite variance V(Xi)=σ2V(X_i) = \sigma^2. This assumption is a convenience of the proof below, not a requirement of the law itself, which holds whenever the mean is finite.3 Since the samples are i.i.d.,

V(Xˉn)=V ⁣(1n(X1+X2+⋯+Xn))=1n2V(X1+X2+⋯+Xn)=nσ2n2=σ2n\begin{aligned} V(\bar{X}_n) &= V\!\left(\tfrac{1}{n}(X_1 + X_2 + \dots + X_n)\right) \\ &= \frac{1}{n^2} V(X_1 + X_2 + \dots + X_n) \\ &= \frac{n\sigma^2}{n^2} = \frac{\sigma^2}{n} \end{aligned}

and we know that

E(Xˉn)=μE(\bar{X}_n) = \mu

Chebyshev’s inequality states that, for any random variable YY with finite mean and variance and any ϵ>0\epsilon > 0,2

P(∣Y−E(Y)∣≥ϵ)≤V(Y)ϵ2P(|Y - E(Y)| \geq \epsilon) \leq \frac{V(Y)}{\epsilon^2}

Applying it to Xˉn\bar{X}_n, whose mean is μ\mu and whose variance is σ2/n\sigma^2/n,

P(∣Xˉn−μ∣≥ϵ)≤σ2nϵ2P(|\bar{X}_n - \mu| \geq \epsilon) \leq \frac{\sigma^2}{n\epsilon^2}

Taking the complement,

P(∣Xˉn−μ∣<ϵ)=1−P(∣Xˉn−μ∣≥ϵ)≥1−σ2nϵ2\begin{aligned} P(|\bar{X}_n - \mu| < \epsilon) &= 1 - P(|\bar{X}_n - \mu| \geq \epsilon) \\ &\geq 1 - \frac{\sigma^2}{n\epsilon^2} \end{aligned}

As n→∞n \to \infty the right-hand side goes to 1, and since a probability can never exceed 1, the left-hand side is squeezed to 1 as well, which gives

Xˉn→pμ\bar{X}_n \overset{p}{\to} \mu

The proof also explains the shape of the simulations. The variance of the average shrinks as σ2/n\sigma^2/n, so its typical deviation from μ\mu shrinks like 1/n1/\sqrt{n} — the large early swings and slow final narrowing in the coin and dice plots are exactly this rate at work.

References

  1. Random walk. Wikipedia. https://en.wikipedia.org/wiki/Random_walk ↩

  2. DeGroot, M. H. & Schervish, M. J. Probability and Statistics. (Pearson Education, 2012). ↩ ↩2

  3. Loève, M. Probability Theory. (Springer, 1977). ↩ ↩2 ↩3

  4. Convergence of random variables. Wikipedia. https://en.wikipedia.org/wiki/Convergence_of_random_variables ↩

  5. Tao, T. The strong law of large numbers. What’s new (2008). https://terrytao.wordpress.com/2008/06/18/the-strong-law-of-large-numbers/ ↩

← All articles