01 / 21

Gibbs & Metropolis-Hastings
Sampling

Master two foundational Markov chain Monte Carlo (MCMC) algorithms for drawing samples from complex posterior distributions.

Graduate Statistics

Why Do We Need MCMC?

In Bayesian statistics we want to compute the posterior \( p(\theta \mid y) \propto p(y \mid \theta)\,p(\theta) \). For most real problems, the normalizing constant \( \int p(y \mid \theta)\,p(\theta)\,d\theta \) is analytically intractable.

MCMC methods let us approximate expectations, quantiles, and credible intervals using these correlated samples: even without knowing the normalizing constant.

Markov Chain Foundations

Markov Property
The next state depends only on the current state: \( p(\theta^{(t+1)} \mid \theta^{(t)}, \ldots, \theta^{(0)}) = p(\theta^{(t+1)} \mid \theta^{(t)}) \)
Stationary Distribution
A distribution \( \pi \) is stationary if \( \pi = \pi \, T \), where \( T \) is the transition kernel. MCMC chains are designed so \( \pi = p(\theta \mid y) \).
Ergodicity
If the chain is irreducible and aperiodic, then regardless of the starting point, it converges to \( \pi \) as \( t \to \infty \).

Detailed Balance: the Key Condition

A sufficient (though not necessary) condition for \( \pi \) to be the stationary distribution is detailed balance:

This says the probability of "being at \(\theta\) and transitioning to \(\theta'\)" equals the reverse. Both Gibbs sampling and Metropolis-Hastings are designed to satisfy detailed balance with respect to the target posterior.

\(\theta\)
\(\theta'\)
\(\pi(\theta)\,T(\theta\!\to\!\theta')\)
\(\pi(\theta')\,T(\theta'\!\to\!\theta)\)
=

The Metropolis-Hastings Algorithm

1
Initialize \( \theta^{(0)} \) to an arbitrary starting value.
2
Propose a candidate \( \theta^* \sim q(\theta^* \mid \theta^{(t)}) \) from a proposal distribution.
3
Compute the acceptance ratio: \( \alpha = \min\!\left(1,\;\dfrac{p(\theta^* \mid y)\,q(\theta^{(t)} \mid \theta^*)}{p(\theta^{(t)} \mid y)\,q(\theta^* \mid \theta^{(t)})}\right) \)
4
Accept or reject: draw \( u \sim \text{Uniform}(0,1) \). If \( u \leq \alpha \), set \( \theta^{(t+1)} = \theta^* \); otherwise \( \theta^{(t+1)} = \theta^{(t)} \).
5
Repeat steps 2-4 for \( t = 1, 2, \ldots, N \). Discard the first \( B \) samples (burn-in).

Understanding the Acceptance Ratio

The acceptance ratio \( \alpha \) balances two forces: how much the target favors the proposal versus how much the proposal distribution skews towards it.

Symmetric proposal
If \( q(\theta^* \mid \theta) = q(\theta \mid \theta^*) \), the Hastings correction is 1 and we get the original Metropolis algorithm.
No normalizing constant needed
Since \( \alpha \) uses a ratio of target densities, the unknown normalizing constant cancels.

Visualizing a Trace Plot

A trace plot shows the sampled values \( \theta^{(t)} \) over iterations. Good mixing looks like a "fuzzy caterpillar" oscillating around the posterior mean.

Iteration t
\(\theta\)
\(E[\theta|y]\)
Burn-in Rejected

The Gibbs Sampler

Gibbs sampling is a special case of Metropolis-Hastings where the proposal is always accepted. It works by cycling through each parameter and sampling from its full conditional distribution.

1
Initialize all parameters \( \theta_1^{(0)}, \theta_2^{(0)}, \ldots, \theta_p^{(0)} \).
2
For each \( j = 1, \ldots, p \): draw \( \theta_j^{(t+1)} \sim p(\theta_j \mid \theta_{-j}^{(\text{current})}, y) \)
3
Use the latest values: when sampling \( \theta_j \), use updated values \( \theta_1^{(t+1)}, \ldots, \theta_{j-1}^{(t+1)} \) and old values \( \theta_{j+1}^{(t)}, \ldots \)
4
Repeat for \( N \) iterations, discarding burn-in.

Gibbs Is MH with \( \alpha = 1 \)

If we set the MH proposal to be the full conditional \( q(\theta_j^* \mid \theta_{-j}) = p(\theta_j^* \mid \theta_{-j}, y) \), then the acceptance ratio simplifies:

So every proposal is accepted. This is why Gibbs sampling requires no tuning of proposal distributions, but it demands closed-form full conditionals.

Gibbs in Two Dimensions

For a bivariate target \( p(\theta_1, \theta_2 \mid y) \), Gibbs alternates between sampling \(\theta_1\) (horizontal moves) and \(\theta_2\) (vertical moves).

Start
sample \(\theta_1\)
sample \(\theta_2\)
\(\theta_1\)
\(\theta_2\)

Gibbs vs. Metropolis-Hastings

Gibbs Sampling Metropolis-Hastings
Proposal Full conditional \( p(\theta_j \mid \theta_{-j}, y) \) Any \( q(\theta^* \mid \theta^{(t)}) \)
Acceptance rate Always 1 Depends on \( \alpha \)
Tuning required None Proposal variance
Requirement Known full conditionals Only evaluate \( p(\theta) \) up to a constant
Update style One component at a time Full vector or block
Generality Narrower (conjugate models) Universal

Tuning the Proposal Distribution

In MH, the proposal variance \( \sigma_q^2 \) crucially affects performance. The common choice is a random walk: \( \theta^* = \theta^{(t)} + \epsilon \), where \( \epsilon \sim \mathcal{N}(0, \sigma_q^2) \).

\( \sigma_q \) too small
High acceptance rate but tiny steps: the chain explores slowly (high autocorrelation, low effective sample size).
\( \sigma_q \) too large
Most proposals land in low-density regions and are rejected: the chain gets stuck (low acceptance rate).
\( \sigma_q \) just right
Target acceptance rate of ~23-44% (Roberts et al., 1997). For 1D targets, ~44%; for high dimensions, ~23%.

Example 1: MH for a Normal Mean

Suppose \( y_1, \ldots, y_n \overset{\text{iid}}{\sim} \mathcal{N}(\mu, \sigma^2) \) with known \( \sigma^2 = 1 \), prior \( \mu \sim \mathcal{N}(0, 10^2) \). We want to sample from \( p(\mu \mid y) \).

1
Target: \( p(\mu \mid y) \propto \exp\!\left(-\frac{1}{2}\sum(y_i - \mu)^2\right)\exp\!\left(-\frac{\mu^2}{200}\right) \)
2
Proposal: \( \mu^* \sim \mathcal{N}(\mu^{(t)}, \sigma_q^2) \) with \( \sigma_q = 0.5 \). Symmetric, so no Hastings correction.
3
Acceptance ratio: \( \alpha = \min\!\left(1,\;\frac{p(\mu^* \mid y)}{p(\mu^{(t)} \mid y)}\right) \). Compute log-ratio to avoid numerical underflow.
4
In practice: compute the log-posterior ratio \( r = \ell(\mu^*) - \ell(\mu^{(t)}) \), where \( \ell \) is the log-posterior. Then \( \alpha = \min(1, \exp(r)) \).

Example 2: Gibbs for Bivariate Normal

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

With \( \rho = 0.8 \), the conditionals are narrow, so the Gibbs sampler takes small, correlated steps. High correlation \( \to \) slower mixing.

Example 3: Block Gibbs for Hierarchical Models

Consider a Normal hierarchical model: \( y_{ij} \mid \mu_j \sim \mathcal{N}(\mu_j, \sigma^2) \), \( \mu_j \mid \mu_0, \tau^2 \sim \mathcal{N}(\mu_0, \tau^2) \).

1
Sample group means: For each \( j \), draw \( \mu_j \mid y, \mu_0, \tau^2, \sigma^2 \). This is a conjugate Normal full conditional.
2
Sample hyperparameters: Draw \( \mu_0 \mid \mu_1, \ldots, \mu_J, \tau^2 \) (Normal conditional).
3
Sample variances: Draw \( \tau^2 \mid \mu_0, \mu_1, \ldots \) and \( \sigma^2 \mid y, \mu_1, \ldots \) from Inverse-Gamma conditionals.

Common Mistakes

✕ Insufficient Burn-in
Using early samples before the chain reaches stationarity. Always discard the first \(B\) iterations and check trace plots.
✕ Ignoring Autocorrelation
Treating MCMC samples as independent. Use effective sample size (ESS) to assess how many "independent" draws you have.
✕ Bad Proposal Variance
Not tuning \( \sigma_q \). Monitor acceptance rate: aim for 23-44%, not 0.1% or 99%.
✕ Single Chain Fallacy
Running only one chain. Use multiple chains from dispersed starts and compute \( \hat{R} \) (Gelman-Rubin diagnostic).
✕ Assuming Gibbs Always Works
Gibbs fails on highly correlated parameters. Check for slow mixing and consider reparameterization or block updates.

Question 1 of 5

Why can we use a ratio of target densities in the MH acceptance probability, rather than evaluating each density exactly?

Question 2 of 5

In Gibbs sampling, the acceptance probability for each proposed value is:

Question 3 of 5

If the MH proposal variance \( \sigma_q^2 \) is set much too large, what happens?

Question 4 of 5

For a bivariate normal target with correlation \( \rho = 0.99 \), why does Gibbs sampling mix slowly?

Question 5 of 5

Which condition ensures that a Markov chain has the target distribution \( \pi \) as its stationary distribution?