- Introduced by
- Stuart Geman & Donald Geman
- Year
- 1984
- Type
- MCMC sampling algorithm
- Family
- Bayesian / computational
What does it do?
Gibbs sampling is a Markov chain Monte Carlo algorithm that approximates a high-dimensional posterior distribution by repeatedly drawing each parameter from its full conditional distribution given all other parameters and the data.
Each draw comes directly from a conditional distribution, so no proposal is rejected. Closed-form conditionals do not by themselves guarantee efficient exploration, however: when parameters are strongly correlated in the posterior, the chain can mix slowly even though every draw is accepted.
The intuition behind the method
Imagine you need to explore a mountain landscape in many dimensions at once, which is too complex to map analytically. Gibbs sampling fixes all but one dimension, moves along that dimension to a position drawn from its conditional distribution given everything else, then does the same for the next dimension, and so on. After many such sweeps, the visited points represent the full joint landscape. The key idea is that sampling one parameter at a time, given the rest, is often easier than sampling all parameters jointly.
History
Gibbs distributions, the statistical-mechanics concept behind much of this literature, were brought into image analysis by Stuart and Donald Geman's 1984 paper on Bayesian image restoration. The paper draws an analogy between images and statistical-mechanics systems, uses Gibbs distributions and Markov random field models for the image model, and proposes an annealing-based relaxation for maximum a posteriori image estimation. Its abstract therefore concerns Gibbs distributions and MAP relaxation rather than a sampler that draws each parameter from its full conditional. Gelfand and Smith's 1990 paper reviewed and compared the Gibbs sampler with stochastic substitution and sampling-importance-resampling as sampling-based approaches to calculating marginal densities, and illustrated their use for Bayesian posterior densities of structured models. Software such as WinBUGS and, later, JAGS made Gibbs-based inference widely used in applied Bayesian work.
When to use it
Use Gibbs sampling when the joint posterior is intractable but all full conditional distributions can be derived in closed form, the typical situation with conjugate Bayesian models such as Bayesian linear regression with Normal-Inverse-Gamma priors, Gaussian mixture models, and Latent Dirichlet Allocation. It is also useful when parameters naturally group into blocks with tractable conditionals, allowing a blocked Gibbs strategy. Avoid Gibbs sampling when full conditionals cannot be computed analytically (use Metropolis-Hastings or HMC instead), when parameters are highly correlated in the posterior (causing slow mixing and requiring many more iterations), or when the likelihood is non-conjugate so that the conditionals lack a standard form.
Limitations and considerations
- Requires analytically tractable full conditional distributions; non-conjugate models demand auxiliary-variable augmentation or hybrid methods such as a Metropolis step within the sampler.
- Highly correlated parameters cause slow mixing: the chain takes many small steps and requires far more iterations to explore the posterior.
- Because coordinates are updated one after another, high-dimensional problems can mix more slowly than with gradient-based samplers such as HMC, which use gradient information to make larger, informed moves.
- Convergence can be hard to detect; poorly initialised or poorly mixed chains yield biased posterior summaries.
- Correctness depends on the model specification: the full conditionals must be derived from a single joint posterior, which must be proper (check propriety when using improper priors), and the chain must be irreducible and ergodic over the posterior's support for its draws to represent the target distribution.
Source conditions do not replace independent scientific review or validation by an analysis engine.
Strengths
- Every proposed draw is accepted, so there is no wasted computation from rejections as in Metropolis-Hastings.
- Simple to implement for conjugate models: each step is a direct draw from a named distribution.
- Parameters that are conditionally independent given the rest, such as the latent indicators in a mixture model, can be updated together in one block, which keeps the sweep simple for topic models and state-space models.
- Blocked Gibbs can group correlated parameters into one draw, which can improve mixing relative to updating them one at a time.
- Conceptually transparent: the algorithm maps directly onto the model's conditional structure.
Applications
- Bayesian linear and generalised linear models with conjugate priors, including Normal-Inverse-Gamma regression.
- Latent Dirichlet Allocation (LDA) for topic modelling, where collapsed Gibbs sampling is one commonly used inference approach.
- Gaussian mixture models and hidden Markov models, where latent indicator variables have tractable conditionals.
- State-space and time-series models with data augmentation schemes.
- Bayesian vector autoregression (BVAR) and error-correction models estimated by Gibbs sampling, as implemented in econometric software libraries.
- Bayesian variable selection using spike-and-slab priors and stochastic search.
- Hierarchical Bayesian models in ecology, epidemiology, and educational testing.
How it works
How it works
Choose starting values for all parameters theta_1, ..., theta_d. These can be prior draws, maximum likelihood estimates, or simple defaults. The starting values affect how long burn-in must last, not the target distribution the chain converges to.
Derive or identify the full conditional distribution for each parameter: the distribution of theta_i given all other parameters theta_{-i} and the data y. For models with conjugate priors these conditionals are standard distributions (Normal, Gamma, Beta, Dirichlet) that can be sampled directly.
Formula
p(\theta_i \mid \theta_{-i}, \mathbf{y})Sweep through all parameters one at a time (or in blocks), drawing each from its full conditional given the current values of all others. One complete sweep constitutes one iteration of the Gibbs sampler and produces one joint sample from the chain.
Formula
\theta_i^{(t+1)} \sim p(\theta_i \mid \theta_{-i}^{(t)}, \mathbf{y}), \quad i = 1,\ldots,dDiscard an initial burn-in period during which the chain moves from its starting point toward the high-probability region of the posterior. Convergence is assessed with diagnostics such as R-hat (potential scale reduction factor), effective sample size, and trace plots, ideally across several chains started from dispersed values.
After burn-in, average the retained post-burn-in draws to estimate posterior means, and compute quantiles of those draws for credible intervals. For posterior predictive checks or forecasts, take each retained draw theta^(t), simulate a new observation y_rep from the likelihood p(y_rep | theta^(t)), and summarise the resulting draws.
Formula
\hat{\theta}_i = \frac{1}{T}\sum_{t=1}^{T}\theta_i^{(t)}Common pitfalls
- Skipping convergence diagnostics (R-hat, effective sample size, trace plots) and treating all post-burn-in samples as valid.
- Using a burn-in period that is too short when the starting values are far from the posterior mass.
- Ignoring strong posterior correlations between parameters, which cause the sampler to explore only a narrow slice of the posterior per iteration.
- Conflating the Gibbs sampler's guaranteed acceptance with guaranteed correctness: the chain must still mix and converge.
- Thinning usually discards information for posterior mean estimates; it is mainly justified when storage is constrained, not as a way to improve the estimates.
Frequently asked questions
Why does Gibbs sampling always accept? Is it not too good to be true?
Gibbs draws exactly from the correct conditional distribution, so the acceptance probability is always 1 by construction; there is no approximation in the draw itself. The caveat is that this only works when the full conditional is analytically available and can be sampled efficiently. When it cannot, you must fall back to a Metropolis step within the sampler.
How is Gibbs sampling related to Metropolis-Hastings?
Gibbs sampling is a special case of Metropolis-Hastings where each proposal is drawn exactly from the conditional, giving an acceptance ratio of exactly 1. In practice the two are often combined: a Metropolis-within-Gibbs scheme uses Metropolis steps for parameters whose conditionals are not standard distributions.
How many burn-in iterations do I need?
There is no universal rule. Run several chains from dispersed starting points and check R-hat (values close to 1 are reassuring) and the trace plots. The required burn-in depends on how far the starting values are from the posterior and on how well the model's conditional structure mixes. Well-conditioned models may need little burn-in, while strongly correlated or poorly specified models may need far more.
When should I use HMC instead of Gibbs?
Prefer Hamiltonian Monte Carlo when your model has non-conjugate likelihoods, strong posterior correlations, or a continuous high-dimensional parameter space. HMC uses gradient information to make large, informed jumps, mixing more efficiently in those settings. Gibbs remains preferable when conjugate conditionals are available and the model structure naturally decomposes into tractable blocks.
What software implements Gibbs sampling?
JAGS (Just Another Gibbs Sampler) and WinBUGS/OpenBUGS are dedicated Gibbs-based samplers widely used in applied Bayesian statistics. Stan is built around HMC/NUTS rather than Gibbs steps. In R, the rjags and R2jags packages interface with JAGS.
Related methods
Sources
Geman, S. & Geman, D. (1984). Stochastic relaxation, Gibbs distributions, and the Bayesian restoration of images. IEEE Transactions on Pattern Analysis and Machine Intelligence, 6(6), 721-741.
Open DOI referenceGelfand, A. E. & Smith, A. F. M. (1990). Sampling-based approaches to calculating marginal densities. Journal of the American Statistical Association, 85(410), 398-409.
Open DOI referenceIntroduction to Spatial Econometrics, Chapter 6: VAR and Error Correction Models, p. 266