Margossian 2022 - Stan Workflow and MCMC Diagnostics

Summary

A Stan program is a recipe for ; automatic differentiation supplies its gradient to HMC. Convergence is checked with split and effective sample size (the authors consider usually sufficient for posterior summaries, since the Monte Carlo variance is then 1% of the posterior variance). The single-patient two-compartment fit passes these checks (Table 2).

Overview

The paper frames modelling as a Bayesian workflow: build, fit, check inference, check the model, revise. This note covers the inference half.

Main Content

Bayesian setup and why HMC

Given data and latent , the joint is and inference targets the posterior and derived quantities . The normalizing constant is intractable, so MCMC methods that use only the unnormalized density are used. HMC requires and many times at fixed and so needs continuous parameters (discrete parameters require special treatment, not covered). Stan supplies NUTS (adaptive HMC), ADVI, penalized maximum likelihood, and newer prototypes (adjoint-differentiated Laplace approximations, cross-chain warmup, Pathfinder initialization). Divergent-transition detection, split , bulk/tail ESS are automated. NONMEM also supports an HMC sampler but at the time lacked some diagnostics for population models.

Stan program blocks (Sec. 2.3)

BlockRole
functionsuser functions, e.g. the ODE right-hand side
datafixed inputs: event schedule, observations
parametersquantities HMC explores; constraints via lower/upper are handled by transforming to an unconstrained scale (positive variables on the log scale)
transformed parametersderived quantities such as from the ODE solve
modelincrements target with log prior plus log likelihood; ~ statements are shorthand
generated quantitiessimulated predictions, pointwise log-likelihoods, run once per draw

Truncation at zero of a Half-Normal prior on follows from declaring lower-bounded by 0. Fitting is run from R with cmdstanr, with posterior, bayesplot, loo for summaries; code in the torsten_tutorial_1_supplementary repository.

Checking inference (Sec. 2.5)

and effective sample size

Several chains start at different points. intuitively compares total variance across all chains with the average within-chain variance; if chains mix, both estimate the posterior variance and as grows. Stan uses the improved (rank-normalized, split) of Vehtari et al. (2020). Under regularity conditions the MCMC estimator satisfies a CLT with variance , with because of autocorrelation. Bulk-ESS governs central summaries; tail-ESS governs extreme quantiles.

Rules of thumb from the authors: ; usually sufficient in applied work; if for some quantity, lengthen the sampling phase. Even with all chains could share a bias, and warmup must be long enough.

Table 2 (two-compartment fit, 2,000 draws).

Parametermeanmediansdq5q95ESS bulkESS tail
CL10.010.00.3789.3910.61.0015801348
Q19.819.54.0013.826.81.009851235
41.240.89.7125.657.71.007321120
12412318.097.11551.0018771279
1.731.670.5231.012.681.007621108
0.2240.2220.02440.1870.2691.0115491083

Interpretation. CL is tightly identified (sd 0.378 vs prior sd about 2.5); , and remain wide relative to the data: with this sampling design the absorption-phase and distribution parameters are weakly identified, and the posterior is informed partly by the prior. The data-generating values are not given in Table 2; CL near 10 matches the prior centre.

Examples

Computing requirements. The relative Monte Carlo standard error of a posterior mean is of the posterior sd: at it is 10% of the posterior sd, so error variance is 1% of the posterior variance (the authors’ justification).

PyMC/ArviZ equivalent.

import arviz as az
az.summary(idata, var_names=["CL","Q","Vc","Vp","ka","sigma"], round_to=3)   # r_hat, ess_bulk, ess_tail
az.plot_trace(idata)

Connections

HMC and Stan in Practice, MCMC Basics, Efficient MCMC, Bayesian Workflow - Overview, Izmailov 2021 - Mixing Diagnostics and Posterior Geometry (function-space ).

See Also

Margossian 2022 - Bayesian Pharmacometrics - Overview.