Skip to main content

Sampling MCMC

Gibbs Sampling

The classical conditional-update MCMC algorithm: sample each variable from its full conditional, accept every proposal, and then pay close attention to mixing, blocking, and hierarchical parameterization.

CoreTier 1StableSupporting~60 min

Why This Matters

Gibbs sampling is a foundational MCMC algorithm with a long history in applied Bayesian statistics. It powered earlier-generation tools like BUGS and JAGS, and remains standard whenever a model has full conditionally conjugate structure. Modern general-purpose Bayesian workflows more often reach for Hamiltonian Monte Carlo (Stan defaults to NUTS) because Gibbs mixes poorly in high-dimensional or strongly correlated posteriors. Gibbs still wins when the conditionals are closed-form: hierarchical models with conjugate priors, mixture models, LDA-style latent-variable models, and any setting where sampling from p(xixi)p(x_i \mid x_{-i}) is cheap.

Hide overviewShow overview
Infographic on Gibbs sampling: the cyclic update rule sampling each variable from its full conditional given the others, the convergence guarantee for irreducible chains, and common ML applications (Bayesian linear regression, latent Dirichlet allocation, Boltzmann machines, hierarchical models).
Gibbs sampling decomposes a hard joint distribution into easy conditionals, then iterates.
Gibbs Sampling: Coordinate-wise Conditional SweepsVariables:xxxxStep 1xsamplexfixedxfixedxfixedx₁ ∼ p(x₁ | x₋₁)Step 2xfixedxsamplexfixedxfixedx₂ ∼ p(x₂ | x₋₂)Step 3xfixedxfixedxsamplexfixedx₃ ∼ p(x₃ | x₋₃)Step 4xfixedxfixedxfixedxsamplex₄ ∼ p(x₄ | x₋₄)2D Trajectory (bivariate case): staircase patternstartx₁x₂sample x₁sample x₂

Its appeal is simplicity: instead of designing a and tuning acceptance rates, you just sample each variable from its conditional distribution given the rest. No tuning parameters, no rejections.

Mental Model

The target is a joint distribution (x1,,xd)(x_1, \ldots, x_d) that is hard to sample from directly. The π(xixi)\pi(x_i \mid x_{-i}) are often tractable when the joint is not, for example in Bayesian models with conjugate priors, where each xixix_i \mid x_{-i} reduces to a standard distribution.

Gibbs sampling cycles through the coordinates, drawing each xix_i from its full conditional given the current values of the others. The resulting Markov chain has π\pi as its .

Formal Setup and Notation

Let π(x1,x2,,xd)\pi(x_1, x_2, \ldots, x_d) be the target joint distribution. We assume we can sample from each full conditional distribution.

Definition

Full Conditional Distribution

The full conditional distribution of variable xix_i given all other variables is:

π(xixi)=π(xix1,,xi1,xi+1,,xd)=π(x1,,xd)π(x1,,xd)dxi\pi(x_i \mid x_{-i}) = \pi(x_i \mid x_1, \ldots, x_{i-1}, x_{i+1}, \ldots, x_d) = \frac{\pi(x_1, \ldots, x_d)}{\int \pi(x_1, \ldots, x_d)\, dx_i}

where xix_{-i} denotes all variables except xix_i. In practice, to derive the full conditional for xix_i, you write out the full joint density, treat everything that does not involve xix_i as a constant, and recognize the resulting kernel as a known distribution.

Definition

Gibbs Sampler (Systematic Scan)

Given target π(x1,,xd)\pi(x_1, \ldots, x_d) and initial state (x1(0),,xd(0))(x_1^{(0)}, \ldots, x_d^{(0)}):

At iteration tt, update each component in sequence:

  1. Draw x1(t)π(x1x2(t1),x3(t1),,xd(t1))x_1^{(t)} \sim \pi(x_1 \mid x_2^{(t-1)}, x_3^{(t-1)}, \ldots, x_d^{(t-1)})
  2. Draw x2(t)π(x2x1(t),x3(t1),,xd(t1))x_2^{(t)} \sim \pi(x_2 \mid x_1^{(t)}, x_3^{(t-1)}, \ldots, x_d^{(t-1)})
  3. Continue through all variables...
  4. Draw xd(t)π(xdx1(t),x2(t),,xd1(t))x_d^{(t)} \sim \pi(x_d \mid x_1^{(t)}, x_2^{(t)}, \ldots, x_{d-1}^{(t)})

Note: each update uses the most recent values of all other variables.

Definition

Random Scan Gibbs Sampler

An alternative to systematic scan: at each iteration, choose an index i{1,,d}i \in \{1, \ldots, d\} uniformly at random (or with specified probabilities), and update only xix_i from π(xixi)\pi(x_i \mid x_{-i}).

Random scan has cleaner theoretical properties (it satisfies detailed balance directly), while systematic scan is more commonly used in practice because it updates all variables every iteration.

Gibbs as a Special Case of Metropolis-Hastings

The central theoretical result about Gibbs sampling is that it is a special case of MH where the acceptance probability is always 1.

Theorem

Gibbs Sampling Has Acceptance Probability 1

Statement

Consider a single Gibbs update for variable xix_i: propose xix_i' from π(xixi)\pi(x_i' \mid x_{-i}). This is equivalent to a Metropolis-Hastings step with proposal q(xixi,xi)=π(xixi)q(x_i' \mid x_i, x_{-i}) = \pi(x_i' \mid x_{-i}) and acceptance probability:

α=min ⁣(1,  π(xi,xi)π(xixi)π(xi,xi)π(xixi))=1\alpha = \min\!\left(1,\;\frac{\pi(x_i', x_{-i})\, \pi(x_i \mid x_{-i})}{\pi(x_i, x_{-i})\, \pi(x_i' \mid x_{-i})}\right) = 1

Every proposed move is accepted.

Intuition

The proposal distribution is exactly the full conditional. The optimal distribution for xix_i given everything else. There is no mismatch between proposal and target to correct for, so the acceptance ratio evaluates to 1. You are proposing from exactly the right distribution.

Proof Sketch

By definition of the full conditional: π(xi,xi)=π(xixi)π(xi)\pi(x_i, x_{-i}) = \pi(x_i \mid x_{-i})\,\pi(x_{-i}).

The acceptance ratio becomes:

α=π(xixi)π(xi)π(xixi)π(xixi)π(xi)π(xixi)=π(xi)π(xi)=1\alpha = \frac{\pi(x_i' \mid x_{-i})\,\pi(x_{-i}) \cdot \pi(x_i \mid x_{-i})}{\pi(x_i \mid x_{-i})\,\pi(x_{-i}) \cdot \pi(x_i' \mid x_{-i})} = \frac{\pi(x_{-i})}{\pi(x_{-i})} = 1

The π(xixi)\pi(x_i \mid x_{-i}) and π(xi)\pi(x_{-i}) terms cancel perfectly.

Why It Matters

A single Gibbs update is a valid MH step with acceptance probability 1, so it preserves π\pi as the stationary distribution and avoids proposal tuning and rejected samples. The full guarantees, however, depend on the scan type: random-scan Gibbs is reversible (satisfies detailed balance with respect to π\pi) and is most directly an MH kernel; systematic-scan Gibbs preserves π\pi as a composition of MH kernels but is generally only π\pi-invariant and not reversible. Convergence in either case still requires the standard Markov-chain assumptions on top of π\pi-invariance: π\pi-irreducibility, Harris recurrence on general state spaces, aperiodicity where relevant, and connected positive-density support of π\pi. Compatibility of the full conditionals (a single joint π\pi that realizes them all) is implicit and matters in practice.

Failure Mode

The acceptance rate of 1 does not mean Gibbs sampling is always efficient. The chain can still mix slowly if the variables are highly correlated. When x1x_1 and x2x_2 are strongly correlated, updating one at a time while fixing the other leads to slow, diffusive exploration. The chain takes small steps along the correlation ridge. This is the main weakness of component-wise Gibbs sampling.

Theorem

Convergence of the Gibbs Sampler

Statement

Under mild regularity conditions, the Gibbs sampler is ergodic: starting from any initial state x(0)x^{(0)}, the distribution of x(t)x^{(t)} converges to π\pi in total variation:

limtPt(x(0),)π()TV=0\lim_{t \to \infty} \|P^t(x^{(0)}, \cdot) - \pi(\cdot)\|_{\text{TV}} = 0

For random scan, the chain satisfies detailed balance with respect to π\pi. For systematic scan, the chain satisfies the weaker condition of π\pi-invariance (but not necessarily detailed balance).

Intuition

Each Gibbs update preserves π\pi as the stationary distribution (since it is a valid MH step). As long as the chain can reach any state, guaranteed when the support is connected and the full conditionals have positive density, the chain converges. Random scan gives reversibility automatically; systematic scan gives faster practical convergence but sacrifices reversibility.

Proof Sketch

For random scan: the transition kernel is a mixture of MH kernels, each satisfying detailed balance, so the mixture does too. Irreducibility follows from the positivity of full conditionals on the support.

For systematic scan: the transition kernel is a composition (not mixture) of MH kernels. Each kernel preserves π\pi, so the composition does too. Detailed balance may fail, but π\pi-invariance plus irreducibility and aperiodicity suffice for convergence.

Why It Matters

This theorem ensures that Gibbs sampling produces valid samples from the target distribution, justifying its use in Bayesian computation. The distinction between random and systematic scan matters for theoretical analysis (e.g., proving CLTs for MCMC estimators) but rarely affects practice.

Failure Mode

Convergence can be extremely slow when variables are strongly correlated or when the posterior has multiple well-separated modes. In the latter case, the chain may get trapped in one mode for a very long time. Blocking (grouping correlated variables and updating them jointly) can help with correlations; tempering or other advanced methods are needed for multimodality.

Blocking for Correlated Variables

When variables xix_i and xjx_j are highly correlated, updating them one at a time leads to slow mixing. Blocking (or collapsed Gibbs) groups correlated variables together and samples them jointly:

Instead of updating x1x2,x3x_1 \mid x_2, x_3 then x2x1,x3x_2 \mid x_1, x_3, update (x1,x2)x3(x_1, x_2) \mid x_3 as a block. This requires sampling from the joint conditional π(x1,x2x3)\pi(x_1, x_2 \mid x_3), which may itself require specialized methods, but the resulting chain mixes much faster when x1x_1 and x2x_2 are correlated.

The general principle: block together variables that are strongly correlated a posteriori.

Diagnostics Matter Even When Every Proposal Is Accepted

The most seductive Gibbs-sampling mistake is to see a 100% acceptance rate and infer that the sampler must be efficient. It is not. Gibbs can accept every single update and still produce a terrible effective sample size.

That happens when the conditional updates move only a tiny distance along a strong posterior ridge. The bivariate normal example above shows the core mechanism: when ρ|\rho| is close to 1, the conditional variance shrinks and each update becomes a small creep rather than a meaningful exploration move. The result is high , which makes the much smaller than the chain length suggests.

So the right workflow is:

  • derive the conditionals cleanly,
  • run multiple chains,
  • inspect burn-in and convergence diagnostics,
  • and if ESS remains poor, change the geometry of the update, not just the iteration count.

For hierarchical models this often means blocking, collapsing, or changing the parameterization itself.

Hierarchical Models: Centered, Non-Centered, and Conjugacy

Gibbs sampling has a special relationship to hierarchical models. In one direction, it is often the easiest exact MCMC method when the hierarchy is conditionally conjugate: normal-normal random effects, Gaussian state-space models, latent-class assignments, and older BUGS/JAGS workflows all lean on that structure.

But there is a catch. The parameterization that is best for Gibbs is not always the one that is best for HMC or NUTS.

  • A centered hierarchy often preserves clean conditional conjugacy, so the Gibbs updates stay closed-form.
  • A non-centered hierarchy often reduces posterior correlation between local effects and the global scale, which is better geometry for HMC and NUTS, but may destroy the simple conditional form that made Gibbs attractive in the first place.

So "use the non-centered form" is not a universal rule. It is a rule about a particular geometry-sampler pairing. For the full comparison, see Centered vs. Non-Centered Hierarchical Models.

Gibbs for Conjugate Models

The power of Gibbs sampling is most apparent in conjugate models, where each full conditional belongs to a standard distribution family.

Example

Normal-Normal model (known variance)

Model: xiμN(μ,σ2)x_i \mid \mu \sim \mathcal{N}(\mu, \sigma^2) for i=1,,ni=1,\ldots,n, with prior μN(μ0,τ2)\mu \sim \mathcal{N}(\mu_0, \tau^2).

The full conditional for μ\mu is:

μx1,,xnN ⁣(nxˉ/σ2+μ0/τ2n/σ2+1/τ2,  1n/σ2+1/τ2)\mu \mid x_1, \ldots, x_n \sim \mathcal{N}\!\left(\frac{n\bar{x}/\sigma^2 + \mu_0/\tau^2}{n/\sigma^2 + 1/\tau^2},\; \frac{1}{n/\sigma^2 + 1/\tau^2}\right)

This is a one-block model, so one Gibbs draw gives an exact posterior sample. The power of Gibbs becomes clear in hierarchical extensions with additional latent variables.

Example

Beta-Binomial model

Model: xθBinomial(n,θ)x \mid \theta \sim \text{Binomial}(n, \theta), with prior θBeta(α,β)\theta \sim \text{Beta}(\alpha, \beta).

The full conditional is:

θxBeta(α+x,β+nx)\theta \mid x \sim \text{Beta}(\alpha + x, \beta + n - x)

Again conjugacy gives a closed-form full conditional. In a hierarchical model with multiple groups sharing a common prior, the Gibbs sampler alternates between updating group-level parameters and the hyperparameters.

Example

Gibbs for a bivariate normal

Target: (x1,x2)N ⁣((00),(1ρρ1))(x_1, x_2) \sim \mathcal{N}\!\left(\begin{pmatrix}0\\0\end{pmatrix}, \begin{pmatrix}1 & \rho\\ \rho & 1\end{pmatrix}\right).

The full conditionals are:

x1x2N(ρx2,  1ρ2)x_1 \mid x_2 \sim \mathcal{N}(\rho\, x_2,\; 1 - \rho^2) x2x1N(ρx1,  1ρ2)x_2 \mid x_1 \sim \mathcal{N}(\rho\, x_1,\; 1 - \rho^2)

When ρ|\rho| is close to 1, the chain moves in small steps along the diagonal, illustrating the slow-mixing problem for correlated variables. The autocorrelation of the chain is approximately ρ2\rho^2 per iteration.

Common Confusions

Watch Out

Gibbs requires KNOWN full conditionals

Gibbs sampling requires that you can derive and sample from each full conditional distribution. If the full conditional does not have a recognizable closed form, you cannot use standard Gibbs for that variable. The solution is MH within Gibbs: use a Metropolis-Hastings step (with some proposal distribution) to update that variable, while using exact Gibbs updates for the variables where full conditionals are known. This hybrid approach is extremely common in practice.

Watch Out

Gibbs is not always better than MH

The 100% acceptance rate of Gibbs does not mean it is always more efficient than MH. In high-correlation scenarios, a well-tuned MH proposal that accounts for correlations (or Hamiltonian Monte Carlo, which uses gradient information) can vastly outperform component-wise Gibbs. Gibbs excels when the model has conditionally conjugate structure and moderate correlations.

Watch Out

Acceptance probability 1 does not mean i.i.d. draws

Every Gibbs proposal is accepted because it is drawn from the correct full conditional. That says nothing about how correlated successive full states are. A Gibbs chain can have severe autocorrelation and a tiny effective sample size even though the acceptance rate is exactly 1.

Watch Out

Systematic scan Gibbs does not satisfy detailed balance

A common misconception is that Gibbs sampling always satisfies detailed balance. In fact, only random scan Gibbs satisfies detailed balance. Systematic scan Gibbs, where you cycle through variables in a fixed order, satisfies the weaker property of π\pi-invariance: πK=π\pi K = \pi where KK is the transition kernel. This is sufficient for convergence but means some theoretical tools (e.g., certain CLT results) require more care.

Watch Out

Gibbs updates use the MOST RECENT values

In systematic scan, when updating xix_i, you condition on the values of x1,,xi1x_1, \ldots, x_{i-1} that have already been updated in the current iteration, and the values of xi+1,,xdx_{i+1}, \ldots, x_d from the previous iteration. This is not the same as conditioning on all values from the previous iteration (which would be a different, valid but less efficient, algorithm sometimes called "synchronous" or "parallel" Gibbs).

Summary

  • Gibbs sampling updates each variable from its full conditional, cycling through all variables
  • It is a special case of MH with acceptance probability 1
  • Requires known, tractable full conditional distributions
  • Random scan satisfies detailed balance; systematic scan only satisfies π\pi-invariance
  • Excels in conditionally conjugate models and older hierarchical Bayesian workflows
  • Blocking, collapsing, and parameterization matter because 100% acceptance does not guarantee good ESS
  • When full conditionals are not tractable, use MH within Gibbs or move to a geometry-aware method like HMC

Exercises

ExerciseCore

Problem

Derive the Gibbs sampler for the bivariate normal distribution:

(x1,x2)N ⁣((μ1μ2),(σ12ρσ1σ2ρσ1σ2σ22))(x_1, x_2) \sim \mathcal{N}\!\left(\begin{pmatrix}\mu_1\\\mu_2\end{pmatrix}, \begin{pmatrix}\sigma_1^2 & \rho\sigma_1\sigma_2\\ \rho\sigma_1\sigma_2 & \sigma_2^2\end{pmatrix}\right)

Find both full conditional distributions π(x1x2)\pi(x_1 \mid x_2) and π(x2x1)\pi(x_2 \mid x_1).

ExerciseCore

Problem

Prove that the Gibbs update for variable xix_i, viewed as an MH step with proposal q(xixi,xi)=π(xixi)q(x_i' \mid x_i, x_{-i}) = \pi(x_i' \mid x_{-i}), has acceptance probability exactly 1.

ExerciseAdvanced

Problem

Consider a mixture model: xizi,μ1,μ2N(μzi,1)x_i \mid z_i, \mu_1, \mu_2 \sim \mathcal{N}(\mu_{z_i}, 1) with zi{1,2}z_i \in \{1, 2\}, P(zi=1)=0.5P(z_i = 1) = 0.5, and priors μkN(0,10)\mu_k \sim \mathcal{N}(0, 10). Derive the full conditional distributions for the Gibbs sampler: π(zirest)\pi(z_i \mid \text{rest}) and π(μkrest)\pi(\mu_k \mid \text{rest}).

References

Canonical:

  • Geman & Geman (1984), "Stochastic Relaxation, Gibbs Distributions, and the Bayesian Restoration of Images"
  • Gelfand & Smith (1990), "Sampling-Based Approaches to Calculating Marginal Densities"
  • Casella & George (1992), "Explaining the Gibbs Sampler," The American Statistician 46(3), 167-174. Still one of the cleanest expositions of why the algorithm works.

Current:

  • Robert & Casella, Monte Carlo Statistical Methods (2004), Chapter 10
  • Gelman, Carlin, Stern, Dunson, Vehtari, Rubin, Bayesian Data Analysis (3rd ed., 2013), Chapter 11
  • Brooks et al., Handbook of MCMC (2011), Chapters 1-5
  • Betancourt & Girolami, "Hamiltonian Monte Carlo for Hierarchical Models" (2013, arXiv:1312.0906). Useful contrast for when conditional conjugacy stops being the dominant consideration.

Next Topics

The natural next steps from Gibbs sampling:

Last reviewed: May 6, 2026

Canonical graph

Required before and derived from this topic

These links come from prerequisite edges in the curriculum graph. Editorial suggestions are shown here only when the target page also cites this page as a prerequisite.

Required prerequisites

2

Derived topics

6

+1 more on the derived-topics page.