Monte Carlo Methods in Machine Learning
When you can't compute it exactly, sample it
Many quantities in ML are mathematically well-defined but computationally impossible to calculate exactly — an integral over a billion-dimensional space, an expectation over every possible neural network configuration, the true posterior distribution over a model's weights. Monte Carlo methods sidestep the impossibility of exact computation by estimating these quantities from random samples instead. The approximation gets better as you draw more samples, with a precisely quantifiable error rate.
The core idea: estimate an expectation by sampling
If you want $E[f(X)]$ for some random variable $X$ but can't compute the integral or sum directly, draw $N$ samples $x_1, \dots, x_N$ from $X$'s distribution and average: $E[f(X)] \approx \frac{1}{N}\sum_{i=1}^N f(x_i)$. By the law of large numbers, this average converges to the true expectation as $N \to \infty$.
Concrete example: estimate $\pi$ using random sampling. Scatter points uniformly in a 2×2 square (area 4) centered at the origin, and check what fraction land inside the inscribed unit circle (area $\pi$). The fraction inside approximates $\pi/4$.
Note
Monte Carlo dropout: sampling uncertainty from a trained network
Dropout — randomly zeroing out a fraction of neurons during training — is normally switched off at inference time so predictions are deterministic. Monte Carlo dropout keeps dropout switched on at inference and runs the same input through the network many times, each time with a different random dropout mask. The resulting set of predictions forms a sample from an approximate posterior over outputs, and the spread of that sample approximates the model's uncertainty.
If 20 forward passes on the same input with dropout active give predictions like $[0.81, 0.79, 0.84, 0.76, \dots]$ (all clustered tightly), the model is confident. If they instead give $[0.95, 0.20, 0.63, 0.08, \dots]$ (wildly spread), the model is effectively saying "I don't actually know" — information that a single deterministic forward pass would never reveal.
MCMC: sampling from distributions you can't sample directly
Standard Monte Carlo requires being able to draw independent samples from the distribution you care about — easy for a uniform square, hard for a complicated posterior distribution over millions of model parameters. Markov Chain Monte Carlo (MCMC) solves this by constructing a random walk (a Markov chain) whose long-run visiting frequency matches the target distribution, even when you can't sample from that distribution directly.
The basic recipe (e.g. Metropolis-Hastings): from the current position, propose a random nearby move; accept it with a probability based on how much more (or less) likely the target distribution says the new position is; repeat for many steps. After enough steps, the sequence of visited positions behaves like genuine samples from the target distribution, letting you estimate expectations, variances, or credible intervals over it — even though you never had a formula to sample it directly.
Note
Monte Carlo techniques compared
| Method | What it samples | Typical ML use |
|---|---|---|
| Plain Monte Carlo estimation | Independent samples from a known distribution | Estimating integrals, expected rewards in reinforcement learning |
| Monte Carlo dropout | Multiple stochastic forward passes of a trained network | Approximate uncertainty estimation without a separate Bayesian model |
| MCMC (e.g. Metropolis-Hastings) | A random walk whose stationary distribution matches an intractable target | Bayesian posterior estimation, sampling-based generative models |
What's next
Monte Carlo methods work because they tolerate approximation — but computing with approximate numbers at all brings its own hazards. The next lesson — Numerical Stability in Machine Learning — covers how floating point precision limits, exploding and vanishing values, and tricks like log-sum-exp keep real training runs from silently breaking.
I build these systems professionally.
Whether it's a RAG pipeline, analytics migration, or AI workflow — let's talk.