← back

Demonstrate Law of Large Numbers with Sampling

#342 · Statistics · Easy

⊣ Solve on deep-ml.com

Problem

Demonstrate the Law of Large Numbers by sampling from a distribution and showing that the sample mean converges to the population mean as sample size increases.

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import numpy as np
from typing import Dict, List

def law_of_large_numbers(
    distribution: str = "normal",
    params: dict = None,
    sample_sizes: List[int] = None,
    n_trials: int = 1000,
    seed: int = 42
) -> Dict:
    np.random.seed(seed)

    if params is None:
        params = {"loc": 5.0, "scale": 2.0}
    if sample_sizes is None:
        sample_sizes = [10, 50, 100, 500, 1000, 5000, 10000]

    # Determine true mean
    if distribution == "normal":
        true_mean = params.get("loc", 0)
        sampler = lambda size: np.random.normal(
            params.get("loc", 0), params.get("scale", 1), size
        )
    elif distribution == "uniform":
        low, high = params.get("low", 0), params.get("high", 1)
        true_mean = (low + high) / 2
        sampler = lambda size: np.random.uniform(low, high, size)
    elif distribution == "exponential":
        scale = params.get("scale", 1.0)
        true_mean = scale
        sampler = lambda size: np.random.exponential(scale, size)
    else:
        raise ValueError(f"Unsupported distribution: {distribution}")

    results = []
    for n in sample_sizes:
        means = [float(np.mean(sampler(n))) for _ in range(n_trials)]
        avg_mean = np.mean(means)
        std_mean = np.std(means)
        results.append({
            "sample_size": n,
            "average_sample_mean": round(float(avg_mean), 4),
            "std_of_means": round(float(std_mean), 4),
            "error": round(float(abs(avg_mean - true_mean)), 6)
        })

    return {
        "true_mean": true_mean,
        "distribution": distribution,
        "results": results
    }

Explanation

  1. Choose a distribution with a known true mean (normal, uniform, or exponential).
  2. For each sample size, draw many independent samples of that size and compute the sample mean of each.
  3. Average these sample means and track the standard deviation. As sample size grows, the average converges to the true mean and the standard deviation shrinks.
  4. The Law of Large Numbers states that the sample mean converges in probability to the expected value.

Complexity

  • Time: O(T S N_max) where T is trials, S is the number of sample sizes, N_max is the largest sample size
  • Space: O(N_max) for storing samples