4.9.25Probability Theory & Statistics

Monte Carlo simulation — law of large numbers basis

1,836 words8 min readdifficulty · medium3 backlinks

WHY does Monte Carlo work at all?

WHAT we want: many quantities are secretly expectations.

  • An integral 01g(x)dx=E[g(U)]\int_0^1 g(x)\,dx = \mathbb{E}[g(U)] where UUniform(0,1)U\sim\text{Uniform}(0,1).
  • A probability P(A)=E[1A]P(A) = \mathbb{E}[\mathbf{1}_A] (expectation of an indicator).
  • π\pi, option prices, particle-physics cross-sections — all are averages over randomness.

WHY we can estimate them: if a quantity equals E[X]\mathbb{E}[X], and we draw independent copies X1,,XnX_1,\dots,X_n of XX, the sample mean Xˉn=1nXi\bar X_n=\frac1n\sum X_i should "settle down" to E[X]\mathbb{E}[X]. That settling-down is the Law of Large Numbers.

HOW we use it: generate random samples → plug each into the function → average. Done.


The Law of Large Numbers (derived, not dumped)

Deriving it from first principles

Step 1 — Mean of the sample mean. Why? To know what it's centered on. E[Xˉn]=1ni=1nE[Xi]=1nnμ=μ.\mathbb{E}[\bar X_n]=\frac1n\sum_{i=1}^n\mathbb{E}[X_i]=\frac1n\cdot n\mu=\mu. Why this step? Expectation is linear, so it passes through the sum. The estimator is unbiased.

Step 2 — Variance of the sample mean. Why? To measure spread shrinking. Var(Xˉn)=1n2Var(i=1nXi)=1n2i=1nVar(Xi)=1n2nσ2=σ2n.\operatorname{Var}(\bar X_n)=\frac{1}{n^2}\operatorname{Var}\Big(\sum_{i=1}^n X_i\Big)=\frac{1}{n^2}\sum_{i=1}^n\operatorname{Var}(X_i)=\frac{1}{n^2}\cdot n\sigma^2=\frac{\sigma^2}{n}. Why this step? Independence lets variance of a sum equal the sum of variances (cross-covariances vanish). Pulling out 1n2\frac1{n^2} uses Var(aY)=a2Var(Y)\operatorname{Var}(aY)=a^2\operatorname{Var}(Y).

Step 3 — Chebyshev's inequality. Why? It bounds "how often far from the mean" using only variance. P(Xˉnμε)Var(Xˉn)ε2=σ2nε2.P\big(|\bar X_n-\mu|\ge\varepsilon\big)\le\frac{\operatorname{Var}(\bar X_n)}{\varepsilon^2}=\frac{\sigma^2}{n\varepsilon^2}. Why this step? Chebyshev says deviations bigger than ε\varepsilon are rare when variance is small.

Step 4 — Take the limit. As nn\to\infty, σ2nε20\dfrac{\sigma^2}{n\varepsilon^2}\to0. The squeeze forces the probability to 0. \blacksquare


The Monte Carlo estimator and its error

WHY 1/n1/\sqrt n matters: to get one more decimal digit of accuracy (10× smaller error) you need 100× more samples. Dimension-independence is the payoff: this 1/n1/\sqrt n holds whether XX is 1-D or 1000-D, which is why Monte Carlo crushes grid methods in high dimensions.

Figure — Monte Carlo simulation — law of large numbers basis

Worked Example 1 — Estimating π\pi

WHAT: Throw random darts in the unit square [0,1]2[0,1]^2. Fraction landing inside the quarter-circle (x2+y21x^2+y^2\le1) estimates the area π/4\pi/4.

  • Let X=1{U12+U221}X=\mathbf{1}\{U_1^2+U_2^2\le1\} with U1,U2Unif(0,1)U_1,U_2\sim\text{Unif}(0,1).
  • E[X]=P(inside)=area of quarter diskarea of square=π/41=π4.\mathbb{E}[X]=P(\text{inside})=\dfrac{\text{area of quarter disk}}{\text{area of square}}=\dfrac{\pi/4}{1}=\dfrac\pi4. Why? Uniform points → probability = area ratio.

Estimator: π^=4Xˉn\hat\pi = 4\bar X_n. Why ×4? Because Xˉnπ/4\bar X_n\to\pi/4, multiply by 4 to recover π\pi.

Error: XX is Bernoulli(p=π/40.785p=\pi/4\approx0.785), so σg2=p(1p)0.169\sigma_g^2=p(1-p)\approx0.169. Then SE(π^)=40.169/n\text{SE}(\hat\pi)=4\cdot\sqrt{0.169/n}. For n=104n=10^4, SE 0.016\approx 0.016 — so π^\hat\pi good to ~2 decimals. Why so slow? The fatal 1/n1/\sqrt n.


Worked Example 2 — A 1-D integral

Estimate θ=01ex2dx\theta=\int_0^1 e^{-x^2}\,dx (no elementary antiderivative).

  • Write θ=E[eU2]\theta=\mathbb{E}[e^{-U^2}], UUnif(0,1)U\sim\text{Unif}(0,1). Why? 01g(x)dx=E[g(U)]\int_0^1 g(x)\,dx=\mathbb{E}[g(U)] because the Uniform density is 1 on [0,1][0,1].
  • Draw U1,,UnU_1,\dots,U_n, compute θ^n=1neUi2\hat\theta_n=\frac1n\sum e^{-U_i^2}. Why? It's the sample mean of g(U)g(U); LLN ⇒ θ0.7468\to\theta\approx0.7468.
  • Standard error: estimate σ^g\hat\sigma_g from the samples, report θ^n±1.96σ^g/n\hat\theta_n\pm1.96\,\hat\sigma_g/\sqrt n. Why? CLT gives the interval; honesty requires error bars.

Worked Example 3 — Forecast-then-Verify

Forecast: I run the π\pi estimate with n=100n=100 then n=10000n=10000. How much should the error shrink? Ratio of nn is 100, so 100=10\sqrt{100}=10. Predict: error drops by ~10×.

Verify (typical run): n=100n=100 \Rightarrow error 0.16\sim0.16; n=10000n=10000\Rightarrow error 0.016\sim0.016. ✔ Matches the 1/n1/\sqrt n law. Why this matters: it lets you plan compute budget before running anything.


Common Mistakes (Steel-manned)


Recall Feynman: explain to a 12-year-old

Imagine you want to know what fraction of a giant pizza is pepperoni, but you can't see the whole thing. You blindly poke it with a toothpick lots of times and write down "pepperoni / not pepperoni" each time. After many pokes, the fraction of pepperoni pokes is close to the real answer. The more pokes, the closer — but to be twice as sure you need four times the pokes. That's Monte Carlo, and the "many pokes get you close" guarantee is the Law of Large Numbers.


Active Recall

What does the Weak Law of Large Numbers state?
The sample mean Xˉn\bar X_n of i.i.d. variables converges in probability to the true mean μ\mu: P(Xˉnμε)0P(|\bar X_n-\mu|\ge\varepsilon)\to0 as nn\to\infty.
Why is the Monte Carlo estimator unbiased?
By linearity of expectation, E[Xˉn]=1nE[Xi]=μ\mathbb{E}[\bar X_n]=\frac1n\sum\mathbb{E}[X_i]=\mu, so its expected value equals the target.
Derive the variance of the sample mean.
Var(Xˉn)=1n2Var(Xi)=nσ2n2=σ2n\operatorname{Var}(\bar X_n)=\frac1{n^2}\sum\operatorname{Var}(X_i)=\frac{n\sigma^2}{n^2}=\frac{\sigma^2}{n}, using independence and Var(aY)=a2Var(Y)\operatorname{Var}(aY)=a^2\operatorname{Var}(Y).
What is the standard error of a Monte Carlo estimate?
SE=σg/n\text{SE}=\sigma_g/\sqrt n, where σg2=Var(g(X))\sigma_g^2=\operatorname{Var}(g(X)).
To get 10× smaller Monte Carlo error, how many more samples are needed?
100× as many (because error 1/n\propto 1/\sqrt n).
Which inequality powers the WLLN proof and what does it bound?
Chebyshev's inequality: P(Xˉnμε)Var(Xˉn)/ε2P(|\bar X_n-\mu|\ge\varepsilon)\le \operatorname{Var}(\bar X_n)/\varepsilon^2.
How do you estimate π\pi by Monte Carlo?
Throw uniform points in [0,1]2[0,1]^2, take fraction inside x2+y21x^2+y^2\le1 (= π/4\pi/4), multiply by 4: π^=4Xˉn\hat\pi=4\bar X_n.
How is an integral 01g(x)dx\int_0^1 g(x)dx written as an expectation?
As E[g(U)]\mathbb{E}[g(U)] with UUnif(0,1)U\sim\text{Unif}(0,1), since the Uniform density is 1 on [0,1][0,1].
When can Monte Carlo / LLN fail?
When samples are not independent, or the integrand has infinite mean/variance (heavy tails), so the sample mean doesn't converge nicely.
Why is Monte Carlo preferred in high dimensions?
Its 1/n1/\sqrt n error is independent of dimension, unlike grid methods whose cost explodes with dimension.

Connections

  • Central Limit Theorem — gives the confidence interval / error bars.
  • Chebyshev's Inequality — the engine of the WLLN proof.
  • Strong Law of Large Numbers — almost-sure convergence (stronger than WLLN).
  • Variance and Covariance — why independence makes variance add.
  • Importance Sampling — reduces σg\sigma_g to beat the 1/n1/\sqrt n wall.
  • Numerical Integration — the deterministic alternative Monte Carlo competes with.
  • Bernoulli Distribution — model for indicator-based estimates like π\pi.

Concept Map

integral = E[g(U)]

P(A) = E[indicator]

averages n samples

justified by

derived from

derived from

uses independence

derived from

take limit n to infinity

guarantees

error scales

governs

Quantities as expectations

Monte Carlo estimator

Sample mean X-bar

Weak Law of Large Numbers

Step 1 mean = mu, unbiased

Step 2 variance = sigma^2 / n

Step 3 Chebyshev inequality

Convergence in probability

Accuracy like 1 over sqrt n

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, Monte Carlo ka idea bahut simple hai: jab kisi cheez ka average ya integral seedha calculate nahi kar paate, to hum usko randomly simulate karke, bahut saare samples ka average le lete hain. Maths ki guarantee yeh hai ki agar samples i.i.d. hain, to sample mean Xˉn\bar X_n dheere-dheere true mean μ\mu ke paas pahunch jaata hai. Isi guarantee ko Law of Large Numbers kehte hain.

Proof ki feel samajh lo: sample mean ka expectation exactly μ\mu hota hai (matlab unbiased), aur uska variance σ2/n\sigma^2/n hota hai. Jaise-jaise nn badhta hai, variance gir jaata hai, to estimate spread chhota hota jaata hai. Chebyshev inequality lagao aur nn\to\infty par probability of "far from mean" zero ho jaati hai. Bas yahi pura khel hai.

Sabse important practical baat: error 1/n1/\sqrt n ki tarah girta hai, 1/n1/n ki tarah nahi. Iska matlab — agar 10 guna zyada accuracy chahiye, to 100 guna zyada samples lagenge. Isliye Monte Carlo thoda slow hota hai, par high-dimensional problems mein ekdum zabardast, kyunki yeh 1/n1/\sqrt n dimension par depend nahi karta.

Example yaad rakho: π\pi nikalne ke liye square mein random points daalo, jo quarter-circle ke andar girein unki fraction π/4\pi/4 deti hai, 4 se multiply karke π\pi mil jaata hai. Aur haan — answer ke saath hamesha error bar (±1.96σ^/n\pm1.96\,\hat\sigma/\sqrt n) likhna, kyunki Monte Carlo ka result random hota hai, sirf ek number likhna galti hai.

Go deeper — visual, from zero

Test yourself — Probability Theory & Statistics

Connections