Gibbs sampling: one variable at a time
When each variable's distribution given all the others is easy to draw from, cycle through them. Every move is accepted, yet correlated variables can make Gibbs crawl. Learn the sweep, blocking and collapsing, and the traps.
What runs in CodeSplash
- INF010Gibbs sampling Runs as a notebook
You will be able to
- Describe a systematic-scan Gibbs sweep and why every draw is accepted
- Explain why strong correlation slows Gibbs sampling, and how blocking helps
- Spot two classic bugs, stale conditioning and label switching
Full conditionals
ReadGibbs samplingGibbs samplingMCMC that redraws one variable (or block) at a time from its distribution given all the others; every draw is accepted. splits the unknowns into pieces and redraws each from its full conditional: its distribution given the data and the current values of everything else.
It is Metropolis–Hastings with a proposal so good that the acceptance probability is exactly one. It shines in conjugate models, mixtures and graphical models, where each conditional is a standard distribution: a normal mean given a variance, a variance given a mean, a component label given the parameters.
Reference · INF010 · Runs as a notebookGibbs samplingSamples a joint distribution by repeatedly sampling blocks from their full conditional distributions. It is central to conjugate latent-variable models, graphical models, and Bayesian mixtures;Use the newest values
ReadA systematic scan updates , then using the new , and so on. That detail matters. Drawing every coordinate from conditionals evaluated at the old state, all at once, is a different kernel that generally doesn't keep the target.
The reference's program samples a bivariate normal with correlation , alternating and . After warmup the sample covariance between the two is 0.82, close to the true 0.8.
Predict: more correlation
PredictPredict first
Raise the correlation from 0.8 to nearly 1 and keep the same Gibbs sampler. What happens to its mixing?
Reveal
Far worse. With near 1, each conditional has standard deviation , tiny, so each update barely moves along the long, thin ridge. Acceptance stays at one; exploration collapses. The draws are highly autocorrelated and the effective sample size drops.
Blocking and collapsing
ReadTwo standard remedies, both changes to the transition rather than to the model:
- Blocking. Draw strongly dependent variables together from their joint conditional. Drawing jointly in the example would give independent draws in one step.
- Collapsing. Integrate a variable out analytically, sample the rest, then reconstruct the collapsed variable afterward if you need it. Collapsed Gibbs is how topic models are often fitted.
Compare blocking schemes at matched compute: a joint draw can cost more per step but still win per effective draw.
Pick the Gibbs sampler on the correlated Gaussian and raise the correlation toward 1: acceptance stays at 100% while the effective sample size collapses.
EExplorable · MCMC samplers: acceptance is not mixing
Do four chains of a sampler agree about the target, and does a high acceptance rate mean they will?
Target
The distribution the chains try to sample.
How strongly the Gaussian's two coordinates move together.
Sampler
Gibbs needs each coordinate's conditional distribution; only the Gaussian has them here.
Random-walk proposal scale, or HMC's leapfrog step.
HMC only: steps of the simulated trajectory per proposal.
Draws in each of the four chains.
- Chain 1, starts at (-3, -3)
- Chain 2, starts at (3, 3)
- Chain 3, starts at (-3, 3)
- Chain 4, starts at (3, -3)
- Acceptance rate
- 0.533
- R-hat of x₁
- 1.029
- Bulk ESS of x₁
- 72
- ESS per draw
- 0.018
- Puzzle: Find a sampler and step size with R-hat below 1.01 and a bulk ESS above 400.
- Puzzle: Push the acceptance rate above 90% while R-hat stays above 1.05.
Random-walk Metropolis on the correlated Gaussian: 53% of proposals accepted, R-hat 1.029, bulk ESS 72 of 4000 draws. The four chains still disagree, so these draws don't describe the target yet. The acceptance rate alone says nothing about mixing.
Pitfalls
Read- Acceptance of one proves nothing. Mixing can be arbitrarily slow; check traces, R-hat and ESS for the quantities you report.
- Derive every conditional. An approximate conditional is not exact Gibbs; if you use one, call the method approximate or add a correction step.
- Mixtures switch labels. A Gibbs sampler for a mixture can swap component labels mid-chain. Means of "component 1" then blend components; summarize label-free quantities or fix an ordering.
- Hard constraints trap the chain. If two variables must sum to a constant, updating one at a time can't move at all.
Where it stands in CodeSplash
ReadCheck your understanding
CheckpointCheck yourself
Why does an exact Gibbs update never get rejected?
Show the answer
Answer: b
The proposal is the target's own conditional, so the ratio in the MH rule equals one. Acceptance says nothing about mixing.
Check yourself
Two regression coefficients have posterior correlation 0.998. A one-at-a-time Gibbs sampler gives an effective sample size of 40 from 10,000 draws. What's the best fix?
Show the answer
Answer: b
Correlation forces tiny one-coordinate moves. A block draw follows the ridge; centering often removes the correlation in the first place. Thinning discards information without improving mixing.
Check yourself
A sampler draws from and from at the same time, both using old values. Is that Gibbs sampling?
Show the answer
Answer: b
Sequential Gibbs conditions each draw on the newest values. Simultaneous updates from stale states define another kernel; it is exact only in special cases, such as independent variables.
Recap
Recap- Gibbs redraws each piece from its full conditional, using the newest values; every draw is accepted.
- Strong correlation makes the steps tiny; block or reparameterize.
- Derive each conditional; watch label switching in mixtures.
- Runs as a notebook; Gibbs-style updates power BART's sampler.
Concepts in this lesson