Chapter 10
30 min read
Section 66 of 175

Law of Large Numbers

Fundamental Theorems

Learning Objectives

By the end of this section, you will be able to:

  1. State and distinguish between the Weak and Strong Laws of Large Numbers, understanding their precise mathematical formulations and what each guarantees.
  2. Apply the LLN to calculate sample sizes needed for desired accuracy in estimation problems (polls, A/B tests, Monte Carlo simulations).
  3. Explain why SGD, mini-batch training, and empirical risk minimization work in deep learning, connecting them directly to the LLN.
  4. Recognize when the LLN fails (infinite variance, Cauchy distributions) and understand the implications for robust ML systems.
  5. Calculate the convergence rate of sample means and explain the 1/√n rule that governs estimation precision.
  6. Implement LLN-based algorithms in Python, including Monte Carlo integration and sample size calculators.
Why This Matters for AI/ML Engineers: The Law of Large Numbers is the theoretical foundation for why machine learning works. Every time you compute a loss over a mini-batch, average predictions in an ensemble, or use Monte Carlo methods for Bayesian inference, you are implicitly relying on the LLN. Understanding it deeply will help you design better experiments, choose appropriate batch sizes, and debug training instabilities.

Prerequisites: Convergence Concepts from Chapter 9

This section builds on the convergence concepts from Chapter 9. The Weak Law uses convergence in probability (Section 9.1), while the Strong Law uses almost sure convergence (Section 9.2). If you're unfamiliar with these modes of convergence, review Chapter 9 first.


The Story: From Gambling to Science

The year is 1713. Jacob Bernoulli, a Swiss mathematician, has spent 20 years working on a theorem that would revolutionize probability theory. He called it his “Golden Theorem” (“Theorema Aureum”). The problem he was trying to solve: Can we learn the true probability of an event by observing outcomes?

Consider a biased coin with unknown probability pp of landing heads. If we flip it many times and count the proportion of heads, Bernoulli asked: will this proportion eventually reveal the true pp?

His answer was yes, but proving it rigorously took him until his death in 1705. The result was published posthumously in 1713 in his masterwork Ars Conjectandi(The Art of Conjecturing). We now call this result the Law of Large Numbers.

Historical Note: Later contributions by Poisson (1837), Chebyshev (1867), Markov (1898), Kolmogorov (1933), and others refined and extended Bernoulli's original theorem. The distinction between “Weak” and “Strong” LLN was clarified in the 20th century.

The profound implication of the LLN is this: mathematics bridges the gap between probability (what we expect) and statistics (what we observe). This is why we can learn from data, why polls predict elections, why insurance companies stay solvent, and why neural networks trained on samples generalize to unseen data.


Building Intuition

Why Averages “Work”

Imagine you want to know the average height of adults in a city. You cannot measure everyone, so you sample 100 people and compute their average height. Intuitively, you believe this sample average is “close to” the true population average.

But why should this work? What if your sample is unlucky and contains mostly tall basketball players or short jockeys? The LLN provides the mathematical guarantee: as your sample size grows, the probability of such “unlucky” samples becomes vanishingly small.

The visualization above shows this beautifully. With 10 samples, the running average can wander significantly. With 100 samples, it's closer to the true mean. With 500 samples, it's almost exactly on target. The LLN says: no matter how the randomness plays out, the average eventually converges.

The Core Insight: Individual random samples are unpredictable, but their average becomes increasingly predictable as the sample size grows. Randomness “cancels out” when we aggregate.

Formal Definitions

Let X1,X2,X3,X_1, X_2, X_3, \ldots be a sequence of independent and identically distributed (i.i.d.) random variables with common mean μ=E[Xi]\mu = E[X_i] and variance σ2=Var(Xi)\sigma^2 = \text{Var}(X_i). Define the sample mean:

Xˉn=1ni=1nXi\bar{X}_n = \frac{1}{n} \sum_{i=1}^{n} X_i

Weak Law of Large Numbers (WLLN)

Theorem (WLLN): For any ϵ>0\epsilon > 0:limnP(Xˉnμ>ϵ)=0\lim_{n \to \infty} P\left( \left| \bar{X}_n - \mu \right| > \epsilon \right) = 0

Equivalently: XˉnPμ\bar{X}_n \xrightarrow{P} \mu (convergence in probability)

What this says: For any tolerance ϵ\epsilon you choose (no matter how small), the probability that the sample mean differs from the true mean by more than ϵ\epsilon goes to zero as nn \to \infty.

Strong Law of Large Numbers (SLLN)

Theorem (SLLN): With probability 1:P(limnXˉn=μ)=1P\left( \lim_{n \to \infty} \bar{X}_n = \mu \right) = 1

Equivalently: Xˉna.s.μ\bar{X}_n \xrightarrow{a.s.} \mu (almost sure convergence)

What this says: The sample mean converges to the true mean for almost every possible sequence of samples. Unlike the WLLN, which says the probability of deviation shrinks, the SLLN says the deviation actually becomes zero (with probability 1) as nn \to \infty.

Symbol-by-Symbol Breakdown

SymbolMeaningIntuition
X_iThe i-th random sampleOne observation from the population
μ = E[X]Population mean (expectation)The true average we want to estimate
σ² = Var(X)Population varianceMeasures spread/uncertainty of samples
X̄_nSample mean of n observationsOur estimate of μ from data
ε (epsilon)Tolerance thresholdHow close we need to be to declare success
P(·)Probability of an eventQuantifies likelihood
lim as n→∞Limit as sample size growsWhat happens with infinite data
→^PConvergence in probabilityWLLN guarantee: prob. of error → 0
→^{a.s.}Almost sure convergenceSLLN guarantee: error → 0 with prob. 1

Interactive: Weak vs Strong LLN

The key difference between Weak and Strong LLN is subtle but important. The WLLN says most sequences will be close to the mean at any given nn. The SLLN says every sequence eventually converges and stays close forever.


Convergence Rate: The 1/√n Rule

The LLN tells us that Xˉnμ\bar{X}_n \to \mu, but how fast? The answer comes from the variance of the sample mean:

Var(Xˉn)=Var(1ni=1nXi)=1n2nσ2=σ2n\text{Var}(\bar{X}_n) = \text{Var}\left(\frac{1}{n}\sum_{i=1}^n X_i\right) = \frac{1}{n^2} \cdot n\sigma^2 = \frac{\sigma^2}{n}

The standard error of the sample mean is:

SE(Xˉn)=σnSE(\bar{X}_n) = \frac{\sigma}{\sqrt{n}}
The 1/√n Rule: The standard error decreases as 1/n1/\sqrt{n}. This means:
  • To halve the error, you need 4× more samples
  • To reduce error by 10×, you need 100× more samples
  • High-variance distributions require more samples for the same accuracy

Proof Sketches

WLLN via Chebyshev's Inequality

The simplest proof of the WLLN uses Chebyshev's inequality, which we studied in Chapter 3. Recall that for any random variable YY with finite variance:

P(YE[Y]>ϵ)Var(Y)ϵ2P(|Y - E[Y]| > \epsilon) \leq \frac{\text{Var}(Y)}{\epsilon^2}

Apply this to Xˉn\bar{X}_n:

  1. E[Xˉn]=μE[\bar{X}_n] = \mu (the sample mean is an unbiased estimator)
  2. Var(Xˉn)=σ2/n\text{Var}(\bar{X}_n) = \sigma^2/n (as computed above)
  3. By Chebyshev: P(Xˉnμ>ϵ)σ2nϵ2P(|\bar{X}_n - \mu| > \epsilon) \leq \frac{\sigma^2}{n\epsilon^2}
  4. As nn \to \infty, the right side 0\to 0
Proof Complete: limnP(Xˉnμ>ϵ)=0\lim_{n \to \infty} P(|\bar{X}_n - \mu| > \epsilon) = 0
This proof requires finite variance. More advanced versions of the WLLN (Khinchin's theorem) only require finite mean.

SLLN: Key Ideas

The Strong LLN is harder to prove and typically uses one of these approaches:

  • Borel-Cantelli Lemma: Show that the set of “bad” sequences (those that don't converge) has probability zero.
  • Kolmogorov's approach: Use truncation arguments and the three-series theorem.
  • Martingale methods: Use the martingale convergence theorem.

The key insight is that while individual deviations can happen, the accumulation of infinitely many deviations is impossible (has probability zero).


When the LLN Fails

The LLN requires finite mean. Some distributions violate this condition, and for them, the sample mean never settles down:

The Cauchy Distribution: The Cauchy distribution has no mean (the integral diverges). Its sample mean has the same distribution regardless of nn! This is a famous counterexample showing the LLN's assumptions are necessary.

Implications for ML:

  • Heavy-tailed gradient distributions can cause unstable training
  • Gradient clipping prevents extreme values from dominating updates
  • Robust loss functions (Huber loss) handle outliers better
  • Some financial returns are heavy-tailed; naive averaging is dangerous

Real-World Examples

Example 1: Political Polling

Problem: A pollster wants to estimate the proportion pp of voters supporting Candidate A. They survey n=1000n = 1000 randomly selected voters.

Solution: Let Xi=1X_i = 1 if voter ii supports A, 0 otherwise. Then:

  • E[Xi]=pE[X_i] = p (the true proportion)
  • Xˉn=p^\bar{X}_n = \hat{p} (sample proportion)
  • By LLN: p^p\hat{p} \to p as nn \to \infty
  • Standard error: SE=p(1p)/n0.5/10000.016SE = \sqrt{p(1-p)/n} \leq 0.5/\sqrt{1000} \approx 0.016

With 1000 respondents, the poll is accurate to about ±3% (using a 95% confidence interval of approximately ±2×SE±0.032\pm 2 \times SE \approx \pm 0.032).

Example 2: Insurance Actuarial Science

Problem: An insurance company wants to set premiums for car insurance. They analyze 100,000 policies to estimate the average claim amount.

Solution: Let XiX_i be the claim amount for policy ii. The sample mean Xˉn\bar{X}_n approximates the true expected claim E[X]E[X].

Why This Works: The LLN guarantees that with 100,000 policies, the sample mean is extremely close to the true mean. The company can confidently set premiums at Xˉn+margin\bar{X}_n + \text{margin} knowing they will be profitable on average.

Example 3: Quality Control in Manufacturing

Problem: A factory produces ball bearings that must have diameter 10mm ± 0.1mm. They sample 50 bearings per hour to monitor the process.

Solution: Track Xˉn\bar{X}_n over time. By the LLN:

  • If the process is stable, Xˉn10\bar{X}_n \approx 10mm
  • If Xˉn\bar{X}_n drifts, the machine needs recalibration
  • Standard error helps set control limits for detecting problems

AI/ML Applications (Critical)

The Law of Large Numbers is not just theoretically important—it is the foundation of modern machine learning. Here are the key connections:

Stochastic Gradient Descent

In deep learning, we minimize the expected loss:

L(θ)=E(x,y)P[(fθ(x),y)]L(\theta) = E_{(x,y) \sim P}[\ell(f_\theta(x), y)]

We cannot compute this expectation exactly (we don't know PP), so we approximate it with a sample:

L^(θ)=1ni=1n(fθ(xi),yi)\hat{L}(\theta) = \frac{1}{n}\sum_{i=1}^n \ell(f_\theta(x_i), y_i)
LLN Guarantee: By the LLN, L^(θ)L(θ)\hat{L}(\theta) \to L(\theta) as nn \to \infty. This is why training on finite data works! The mini-batch gradient is an unbiased estimate of the true gradient.

Batch size implications:

  • Larger batches → lower variance gradient estimates → smoother training
  • Smaller batches → more noise → can help escape local minima
  • The 1/√n rule tells us doubling batch size halves the gradient noise

Monte Carlo Methods

Many ML computations require intractable integrals:

  • Bayesian posterior: p(θD)=p(Dθ)p(θ)p(Dθ)p(θ)dθp(\theta|D) = \frac{p(D|\theta)p(\theta)}{\int p(D|\theta)p(\theta)d\theta}
  • Expected values in RL: Eπ[Gt]E_\pi[G_t]
  • Variational bounds: Eq[logp(xz)]E_{q}[\log p(x|z)]

Monte Carlo estimation uses the LLN: sample from the distribution and average:

E[f(X)]1ni=1nf(Xi)E[f(X)] \approx \frac{1}{n}\sum_{i=1}^n f(X_i)

Batch Normalization

Batch normalization computes running statistics:

μ^=1Bi=1Bxi,σ^2=1Bi=1B(xiμ^)2\hat{\mu} = \frac{1}{B}\sum_{i=1}^B x_i, \quad \hat{\sigma}^2 = \frac{1}{B}\sum_{i=1}^B (x_i - \hat{\mu})^2

By the LLN, these estimates converge to the true activation statistics. During training, an exponential moving average tracks the population statistics for inference.

Cross-Validation

K-fold cross-validation averages performance across K validation sets:

CV Score=1Kk=1KScorek\text{CV Score} = \frac{1}{K}\sum_{k=1}^K \text{Score}_k

The LLN justifies this: as we test on more validation examples (larger K, or within each fold), the estimated performance converges to the true generalization performance.


Sample Size Calculator

A practical application of the LLN is determining how many samples you need for a desired level of accuracy. This calculator uses both the Chebyshev bound and the CLT-based normal approximation:


Python Implementation

Here is a complete Python implementation demonstrating the Law of Large Numbers, including multiple distributions and convergence rate verification:

Law of Large Numbers: Complete Python Implementation
🐍lln_demonstration.py
1Import NumPy

NumPy is essential for efficient numerical computations and random number generation.

7Function Definition

This function demonstrates the LLN by generating multiple sequences of samples and tracking their running averages.

9Distribution Parameter

The LLN works for any distribution with finite mean. We test Normal, Uniform, and Exponential.

11Number of Samples

More samples = better convergence. The LLN guarantees the average converges as n → ∞.

13Multiple Sequences

Multiple independent sequences help visualize that ALL sequences converge (Strong LLN).

26Distribution Dictionary

Each entry contains a sampling function and the true mean. The LLN says sample mean → true mean.

EXAMPLE
normal: mean=0, uniform: mean=0.5, exponential: mean=1
34Running Averages Array

Pre-allocate array to store the running average at each sample size for each sequence.

37Generate Samples

Draw n_samples from the distribution. Each sample is an independent draw.

38Cumulative Sum Trick

Running average at position k = cumsum(samples)[k] / k. This is computationally efficient O(n).

EXAMPLE
cumsum([1,2,3]) = [1,3,6] → averages = [1, 1.5, 2]
51Plot Convergence

Visualization function showing how multiple sequences all converge to the same true mean.

56Plot Each Sequence

Each colored line is an independent sequence. They all approach the green dashed line (true mean).

73Final Error

Average absolute error of final sample means. Should be small due to LLN convergence.

88Convergence Rate Analysis

The standard error decreases as 1/√n. This is the theoretical convergence rate from variance of sample mean = σ²/n.

92Verify 1/√n Rate

Empirical SE should match σ/√n. At n=100: SE≈0.1, at n=10000: SE≈0.01.

EXAMPLE
σ=1, n=100 → SE=1/10=0.1
87 lines without explanation
1import numpy as np
2import matplotlib.pyplot as plt
3from typing import List, Tuple
4
5def demonstrate_lln(
6    distribution: str = "normal",
7    n_samples: int = 1000,
8    n_sequences: int = 5,
9    seed: int = 42
10) -> Tuple[np.ndarray, np.ndarray, float]:
11    """
12    Demonstrate the Law of Large Numbers with multiple sequences.
13
14    Parameters:
15    -----------
16    distribution : str
17        The distribution to sample from ("normal", "uniform", "exponential")
18    n_samples : int
19        Maximum number of samples per sequence
20    n_sequences : int
21        Number of independent sequences to generate
22    seed : int
23        Random seed for reproducibility
24
25    Returns:
26    --------
27    Tuple containing running averages, sample sizes, and true mean
28    """
29    np.random.seed(seed)
30
31    # Define distributions and their true means
32    distributions = {
33        "normal": (lambda n: np.random.normal(0, 1, n), 0.0),
34        "uniform": (lambda n: np.random.uniform(0, 1, n), 0.5),
35        "exponential": (lambda n: np.random.exponential(1, n), 1.0),
36    }
37
38    sampler, true_mean = distributions[distribution]
39
40    # Generate samples and compute running averages
41    running_averages = np.zeros((n_sequences, n_samples))
42
43    for seq in range(n_sequences):
44        samples = sampler(n_samples)
45        running_averages[seq] = np.cumsum(samples) / np.arange(1, n_samples + 1)
46
47    sample_sizes = np.arange(1, n_samples + 1)
48
49    return running_averages, sample_sizes, true_mean
50
51def plot_lln_convergence(
52    running_averages: np.ndarray,
53    sample_sizes: np.ndarray,
54    true_mean: float,
55    title: str = "Law of Large Numbers Demonstration"
56) -> None:
57    """Plot the convergence of sample means to the true mean."""
58
59    plt.figure(figsize=(12, 6))
60
61    # Plot each sequence
62    for i, avg in enumerate(running_averages):
63        plt.plot(sample_sizes, avg, alpha=0.7, linewidth=1.5,
64                 label=f"Sequence {i+1}")
65
66    # True mean line
67    plt.axhline(y=true_mean, color="green", linestyle="--",
68                linewidth=2, label=f"True Mean = {true_mean}")
69
70    plt.xlabel("Sample Size (n)", fontsize=12)
71    plt.ylabel("Sample Mean", fontsize=12)
72    plt.title(title, fontsize=14)
73    plt.legend(loc="upper right")
74    plt.grid(True, alpha=0.3)
75
76    # Add convergence annotation
77    final_error = np.abs(running_averages[:, -1] - true_mean).mean()
78    plt.annotate(f"Avg Final Error: {final_error:.4f}",
79                 xy=(0.02, 0.98), xycoords="axes fraction",
80                 fontsize=10, verticalalignment="top",
81                 bbox=dict(boxstyle="round", facecolor="wheat", alpha=0.5))
82
83    plt.tight_layout()
84    plt.show()
85
86# Example usage
87if __name__ == "__main__":
88    # Demonstrate for different distributions
89    for dist in ["normal", "uniform", "exponential"]:
90        avgs, sizes, mu = demonstrate_lln(dist, n_samples=500)
91        plot_lln_convergence(avgs, sizes, mu,
92            title=f"LLN: {dist.capitalize()} Distribution")
93
94    # Show convergence rate (1/sqrt(n))
95    print("\n=== Convergence Rate Analysis ===")
96    avgs, sizes, mu = demonstrate_lln("normal", n_samples=10000, n_sequences=100)
97    std_errors = np.std(avgs, axis=0)
98
99    print(f"At n=100:   SE = {std_errors[99]:.4f}, Theory: {1/np.sqrt(100):.4f}")
100    print(f"At n=1000:  SE = {std_errors[999]:.4f}, Theory: {1/np.sqrt(1000):.4f}")
101    print(f"At n=10000: SE = {std_errors[9999]:.4f}, Theory: {1/np.sqrt(10000):.4f}")
Try it yourself: Run this code with different distributions and sample sizes. Observe how the running average converges to the true mean, and verify that the standard error decreases as 1/√n.

Common Misconceptions


Test Your Understanding


Key Takeaways

  1. The LLN bridges probability and statistics: It guarantees that sample averages converge to population expectations, justifying empirical estimation.
  2. Two versions exist: The WLLN gives convergence in probability; the SLLN gives almost sure convergence (stronger guarantee).
  3. Convergence rate is 1/√n: Standard error = σ/√n, so halving error requires 4× more samples.
  4. Finite mean is required: Distributions like Cauchy violate the LLN because they have no mean.
  5. Foundation of ML: SGD, Monte Carlo, batch normalization, and cross-validation all rely fundamentally on the LLN.
  6. Not magic: The LLN does not make future samples compensate for past ones (Gambler's Fallacy). It works through dilution.

Summary

The Law of Large Numbers is one of the most important theorems in probability theory and forms the theoretical backbone of statistical inference and machine learning. It tells us that sample averages converge to expected values as sample size increases, with convergence rate proportional to 1/√n.

We explored both the Weak LLN (convergence in probability) and Strong LLN (almost sure convergence), understood their proofs via Chebyshev's inequality, and saw cases where the LLN fails (Cauchy distribution).

Most importantly, we connected the LLN to modern AI/ML: it justifies why stochastic gradient descent works, why Monte Carlo methods are reliable, and why we can learn from finite datasets. The next section will explore the Central Limit Theorem, which tells us not just that the sample mean converges, but how it is distributed along the way.

Next Up: The Central Limit Theorem explains why the Normal distribution appears everywhere—it's the limit distribution of sums of random variables, regardless of their original distribution.
Loading comments...