S14 · Lesson 43 · Sampling and Markov chain Monte Carlo
Monte Carlo estimates and their 1/√N error, drawing samples by inverting the cumulative distribution, rejection and importance sampling with their efficiencies, the Metropolis algorithm, and how correlation within a chain reduces its effective sample size
Most Bayesian posteriors aren't Gaussian or conjugate, and their averages are integrals over many dimensions that no formula or grid can handle. The way out is to draw samples and average: Monte Carlo. Its error falls as 1/√N whatever the dimension, which is why it's used for everything from yield analysis of chip designs (A11's Monte Carlo lab) to the posteriors of models with millions of unknowns. The hard part is drawing the samples, and Markov chain Monte Carlo does it for any density you can evaluate up to a constant, by running a random walk whose long-run distribution is the one you want.
- Samples from an exponential distribution with rate λ, drawn by inverting the cumulative distribution: with u uniform on (0, 1).
- The histogram settles onto the density as the number of samples grows, and the sample mean approaches 1/λ.
- Its error shrinks like : a hundred times more samples for one more digit.
Predict
You estimate π by throwing random points into a unit square and counting the fraction inside the quarter circle, times 4. About how many points does a standard error of 0.001 take?
Each point lands inside with probability p = π/4 = 0.785, so the indicator has standard deviation = 0.411, and 4 times its average has standard error 4 × 0.411/√N. Setting that to 0.001 gives N = (1.642/0.001)² = 2.7 million. Monte Carlo is easy and dimension-free, but slow to converge.
Name it
Monte Carlo estimates
with drawn from p. If f has standard deviation σ, the error is , independent of the dimension of x. A target error ε needs samples.
Drawing samples
- Inverse CDF: if u is uniform, has CDF F. For the exponential, .
- Rejection sampling: draw from a proposal q with everywhere and accept with probability . For normalized p and q the acceptance rate is 1/M, which collapses in high dimensions.
- Importance sampling: draw from q and weight each sample by . Unequal weights waste samples: the effective sample size is .
Markov chain Monte Carlo
The Metropolis algorithm proposes a move x → x′ from a symmetric distribution and accepts it with probability
otherwise staying put. Only ratios of p appear, so its normalizing constant is never needed. The rule satisfies detailed balance, so p is the chain's stationary distribution (lesson 42). Proposals that are too small creep; too large and almost all are rejected.
Correlated samples
Successive states are correlated. If the correlation between neighbors is ρ and decays geometrically, N steps are worth about independent samples. Early steps, before the chain reaches its typical region, are discarded as burn-in.
Worked example
A uniform draw u = 0.7 is turned into an exponential sample with rate 2. What's the sample? And what's the effective sample size of 10,000 Metropolis steps with neighbor correlation 0.9?
Show the solution
- Exponential sample.
- Effective sample size. almost 19 steps per independent sample.
Answer the sample is 0.602, and the chain's effective sample size is 526.
Worked example
A Metropolis sampler for a standard Gaussian sits at x = 1 and proposes x′ = 1.5. What's the acceptance probability? And what effective size do importance weights 1, 0.5 and 2 give?
Show the solution
- Acceptance probability. so it moves with probability 0.535; a proposal toward 0 would always be accepted.
- Importance weights. effective samples out of 3.
Answer the acceptance probability is 0.535, and the weights give 2.33 effective samples out of 3.
Worked example
A Gibbs sampler draws from a standard bivariate Gaussian with correlation 0.99 by alternating x | y and y | x. How fast does it move, and what effective sample size do 10,000 sweeps give?
Show the solution
- Step size. Each conditional is : a step of standard deviation 0.14 inside a distribution 1 wide along its long axis.
- Chain correlation. The x values form a chain with lag-one correlation
- Effective sample size.
- The cure. Strongly correlated variables crawl under Gibbs sampling; rotating to the principal axes, or updating the pair jointly, removes the problem.
Answer each step moves only about 0.14, and 10,000 sweeps give an effective sample size of 100.
Check
A quantity with standard deviation 2 is estimated by Monte Carlo. How many samples give a standard error of 0.001?
Check
A uniform draw u = 0.6 is converted to an exponential sample with rate 0.5 by inverting the CDF. What's the sample?
Check
A density on an interval of length 2 has a maximum of 3. Rejection sampling with a uniform proposal scaled to cover that maximum accepts what fraction of proposals?
Check
A Metropolis sampler targets a zero-mean Gaussian with standard deviation 2. It sits at x = 1 and proposes a move 0.5 farther out. What's the acceptance probability?
Check
A Markov chain's successive states have correlation 0.5. About how many independent samples are 10000 steps worth?
Check
Three importance samples have weights 0.5, 0.2 and 4. What's their effective sample size?
Check
A Gibbs sampler targets a standard bivariate Gaussian with correlation 0.9 and runs 100000 sweeps.
(a)
What standard deviation does x | y have, the step size along x?
(b)
What effective sample size do the x values give?
(c)
Where is x | y centered when y = −1?
Check
Importance sampling estimates expectations under N(0, 1) from samples drawn from N(0, s²).
Step 1
What weight does a sample at x get?
Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.
Step 2
What is the mean squared weight?
Explain why
How can the Metropolis algorithm sample from a density whose normalizing constant is unknown?
The algorithm only ever compares the density at the proposed point with the density at the current point, through the ratio p(x′)/p(x), and any normalizing constant cancels in that ratio. The acceptance rule is built so that, in the long run, the probability flow from any state x to any x′ equals the flow back, which is detailed balance. A chain that satisfies detailed balance with respect to p has p as its stationary distribution, so after burn-in its states are distributed according to p, correlated with one another but correct on average. Posteriors are usually known only up to the evidence, so this property is what makes MCMC the workhorse of Bayesian computation.
Practice
Keep practicing
10 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.