Lesson 14 of 15

Law of Large Numbers & CLT

Convergence of Sample Averages

Law of Large Numbers (LLN)

If X1,X2,,XnX_1, X_2, \ldots, X_n are i.i.d. with mean μ\mu, then the sample mean converges to μ\mu in probability as nn \to \infty:

Xˉn=1ni=1nXiPμ\bar{X}_n = \frac{1}{n}\sum_{i=1}^n X_i \xrightarrow{P} \mu

The larger your sample, the closer the average is to the true mean.

Central Limit Theorem (CLT)

Even more remarkable: regardless of the original distribution, the standardized sample mean converges to N(0,1)N(0,1):

Xˉnμσ/ndN(0,1)\frac{\bar{X}_n - \mu}{\sigma / \sqrt{n}} \xrightarrow{d} N(0, 1)

This is why the normal distribution appears everywhere — it is the universal limiting shape of averages.

Box-Muller Transform

To generate ZN(0,1)Z \sim N(0,1) from uniform samples U1,U2Uniform(0,1)U_1, U_2 \sim \text{Uniform}(0,1):

Z=2lnU1cos(2πU2)Z = \sqrt{-2 \ln U_1} \cdot \cos(2\pi U_2)

import math, random

rng = random.Random(42)
u1, u2 = rng.random(), rng.random()
z = math.sqrt(-2 * math.log(u1)) * math.cos(2 * math.pi * u2)
# z is a single standard normal sample

Your Task

Implement sample_mean(n, mu, sigma, seed) using Box-Muller with random.Random(seed). Return the sample mean of nn draws from N(μ,σ2)N(\mu, \sigma^2), rounded to 2 decimal places.

Pyodide loading...
Loading...
Click "Run" to execute your code.