The Feynman experience · Part 1
The law of large numbers
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))]
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, :
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))]
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))]
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 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 converges in probability to if, for every ,2
also written
Now let be independent and identically distributed (i.i.d.) random variables with expected value , and let be the average of the first of them. The law of large numbers states that this sample average converges in probability to the expected value:3
as . 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 has finite variance . 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.,
and we know that
Chebyshev’s inequality states that, for any random variable with finite mean and variance and any ,2
Applying it to , whose mean is and whose variance is ,
Taking the complement,
As 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
The proof also explains the shape of the simulations. The variance of the average shrinks as , so its typical deviation from shrinks like — the large early swings and slow final narrowing in the coin and dice plots are exactly this rate at work.
References
-
Random walk. Wikipedia. https://en.wikipedia.org/wiki/Random_walk ↩
-
DeGroot, M. H. & Schervish, M. J. Probability and Statistics. (Pearson Education, 2012). ↩ ↩2
-
Convergence of random variables. Wikipedia. https://en.wikipedia.org/wiki/Convergence_of_random_variables ↩
-
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/ ↩