S1443 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

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: x=−ln⁡(1−u)/λ with u uniform on (0, 1).
  • The histogram settles onto the density λe−λx as the number of samples grows, and the sample mean approaches 1/λ.
  • Its error shrinks like 1/N: 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?

Name it

Monte Carlo estimates

E[f(x)]≈1N∑nf(xn) with xn drawn from p. If f has standard deviation σ, the error is σ/N, independent of the dimension of x. A target error ε needs N=(σ/ϵ)2 samples.

Drawing samples

  • Inverse CDF: if u is uniform, x=F−1(u) has CDF F. For the exponential, x=−ln⁡(1−u)/λ.
  • Rejection sampling: draw from a proposal q with Mq(x)≥p(x) everywhere and accept with probability p(x)/Mq(x). 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 w=p/q. Unequal weights waste samples: the effective sample size is (∑w)2/∑w2.

Markov chain Monte Carlo

The Metropolis algorithm proposes a move x → x′ from a symmetric distribution and accepts it with probability

min⁡(1,p(x′)p(x))

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 N(1−ρ)/(1+ρ) 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
  1. Exponential sample. x=−ln⁡(0.3)/2=0.602
  2. Effective sample size. Neff=10,000×0.1/1.9=526 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
  1. Acceptance probability. p(1.5)p(1)=e−(2.25−1)/2=0.535 so it moves with probability 0.535; a proposal toward 0 would always be accepted.
  2. Importance weights. Neff=(3.5)21+0.25+4=2.33 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
  1. Step size. Each conditional is N(ρy,1−ρ2): a step of standard deviation 0.14 inside a distribution 1 wide along its long axis.
  2. Chain correlation. The x values form a chain with lag-one correlation ρ2=0.980
  3. Effective sample size. Neff=10,000×1−ρ21+ρ2=100
  4. 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°.

Explain why

How can the Metropolis algorithm sample from a density whose normalizing constant is unknown?

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.