Monte Carlo methods
Be able to estimate probabilities and integrals with simulation and judge the number of runs required.
Prerequisites
- CPython — lists, loops and dictionariesrequired
- DProbability distributionsrequired
Intuition
Monte Carlo: when something is hard to compute exactly — simulate it many times and count.
The probability that the sum of three dice is at least 15? You can compute it combinatorially. Or throw them 100 000 times in code and count the share. The answer is just as usable and takes three lines.
The error shrinks as 1/√n. That means:
- 100 runs → an uncertainty of ~10 %
- 10 000 → ~1 %
- 1 000 000 → ~0.1 %
A hundred times more runs for ten times better precision. That is the method's price — and why you do not simulate your way to arbitrary accuracy.
Code
import numpy as np
rng = np.random.default_rng(0)
# 1. A probability: three dice, a sum ≥ 15
throws = rng.integers(1, 7, size=(200_000, 3)).sum(axis=1)
p = float((throws >= 15).mean())
se = float(np.sqrt(p * (1 - p) / len(throws)))
print(f"{p:.4f} ± {1.96*se:.4f}") # 0.0925 ± 0.0013
# 2. An integral: the area under a curve with no simple antiderivative
f = lambda x: np.exp(-x**2)
x = rng.uniform(0, 2, 500_000)
print(round(float(f(x).mean() * 2), 4)) # 0.8821 (exactly: 0.88208)
# 3. The uncertainty in a result (a parametric bootstrap)
acc = rng.binomial(200, 0.85, size=10_000) / 200
print(np.percentile(acc, [2.5, 97.5]).round(3)) # [0.8 0.9]
Where it is used in ML: bootstrap confidence intervals, dropout at inference as an uncertainty estimate (MC dropout), sampling from generative models, the expected utility in decision analysis, and a simulated environment in RL.
Always compute the margin of error. A Monte Carlo figure without an uncertainty is a guess with unnecessarily many decimals.
Mastery means
- Estimates probabilities and integrals with simulation
- Judges how many runs are required
- Explains the 1/√n convergence
Sign in to do the exercises and build your mastery up.
Sources
- Wikipedia — Monte Carlo-metod (CC BY-SA 4.0) — CC BY-SA 4.0