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.
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
}