TL;DR
A Monte Carlo method answers a question about a random or complicated system by simulating it many times and averaging the results. If you need an area, an average or a probability and have no formula for it, draw random inputs, evaluate the quantity you care about on each one, and take the mean. The law of large numbers says that mean settles on the right answer, and the error shrinks like one over the square root of the number of samples.
Where you'll see it: pricing financial options, estimating the expectations inside variational autoencoders, rendering images by light-path tracing, and checking how a statistical test behaves when the textbook formula does not apply.
Intuition
Guessing by sampling
Suppose you want the area of a lake on a map and have no formula for its outline. Drop pebbles at random across a rectangle that contains the lake, count how many land in the water, and multiply the fraction by the rectangle's area. The pebbles are random points, and the fraction that lands in the lake estimates the fraction of the rectangle that is lake. More pebbles give a better estimate. That is the whole method: replace a calculation you cannot do with a random experiment you can run.
The name is a nod to the casino in Monte Carlo and the randomness of games of chance. The idea is older than the name. Buffon's needle, published in 1777, estimated π by dropping a needle onto lined floorboards. The method became a general tool in the mid-1940s, when Stanislaw Ulam and John von Neumann used it on neutron diffusion problems at Los Alamos.
Why bother with randomness at all? Many quantities are integrals in disguise, and a grid of points stops working as the number of dimensions grows. A grid with 10 points along each of 10 axes already has 10 billion points. Random samples do not have that problem: the error shrinks at the same rate whatever the dimension. That is the reason the method matters in machine learning, where the integrals are over thousands of dimensions.
Here is the idea on a one-dimensional integral. We want the area under between 0 and 1. Each random point gives one value , and the average of those values estimates the average height of the curve, which is the area.
- f(x) = x^2
The steps are the same for any problem of this kind:
flowchart LR A[Write the quantity as an expectation] --> B[Draw random samples] B --> C[Evaluate the function on each sample] C --> D[Average the values] D --> E[Report the estimate with an error bar]
The math
The estimator
Suppose we want , the expected value of applied to a random variable with density . Written as an integral, this is
Draw samples that are independent and each distributed according to . The Monte Carlo estimate is their average:
Here is the number of samples, and (read "mu-hat") is the estimate computed from them.
The estimator is unbiased. Each term has expected value , and the expected value of an average is the average of the expected values, so . The law of large numbers adds that approaches as grows.
How accurate it is
Let be the variance of a single sample's value. Because the samples are independent, variances add:
The standard error, which is the typical size of the error, is therefore
For large the central limit theorem says is roughly Gaussian around , so about 95% of runs land within two standard errors of the true answer. In practice is unknown, so we estimate it from the same samples with the sample variance:
The rule has a practical consequence. To halve the error you need four times as many samples. To gain one more correct digit you need a hundred times as many. The rate does not depend on how many dimensions has, only on .
Integrals as expectations
Any integral over an interval can be written as an expectation. Let be uniform on , which has density . Then
so the estimator is for independent uniform draws . This is the same definite integral from the calculus lesson, computed by sampling instead of by a formula.
Probabilities as expectations
A probability is the expected value of an indicator. If is an event and equals 1 when is in and 0 otherwise, then . The Monte Carlo estimate is simply the fraction of samples that fall in . Because each indicator is 0 or 1, its variance is with , so
Worked examples
Take the unit square. The quarter circle inside it has area . Draw five random points in the square and check each one.
- : , inside.
- : , outside.
- : , inside.
- : , outside.
- : , inside.
Three of five points are inside, so the fraction is . Since the square has area 1, the circle's area is estimated as , and is estimated as .
The error bar is large. The standard error of the fraction is , so the standard error on is . The estimate is honest but nearly useless. Five samples show the method working; shrinking that error bar takes many more samples, at the rate.
We estimate , whose exact value is . The uniform draws are .
- Evaluate the function: , , , .
- Average them: .
- Estimate the sample standard deviation. The deviations from 0.3975 are , , , . Their squares sum to , so and .
- Standard error: .
The result is . The true value is well within one standard error, so the answer is consistent with the truth. The interpretation is that four samples give a rough answer, and you should not trust the third decimal place.
A risk model needs for a standard Gaussian . The exact value is . With samples, suppose 231 exceed 2, so the estimate is .
- Standard error: .
- The estimate is consistent with .
- To cut the error tenfold, to about , you need a hundred times more samples, one million in total.
This is where plain Monte Carlo becomes expensive. Rare events need enormous sample counts, which is why variance-reduction ideas such as importance sampling exist.
Tradeoffs and failure modes
Monte Carlo is the right tool when the problem is high-dimensional or the integrand is too complicated to integrate by hand. It is the wrong tool when a smooth low-dimensional integral can be done more accurately by a deterministic rule.
| Situation | Problem | Better choice |
|---|---|---|
| One or two dimensions, smooth integrand | Monte Carlo converges slowly compared with a grid | Quadrature such as Simpson's rule or Gauss quadrature |
| Rare event with tiny probability | Almost no samples hit the event, so the relative error is huge | [[importance-sampling |
| Cannot draw samples from directly | No simple sampler exists | [[markov-chain |
| Need an exact answer | Monte Carlo gives an estimate with error | Analytic methods, if they exist |
| Heavy-tailed with infinite variance | The standard error formula breaks and the central limit theorem may not apply | Robust estimators, or a different model |
Practical failures are more common than theoretical ones. A poor random number generator can make the samples correlated, and then the effective sample size is smaller than . Samples from a Markov chain are correlated by construction, so the error bar from the formula above is too optimistic unless you correct for it. A small with a heavy-tailed often produces an error bar that looks tight and is wrong. The fix in all cases is to check the estimate with more samples, a different seed, or a second method before trusting it.
History
Buffon's needle (1777) is an early example of estimating π by random sampling, though it was not presented as a computational method. The modern method came out of work on the Manhattan Project. In the mid-1940s Stanislaw Ulam, working on neutron diffusion, realised that random simulation could handle problems that deterministic calculation could not. John von Neumann helped develop the approach on early computers at Los Alamos. Nicholas Metropolis suggested the name, and Metropolis and Ulam published the first description in 1949.
It caught on because electronic computers made each sample cheap, and the approach handles problems that resist every other technique. In 1953 Metropolis and colleagues introduced a sampling scheme for statistical mechanics that became the basis of Markov chain Monte Carlo, and it remains central to Bayesian statistics and machine learning.
Check yourself
QuestionWhat does the law of large numbers guarantee about as grows?Show answer
It converges to the true expected value . Each extra sample pulls the average closer, so for large enough the estimate is as close to as you like. The law of large numbers is what makes Monte Carlo consistent: the estimate is right in the limit.
QuestionYou draw from a uniform distribution on . What is the Monte Carlo estimate of ?Show answer
The mean of the samples is . By linearity, , so the estimate is . You can also apply to each sample first: values are , which average to .
QuestionWhy does the Monte Carlo error rate not depend on the dimension of ?Show answer
The standard error is , and is the only sample-size term. The dimension only enters through , which is the spread of . A grid needs a number of points that grows exponentially with dimension, but random samples are evaluated the same way in any dimension, so the same gives the same rate.
QuestionA portfolio loses more than 10 million dollars with probability per year. You simulate years with plain Monte Carlo. What is the chance you see no losses that large, and what should you do instead?Show answer
Each year misses with probability , so the chance of no hits in independent years is . You would most likely see zero events and an estimate of zero, with almost no information. Use importance sampling, which oversamples the tail and corrects the weights, or a model built for extreme values.
Sources
- Monte Carlo method (Wikipedia): https://en.wikipedia.org/wiki/Monte_Carlo_method
- Bishop, Pattern Recognition and Machine Learning, §11.1: https://www.microsoft.com/en-us/research/publication/pattern-recognition-machine-learning/
- Importance sampling (Wikipedia): https://en.wikipedia.org/wiki/Importance_sampling