Quant Memo
Advanced

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.

start converged region horizontal step: update x | y. vertical step: update y | x.
Each step of the chain moves in only one direction at a time — update one parameter given the current value of the other, then swap. The staircase path zig-zags but, after enough steps, its visited points densely trace out the true two-dimensional posterior (shaded contours).

The algorithm, one symbol at a time

Suppose the posterior is over two parameters, θ1\theta_1 and θ2\theta_2 ("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: p(θ1θ2,data)p(\theta_1 \mid \theta_2, \text{data}) and p(θ2θ1,data)p(\theta_2 \mid \theta_1, \text{data}) — "the distribution of θ1\theta_1 if you already knew θ2\theta_2," and vice versa. Starting from any initial guess (θ1(0),θ2(0))(\theta_1^{(0)}, \theta_2^{(0)}), at each iteration tt:

θ1(t+1)p(θ1θ2(t), data),θ2(t+1)p(θ2θ1(t+1), data).\theta_1^{(t+1)} \sim p\big(\theta_1 \mid \theta_2^{(t)},\ \text{data}\big), \qquad \theta_2^{(t+1)} \sim p\big(\theta_2 \mid \theta_1^{(t+1)},\ \text{data}\big) .

In words: draw a new value for θ1\theta_1 from its conditional distribution, using the current value of θ2\theta_2; then draw a new value for θ2\theta_2 from its conditional distribution, using the value of θ1\theta_1 you just drew — and repeat. Note the second step uses the freshly updated θ1\theta_1, not the old one; each new draw is used as soon as it's available. Repeating this for many iterations produces a sequence (θ1(1),θ2(1)),(θ1(2),θ2(2)),(\theta_1^{(1)}, \theta_2^{(1)}), (\theta_1^{(2)}, \theta_2^{(2)}), \dots 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 p(θ1,θ2data)p(\theta_1, \theta_2 \mid \text{data}), 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) y=[1.2,0.5,0.8,2.1,0.4]y = [1.2, -0.5, 0.8, 2.1, 0.4], sample mean yˉ=0.80\bar y = 0.80. Model: yiN(μ,σ2)y_i \sim N(\mu, \sigma^2), with a simplified setup where, conditional on σ2\sigma^2, the posterior for μ\mu is N ⁣(yˉ, σ2/n)N\!\left(\bar y,\ \sigma^2/n\right), and conditional on μ\mu, the posterior for σ2\sigma^2 (using an inverse-gamma prior, omitted here for brevity) works out to something proportional to an inverse-gamma with rate depending on (yiμ)2\sum (y_i-\mu)^2.

Iteration 1. Start with σ2(0)=1.0\sigma^{2(0)} = 1.0. Draw μ(1)N(0.80, 1.0/5)=N(0.80,0.20)\mu^{(1)} \sim N(0.80,\ 1.0/5) = N(0.80, 0.20) — suppose this draw comes out μ(1)=0.65\mu^{(1)} = 0.65. Now compute (yi0.65)2=(0.55)2+(1.15)2+(0.15)2+(1.45)2+(0.25)2=0.303+1.323+0.023+2.103+0.063=3.814\sum(y_i - 0.65)^2 = (0.55)^2+(-1.15)^2+(0.15)^2+(1.45)^2+(-0.25)^2 = 0.303+1.323+0.023+2.103+0.063=3.814. Draw σ2(1)\sigma^{2(1)} from its conditional using this sum — suppose it comes out σ2(1)=0.85\sigma^{2(1)} = 0.85.

Iteration 2. Draw μ(2)N(0.80, 0.85/5)=N(0.80,0.17)\mu^{(2)} \sim N(0.80,\ 0.85/5) = N(0.80, 0.17) — suppose μ(2)=0.92\mu^{(2)} = 0.92. Recompute (yi0.92)2\sum(y_i-0.92)^2 and draw σ2(2)\sigma^{2(2)} from the updated conditional. Notice each new μ\mu draw uses whatever σ2\sigma^2 was most recently sampled, and each new σ2\sigma^2 draw uses whatever μ\mu was most recently sampled — the chain never needs the full joint distribution of (μ,σ2)(\mu, \sigma^2) 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, μ(0)=10.0\mu^{(0)} = 10.0 (wildly off — no return in the data is anywhere near 10%). Iteration 1 draws σ2(1)\sigma^{2(1)} conditional on μ=10.0\mu=10.0: (yi10)2\sum(y_i-10)^2 is enormous (roughly 402402), so the conditional posterior for σ2\sigma^2 comes out very large, reflecting "if μ\mu really were 10, the data would look wildly overdispersed." But the next step draws μ(1)N(yˉ,σ2(1)/n)=N(0.80, large/5)\mu^{(1)} \sim N(\bar y, \sigma^{2(1)}/n) = N(0.80,\ \text{large}/5) — centered correctly at 0.800.80 regardless of how bad the starting μ\mu was, just with a temporarily wide spread. Within a handful of iterations, μ\mu has moved to sit near 0.800.80 and σ2\sigma^2 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.

Convergence explorer
true meansamples →
after n = 200estimate -0.025error 0.025std error 0.071

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 R^\hat R 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 kk-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
ShareTwitterLinkedIn