Markov Chain Monte Carlo

Rigorous derivation of MCMC methods for Bayesian inference: the Metropolis-Hastings algorithm, detailed balance, ergodicity, Gibbs sampling as a special case, Hamiltonian Monte Carlo, diagnostics, and convergence theory.

Advanced

Table of Contents

  1. Learning Objectives
  2. Notation
  3. Core Intuition
  4. The Sampling Problem
  5. Markov Chains: Foundations
  6. The Metropolis-Hastings Algorithm
  7. Detailed Balance and Correctness
  8. Gibbs Sampling
  9. Hamiltonian Monte Carlo
  10. Convergence Diagnostics
  11. Common Pitfalls
  12. Research Perspective
  13. Summary
  14. Exercises

Learning Objectives

  1. Explain why direct sampling from posterior distributions is generally intractable.
  2. Define Markov chains and state the conditions for ergodicity.
  3. Derive the Metropolis-Hastings acceptance probability from detailed balance.
  4. Show that Gibbs sampling is a special case of Metropolis-Hastings.
  5. Derive the Hamiltonian dynamics underlying HMC and explain its advantages.
  6. Apply convergence diagnostics (R-hat, effective sample size, trace plots).

Notation

  • π(θ)\pi(\boldsymbol\theta) — target distribution (posterior), known up to normalizing constant
  • q(θθ)q(\boldsymbol\theta' \mid \boldsymbol\theta) — proposal distribution
  • α(θθ)\alpha(\boldsymbol\theta' \mid \boldsymbol\theta) — acceptance probability
  • T(θθ)T(\boldsymbol\theta' \mid \boldsymbol\theta) — transition kernel of the chain
  • θ(t)\boldsymbol\theta^{(t)} — state of chain at iteration tt

Core Intuition

Bayesian inference requires computing expectations under the posterior p(θD)p(\boldsymbol\theta \mid \mathcal{D}), which involves intractable integrals. MCMC constructs a Markov chain whose stationary distribution is the posterior. Running the chain generates correlated samples; the empirical average of these samples converges to the posterior expectation by the ergodic theorem.

MCMC Random Walk

Acceptance = 70.0%
Step size
0.80
Samples
60
AcceptedRejected
Explore: Small steps → high acceptance but slow mixing. Large steps → low acceptance, chain gets stuck. Aim for ~20–40% acceptance.

The Sampling Problem

We want to compute:

Eπ[h(θ)]=h(θ)π(θ)dθ,(1)\mathbb{E}_{\pi}[h(\boldsymbol\theta)] = \int h(\boldsymbol\theta)\pi(\boldsymbol\theta)\,d\boldsymbol\theta, \tag{1}

where π(θ)=p(θD)=p(Dθ)p(θ)p(D)\pi(\boldsymbol\theta) = p(\boldsymbol\theta \mid \mathcal{D}) = \frac{p(\mathcal{D} \mid \boldsymbol\theta)p(\boldsymbol\theta)}{p(\mathcal{D})}.

The normalizing constant p(D)=p(Dθ)p(θ)dθp(\mathcal{D}) = \int p(\mathcal{D} \mid \boldsymbol\theta)p(\boldsymbol\theta)\,d\boldsymbol\theta is typically intractable. We can evaluate π~(θ)=p(Dθ)p(θ)\tilde{\pi}(\boldsymbol\theta) = p(\mathcal{D} \mid \boldsymbol\theta)p(\boldsymbol\theta) pointwise, but cannot normalize it.

MCMC solves this: it only requires π~\tilde{\pi} up to a constant.


Markov Chains: Foundations

Definition. A sequence {θ(t)}\{\boldsymbol\theta^{(t)}\} is a Markov chain if p(θ(t+1)θ(t),,θ(0))=T(θ(t+1)θ(t))p(\boldsymbol\theta^{(t+1)} \mid \boldsymbol\theta^{(t)}, \ldots, \boldsymbol\theta^{(0)}) = T(\boldsymbol\theta^{(t+1)} \mid \boldsymbol\theta^{(t)}).

Stationary distribution. π\pi is stationary for TT if:

π(θ)=T(θθ)π(θ)dθ.(2)\pi(\boldsymbol\theta') = \int T(\boldsymbol\theta' \mid \boldsymbol\theta)\pi(\boldsymbol\theta)\,d\boldsymbol\theta. \tag{2}

Ergodic Theorem. If the chain is irreducible and aperiodic with stationary distribution π\pi, then:

1Nt=1Nh(θ(t))a.s.Eπ[h(θ)]as N.(3)\frac{1}{N}\sum_{t=1}^N h(\boldsymbol\theta^{(t)}) \xrightarrow{a.s.} \mathbb{E}_\pi[h(\boldsymbol\theta)] \quad \text{as } N \to \infty. \tag{3}

The Metropolis-Hastings Algorithm

Algorithm:

  1. Initialize θ(0)\boldsymbol\theta^{(0)}.
  2. For t=0,1,2,t = 0, 1, 2, \ldots:
    • Propose θq(θ(t))\boldsymbol\theta' \sim q(\cdot \mid \boldsymbol\theta^{(t)}).
    • Compute acceptance probability:
α(θθ(t))=min(1,π~(θ)q(θ(t)θ)π~(θ(t))q(θθ(t))).(4)\alpha(\boldsymbol\theta' \mid \boldsymbol\theta^{(t)}) = \min\left(1, \frac{\tilde{\pi}(\boldsymbol\theta')q(\boldsymbol\theta^{(t)} \mid \boldsymbol\theta')}{\tilde{\pi}(\boldsymbol\theta^{(t)})q(\boldsymbol\theta' \mid \boldsymbol\theta^{(t)})}\right). \tag{4}
  • With probability α\alpha: set θ(t+1)=θ\boldsymbol\theta^{(t+1)} = \boldsymbol\theta'. Otherwise: θ(t+1)=θ(t)\boldsymbol\theta^{(t+1)} = \boldsymbol\theta^{(t)}.

The normalizing constants cancel in the ratio π~(θ)/π~(θ(t))\tilde{\pi}(\boldsymbol\theta')/\tilde{\pi}(\boldsymbol\theta^{(t)}).


Detailed Balance and Correctness

Detailed balance (sufficient condition for π\pi to be stationary):

π(θ)T(θθ)=π(θ)T(θθ).(5)\pi(\boldsymbol\theta)T(\boldsymbol\theta' \mid \boldsymbol\theta) = \pi(\boldsymbol\theta')T(\boldsymbol\theta \mid \boldsymbol\theta'). \tag{5}

Theorem. The MH transition kernel satisfies detailed balance with respect to π\pi.

Proof. The transition kernel is T(θθ)=q(θθ)α(θθ)T(\boldsymbol\theta' \mid \boldsymbol\theta) = q(\boldsymbol\theta' \mid \boldsymbol\theta)\alpha(\boldsymbol\theta' \mid \boldsymbol\theta) for θθ\boldsymbol\theta' \neq \boldsymbol\theta. Then:

π(θ)q(θθ)α(θθ)=min(π(θ)q(θθ),  π(θ)q(θθ)),\pi(\boldsymbol\theta)q(\boldsymbol\theta' \mid \boldsymbol\theta)\alpha(\boldsymbol\theta' \mid \boldsymbol\theta) = \min\bigl(\pi(\boldsymbol\theta)q(\boldsymbol\theta' \mid \boldsymbol\theta),\; \pi(\boldsymbol\theta')q(\boldsymbol\theta \mid \boldsymbol\theta')\bigr),

which is symmetric in (θ,θ)(\boldsymbol\theta, \boldsymbol\theta'), establishing detailed balance. \blacksquare


Gibbs Sampling

For θ=(θ1,,θd)\boldsymbol\theta = (\theta_1, \ldots, \theta_d), Gibbs sampling updates one coordinate at a time from its full conditional:

θj(t+1)p(θjθ1(t+1),,θj1(t+1),θj+1(t),,θd(t)).(6)\theta_j^{(t+1)} \sim p(\theta_j \mid \theta_1^{(t+1)}, \ldots, \theta_{j-1}^{(t+1)}, \theta_{j+1}^{(t)}, \ldots, \theta_d^{(t)}). \tag{6}

Proposition. Gibbs sampling is a special case of Metropolis-Hastings where the proposal q(θθ)q(\boldsymbol\theta' \mid \boldsymbol\theta) proposes from the full conditional distribution, and the acceptance probability is always 1.

Proof. Let q(θθ)=p(θjθj)q(\boldsymbol\theta' \mid \boldsymbol\theta) = p(\theta_j' \mid \boldsymbol\theta_{-j}). Then:

π(θ)q(θθ)π(θ)q(θθ)=p(θj,θj)p(θjθj)p(θj,θj)p(θjθj)=p(θjθj)p(θj)p(θjθj)p(θjθj)p(θj)p(θjθj)=1.\frac{\pi(\boldsymbol\theta')q(\boldsymbol\theta \mid \boldsymbol\theta')}{\pi(\boldsymbol\theta)q(\boldsymbol\theta' \mid \boldsymbol\theta)} = \frac{p(\theta_j', \boldsymbol\theta_{-j})p(\theta_j \mid \boldsymbol\theta_{-j})}{p(\theta_j, \boldsymbol\theta_{-j})p(\theta_j' \mid \boldsymbol\theta_{-j})} = \frac{p(\theta_j' \mid \boldsymbol\theta_{-j})p(\boldsymbol\theta_{-j})p(\theta_j \mid \boldsymbol\theta_{-j})}{p(\theta_j \mid \boldsymbol\theta_{-j})p(\boldsymbol\theta_{-j})p(\theta_j' \mid \boldsymbol\theta_{-j})} = 1.

So α=min(1,1)=1\alpha = \min(1, 1) = 1. \blacksquare


Hamiltonian Monte Carlo

HMC augments the parameter space with "momentum" variables p\mathbf{p} and simulates Hamiltonian dynamics to propose distant states with high acceptance probability.

Joint distribution: p(θ,p)exp(H(θ,p))p(\boldsymbol\theta, \mathbf{p}) \propto \exp(-H(\boldsymbol\theta, \mathbf{p})) where:

H(θ,p)=U(θ)+K(p)=logπ~(θ)+12pTM1p.(7)H(\boldsymbol\theta, \mathbf{p}) = U(\boldsymbol\theta) + K(\mathbf{p}) = -\log\tilde{\pi}(\boldsymbol\theta) + \frac{1}{2}\mathbf{p}^T\mathbf{M}^{-1}\mathbf{p}. \tag{7}

Leapfrog integrator (discrete approximation to Hamilton's equations):

pt+ϵ/2=ptϵ2θU(θt)\mathbf{p}_{t+\epsilon/2} = \mathbf{p}_t - \frac{\epsilon}{2}\nabla_\theta U(\boldsymbol\theta_t) θt+ϵ=θt+ϵM1pt+ϵ/2\boldsymbol\theta_{t+\epsilon} = \boldsymbol\theta_t + \epsilon\mathbf{M}^{-1}\mathbf{p}_{t+\epsilon/2} pt+ϵ=pt+ϵ/2ϵ2θU(θt+ϵ)(8)\mathbf{p}_{t+\epsilon} = \mathbf{p}_{t+\epsilon/2} - \frac{\epsilon}{2}\nabla_\theta U(\boldsymbol\theta_{t+\epsilon}) \tag{8}

After LL leapfrog steps, accept/reject with MH correction (accounts for discretization error).

Advantage: HMC proposes states far from the current position while maintaining high acceptance rates, greatly reducing autocorrelation compared to random-walk MH.


Convergence Diagnostics

  • Trace plots: Visual inspection for stationarity (chain should look like white noise after burn-in).
  • R^\hat{R} (Gelman-Rubin): Run multiple chains; R^1\hat{R} \approx 1 indicates convergence. R^>1.01\hat{R} > 1.01 suggests non-convergence.
  • Effective Sample Size (ESS): ESS=N/(1+2kρk)\text{ESS} = N / (1 + 2\sum_k \rho_k) where ρk\rho_k is autocorrelation at lag kk. Low ESS means high autocorrelation.
  • Burn-in: Discard initial samples before the chain reaches stationarity.

Common Pitfalls

Pitfall 1. Using too narrow a proposal in MH: high acceptance rate but slow exploration (random walk behavior). Optimal acceptance rate ≈ 23% for high-dimensional targets.

Pitfall 2. Not accounting for multimodality. Standard MCMC can get trapped in a single mode. Use tempering or multiple initialization strategies.

Pitfall 3. Declaring convergence too early. Short chains may appear stationary but haven't explored the full posterior. Always run multiple chains with dispersed initializations.


Research Perspective

MCMC was introduced by Metropolis et al. (1953) and generalized by Hastings (1970). Modern advances include the No-U-Turn Sampler (NUTS, Hoffman & Gelman, 2014) which automatically tunes HMC trajectory length, stochastic gradient MCMC for large datasets, and transport-based methods that combine variational inference with MCMC.


Summary

  • MCMC generates samples from distributions known only up to a constant.
  • Metropolis-Hastings uses propose-accept/reject; correctness follows from detailed balance.
  • Gibbs sampling cycles through full conditionals; always accepts.
  • HMC uses gradient information for efficient exploration of high-dimensional posteriors.
  • Convergence is assessed via R^\hat{R}, ESS, and trace plots.

Exercises

Exercise 1. Prove that detailed balance implies stationarity: if πT=π\pi T = \pi follows from equation (5).

Exercise 2. For a 1D Gaussian target π(θ)=N(0,1)\pi(\theta) = \mathcal{N}(0, 1) with Gaussian proposal q(θθ)=N(θ,σ2)q(\theta' \mid \theta) = \mathcal{N}(\theta, \sigma^2), compute the acceptance probability and discuss the effect of σ\sigma.

Exercise 3. Show that the leapfrog integrator is time-reversible and volume-preserving (symplectic).

Exercise 4. Derive the Gibbs sampler for the bivariate Gaussian (θ1,θ2)(\theta_1, \theta_2) with correlation ρ\rho, and compute the autocorrelation of the chain.

Exercise 5. Show that ideal HMC (exact Hamiltonian dynamics, no discretization error) has acceptance probability 1.