- Introduced by
- Gelfand & Smith (1990), building on Geman & Geman (1984)
- Year
- 1990
- Type
- Bayesian computational sampler
- Family
- Bayesian / computational
What does it do?
Hierarchical Markov chain Monte Carlo runs MCMC sampling on a hierarchical Bayesian model, drawing jointly from the posterior of the unit-level parameters and the hyperparameters that govern them.
Because all levels are sampled together, uncertainty at the population level carries into the estimates for individual groups. The sampler is usually a Gibbs, Metropolis-Hastings, or Hamiltonian Monte Carlo scheme, or a combination of these.
The intuition behind the method
Imagine a study with students nested in classrooms nested in schools. Each school has its own effect, but those effects are themselves drawn from a shared distribution governed by hyperparameters. Hierarchical MCMC samples all of these unknowns together: it updates the lower-level parameters given the hyperparameters, then updates the hyperparameters given the lower-level values, and repeats. After enough iterations the chain's draws approximate the joint posterior, so uncertainty at each level is carried into the others.
History
Gelfand and Smith (1990) popularised Gibbs sampling for Bayesian posterior computation in general, which made high-dimensional hierarchical models practical to fit; hierarchical applications developed from this work. Their approach builds on the Gibbs sampler introduced by Geman and Geman (1984) for image restoration. Gelman and colleagues' Bayesian Data Analysis, first published in 1995 with a third edition in 2013, is a standard applied reference for the method. Gradient-based samplers such as HMC and the No-U-Turn Sampler were adopted for hierarchical models later and are now implemented in Stan.
When to use it
Choose hierarchical MCMC when observations fall into groups (students in schools, patients in hospitals, repeated measurements within individuals) and the groups are expected to differ while still resembling one another, so partial pooling is appropriate. The approach assumes that units within a group are exchangeable given the hyperparameters and that observations are conditionally independent given the unit-level parameters. If groups differ systematically, covariates or a different grouping structure should be added. The method is most useful when group sizes vary widely, because small groups borrow strength from the population-level distribution rather than producing unstable estimates on their own. Avoid it when the observations are genuinely independent with no meaningful grouping, when the computational cost is prohibitive and an approximation would be needed, or when a simpler non-hierarchical model fits as well under cross-validation.
Limitations and considerations
- Computationally expensive for large datasets or many groups, particularly when gradients must be evaluated repeatedly for HMC.
- Requires careful prior specification for hyperparameters; poorly chosen hyperpriors can lead to funnel-shaped posteriors that are difficult to sample.
- Convergence diagnosis is more complex than for flat models: each parameter must be checked, and hierarchical funnels may cause slow mixing.
- Model specification demands understanding of the grouping structure; mis-specifying levels leads to biased shrinkage.
Source conditions do not replace independent scientific review or validation by an analysis engine.
Strengths
- Handles nested and grouped data structures naturally, respecting the data-generating process.
- Partial pooling shrinks small-group estimates toward the population mean, reducing overfitting without discarding group variation.
- Propagates uncertainty across all levels of the hierarchy into predictions and credible intervals.
- Flexible: any combination of Gibbs, Metropolis-Hastings, or HMC steps can target different parameter blocks.
- Probabilistic programming languages such as Stan, PyMC and JAGS let the user write the hierarchy directly, rather than coding each update step by hand.
Applications
- Educational research: estimating school and classroom effects on student outcomes while borrowing strength across small schools.
- Clinical trials: modelling patient responses within treatment centres, accounting for centre-level variation.
- Ecology: species abundance models with observations nested within sites and sites within regions.
- Sports analytics: team and player performance models that share a population distribution across groups.
- Longitudinal studies: repeated-measures data where individual trajectories vary around a population mean curve.
How it works
How it works
A hierarchical model is specified: observations depend on unit-level parameters theta, which are themselves drawn from a distribution governed by hyperparameters phi. The joint posterior p(theta, phi | y) is proportional to the likelihood times the prior on theta given phi times the hyperprior on phi. This three-tier structure is the defining feature.
Formula
p(\theta, \phi \mid y) \propto p(y \mid \theta)\, p(\theta \mid \phi)\, p(\phi)
The key to efficient MCMC in this setting is deriving or approximating the full conditional distributions for each block of parameters, meaning the distribution of one set of unknowns given all others. When conjugate priors are used, full conditionals are available in closed form, enabling pure Gibbs sampling. For blocks without a closed-form full conditional, Metropolis-Hastings steps can be used, which require only the unnormalised log density. HMC or NUTS can also update such blocks, but they additionally need the gradient of the log density. Both are applied as samplers to a block, so they do not require its full conditional to be available in closed form.
The sampler iterates: (1) draw each unit-level parameter theta_i from its full conditional given phi and the data; (2) draw the hyperparameters phi from their full conditional given all theta_i. This alternation is Gibbs sampling at the hierarchical level. Non-conjugate models replace or supplement Gibbs steps with Metropolis-Hastings proposals or NUTS (No-U-Turn Sampler) for gradient-based exploration.
Multiple parallel chains are run from dispersed starting points. The Gelman-Rubin R-hat statistic compares between-chain to within-chain variance; values below 1.01 are commonly taken to indicate convergence. Effective sample size (ESS) for each parameter, including the hyperparameters, confirms that the chain has explored the posterior adequately, accounting for autocorrelation. For HMC or NUTS runs, divergent transitions should also be checked, since they indicate that the sampler is not exploring the posterior reliably.
Formula
\hat{R} = \sqrt{\frac{\hat{V}}{W}}Once convergence is confirmed, posterior draws are summarised: posterior means or medians as point estimates, 95% credible intervals as uncertainty bounds, and shrinkage patterns across units. Posterior predictive checks compare replicated data from the fitted model with the observed data, showing whether the hierarchical structure reproduces the main features of the observations.
Common pitfalls
- Using improper or very flat hyperpriors on variance components, which can let the sampler wander into extreme values of the group-level variance and mix poorly.
- Declaring convergence based on only one or two chains instead of running at least four from dispersed starting points.
- Relying on one parameterisation without checking for funnel-shaped posteriors: when a group-level variance is small, the centred form can mix poorly, and the non-centred form (theta_i = mu + sigma * z_i) often helps, but neither form is always better, so both are worth comparing.
- Forgetting to perform posterior predictive checks, so model misfit at any level goes undetected.
- Reading a 95% credible interval from the hierarchical posterior as if it were a frequentist confidence interval.
Frequently asked questions
What is partial pooling and why does it matter?
Partial pooling means each group's estimate is pulled toward the overall population mean, with the degree of shrinkage determined by how much data the group has and how variable the groups are. Groups with few observations are shrunk more; groups with many observations stay closer to their own data. This avoids the extremes of complete pooling (one estimate for all groups) and no pooling (independent estimates that overfit small groups).
What is the funnel problem in hierarchical MCMC?
When a group-level variance hyperparameter (sigma) approaches zero, the unit-level parameters theta_i are forced to cluster tightly, creating a narrow funnel shape in the joint posterior, and standard samplers can get stuck in its neck. Writing theta_i = mu + sigma * z_i and sampling the standardised z_i instead (the non-centred parameterisation) decouples the scales and often improves mixing in this situation. It is not uniformly better: when each group has a lot of data, the centred form can mix better, so both parameterisations are worth comparing using the convergence diagnostics.
How many levels can a hierarchical model have?
In principle, as many as the data support (students, classrooms, schools, districts). In practice, each additional level adds computational cost and requires its own prior specification. Models with many levels become hard to identify unless every level has substantial data.
How does this differ from a standard (flat) MCMC?
In a flat MCMC model all parameters are on the same level and are sampled from their joint posterior directly. In hierarchical MCMC the parameters have their own distribution governed by hyperparameters, so the sampler must explore both levels simultaneously. The benefit is automatic regularisation and information sharing across groups; the cost is greater complexity and slower mixing.
Which software implements hierarchical MCMC?
Stan (via RStan, PyStan, or CmdStan) uses HMC/NUTS and is widely used for complex hierarchical models. BUGS and JAGS rely mainly on Gibbs and Metropolis-type updates and are common in the social and health sciences. PyMC (Python) and Turing.jl (Julia) are other options.
Related methods
Sources
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-1439840955Robert, C. P. & Casella, G. (2004). Monte Carlo Statistical Methods (2nd ed.). Springer.
ISBN: 978-0387212395