Multilevel Gibbs Sampling

Statistics
Introduced by
Geman & Geman (1984); applied to multilevel models by Gelfand & Smith (1990)
Year
1990
Type
MCMC sampling algorithm
Family
Bayesian / computational

What does it do?

Multilevel Gibbs sampling applies the Gibbs MCMC algorithm to hierarchical (multilevel) Bayesian models, cycling through the conditional distributions of group-level parameters and population-level hyperparameters in turn.

It exploits the conditional independence structure of the hierarchy, so each block can be drawn directly from its full conditional when that conditional has a known form.

The intuition behind the method

In a multilevel model, observations are nested inside groups, such as students within schools or patients within clinics. Gibbs sampling breaks the hard joint-posterior problem into a sequence of simpler block-by-block draws. It first updates each group's parameters given that group's data and the current hyperparameters, then updates the hyperparameters given all the group-level draws. When every block can be drawn exactly from its full conditional, the chain's stationary distribution is the joint posterior. In practice, finite run length, burn-in and slow mixing mean that the resulting summaries are approximations, and they should be checked with convergence diagnostics before they are reported.

History

Gibbs sampling was introduced by Geman and Geman (1984) for image analysis. Gelfand and Smith (1990) are generally credited with bringing it into mainstream Bayesian computation. Gelman and Hill (2007) cover multilevel regression models for applied data analysis; their emphasis is on fitting these models with available software packages rather than on Gibbs sampling specifically. Gelman et al. (2013) provides a general treatment of Bayesian hierarchical modelling.

When to use it

Choose multilevel Gibbs sampling when observations are clustered or nested and the full conditional of every block has a known form that can be sampled directly. In practice this means conjugate or conditionally conjugate priors, such as Normal-Normal hierarchies for continuous outcomes or Poisson-Gamma hierarchies for counts. Do not use it when the full conditionals are non-conjugate. Binary outcomes modelled with a logit link are a common example, because their conditionals have no standard form; Metropolis-within-Gibbs or Hamiltonian Monte Carlo is the usual alternative in that case. It is also a weak choice for very high-dimensional models with poor chain mixing, because each sweep becomes costly and successive draws become strongly autocorrelated.

Limitations and considerations

  • Requires analytically tractable full conditionals; non-conjugate settings demand Metropolis-within-Gibbs or HMC steps.
  • Can mix slowly when group-level parameters and hyperparameters are highly correlated, leading to long autocorrelation times.
  • Computational cost scales with the number of groups J and the within-group sample sizes, making very large hierarchies expensive.
  • Assessing convergence across many group-level chains requires care; standard univariate R-hat may miss multivariate non-convergence.

Source conditions do not replace independent scientific review or validation by an analysis engine.

Strengths

  • Exploits the conditional independence of multilevel structure, making each sweep tractable even for large J.
  • With conjugate priors, all draws are exact (no accept/reject step), so each iteration is computationally cheap.
  • Automatically implements partial pooling: groups with sparse data borrow strength from the population-level hyperparameters.
  • Provides full posterior distributions at both the group and population level, enabling genuine uncertainty quantification.
  • Conceptually transparent — each block's update is interpretable as a Bayesian updating step in a simpler sub-model.

Applications

  • Educational research: school- and student-level effects on test scores, with school effects drawn from a Normal hyperprior.
  • Clinical trials: patient outcomes nested within sites, with site-specific random effects shrunk toward a common mean.
  • Small-area estimation: group means for geographic units with sparse samples, borrowing strength through the hyperprior.
  • Longitudinal data: repeated measurements nested within individuals, with individual-level effects drawn from a population distribution.

How it works

How it works

1

Specify the three-level data-generating process: observations are drawn from a group-specific likelihood parameterised by theta_j; group parameters theta_j share a common hyperprior governed by phi; phi itself has a prior. This conditional factorisation is what makes block-wise Gibbs feasible.

Formula
y_{ij} \sim p(y_{ij} \mid \theta_j),\quad \theta_j \sim p(\theta_j \mid \phi),\quad \phi \sim p(\phi)
2

Derive the full conditional for each block — the distribution of one set of unknowns given everything else. For conjugate priors (Normal-Normal, Poisson-Gamma, etc.) these conditionals are known distributions from which direct sampling is possible without rejection steps.

Formula
p(\theta_j \mid \mathbf{y}_j, \phi) \propto p(\mathbf{y}_j \mid \theta_j)\, p(\theta_j \mid \phi)
3

Perform one Gibbs sweep: sample each group's theta_j from its full conditional p(theta_j | data_j, phi), cycling over all J groups. This exploits the independence of groups given phi, so group updates can even be parallelised.

4

Sample the hyperparameters phi from their full conditional, which pools information from all J group estimates. This is the step that produces partial pooling: groups with little data are shrunk toward the grand mean encoded in phi.

Formula
p(\phi \mid \{\theta_j\}) \propto p(\{\theta_j\} \mid \phi)\, p(\phi)
5

Run multiple chains and monitor convergence via R-hat diagnostics and effective sample size. After a burn-in period, retain posterior draws to compute group-level and population-level means, variances, and credible intervals.

Common pitfalls

  • Treating a prior as conjugate when it is only approximately so; the closed-form draws are then not exact draws from the target posterior.
  • Stopping after too short a burn-in, especially when chains start far from the bulk of the posterior, so early draws contaminate the group and hyperparameter summaries.
  • Reporting effective sample size as if the draws were independent, without checking autocorrelation within each chain.
  • Leaving strongly correlated group effects and hyperparameters in a centred form, which slows mixing between levels and can make a single chain look settled while the posterior is poorly explored.
  • Reading a group estimate as if it were the raw group mean. Under partial pooling, groups with little data are shrunk toward the population mean, so their posterior means lie between their raw means and the grand mean.
  • Interpreting the between-group variance or other hyperparameters as well determined when the number of groups is small. With few groups, the hyperparameter posterior can be dominated by its prior, so results should be checked for sensitivity to the hyperprior.

Frequently asked questions

What distinguishes multilevel Gibbs sampling from plain Gibbs sampling?

Plain Gibbs sampling cycles over individual parameters. Multilevel Gibbs sampling exploits the hierarchical structure to draw entire blocks (all group-level parameters and then the hyperparameters) sequentially. This block structure keeps each sweep tractable and builds the partial-pooling property into every sweep.

When should I use HMC instead of Gibbs for a multilevel model?

When the priors are not conjugate, when the full conditionals have no standard form, or when parameters are strongly correlated across levels (a funnel-shaped posterior), Hamiltonian Monte Carlo, as implemented in Stan, is usually the better choice. Gibbs is most direct when the full conditionals are known distributions, as in Normal-Normal or conjugate Poisson-Gamma hierarchies.

How do I diagnose poor mixing in a multilevel Gibbs chain?

Compare R-hat across chains for all parameters (values well above 1 indicate non-convergence), inspect trace plots, and compute the effective sample size. Slow mixing between group parameters and hyperparameters is a common symptom. A reparameterisation such as the non-centred form is a frequently used remedy, but whether it helps depends on the model.

Does multilevel Gibbs sampling require balanced group sizes?

No. Groups can have very different sample sizes. Groups with few observations are shrunk more strongly toward the hyperprior mean, which is the partial-pooling property. Unbalanced designs are one of the main reasons to use hierarchical models.

What software implements multilevel Gibbs sampling?

BUGS and JAGS implement Gibbs and Metropolis-within-Gibbs sampling for hierarchical models written in a symbolic model language, and the sampler used for each parameter depends on the model and the software. Stan, which the brms R package calls, uses Hamiltonian Monte Carlo by default. Check each package's documentation to confirm which samplers it uses for a given model class.

Related methods

Sources

  1. Gelman, A. & Hill, J. (2007). Data Analysis Using Regression and Multilevel/Hierarchical Models. Cambridge University Press.

    ISBN: 978-0521686891
  2. Gelman, A., Carlin, J. B., Stern, H. S., Dunson, D. B., Vehtari, A. & Rubin, D. B. (2013). Bayesian Data Analysis (3rd ed.). CRC Press.

    ISBN: 978-1439840955