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
- Probability & Distributions — Spam, densities, and the math of uncertainty
- Statistics Essentials — Mean, variance, standard deviation, and the Central Limit Theorem
- Bayes' Theorem — Updating beliefs with evidence
- MLE & MAP Estimation — Maximum likelihood and maximum a posteriori — why training works
- Diffusion Models — Generate images by learning to reverse a gradual noising process
- Autoencoders & VAEs — From compression to generation — standard and variational autoencoders
References
- Metropolis & Ulam (1949), "The Monte Carlo Method", Journal of the American Statistical Association
- Metropolis et al. (1953), "Equation of State Calculations by Fast Computing Machines", Journal of Chemical Physics
- Hastings (1970), "Monte Carlo Sampling Methods Using Markov Chains and Their Applications", Biometrika
- MacKay (2003), "Information Theory, Inference, and Learning Algorithms", chapters 29–30
Last updated