22  MCMC in Depth

22.1 Learning objectives

By the end of this chapter you should be able to:

  • Describe Hamiltonian Monte Carlo (HMC) as a Metropolis proposal that uses gradient information to construct long, low-rejection trajectories.
  • Identify the No-U-Turn Sampler (NUTS) as the adaptive variant of HMC that automates trajectory length, and explain why it is the default for modern probabilistic programming systems.
  • Compute and interpret modern MCMC convergence diagnostics: \(\hat R\) (rank-normalized), bulk and tail effective sample size, divergent transitions, E-BFMI, and the energy diagnostic.
  • Recognize the reparameterization patterns that fix hierarchical-prior pathologies: the non-centered parameterization, the change-of-variable Jacobian, and the standardization of input variables.
  • Run multiple chains in parallel correctly, with reproducible RNG and proper diagnostics across chains.
  • Diagnose failures: chains stuck on different modes, funnel pathologies, ill-conditioned posteriors, and what each looks like in bayesplot::mcmc_pairs().

22.2 Orientation

The preceding chapter on Bayesian computation (Chapter 21) introduced MCMC at the level of the Metropolis-Hastings algorithm and the Gibbs sampler. That treatment shows the mathematical structure, but the samplers it produces are slow on realistic posteriors. Modern applied Bayesian work instead uses Hamiltonian Monte Carlo (HMC), or its adaptive descendant the No-U-Turn Sampler (NUTS). These are available through Stan, PyMC, NumPyro, or the R interfaces rstan, cmdstanr, rstanarm, and brms.

In this chapter we shall treat HMC and NUTS in some depth. We give enough of the mechanics to explain why the methods work and what their diagnostics mean. We also cover the practical bookkeeping that distinguishes a converged chain from one that only looks converged. The next chapter (Chapter 23) covers the application layer (Stan, posterior workflows, LOO/WAIC); the present chapter covers the algorithmic and diagnostic layer.

We begin with the judgments that fall to the statistician. We then develop HMC and NUTS, the modern convergence diagnostics, and the reparameterizations that repair difficult posterior geometry. The chapter closes with parallel chains, a worked example on the Eight Schools data, and a discussion of collaborating with an LLM on MCMC.

22.3 The statistician’s contribution

Modern MCMC tools handle the algorithmic mechanics automatically. The judgments at the center of this chapter concern reading diagnostics correctly and recognizing pathological posterior shapes from their diagnostic signatures. They also concern the parameterization choices that determine whether the sampler can converge at all.

\(\hat R\) near 1 is necessary, not sufficient. A chain with \(\hat R = 1.001\) may still have a low effective sample size, divergences, or trapped behavior on a multimodal posterior. Verification therefore requires the full diagnostic suite: divergences, E-BFMI, ESS bulk and tail, trace plots, and pair plots.

Reparameterization is a modeling choice. A hierarchical model with a centered parameterization (\(\mu_j \sim N(\mu, \sigma)\)) has a different posterior geometry from the same model with a non-centered parameterization (\(\mu_j = \mu + \sigma z_j, z_j \sim N(0, 1)\)). The mathematics is the same; the sampler’s behavior is not. Matching the parameterization to the information level of the data is the difference between a fast, clean fit and an MCMC chain that looks converged. That is, the second chain appears converged but never explores the funnel of the variance parameter.

Divergences are not, in general, a tuning issue. When NUTS reports divergent transitions, the standard advice is ‘increase adapt_delta’. That advice addresses the symptom. The underlying cause is usually geometric: the posterior has a region the sampler cannot integrate accurately. Increasing adapt_delta may hide the divergences, but it does not change the geometry, and the chain still mis-samples the difficult region. The cure is reparameterization, prior tightening, or model simplification.

Multiple chains, multiple starting points. Convergence of one chain to a fixed point does not imply convergence to the posterior. Our protection against multimodality and non-stationarity is to run multiple chains from dispersed starting values, with \(\hat R\) across chains as the consistency check.

22.4 Hamiltonian Monte Carlo

HMC is a Metropolis-Hastings sampler with a particular kind of proposal. It augments the parameter \(\boldsymbol\theta\) with an auxiliary momentum variable \(\mathbf{p}\). It then uses Hamiltonian dynamics on the joint \((\boldsymbol\theta, \mathbf{p})\) space to propose long, low-rejection moves.

In symbols, the Hamiltonian for a target \(p(\boldsymbol\theta)\) is

\[ H(\boldsymbol\theta, \mathbf{p}) = -\log p(\boldsymbol\theta) + \frac{1}{2} \mathbf{p}^T M^{-1} \mathbf{p} \]

where \(M\) is a (positive-definite) mass matrix, often chosen to roughly equal the posterior covariance. The two halves of \(H\) are the potential energy (the negative log target) and the kinetic energy (a quadratic in \(\mathbf{p}\)).

Hamilton’s equations describe how \((\boldsymbol\theta, \mathbf{p})\) evolves under \(H\):

\[ \frac{d\boldsymbol\theta}{dt} = \frac{\partial H}{\partial \mathbf{p}} = M^{-1} \mathbf{p}, \quad \frac{d\mathbf{p}}{dt} = -\frac{\partial H}{\partial \boldsymbol\theta} = \nabla \log p(\boldsymbol\theta). \]

The continuous-time evolution preserves \(H\) exactly. In practice we discretize it with the leapfrog integrator:

\[ \begin{aligned} \mathbf{p}^{(t + \epsilon/2)} &= \mathbf{p}^{(t)} + \frac{\epsilon}{2} \nabla \log p(\boldsymbol\theta^{(t)}) \\ \boldsymbol\theta^{(t + \epsilon)} &= \boldsymbol\theta^{(t)} + \epsilon M^{-1} \mathbf{p}^{(t + \epsilon/2)} \\ \mathbf{p}^{(t + \epsilon)} &= \mathbf{p}^{(t + \epsilon/2)} + \frac{\epsilon}{2} \nabla \log p(\boldsymbol\theta^{(t + \epsilon)}). \end{aligned} \]

We take \(L\) leapfrog steps and propose \((\boldsymbol\theta^{(t+L\epsilon)}, -\mathbf{p}^{(t+L\epsilon)})\) as the Metropolis candidate. The candidate is accepted with probability \(\min(1, \exp(H_{\text{old}} - H_{\text{new}}))\). The discretization error in the leapfrog integrator is small but nonzero, so the acceptance probability is slightly below 1. The Metropolis accept/reject step corrects for that error, and the sampler therefore still targets the exact posterior.

The two tuning knobs are:

  • Step size \(\epsilon\). A smaller step size produces more accurate trajectories and higher acceptance, but it needs more steps to cover the same distance.
  • Trajectory length \(L\). Longer trajectories produce proposals farther from the current state, which reduces autocorrelation, but they cost more gradient evaluations.

Tuning these by hand is delicate. NUTS automates the tuning, as we now describe.

22.4.1 The No-U-Turn Sampler (NUTS)

NUTS (Hoffman and Gelman, 2014) extends HMC with two adaptations:

  1. Automatic trajectory length. NUTS extends the trajectory in random directions until it begins to double back on itself (forms a U-turn) or reaches a maximum tree depth. The maximum is typically 10, giving up to \(2^{10} - 1 = 1023\) leapfrog steps. The trajectory automatically stops just before the proposal would loop back.
  2. Automatic step-size adaptation. During warmup, NUTS adjusts \(\epsilon\) to achieve a target acceptance rate (default 0.8 in Stan).

The result is a sampler with no user-tunable parameters except the target acceptance rate (adapt_delta in Stan, defaulting to 0.8). For most well-formulated posteriors, NUTS produces hundreds to thousands of effective samples per second. That is hundreds of times faster than random-walk Metropolis.

NUTS is the default in Stan, PyMC, and NumPyro, and in the R wrappers rstanarm, brms, rstan, and cmdstanr. For routine Bayesian analyses we do not implement NUTS ourselves; we use one of these.

22.5 Modern convergence diagnostics

22.5.1 \(\hat R\) (rank-normalized, split, with folding)

The classical Gelman-Rubin diagnostic compares within-chain to between-chain variance. The 2021 update by Vehtari et al. introduces three improvements:

  • Rank normalization. Replace samples by their ranks, then transform via \(\Phi^{-1}\) to a normal scale. This makes the diagnostic robust to heavy tails and asymmetric posteriors.
  • Split. Each chain is split in two halves, and \(\hat R\) is computed across the resulting \(2C\) chains rather than the original \(C\). This detects within-chain non-stationarity.
  • Folding. Compute \(\hat R\) on \(|x - \text{median}(x)|\) in addition to \(x\) itself. This detects scale (variance) non-stationarity.

The threshold is \(\hat R < 1.01\) for both the regular and folded versions. The convention \(\hat R < 1.1\) from the older literature is too lax for modern applied Bayesian work, and we recommend 1.01.

library(posterior)
draws <- as_draws_array(stan_fit)
summarise_draws(draws, default_convergence_measures())
#> # A tibble: 5 × 4
#>   variable    rhat ess_bulk ess_tail
#>   <chr>      <dbl>    <dbl>    <dbl>
#> 1 b_intercept 1.00     2840     2120
#> 2 b_x         1.00     2950     2470
#> 3 sigma       1.00     1980     2350
#> 4 ...

22.5.2 Effective sample size

ESS is the number of independent samples that would give the same standard error as the autocorrelated MCMC samples. ESS bulk measures the effective sample size for typical-value summaries (mean, median). ESS tail measures it for tail summaries (95% credible intervals). The convention is that at least 400 in each is required for reliable summaries. Below that, the bounds of posterior intervals are noisy.

For most posteriors, ESS bulk and tail are similar. When they differ substantially, the tail of the posterior is slow-mixing, and the reporting of tail quantiles is unreliable.

22.5.3 Divergent transitions

A divergence in NUTS occurs when the leapfrog integrator fails to track the true Hamiltonian dynamics. Typically this happens because the integrator step size is too large for a high-curvature region. Divergences indicate that NUTS is not exploring some part of the posterior accurately, and the resulting samples are biased away from that region.

By default Stan reports the divergence count as part of its warning system. Any positive count is concerning; double digits are a serious problem.

The textbook fix is to increase adapt_delta from 0.8 to 0.95 or 0.99. This reduces the step size, which often fixes the immediate problem. Unfortunately, it treats only the symptom. The underlying cause is usually a posterior geometry that requires a different parameterization, a topic we take up below.

22.5.4 E-BFMI

The Energy-based Bayesian Fraction of Missing Information diagnoses whether the chain explores the energy distribution efficiently. Values below 0.3, the nominal threshold in the Stan documentation, indicate problems. We note that rstan::check_energy() warns only below the laxer cutoff of 0.2, so a chain with E-BFMI between the two values passes that check silently. The canonical example is the funnel pathology, in which the chain repeatedly fails to sample the bottom of the funnel.

# Stan reports E-BFMI per chain
rstan::check_energy(fit)        # warning if any chain has E-BFMI < 0.2

22.5.5 Pair plots

For diagnosing geometric pathologies, the most useful visual is a pair plot colored by divergence:

library(bayesplot)
mcmc_pairs(stan_fit, pars = c("mu", "sigma", "tau"),
           np = nuts_params(stan_fit))

Divergent transitions cluster in regions of the posterior that the sampler cannot integrate accurately. In hierarchical models they typically cluster in the bottom of the funnel (small \(\tau\), small group means).

22.5.6 Trace plots

The classical trace plot shows the sampled value of each parameter against iteration. A converged chain looks like white noise around a stable mean. Chains that drift, chains that get stuck, and chains that explore different regions on different runs are all visually obvious in a trace plot.

mcmc_trace(stan_fit, pars = c("mu", "sigma"))

22.6 Reparameterization: centered vs. non-centered

The single most common reason a hierarchical Bayesian model produces divergences is a mismatch between the parameterization and the information level of the data. To illustrate, consider the canonical example: a hierarchical normal model with group means \(\mu_j \sim N(\mu, \tau)\) and observation-level likelihood \(y_{ij} \sim N(\mu_j, \sigma)\).

Centered parameterization (CP):

parameters {
  real mu;
  real<lower=0> tau;
  real<lower=0> sigma;
  vector[J] mu_j;
}
model {
  mu_j ~ normal(mu, tau);
  for (j in 1:J)
    y[j] ~ normal(mu_j[j], sigma);
}

Non-centered parameterization (NCP):

parameters {
  real mu;
  real<lower=0> tau;
  real<lower=0> sigma;
  vector[J] z_j;       // standard normal
}
transformed parameters {
  vector[J] mu_j = mu + tau * z_j;
}
model {
  z_j ~ normal(0, 1);
  for (j in 1:J)
    y[j] ~ normal(mu_j[j], sigma);
}

The two programs parameterize the same model, and the posterior on \((\mu, \tau, \mu_j)\) is identical in distribution. However, the posterior geometry differs:

  • Under CP, the posterior on \((\tau, \mu_j)\) has a funnel shape. When \(\tau\) is small, \(\mu_j\) is squeezed close to \(\mu\), which produces high curvature in the joint posterior.
  • Under NCP, the posterior on \((\tau, z_j)\) is roughly spherical: \(z_j\) stays around its prior \(N(0, 1)\) regardless of \(\tau\).

CP works when the data are informative about each \(\mu_j\) (many observations per group). The posterior on \(\mu_j\) is then dominated by the data rather than the prior, and the funnel is largely absent. NCP works when the data are weakly informative about each \(\mu_j\) (few observations per group). In that case the posterior is dominated by the prior, and the funnel is present in CP but absent in NCP.

The general rule is to start with NCP for any hierarchical-prior parameter that is not strongly identified by the data. We switch to CP only when both work and CP gives more efficient sampling.

Question. Your hierarchical model has \(J = 50\) groups, with observations per group ranging from 2 to 10. Stan reports 47 divergences out of 4000 post-warmup samples. You increase adapt_delta to 0.99 and the divergences drop to 8. Should you publish?

Answer.

No. The divergences dropped because the smaller step size made the sampler more careful, but the underlying geometry is still bad. The few-observations-per-group setting is exactly the case in which CP fails. The data do not pin down each \(\mu_j\), so the posterior has a funnel shape that NCP would handle. The fix is to reparameterize to NCP: \(\mu_j = \mu + \tau z_j\) with \(z_j \sim N(0, 1)\). This typically eliminates the divergences and produces a faster sampler. Publishing with adapt_delta = 0.99 and 8 remaining divergences means that the bottom of the funnel was never properly sampled. As a result, the variance estimate \(\tau\) is biased toward larger values, and the group-level estimates are biased toward the global mean. We would refit with NCP, verify zero divergences, and only then report.

22.7 Other reparameterizations

Beyond the centered and non-centered forms, several other reparameterizations recur.

Standardize predictors. A predictor measured in millions has a coefficient that takes values in \(10^{-6}\), while one measured in tenths has a coefficient in 1. Mixing both in one regression produces a posterior on \(\boldsymbol\beta\) with very different scales per coefficient. The sampler’s mass matrix has trouble adapting to such a posterior. Standardizing every continuous predictor to mean 0, SD 1 puts the coefficients on comparable scales and aids sampling.

Log-transform positive variables. A posterior on \(\sigma > 0\) is more naturally sampled in the unconstrained \(\log \sigma\) space. Stan automatically applies the transform for <lower=0> declarations and includes the Jacobian, and PyMC and similar systems do the same. Hand-rolled MCMC must include the change-of-variable Jacobian explicitly.

Logit-transform probabilities. Probabilities live in \((0, 1)\), and sampling on the logit scale removes the boundary effects.

Cholesky parameterization of covariance matrices. For multivariate-normal random effects with covariance \(\Sigma\), we parameterize via the lower-triangular Cholesky factor \(L\) such that \(\Sigma = L L^T\). Stan’s lkj_corr_cholesky prior works on the Cholesky factor of the correlation matrix. This avoids the positive-definiteness constraint and produces substantially better sampling.

22.8 Parallel chains

Modern Bayesian software runs multiple chains in parallel by default. The standard configuration is as follows:

  • 4 chains (Stan default), each on its own CPU core.
  • 1000 warmup + 1000 sampling iterations per chain (Stan default).
  • Random initialization dispersed in the parameter space (Stan default: uniform on \([-2, 2]\) in the unconstrained space).

The samples from the four chains are pooled for inference, and their consistency provides the \(\hat R\) diagnostic.

For long-running models, cmdstanr and rstan can run chains in parallel. We set parallel_chains = 4 in cmdstanr or cores = 4 in rstan (or as many cores as are available).

library(cmdstanr)
fit <- stan_model$sample(
  data    = stan_data,
  chains  = 4,
  parallel_chains = 4,
  iter_warmup   = 1000,
  iter_sampling = 1000,
  seed    = 47
)

For RNG reproducibility we set seed. Each chain receives a derived seed, and running with the same seed produces identical samples. Without a seed, the results are not reproducible.

22.9 Worked example: Eight Schools

The Eight Schools dataset (Rubin, 1981; Gelman et al., 2013) is the canonical hierarchical-Bayesian example. The data consist of estimated treatment effects of an SAT-prep coaching intervention at eight schools, together with their standard errors:

schools_data <- list(
  J = 8,
  y = c(28, 8, -3, 7, -1, 1, 18, 12),
  sigma = c(15, 10, 16, 11, 9, 11, 10, 18)
)

We fit a hierarchical model with the non-centered parameterization:

data {
  int<lower=0> J;
  vector[J] y;
  vector<lower=0>[J] sigma;
}
parameters {
  real mu;
  real<lower=0> tau;
  vector[J] eta;
}
transformed parameters {
  vector[J] theta = mu + tau * eta;
}
model {
  mu  ~ normal(0, 5);
  tau ~ cauchy(0, 5);
  eta ~ normal(0, 1);
  y   ~ normal(theta, sigma);
}

The fit and the diagnostic checks are as follows:

library(cmdstanr)
mod <- cmdstan_model('eight-schools-ncp.stan')
fit <- mod$sample(data = schools_data, chains = 4,
                  parallel_chains = 4, seed = 47, refresh = 0)

fit$summary()                  # tibble of point estimates, ESS, R-hat
fit$diagnostic_summary()       # divergences, E-BFMI, max tree depth

Under NCP this model samples cleanly in seconds, with zero divergences, \(\hat R\) near 1, and ESS in the thousands. Re-running with the centered parameterization typically produces dozens of divergences.

22.10 Collaborating with an LLM on MCMC

LLMs handle Stan and PyMC code competently, including both centered and non-centered parameterizations. They are less reliable on the interpretation of diagnostics. Specifically, they are less reliable at translating diagnostic output into either ‘this is fine, publish’ or ‘this is broken, fix the parameterization’.

Prompt 1: write Stan code with appropriate parameterization. Describe the model and the information level of the data (how many observations per group). Then ask: ‘write Stan code for this hierarchical model. Choose between centered and non-centered parameterization based on the information level.’

What to watch for. Few observations per group (under roughly 5 to 10) call for NCP. Many observations per group (50+) is the regime in which CP works. The LLM should make this choice and explain it. If it picks one without explanation, ask why.

Verification. Fit the model with the chosen parameterization and check for divergences. If divergences appear, try the alternative. The correct choice usually has zero divergences.

Prompt 2: interpret diagnostic output. Paste the output of fit$diagnostic_summary() (divergences, E-BFMI, max tree depth) and fit$summary() (R-hat, ESS). Then ask: ‘is this chain converged? What should I worry about?’

What to watch for. The LLM should check \(\hat R < 1.01\) on every parameter and ESS bulk and tail above 400 on every parameter. It should also check that divergences = 0, E-BFMI > 0.3, and the max tree depth is not saturated. If any check fails, the diagnosis depends on which one. Divergences point to the parameterization or to adapt_delta; low ESS points to chain length; saturated tree depth points to step size.

Verification. Run with adjusted settings (more iterations, NCP, higher adapt_delta) and confirm that the diagnostic improves.

Prompt 3: diagnose a specific pathology. Paste a pair plot description (or the actual mcmc_pairs output) showing divergences clustered in a particular region. Then ask: ‘what kind of geometric pathology produces this pattern, and what reparameterization would fix it?’

What to watch for. Divergences clustered at small \(\tau\) in a hierarchical model indicate the funnel pathology, which NCP fixes. Divergences clustered at the boundary of a constrained parameter (e.g., \(\rho \to 1\) in a correlation) indicate a scale issue, and a tighter prior merits consideration. Divergences along a thin diagonal indicate an ill-conditioned posterior, which needs a reparameterization that breaks the correlation.

Verification. Apply the recommended reparameterization, refit, and verify that the divergences are gone.

Modern MCMC diagnostics are informative if we read them, and an LLM helps us interpret them faster than we would alone. Deciding whether to trust its diagnosis enough to publish, however, requires checking that diagnosis against the actual posterior behavior.

22.11 Principle in use

Three habits define defensible MCMC use:

  1. Read the full diagnostic suite. This means \(\hat R\), ESS (bulk and tail), divergences, and E-BFMI. Any failure is a reason to investigate before reporting.
  2. Reparameterize before retuning. When divergences appear, the first move is the non-centered parameterization (for hierarchical models) or input standardization (for regression). Tightening adapt_delta is the second move, not the first.
  3. Multiple chains, dispersed starts. Four chains with random initialization protect against multimodality. They also protect against chains that look converged in isolation but disagree with one another.

22.12 Exercises

  1. Fit the Eight Schools model with both centered and non-centered parameterizations. Compare divergences, ESS, and posterior summaries. Confirm the reparameterization reproduces the same posterior in distribution but with cleaner sampling.
  2. Construct a hierarchical model where the data are highly informative (many observations per group) and one where they are weakly informative (few). For each, compare CP and NCP. The advantage of NCP should diminish in the data-rich regime.
  3. Implement the leapfrog integrator and a basic HMC sampler in R. Test on a 2D Gaussian target. Compare to random-walk Metropolis at fixed computational budget.
  4. Use bayesplot::mcmc_pairs with divergence coloring on a hierarchical model that produces divergences. Visually identify the funnel; describe its location in \((\tau, \mu_j)\)-space.
  5. Run a Stan model with seed = 47 four separate times. Confirm that all four runs produce identical samples (same fit$draws()). Then run without a seed and observe non-reproducibility.

22.13 Further reading

  • (Hoffman & Gelman, 2014), Hoffman and Gelman, the NUTS paper.
  • (Betancourt, 2017), Betancourt, ‘A conceptual introduction to Hamiltonian Monte Carlo’. The clearest geometric treatment.
  • (Vehtari et al., 2021), Vehtari et al., the modern \(\hat R\) paper.
  • The Stan User’s Guide and the bayesplot, posterior, and cmdstanr package documentation.