Lensa ML

How Monte Carlo Works

Step 1 of 6: Random Sampling

Drawing observations from a distribution

Number of samples (N)30
0.00.10.20.30.40.50.60.70.80.91.0ValueCountmean=0.556
N samples
30
Mean
0.5561
Std dev
0.2672

With more samples, the histogram starts to look like a uniform distribution, and the mean settles closer to 0.5.

Drag Number of samples (N) from 5 to 200. Each value comes from a pseudo-random generator, a deterministic formula whose output looks uniform on [0, 1], so the same N always gives the same histogram. Real libraries use generators such as the Mersenne Twister or PCG, seeded so that experiments can be repeated. Samples from other distributions are built from these uniforms. Inverse transform sampling, for example, turns a uniform u into −ln(1 − u)/λ, an exponential sample with rate λ, by inverting that distribution's CDF.

Step 2 of 6: Law of Large Numbers

Sample means converge to the true mean

Number of samples (N)50
0.5 ± 0.050.20.350.50.650.8Sample numberRunning mean
N
50
Running mean
0.4594
True mean
0.5000
Error
0.0406

The running mean is settling down and approaching 0.5. Fluctuations are smaller, but the estimate still wanders around the true value.

Drag Number of samples (N) from 1 to 500 and follow the running mean toward the dashed line at 0.5. With all 500 samples it reaches 0.485. The law of large numbers is the guarantee behind every Monte Carlo method: any expectation E[f(X)] can be estimated by averaging f over samples, provided the expectation is finite. The name comes from the Monte Carlo casino. Stanislaw Ulam and John von Neumann developed the method at Los Alamos in the 1940s for neutron diffusion problems, and Metropolis and Ulam published it in 1949.

Step 3 of 6: Estimating π

Throw darts to approximate a circle's area

Number of darts (N)50
011xyinsideoutside
N darts
50
Inside
40
π estimate (click)
3.2000
Error
0.0584

With more darts, the estimate improves. A quarter-circle of radius 1 has area π/4, so 4 times the inside ratio estimates π.

Drag Number of darts (N) from 10 to 300, and click the π estimate card for the arithmetic. Each dart is a point (x, y) drawn uniformly in the unit square, and it counts as inside when x² + y² ≤ 1, so each dart is a yes/no trial that succeeds with probability π/4 ≈ 0.785. With these points, 50 darts give 3.20 and 300 give 3.147. Series formulas compute π far faster. The example matters because the same membership test estimates any area or volume, including shapes with no formula.

Step 4 of 6: Monte Carlo Integration

Approximate integrals by random sampling

Number of samples (N)30
Estimate = (1/N) × Σ f(xᵢ) where xᵢ ~ Uniform[0,1]est2/πxf(x) = sin(πx)00.5100.250.50.751
N
30
Estimate (click)
0.6256
True value (click)
0.6366
Error
0.0110

With few samples, each random x-value contributes a noisy estimate of the integral. The average of f(xᵢ) values is a rough approximation of the area under sin(πx).

Drag Number of samples (N) from 10 to 300, and click the Estimate card to see the calculation. The estimator is ∫ f(x) dx ≈ (b − a) · (1/N) Σ f(xᵢ), with xᵢ uniform on [a, b]. Here [a, b] = [0, 1], so it is just the average of sin(πxᵢ): 0.626 at N = 30 and 0.629 at N = 300, against 2/π ≈ 0.637. In one dimension a simple grid rule does better. The advantage appears in many dimensions, where 10 grid points per axis in 100 dimensions would need 10¹⁰⁰ evaluations.

Step 5 of 6: Importance Sampling

Sample smarter, not harder

Number of samples (N)40
Proposal center0.70
targetproposal00.250.50.751uniformx
N
40
Uniform est.
0.920
Importance est.
0.945
Var ratio
0.08

The proposal distribution closely matches the target peak. Importance sampling concentrates samples where the target is large, reducing variance compared to uniform sampling.

Drag Proposal center from 0 to 1 and Number of samples (N) from 10 to 200. The target is a narrow Gaussian at 0.7 with σ = 0.15, so the integral over [0, 1] is about 0.977. The proposal is a Gaussian with σ = 0.2, truncated to [0, 1] and renormalized, so with the proposal centered on 0.7 and 200 samples the estimate is 0.973 at about a tenth of the uniform variance. Samples come from the proposal q, and each gets the weight w = p(x)/q(x), which corrects for sampling in the wrong place: E_p[f] = E_q[f · p/q]. The VAR RATIO card divides the importance-sampling variance by the uniform-sampling variance, so values below 1 mean the proposal helps. Off-policy reinforcement learning, PPO included, uses the same ratio.

Step 6 of 6: MCMC — Metropolis-Hastings

A random walk that samples from any distribution

Step size0.80
Number of steps (N)100
-1-100112233startacceptedrejected
N steps
100
Accepted
74
Accept rate
74%
Chain mean
1.4, 0.9

The chain is mixing reasonably. With more steps, the chain mean will converge toward the target center (1, 1). Under mild conditions (for example, the chain can reach every region where the target has mass), Metropolis-Hastings eventually samples from the target, but tuning the step size is critical for efficiency.

Drag Step size from 0.1 to 5.0 and Number of steps (N) from 10 to 200. The chain starts at (0, 0) and targets a 2D Gaussian centered at (1, 1) with σ = 0.8. Each step proposes a random move and accepts it with probability min(1, p(new)/p(current)). Only that ratio is needed, so the normalizing constant cancels, which is why MCMC can sample Bayesian posteriors whose evidence term is unknown. Early steps still reflect the starting point and are usually discarded as burn-in. For high-dimensional random-walk proposals, theory suggests an acceptance rate near 23%.

What are Monte Carlo methods?

Monte Carlo methods estimate quantities by random sampling: draw many random inputs, compute something for each one, and average the results. An integral, an expectation or a probability that is hard to compute exactly can be approximated this way, and the estimate improves as the number of samples grows.

Machine learning uses sampling everywhere. Stochastic gradient descent estimates the full gradient from a random mini-batch, dropout samples a random subnetwork at each training step, and language models generate text by sampling the next token. Bayesian methods use Markov chain Monte Carlo (MCMC) to draw samples from posteriors, and reinforcement learning estimates expected returns by averaging over sampled episodes.

Key ideas

  • A Monte Carlo estimate replaces an expectation with an average over random samples.
  • The law of large numbers guarantees that the sample average converges to the expected value.
  • Any integral can be written as an expectation and estimated by sampling.
  • The Monte Carlo convergence rate does not depend on the number of dimensions, unlike grid methods.
  • Importance sampling draws from a proposal q and reweights each sample by p/q.
  • MCMC builds a random walk whose long-run distribution is the target, using only ratios of probabilities.

Common questions

What is the difference between Monte Carlo and MCMC?

Plain Monte Carlo draws independent samples directly from the distribution. MCMC is for distributions you cannot sample directly: it runs a Markov chain whose samples eventually follow the target. MCMC samples are correlated, so 1,000 of them carry less information than 1,000 independent draws, a gap measured by the effective sample size.

What is rejection sampling?

Rejection sampling draws x from a simple proposal q, where M·q(x) ≥ p(x) everywhere, and keeps x with probability p(x) / (M·q(x)). The kept samples follow p exactly. It wastes most proposals when M is large, which is common in high dimensions.

Are pseudo-random numbers good enough for Monte Carlo?

For almost all ML and simulation work, yes. Modern generators pass strict statistical tests, and fixing the seed makes results reproducible. Quasi-random sequences such as Sobol points go further by spreading points evenly on purpose, which lowers the error for smooth integrands.

What is the reparameterization trick?

A sample z drawn from N(μ, σ²) has no gradient with respect to μ and σ. Writing it as z = μ + σ·ε, with ε drawn from N(0, 1), moves the randomness into ε, so gradients flow through μ and σ. VAEs rely on this to train their encoders with Monte Carlo estimates of the loss.

Related explainers

References

  1. Metropolis & Ulam (1949), "The Monte Carlo Method", Journal of the American Statistical Association
  2. Metropolis et al. (1953), "Equation of State Calculations by Fast Computing Machines", Journal of Chemical Physics
  3. Hastings (1970), "Monte Carlo Sampling Methods Using Markov Chains and Their Applications", Biometrika
  4. MacKay (2003), "Information Theory, Inference, and Learning Algorithms", chapters 29–30

Last updated