Gibbs Sampling
A way to draw samples from a complicated joint posterior distribution you cannot write down directly, by repeatedly sampling one variable at a time from its conditional distribution while holding everything else fixed.
Prerequisites: Bayes' Theorem, Markov Chains
A Bayesian posterior over several parameters at once — say, a population mean, a population variance, and a dozen group-level effects — often has no clean formula you can sample from directly. You know the shape of the distribution up to a constant, but you can't just plug it into a random-number generator the way you can for a normal or a beta. Gibbs sampling sidesteps the problem entirely: instead of sampling all the parameters jointly, which is hard, it samples them one at a time, each conditional on the current values of all the others — which, for a huge and useful class of models, turns out to be easy, because each individual conditional distribution is something simple like a normal or a beta. Cycle through all the variables enough times, and the sequence of samples you get behaves exactly as if you'd drawn from the full, complicated joint posterior all along.
An analogy: guessing a room's temperature and humidity by adjusting one dial at a time
Imagine you're trying to find the combination of thermostat and humidifier settings that a room's occupants find comfortable, but you can only test one dial at a time — turn the thermostat while keeping humidity fixed, see how comfort responds, then turn the humidifier while keeping the new temperature fixed, and so on. You never directly search the two-dial space at once. And yet, alternating between the two one-dial adjustments long enough, your sequence of settings wanders around and eventually spends time in each combination roughly in proportion to how comfortable it is — you've explored the full two-dimensional comfort landscape using only one-dimensional moves. Gibbs sampling is this exact strategy applied to probability: sample each parameter's conditional distribution — "given everything else is fixed, what's this one's distribution?" — one at a time, in a loop, and the accumulated samples trace out the full joint posterior.
The algorithm, one symbol at a time
Suppose the posterior is over two parameters, and ("theta sub 1," "theta sub 2") — the idea extends to any number. Gibbs sampling requires that you can sample from the two full conditional distributions: and — "the distribution of if you already knew ," and vice versa. Starting from any initial guess , at each iteration :
In words: draw a new value for from its conditional distribution, using the current value of ; then draw a new value for from its conditional distribution, using the value of you just drew — and repeat. Note the second step uses the freshly updated , not the old one; each new draw is used as soon as it's available. Repeating this for many iterations produces a sequence that is a Markov chain: each state depends only on the immediately preceding one. Under mild conditions, this chain's long-run distribution — the distribution of states it visits, once it's run long enough to forget its starting point — converges exactly to the true joint posterior , even though no step ever directly sampled from that joint distribution.
The early samples, taken before the chain has "forgotten" its arbitrary starting point, are discarded as burn-in; the remaining samples are treated as (correlated, but asymptotically valid) draws from the posterior, and any posterior summary — a mean, a credible interval — is computed by simply averaging or ranking across the retained samples.
Gibbs sampling turns one hard joint-sampling problem into a loop of easy one-variable sampling problems: sample each parameter given the current values of all the others, cycle through all parameters repeatedly, and after enough iterations the accumulated samples represent the full joint posterior — without ever having directly drawn from it.
Worked example 1: a two-step Gibbs sampler for a normal mean and variance
Data: 5 observed daily returns (in percent) , sample mean . Model: , with a simplified setup where, conditional on , the posterior for is , and conditional on , the posterior for (using an inverse-gamma prior, omitted here for brevity) works out to something proportional to an inverse-gamma with rate depending on .
Iteration 1. Start with . Draw — suppose this draw comes out . Now compute . Draw from its conditional using this sum — suppose it comes out .
Iteration 2. Draw — suppose . Recompute and draw from the updated conditional. Notice each new draw uses whatever was most recently sampled, and each new draw uses whatever was most recently sampled — the chain never needs the full joint distribution of together, only these two conditionals, each of which is a standard, easy-to-sample distribution.
Worked example 2: why the chain still converges even from a bad start
Start the same sampler from a deliberately bad initial guess, (wildly off — no return in the data is anywhere near 10%). Iteration 1 draws conditional on : is enormous (roughly ), so the conditional posterior for comes out very large, reflecting "if really were 10, the data would look wildly overdispersed." But the next step draws — centered correctly at regardless of how bad the starting was, just with a temporarily wide spread. Within a handful of iterations, has moved to sit near and has shrunk back toward a value consistent with the data's actual spread — the chain "forgets" its bad start exponentially fast. This is exactly why practitioners discard the first several hundred iterations (burn-in): early samples are still contaminated by the arbitrary starting point, and only the samples after convergence are treated as genuine posterior draws.
The explorer's running average settling down after an initial noisy stretch is the same phenomenon as burn-in: early iterations reflect the starting point more than the truth, and only the later, settled portion of the chain should be trusted.
What this means in practice
- Hierarchical Bayesian models are Gibbs sampling's home turf — the population mean, population variance, and every group-level effect each have simple conditional distributions even though their joint posterior is intractable, which is exactly the setup Gibbs sampling was built for.
- Convergence diagnostics matter. Trace plots (the sampled value over iterations), the Gelman-Rubin statistic comparing multiple independent chains, and effective sample size all check whether the chain has actually settled — treating early, unconverged samples as valid posterior draws silently corrupts every downstream summary.
- Samples are autocorrelated, not independent. Consecutive Gibbs draws are correlated with each other (each depends on the last), so the effective number of independent samples is smaller than the raw count — thinning (keeping every -th sample) or simply running longer chains compensates.
- When conditionals aren't simple, Gibbs doesn't apply directly — that's the regime for Metropolis-Hastings or, for speed at scale, variational inference, which trades exactness for a much faster approximation.
The classic confusion: mistaking the order in which Gibbs sampling updates variables — one at a time — for evidence that it's only approximating the joint posterior, or that the variables are somehow being treated as independent. Neither is true: Gibbs sampling is a mathematically exact method for sampling from the joint posterior (in the long run, after convergence), and it fully respects any dependence between the variables — the whole point is that each conditional draw is conditioned on the current, dependent value of the other variables, which is precisely how the correlation between them gets encoded into the resulting sample sequence. The approximation, if there is one, comes only from finite chain length and the discarded burn-in — not from the one-at-a-time update scheme itself.
Related concepts
Practice in interviews
Further reading
- Gelman et al., Bayesian Data Analysis, ch. 11
- Casella & George (1992), The American Statistician