01 / 23

Markov Chain
Monte Carlo

Explore the foundations of MCMC sampling: how random walks through state space let us approximate complex probability distributions that are analytically intractable.

Graduate Statistics

Why Do We Need MCMC?

In Bayesian inference, we want the posterior distribution \( p(\theta \mid \mathbf{y}) \). By Bayes' theorem:

The normalizing constant \( p(\mathbf{y}) = \int p(\mathbf{y} \mid \theta)\, p(\theta)\, d\theta \) is often a high-dimensional integral that cannot be computed in closed form. MCMC sidesteps this by drawing correlated samples from the posterior without evaluating the integral.

Two Key Ingredients

Markov Chain
A sequence of random variables \( \theta^{(0)}, \theta^{(1)}, \ldots \) where each state depends only on the previous state: \( p(\theta^{(t+1)} \mid \theta^{(0)}, \ldots, \theta^{(t)}) = p(\theta^{(t+1)} \mid \theta^{(t)}) \).
Monte Carlo
Estimate expectations by averaging over samples: \( \mathbb{E}[f(\theta)] \approx \frac{1}{N} \sum_{t=1}^{N} f(\theta^{(t)}) \). The law of large numbers guarantees convergence as \( N \to \infty \).

Convergence to the Stationary Distribution

Iteration \( t \)
Density
Target \( \pi(\theta) \)
\( t = 50 \)
\( t = 500 \)
\( t = 5000 \)
Burn-in region

The Detailed Balance Condition

For a Markov chain to have stationary distribution \( \pi \), a sufficient (not necessary) condition is detailed balance:

where \( T(\theta' \mid \theta) \) is the transition kernel. This means the "probability flow" from \( \theta \) to \( \theta' \) equals the reverse flow. A chain satisfying this is called reversible.

The Metropolis-Hastings Algorithm

1
Initialize \( \theta^{(0)} \) arbitrarily.
2
Propose a candidate \( \theta^* \sim q(\theta^* \mid \theta^{(t)}) \) from a proposal distribution.
3
Compute the acceptance ratio: \( \alpha = \min\!\left(1, \; \frac{\pi(\theta^*)\, q(\theta^{(t)} \mid \theta^*)}{\pi(\theta^{(t)})\, q(\theta^* \mid \theta^{(t)})}\right) \)
4
Accept or reject: draw \( u \sim \text{Uniform}(0,1) \). If \( u \le \alpha \), set \( \theta^{(t+1)} = \theta^* \); otherwise \( \theta^{(t+1)} = \theta^{(t)} \).
5
Repeat steps 2-4 for \( t = 1, 2, \ldots, N \).

Metropolis-Hastings: Accept/Reject

\( \pi(\theta) \)
\( \theta^{(t)} \)
\( \theta^*_1 \) ✓
\( \theta^*_2 \) ✕
\( \alpha_1 = 0.93 \) → Accept
\( \alpha_2 = 0.12 \) → Reject
\( q(\theta^* \mid \theta^{(t)}) \)

Metropolis-Hastings vs. Gibbs Sampling

Metropolis-Hastings Gibbs Sampling
Proposal User-chosen \( q(\theta^* \mid \theta^{(t)}) \) Full conditionals \( p(\theta_j \mid \theta_{-j}) \)
Acceptance step Yes, with ratio \( \alpha \) No: every draw is always accepted
Tuning needed Proposal variance tuning required None if conditionals are known
Dimensionality Updates all components at once Updates one component at a time
Best when Conditionals are unavailable Conditionals are tractable (e.g., conjugate models)

How Gibbs Sampling Works

For a parameter vector \( \boldsymbol{\theta} = (\theta_1, \theta_2, \ldots, \theta_d) \), Gibbs cycles through each component:

1
Initialize \( \boldsymbol{\theta}^{(0)} = (\theta_1^{(0)}, \ldots, \theta_d^{(0)}) \).
2
Sample \( \theta_1^{(t+1)} \sim p(\theta_1 \mid \theta_2^{(t)}, \theta_3^{(t)}, \ldots, \theta_d^{(t)}) \).
3
Sample \( \theta_2^{(t+1)} \sim p(\theta_2 \mid \theta_1^{(t+1)}, \theta_3^{(t)}, \ldots, \theta_d^{(t)}) \).
4
Continue updating each \( \theta_j \) using the most recent values of all other components.
5
Repeat the full sweep for \( t = 1, 2, \ldots, N \).

Gibbs Sampling: Staircase Pattern

\( \theta_1 \)
\( \theta_2 \)
Start Converged
Horizontal: sample \( \theta_1 \)
Vertical: sample \( \theta_2 \)
(alternating conditionals)

Tuning the Proposal Distribution

Too Narrow
Small steps \(\sigma^2_q \ll 1\): high acceptance rate but slow exploration. The chain barely moves, producing highly correlated samples.
Just Right
Optimal: acceptance rate near 23.4% (for \(d \ge 5\)) or 44% (for \(d=1\)). Efficient mixing and fast convergence.
Too Wide
Large steps \(\sigma^2_q \gg 1\): proposals land in low-density tails, so most are rejected. Chain stays stuck at current value.

Convergence Diagnostics

Trace Plots
Visual inspection: a "hairy caterpillar" pattern indicates good mixing. Trends or slow wandering suggest non-convergence.
\( \hat{R} \) (Gelman-Rubin)
Compare between-chain vs. within-chain variance across multiple chains. Target: \( \hat{R} < 1.01 \).
Effective Sample Size
\( n_{\text{eff}} = \frac{N}{1 + 2\sum_{k=1}^{\infty} \rho_k} \) corrects for autocorrelation. Low \( n_{\text{eff}} \) means the chain is inefficient.

Example: MH for a Normal Mean

Suppose we observe \( y_1, \ldots, y_n \sim \mathcal{N}(\mu, 1) \) and use a flat prior \( p(\mu) \propto 1 \). The posterior is \( \mu \mid \mathbf{y} \sim \mathcal{N}(\bar{y}, 1/n) \). Let us use MH with proposal \( q(\mu^* \mid \mu) = \mathcal{N}(\mu, \sigma_q^2) \).

Example: Gibbs for a Bivariate Normal

Target: \( (\theta_1, \theta_2) \sim \mathcal{N}\!\left(\mathbf{0}, \begin{pmatrix} 1 & \rho \\ \rho & 1 \end{pmatrix}\right) \). The full conditionals are:

Each Gibbs iteration: sample \( \theta_1^{(t+1)} \) from the first conditional using the current \( \theta_2^{(t)} \), then sample \( \theta_2^{(t+1)} \) from the second using the fresh \( \theta_1^{(t+1)} \).

Reading a Trace Plot

Iteration
\( \mu \)
Burn-in
\( \bar{y} \)
2.0 3.0 1.0
✓ Good mixing
Rapid, irregular oscillations around the mean: the chain explores the distribution efficiently.
✕ Poor mixing
Slow trends, long flat regions, or drifting: the chain is stuck and not representative.

Common Mistakes

✕ No Burn-in Removal
Including early transient samples that reflect the arbitrary starting point rather than the target distribution.
✕ Treating Samples as Independent
MCMC samples are autocorrelated. Using raw \(N\) instead of \(n_{\text{eff}}\) for standard errors grossly underestimates uncertainty.
✕ Running One Short Chain
A single chain may appear converged but be trapped in a local mode. Always run multiple chains from dispersed starting values.

MCMC Methods: Summary

Metropolis-Hastings Gibbs Sampling HMC
Proposal Any user-chosen \(q\) Full conditionals Hamiltonian dynamics
Acceptance \(\alpha \le 1\) Always 1 \(\alpha \approx 1\) (leapfrog)
Tuning Proposal variance None (if conjugate) Step size, path length
High \(d\) Degrades quickly Works if conditionals tractable Scales well
Requires \(\pi(\theta)\) up to constant All full conditionals \(\nabla \log \pi(\theta)\)

Step-by-Step: Running MCMC

1
Specify the model: write down the likelihood \( p(\mathbf{y} \mid \boldsymbol{\theta}) \) and prior \( p(\boldsymbol{\theta}) \).
2
Choose the sampler: Gibbs if full conditionals are available; otherwise MH or HMC.
3
Run multiple chains (at least 4) from dispersed starting values.
4
Discard burn-in (first 50% or until \( \hat{R} < 1.01 \)).
5
Diagnose convergence: check trace plots, \( \hat{R} \), and \( n_{\text{eff}} \).
6
Summarize posterior: compute means, credible intervals, and posterior predictive checks.

Question 1 of 5

What is the primary purpose of the burn-in period in MCMC?

Question 2 of 5

In the Metropolis-Hastings algorithm with a symmetric proposal \( q(\theta^* \mid \theta) = q(\theta \mid \theta^*) \), the acceptance ratio simplifies to:

Question 3 of 5

Why is Gibbs sampling considered a special case of Metropolis-Hastings?

Question 4 of 5

A Metropolis-Hastings chain has an acceptance rate of 2%. What is the most likely problem?

Question 5 of 5

The Gelman-Rubin statistic \( \hat{R} \) compares: