Historical Context & Motivation
Bayesian inference requires computing the posterior distribution p(θ | y) ∝ p(y | θ) p(θ), but in most realistic models the normalizing constant — the marginal likelihood p(y) — involves an intractable integral over a high-dimensional parameter space. Before the advent of Markov chain Monte Carlo (MCMC) methods, practitioners were largely confined to conjugate models or crude numerical quadrature, both of which scale poorly with dimensionality. The development of MCMC fundamentally changed statistical practice by offering a general-purpose computational strategy: construct a Markov chain whose stationary distribution equals the target posterior, run the chain long enough, and treat the resulting samples as draws from the posterior. Two of the most influential MCMC algorithms — the Metropolis–Hastings algorithm and Gibbs sampling — emerged from different scientific communities and converged into the cornerstone toolkit of modern Bayesian computation.
The central question that both the Metropolis–Hastings algorithm and the Gibbs sampler answer is deceptively simple: how can we generate samples from a probability distribution that we can evaluate (up to a normalizing constant) but cannot sample from directly? Understanding the conceptual foundations of these algorithms — their construction, convergence guarantees, and relative strengths — is essential for any practitioner of Bayesian statistics, whether the goal is fitting a simple hierarchical model or deploying a complex state-space model in a research setting.
Core Principles & Definitions
Before examining the specific algorithms, it is important to ground ourselves in the conceptual machinery that makes MCMC work. Both the Metropolis–Hastings algorithm and the Gibbs sampler are instances of a broader family of methods that exploit the theory of Markov chains — stochastic processes in which the probability of transitioning to the next state depends only on the current state, not on the history of states visited. The key insight is that if we design a chain with a transition kernel satisfying certain regularity conditions (irreducibility, aperiodicity, and positive recurrence), the chain will possess a unique stationary distribution. By engineering that stationary distribution to equal our target posterior, the chain's trajectory becomes an (asymptotically valid) sample from the posterior.
Markov Property
Stationary Distribution
Detailed Balance
Ergodicity & Convergence
Burn-in & Mixing
Visual Explanation — How the Algorithms Explore Parameter Space
The following diagram contrasts the behavior of the Metropolis–Hastings algorithm with that of the Gibbs sampler on a bivariate posterior. Metropolis–Hastings proposes moves in arbitrary directions, accepting or rejecting each proposal via an acceptance probability. Gibbs sampling, by contrast, cycles through each coordinate in turn, drawing from the full conditional distribution along that axis. The visual difference is striking: Metropolis–Hastings traces a random walk with occasional rejections (shown as stationary dots), while Gibbs sampling moves in axis-aligned steps, always accepting.
Observe the characteristic difference in trajectory shape. The Metropolis–Hastings path wanders freely through parameter space, but the red dots — rejected proposals — represent wasted computation. The Gibbs sampler, in the right panel, takes orthogonal steps: each iteration updates one coordinate while holding the others fixed. This structure eliminates rejections entirely but introduces strong autocorrelation when the posterior exhibits high correlation between parameters, since axis-aligned moves struggle to traverse the long axis of an elongated elliptical contour. This visual contrast encapsulates the fundamental trade-off between the two algorithms: Metropolis–Hastings offers flexibility in proposal design at the cost of potential rejections, while Gibbs sampling guarantees acceptance at the cost of requiring tractable full conditionals and potentially slow mixing under strong correlations.
Mathematical Framework
The Metropolis–Hastings Algorithm
Suppose we wish to sample from a target density π(θ) that we can evaluate up to a normalizing constant — that is, we know π(θ) ∝ f(θ) for some computable function f. The Metropolis–Hastings algorithm proceeds by iterating two steps at each time t. First, a candidate θ* is drawn from a proposal distribution q(θ* | θ(t)). Second, the candidate is accepted with a probability α that corrects for any asymmetry in the proposal distribution and for the ratio of target densities.
After computing α, we draw u ~ Uniform(0, 1). If u ≤ α, we set θ(t+1) = θ*; otherwise θ(t+1) = θ(t). This accept-reject mechanism ensures that the chain satisfies detailed balance with respect to π, and therefore π is the stationary distribution of the chain. The beauty of the construction is that the unknown normalizing constant cancels in the ratio, so we never need to compute p(y).
The Gibbs Sampler
Now consider a parameter vector θ = (θ₁, θ₂, …, θ_d). The Gibbs sampler replaces the joint sampling problem with a sequence of univariate (or lower-dimensional) sampling problems. At each iteration, the algorithm cycles through the d components, drawing each from its full conditional distribution — the distribution of that component given the current values of all others.
Detailed Comparison — MH vs. Gibbs
While the Gibbs sampler can be viewed as a special case of Metropolis–Hastings, the practical considerations governing their use are quite different. The diagram below illustrates a decision flowchart for choosing between the two algorithms, and the accompanying table summarizes their key properties.
| Feature | Metropolis–Hastings | Gibbs Sampling |
|---|---|---|
| Proposal mechanism | User-specified q(θ* | θ); can be any distribution on the parameter space | Full conditional distributions p(θⱼ | θ₋ⱼ, y); must be available in tractable form |
| Acceptance rate | Varies with q; optimal ≈ 23.4% for random walk MH in high dimensions (Roberts et al., 1997) | 100% — every draw is accepted because the proposal equals the full conditional |
| Tuning required | Yes — proposal scale/shape must be tuned for good acceptance and mixing | Minimal — no proposal tuning, but may need blocking or reparameterization |
| Dimensionality | Can propose moves in all dimensions simultaneously; struggles in very high dimensions without gradient information | Updates one (or a block of) component(s) per step; scales well if conditionals are easy |
| Correlated posteriors | Can use non-axis-aligned proposals to navigate correlation structure | Axis-aligned moves cause slow mixing under strong posterior correlations |
Worked Example — Gibbs Sampler for a Normal Model
Consider the classical conjugate normal model. We observe y₁, y₂, …, yₙ iid ~ Normal(μ, σ²), and we wish to perform Bayesian inference on both the mean μ and the variance σ² (or equivalently the precision τ = 1/σ²). We place standard conjugate priors: μ ~ Normal(μ₀, σ₀²) and τ ~ Gamma(a, b). The joint posterior p(μ, τ | y) does not simplify to a single recognizable distribution in closed form, but the full conditionals do. This makes the problem ideal for the Gibbs sampler.
Strengths, Limitations, and Practical Considerations
| Consideration | Strength | Limitation |
|---|---|---|
| Ease of implementation | Gibbs is simple when full conditionals are known; MH requires only the ability to evaluate the target up to a constant. | MH needs careful tuning of the proposal; Gibbs requires derivation of every full conditional, which may be analytically demanding. |
| Mixing speed | MH can use non-axis-aligned or gradient-informed proposals; Gibbs can use blocking to update correlated parameters jointly. | Random walk MH mixes slowly in high dimensions; standard Gibbs suffers under strong posterior correlations. |
| Generality | MH applies to virtually any target density; it is the most general MCMC framework. | Gibbs is restricted to models where all full conditionals are available in tractable form. |
| Convergence diagnosis | Standard diagnostics (R-hat, trace plots, ESS) apply to both algorithms equally. | Slow mixing can masquerade as convergence; multimodal targets are challenging for both methods without specialized extensions. |
| Scalability | Gibbs naturally decomposes the problem; data-augmentation variants (e.g., EM-like) are powerful for latent variable models. | Both methods can be prohibitively slow for very large datasets without sub-sampling or variational approximation. |
Connection to Advanced MCMC Methods
The Metropolis–Hastings algorithm and the Gibbs sampler laid the conceptual groundwork for a rich ecosystem of advanced MCMC methods. Understanding where these foundational algorithms sit in the broader landscape helps contextualize their role in modern Bayesian practice. The table below highlights the progression from classical MCMC to its modern descendants.
| Classical MCMC | Modern Extension | Key Innovation |
|---|---|---|
| Random Walk Metropolis–Hastings | Hamiltonian Monte Carlo (HMC) | Uses gradient information (Hamiltonian dynamics) to propose distant, high-probability states; dramatically reduces random-walk behavior. |
| HMC with manual tuning | No-U-Turn Sampler (NUTS) | Automatically tunes the trajectory length in HMC by detecting when the trajectory begins to double back; implemented in Stan. |
| Standard Gibbs Sampler | Slice Sampling | Introduces an auxiliary variable to define a 'slice' under the density; avoids the need for a closed-form full conditional while maintaining automatic acceptance. |
| Fixed-dimension MH | Reversible-Jump MCMC | Extends MH to spaces of varying dimension, enabling Bayesian model selection (e.g., choosing the number of mixture components). |
| Single-chain MCMC | Parallel Tempering | Runs multiple chains at different 'temperatures'; hot chains explore broadly and swap states with cold chains, enabling escape from local modes. |
Despite the proliferation of more sophisticated samplers, the Gibbs sampler and Metropolis–Hastings remain indispensable. Many modern algorithms use them as sub-routines — for instance, a Metropolis-within-Gibbs strategy applies MH steps for components whose full conditionals are not available in closed form, while using standard Gibbs updates for the remaining components. This hybrid approach exemplifies the modular nature of MCMC: once you understand the two foundational algorithms, composing and extending them is largely a matter of engineering. Furthermore, the conceptual clarity of MH and Gibbs provides the theoretical lens through which we analyze more advanced methods — their convergence properties, detailed balance conditions, and mixing diagnostics all trace back to the principles introduced in this lesson.
Practice Problems
Lesson Summary
This lesson introduced the two foundational Markov chain Monte Carlo algorithms in Bayesian statistics. The Metropolis–Hastings algorithm is the most general MCMC framework: it proposes candidate states from a user-chosen proposal distribution and accepts or rejects them via an acceptance probability that corrects for any mismatch between the proposal and the target posterior. The key identity — that the unknown normalizing constant cancels in the acceptance ratio — is what makes Bayesian computation feasible for complex models. The Gibbs sampler is a special case of MH that decomposes the joint sampling problem into a cycle of draws from full conditional distributions, achieving a 100% acceptance rate at every step. Both algorithms rely on the theory of Markov chains — specifically detailed balance and ergodicity — to guarantee convergence to the target posterior.
In practice, Gibbs sampling excels in conditionally conjugate models where full conditionals are recognizable distributions, while Metropolis–Hastings serves as the fallback for any parameter whose full conditional is intractable. The Metropolis-within-Gibbs hybrid combines the best of both worlds. Challenges such as slow mixing under strong posterior correlations and convergence diagnostics remain central practical concerns, and modern successors like Hamiltonian Monte Carlo address these by incorporating gradient information. Mastery of Gibbs and MH provides the conceptual foundation needed to understand, implement, and critique the entire MCMC toolkit.