Gibbs & Metropolis-Hastings
Sampling
Master two foundational Markov chain Monte Carlo (MCMC) algorithms for drawing samples from complex posterior distributions.
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
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.
The Metropolis-Hastings Algorithm
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.
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.
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.
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).
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) \).
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) \).
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) \).
Common Mistakes
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?