Probability and EstimationPart 3 of 5Intermediate · Updated July 31, 2026

Monte Carlo Simulation

Many integrals in estimation have no closed form. Grid-based quadrature handles them in one or two dimensions, but its cost grows exponentially with dimension. Monte Carlo estimation replaces the grid with random draws, whose error does not depend on dimension.

An integral is an expectation, estimated by a sample mean

The core idea is to write the integral as the expected value of a function under some distribution, then estimate that expectation by averaging the function over random draws. For

I=∫Df(x) p(x) dx=Ep[f(X)]I = \int_{\mathcal{D}} f(x)\, p(x)\, \mathrm{d}x = \mathbb{E}_{p}[f(X)]

draw NN independent samples x1,…,xNx_1, \dots, x_N from pp and average:

I^N=1N∑i=1Nf(xi)\hat I_N = \frac{1}{N} \sum_{i=1}^{N} f(x_i)

The estimator is unbiased, E[I^N]=I\mathbb{E}[\hat I_N] = I for every NN, and by the law of large numbers it converges to II as NN grows.

The error falls as one over root N in any dimension

The samples are independent, so the variance of the mean is the variance of one term divided by NN:

Var⁡(I^N)=σ2N,error∼σN,σ2=Var⁡p(f(X))\operatorname{Var}(\hat I_N) = \frac{\sigma^2}{N}, \qquad \text{error} \sim \frac{\sigma}{\sqrt{N}}, \qquad \sigma^2 = \operatorname{Var}_p(f(X))

Each additional decimal digit of accuracy costs a hundred times more samples. In one dimension the trapezoid rule, with error O(h2)\mathcal{O}(h^2) in the spacing hh, achieves O(N−2)\mathcal{O}(N^{-2}).

The dimension dd does not appear in σ/N\sigma/\sqrt N, but it does for a grid. Covering a dd-dimensional cube at spacing hh takes N=(1/h)dN = (1/h)^d points, so the trapezoid rule’s error becomes O(N−2/d)\mathcal{O}(N^{-2/d}):

dimensiontrapezoidMonte Carlo
1N−2N^{-2}N−1/2N^{-1/2}
4N−1/2N^{-1/2}N−1/2N^{-1/2}
10N−1/5N^{-1/5}N−1/2N^{-1/2}
50N−1/25N^{-1/25}N−1/2N^{-1/2}

Below about four dimensions a grid is more accurate, deterministic, and controlled by a parameter the user sets. Above that, Monte Carlo converges faster, and in high dimension it is often the only feasible method.

Worked example: estimating π

Sample points uniformly in the unit square and count the fraction inside the quarter circle. Here ff is an indicator with mean π/4≈0.785\pi/4 \approx 0.785, so σ2=p(1−p)≈0.169\sigma^2 = p(1-p) \approx 0.169 and σ≈0.41\sigma \approx 0.41. The standard error of the estimate of π\pi is 4σ/N≈1.64/N4\sigma/\sqrt{N} \approx 1.64/\sqrt{N}:

  • N=100N = 100: about ±0.16\pm 0.16
  • N=10,000N = 10{,}000: about ±0.016\pm 0.016
  • N=1,000,000N = 1{,}000{,}000: about ±0.0016\pm 0.0016

Four correct digits require on the order of a million samples. In this low-dimensional problem quadrature is far more efficient; the example illustrates the convergence rate, not the method’s advantage.

Importance sampling

With the rate fixed at N−1/2N^{-1/2}, accuracy can be improved only by reducing σ\sigma. If most of the integrand’s contribution comes from a region that pp rarely visits, most draws from pp contribute little. Importance sampling draws instead from a distribution qq concentrated in that region and corrects for the substitution:

I=∫f(x) p(x) dx=∫f(x) p(x)q(x) q(x) dx=Eq ⁣[f(X) p(X)q(X)]I = \int f(x)\, p(x)\, \mathrm{d}x = \int \frac{f(x)\, p(x)}{q(x)}\, q(x)\, \mathrm{d}x = \mathbb{E}_q\!\left[\frac{f(X)\, p(X)}{q(X)}\right]

The estimator becomes a weighted average with weights wi=p(xi)/q(xi)w_i = p(x_i)/q(x_i). It remains unbiased for any qq that is non-zero wherever fpf p is non-zero.

The variance depends on qq. A proposal shaped like ∣f∣p|f| p can reduce σ\sigma by orders of magnitude. A proposal with thinner tails than pp increases it: the weights p/qp/q grow without bound in the tails, a single sample can dominate the average, and the variance may be infinite even though individual runs appear stable. The usual diagnostic is the effective sample size,

Neff=(∑iwi)2∑iwi2N_{\text{eff}} = \frac{\left(\sum_i w_i\right)^2}{\sum_i w_i^2}

If 10,000 samples give Neff=40N_{\text{eff}} = 40, almost all the weight lies on a few draws and the estimate has roughly the precision of forty independent samples.

Assumptions and limitations

  • Monte Carlo is appropriate when the dimension is high, when the domain is too irregular to mesh, or when a process can be simulated but its density cannot be written down. These conditions arise in Bayesian inference, rendering, and physical and financial simulation.
  • In low dimension a grid is usually better. A pair of nested one-dimensional integrals along two curves, for instance, is better handled by quadrature, which offers controllable accuracy and reproducible results.
  • Importance sampling requires a proposal with tails at least as heavy as the target’s; otherwise its variance can be infinite.
  • The method assumes that samples can be drawn from pp. A Bayesian posterior is usually known only up to a normalising constant, so this assumption fails.

Summary

  • Any expectation can be estimated by a sample mean, without bias.
  • The standard error is σ/N\sigma/\sqrt N in every dimension, which is slow in low dimension and favourable in high dimension.
  • Importance sampling lowers σ\sigma by sampling where the integrand matters and reweighting; the effective sample size measures how well it is working.

The next tutorial samples a posterior known only up to a constant, using importance sampling and a Markov chain method.