Metropolis-Hastings Algorithm

mcmcbayesianmetropolis-hastingsconvergence-diagnosticsergodicityreversibilitytutorialcandidate-generating

Definition

The Metropolis-Hastings (MH) algorithm is a general-purpose Markov Chain Monte Carlo (MCMC) method that generates samples from a target distribution π(θ)\pi(\theta) by iterating a proposal–accept/reject step: at each iteration draw a candidate θ\theta^* from a proposal density q(θθt1)q(\theta^*|\theta^{t-1}), then accept it with probability

r=min ⁣(1,  π(θ)q(θt1θ)π(θt1)q(θθt1))r = \min\!\left(1,\; \frac{\pi(\theta^*)\, q(\theta^{t-1} \mid \theta^*)}{\pi(\theta^{t-1})\, q(\theta^* \mid \theta^{t-1})}\right)

If rejected, the chain stays at θt1\theta^{t-1}. The ratio corrects for asymmetric proposals so that the chain's invariant distribution is exactly π\pi, regardless of the proposal form. The acceptance function is derived from the reversibility condition (detailed balance): requiring π(x)p(x,y)=π(y)p(y,x)\pi(x)p(x,y) = \pi(y)p(y,x) and minimizing rejection probability yields the Hastings form uniquely (Chib-Greenberg 1995; Peskun 1973).

Key Ideas

How It Works

The Algorithm

At iteration tt:

  1. Draw θq(θt1)\theta^* \sim q(\cdot \mid \theta^{t-1}).
  2. Compute r=min(1,  π(θ)q(θt1θ)/[π(θt1)q(θθt1)])r = \min(1,\; \pi(\theta^*)q(\theta^{t-1}|\theta^*) / [\pi(\theta^{t-1})q(\theta^*|\theta^{t-1})]).
  3. Set θt=θ\theta^t = \theta^* with probability rr; else θt=θt1\theta^t = \theta^{t-1}.

Discard an initial burn-in; use {θt}\{\theta^t\} as approximate draws from π\pi.

Derivation from Reversibility (Chib-Greenberg 1995)

The MH acceptance function follows from the detailed balance (reversibility) condition: the chain's transition kernel p(x,y)p(x,y) must satisfy

π(x)p(x,y)=π(y)p(y,x)x,y\pi(x)\,p(x,y) = \pi(y)\,p(y,x) \quad \forall\, x, y

For a proposal q()q(\cdot|\cdot), write p(x,y)=q(x,y)α(x,y)p(x,y) = q(x,y)\,\alpha(x,y) for yxy \neq x. Substituting and rearranging:

π(x)q(x,y)α(x,y)=π(y)q(y,x)α(y,x)\pi(x)\,q(x,y)\,\alpha(x,y) = \pi(y)\,q(y,x)\,\alpha(y,x)

The Peskun-optimal α\alpha that satisfies this and maximises α(x,y)+α(y,x)\alpha(x,y) + \alpha(y,x) (minimising total rejection) is

α(x,y)=min ⁣(1,π(y)q(y,x)π(x)q(x,y))\alpha(x,y) = \min\!\left(1,\, \frac{\pi(y)\,q(y,x)}{\pi(x)\,q(x,y)}\right)

When π(y)q(y,x)>π(x)q(x,y)\pi(y)q(y,x) > \pi(x)q(x,y), set α(x,y)=1\alpha(x,y) = 1 and α(y,x)=π(x)q(x,y)/[π(y)q(y,x)]<1\alpha(y,x) = \pi(x)q(x,y)/[\pi(y)q(y,x)] < 1.

Five Candidate-Generating Families (Chib-Greenberg 1995)

Family Proposal Acceptance ratio
Random walk y=x+εy = x + \varepsilon, qq symmetric min{1,π(y)/π(x)}\min\{1,\, \pi(y)/\pi(x)\}
Independence chain q(yx)=q(y)q(y \mid x) = q(y), independent of xx min{1,π(y)q(x)/[π(x)q(y)]}\min\{1,\, \pi(y)q(x)/[\pi(x)q(y)]\}
Rejection-sampling πψh\pi \propto \psi h, propose from hh min{1,ψ(y)/ψ(x)}\min\{1,\, \psi(y)/\psi(x)\}; Gibbs = limit
Autoregressive / reflection y=a+B(xa)+zy = a + B(x-a) + z; B=IB = -I reflects Full Hastings ratio; negative serial corr.
Block-at-a-time Partition θ=(θ1,θ2,)\theta = (\theta_1, \theta_2, \ldots); update blocks in turn Block-specific Hastings ratios

M-H Accept-Reject (A-R) Algorithm (Chib-Greenberg 1995, §6.1)

When the target f()f(\cdot) is bounded above by ch()c \cdot h(\cdot) on a set C={x:f(x)<ch(x)}C = \{x: f(x) < c\,h(x)\}, a pseudodominating density

q(y)={f(y)/(cd)yCh(y)/dyCq(y) = \begin{cases} f(y)/(cd) & y \in C \\ h(y)/d & y \notin C \end{cases}

(dd chosen so qq integrates to 1) can be used as the MH proposal. The acceptance probabilities for four cases (x/C,y/C)(x \in/\notin C, y \in/\notin C) are tabulated in Chib-Greenberg (1995) Table 1. The algorithm combines the efficiency of rejection sampling inside the "easy" region CC with a Metropolis correction outside it, ensuring invariance of π\pi without burn-in bias.

Six Metropolis Kernel Types (Tierney 1994)

Tierney (1994) unifies the MH literature under six kernel types:

Kernel Proposal q(θ)q(\cdot \mid \theta) Notes
Random-walk θ=θ+ε\theta^* = \theta + \varepsilon, εN(0,τ2)\varepsilon \sim \mathcal{N}(0,\tau^2) Most common; tune τ\tau for acceptance rate 0.440.44 (scalar) / 0.230.23 (high-dim)
Independence chain q(θθ)=q(θ)q(\theta^* \mid \theta) = q(\theta^*) — independent of current state Efficient when qπq \approx \pi; collapses when tails of qq are too thin
Rejection-sampling chain Embeds a reject-sampling step in MH; guarantees exactly π\pi even when the envelope occasionally fails Useful for bounded supports
Autoregressive / shrinkage θ=ρθ+ε\theta^* = \rho\,\theta + \varepsilon, εN(0,σε2)\varepsilon \sim \mathcal{N}(0,\sigma^2_\varepsilon) Shrinks proposals toward origin; useful for near-stationary targets
Grid-based (Griddy) Evaluate π\pi on a grid; cumulative distribution function (CDF) inversion to propose Embeds Griddy-Gibbs within MH framework
Reflection θ=2aθ\theta^* = 2a - \theta reflected around aa Induces negative serial correlations, reducing asymptotic variance

Convergence Theory (Tierney 1994)

Drawing on Nummelin (1984) general state-space theory:

Theorem 1 (Ergodic convergence). If the chain is π\pi-irreducible and aperiodic, then π\pi is the unique invariant distribution and for π\pi-a.e. starting point xx: Pn(x,)π()0\|P^n(x,\cdot) - \pi(\cdot)\| \to 0

Geometric ergodicity via drift condition. If there exist V:E[1,)V: E \to [1,\infty), a "small" set CC, and constants λ<1\lambda < 1, b<b < \infty such that: PV(x)λV(x)+b1C(x)PV(x) \leq \lambda V(x) + b\cdot\mathbf{1}_C(x) then Pn(x,)πM(x)ρn\|P^n(x,\cdot) - \pi\| \leq M(x)\rho^n for some ρ<1\rho < 1. This is the standard sufficient condition for CLT validity.

Theorem 4 (CLT). If the chain is geometrically ergodic and gL2+ε(π)g \in L^{2+\varepsilon}(\pi): n ⁣(n1j=1ng(θj)Eπ[g])dN(0,σ2)\sqrt{n}\!\left(n^{-1}\sum_{j=1}^n g(\theta^j) - E_\pi[g]\right) \xrightarrow{d} \mathcal{N}(0,\sigma^2)

The asymptotic variance is the spectral density of the chain at zero frequency: σ2=Varπ(g)+2j=1Covπ(g(θ0),g(θj))\sigma^2 = \mathrm{Var}_\pi(g) + 2\sum_{j=1}^\infty \mathrm{Cov}_\pi(g(\theta^0),\, g(\theta^j))

Geometric ergodicity guarantees this sum converges absolutely. Uniform ergodicity (stronger) requires only gL2(π)g \in L^2(\pi) (Theorem 5).

Metropolis-within-Gibbs

When a Gibbs sampler has one or more blocks with non-standard full conditionals, MH steps can be nested inside the Gibbs cycle. Each block that cannot be drawn directly is replaced by one or more MH steps targeting that block's full conditional; blocks with standard conjugate forms continue as direct draws. The resulting algorithm is a hybrid (mixture/cycle of kernels) that retains π\pi as its invariant distribution. See Time-Varying Parameter VAR for the five-block Metropolis-within-Gibbs sampler of Cogley-Morozov-Sargent (2003) and Noninformative Prior for VAR for the hit-and-run variant.

WLS Proposal for GLMMs (Gamerman 1997)

For generalized linear mixed models (GLMMs), a highly efficient weighted least squares (WLS) Metropolis proposal is obtained from the iteratively reweighted least squares (IWLS) linearization of the non-Gaussian likelihood. At state η(t)\eta^{(t)}, compute pseudo-response y~i=ηi+(yiμi)g(μi)\tilde{y}_i = \eta_i + (y_i - \mu_i)g'(\mu_i) and weight Wi1=b(θi)[g(μi)]2W_i^{-1} = b''(\theta_i)[g'(\mu_i)]^2. The resulting working-model posterior N(m(t),C(t))N(m^{(t)}, C^{(t)}) — a generalized least squares (GLS) posterior for the linearized Normal problem — serves as an independence-chain proposal whose acceptance rate exceeds 85–98% in practice. This is an instance of the independence chain family where the proposal is locally calibrated to the posterior curvature rather than fixed. See Generalized Linear Mixed Model for full details.

Prefetching: Parallelising MH via a Metropolis Tree (Strid 2010)

Standard MH is intrinsically serial: each evaluation of π(θ)\pi(\theta^*) must complete before the next proposal can be formed. Prefetching breaks this bottleneck by speculating on the accept/reject outcome and evaluating the posterior at multiple future candidate states in parallel.

Metropolis tree. With PP processors, build a binary tree where each node corresponds to a possible future state: the left branch (acceptance) moves to θ\theta^*; the right branch (rejection) stays at θt1\theta^{t-1}. A tour of size PP selects PP nodes and evaluates them in parallel. The expected draws per tour: D(P)=p=1PPr(node p selected)D(P) = \sum_{p=1}^P \Pr(\text{node } p \text{ selected}) is the theoretical speedup over one serial step.

Five variants. Algorithms differ in how branch probabilities are assigned (which determines DD):

Variant Branch probabilities DD at P=7P=7, α=0.25\alpha=0.25
Basic (Brockwell 2006) All 0.5 3.00; lower bound log2(P+1)\log_2(P+1)
Static Known α\alpha: reject = 1α1-\alpha, accept = α\alpha 3.54
Dynamic (uniform) Empirical αk\alpha_k from pilot histogram over U[0,1]U[0,1] bins 3.81
Dynamic (posterior approx.) min(β,p(θacc)/p(θcur))\min(\beta,\, p^*(\theta_\text{acc})/p^*(\theta_\text{cur})) with p=N(θm,Hm1)p^* = \mathcal{N}(\theta_m, H_m^{-1}) 4.58
Most likely path Run PP steps ahead with pp^*; non-branching tour 4.99

Optimal acceptance rate shifts downward. The joint expected output E(α,P)=E1(α)×D(α,P)E(\alpha,P) = E^1(\alpha) \times D(\alpha,P) is maximised at lower α\alpha than the serial optimum 0.234 because lower α\alpha lengthens accepted-state runs, increasing DD:

PP αopt,P\alpha_\text{opt,P}
1 0.234
7 0.120
\infty 0

Smets-Wouters benchmark (Table 1, α=0.25\alpha=0.25). Most likely path achieves theoretical speedup 4.99×4.99\times (P=7P=7) and actual cluster speedup 5.15×5.15\times on a Lenngren high-performance computing (HPC) cluster with 8 processors (80 min serial → 15 min parallel). Independence chain MH is embarrassingly parallel (S(P)PS(P)\approx P) but achieves only 13% acceptance on the 40-parameter Smets-Wouters (SW) model, making its parallelism advantage illusory.

Limitations. Practical ceiling 10\approx 101515 processors; most likely path inapplicable with discontinuous likelihoods (nonlinear Dynamic Stochastic General Equilibrium (DSGE)/particle filter); Gibbs steps (α=1\alpha=1) incompatible with prefetching.

Tuning and Diagnostics

Why It Matters

Open Questions

Related