Skip to content

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.

Advanced ~12 min8 steps

What runs in CodeSplash

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

Read first

01

Full conditionals

Read

Gibbs 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.

zj(t+1)∼p(zj∣z1(t+1),…,zj−1(t+1), zj+1(t),…,zd(t), x).z_j^{(t+1)}\sim p\big(z_j\mid z_1^{(t+1)},\ldots,z_{j-1}^{(t+1)},\ z_{j+1}^{(t)},\ldots,z_d^{(t)},\ x\big).

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;
02

Use the newest values

Read

A systematic scan updates z1z_1, then z2z_2 using the new z1z_1, 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 ρ=0.8\rho=0.8, alternating z1∣z2∼N(ρz2, 1−ρ2)z_1\mid z_2\sim N(\rho z_2,\,1-\rho^2) and z2∣z1∼N(ρz1, 1−ρ2)z_2\mid z_1\sim N(\rho z_1,\,1-\rho^2). After warmup the sample covariance between the two is 0.82, close to the true 0.8.

03

Predict: more correlation

Predict

Predict first

Raise the correlation from 0.8 to nearly 1 and keep the same Gibbs sampler. What happens to its mixing?

It mixes just as well: every draw is still acceptedIt mixes far worse: each step moves only a little
Reveal

Far worse. With ρ\rho near 1, each conditional has standard deviation 1−ρ2\sqrt{1-\rho^2}, 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.

04

Blocking and collapsing

Read

Two standard remedies, both changes to the transition rather than to the model:

  • Blocking. Draw strongly dependent variables together from their joint conditional. Drawing (z1,z2)(z_1,z_2) 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.

0.90

How strongly the Gaussian's two coordinates move together.

Sampler

Gibbs needs each coordinate's conditional distribution; only the Gaussian has them here.

0.50

Random-walk proposal scale, or HMC's leapfrog step.

10

HMC only: steps of the simulated trajectory per proposal.

1000

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.

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.

05

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.
06

Where it stands in CodeSplash

Read
07

Check your understanding

Checkpoint

Check 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 z1z_1 from p(z1∣z2old)p(z_1\mid z_2^{old}) and z2z_2 from p(z2∣z1old)p(z_2\mid z_1^{old}) 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.

08

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