Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.Is the model any good?
A Bayesian model always produces a posterior, even when the model is wrong. So you test it like any engineering design. Before seeing data, simulate from the priors: do the fake data look possible? Then check the fitting code on simulated data where you know the truth. After fitting, simulate new data from the fitted model and compare it with the real data. If the real data have features the model never reproduces, such as too many zeros or too much spread, the model is missing something. Finally, compare candidate models by how well they predict data they did not see. The demo shows predictions from a fitted model with their uncertainty.
Radiation-induced upsets in spacecraft memory arrive in bursts when the spacecraft passes through the South Atlantic Anomaly or during solar events. A Poisson model fitted to daily counts underestimates bad days. Posterior predictive checks reveal it, and a negative binomial or hierarchical model fixes it before it drives error-correction sizing.
- 1. A posterior predictive check compares…
- 2. A prior predictive check uses…
- 3. Models are compared by…
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.By hand
- Workflow (Gelman et al., 2020; Betancourt’s principled workflow): (1) generative model; (2) prior predictive checks; (3) fit simulated data and run SBC; (4) fit real data and check R-hat, ESS and divergences; (5) posterior predictive checks; (6) expand, compare or average models; iterate.
- Prior predictive checks: simulate y from the priors; flag priors that make impossible data likely, such as 10 000 upsets a day, or that rule out plausible data. Weakly informative priors on the right scale help both inference and checks.
- SBC (Talts et al., 2018; Modrák et al., 2023): draw θ from the prior, y given θ, fit, record the rank of θ among L posterior draws. With correct inference the ranks are uniform. A U shape means an overconfident posterior, an inverted U an underconfident one, and a slope a biased one.
- Convergence: rank-normalised split R-hat below 1.01 and bulk and tail ESS above about 400 (Vehtari et al., 2021); no divergent transitions; E-BFMI above 0.3.
- Posterior predictive checks (Rubin, 1984; Gelman, Meng and Stern, 1996): choose statistics T that matter for the use, such as dispersion, zeros, maxima or autocorrelation, and compare T(y) with the distribution of T(y_rep). The p-value P(T(y_rep) ≥ T(y)) near 0 or 1 signals misfit. Graphical checks overlay densities or rootograms.
- Model comparison: expected log predictive density by PSIS-LOO (Vehtari, Gelman and Gabry, 2017; Pareto k̂ above 0.7 means unreliable), WAIC (Watanabe, 2010), or K-fold CV. Report differences with standard errors; use stacking to average models.
- 1. A U-shaped SBC rank histogram means the posterior is…
- 2. A Pareto k̂ above 0.7 in PSIS-LOO means…
- 3. Current guidance asks for R-hat below…
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.Diagnostics and checks
Exercise 1.
- Split R-hat.
- The posterior predictive p-value.
- The SBC rank.
- WAIC and leave-one-out elpd by truncated importance sampling.
Practice.
- Write the model so it can generate data: the same code then gives prior predictive, SBC and posterior predictive simulations.
- Choose posterior predictive statistics tied to the decision (peaks, zeros, tails), not only the mean, which most models fit.
- Run SBC on a reduced number of simulations whenever you change the model or sampler; use the Stan SBC R package or a short loop in PyMC or NumPyro.
- Compare models with az.compare (PSIS-LOO) and check Pareto k̂; if many are high, use K-fold CV.
- Expand the model where checks fail (overdispersion → negative binomial or random effects; zeros → zero-inflation; trends → time terms), then check again.
import pymc as pm, arviz as az
with pm.Model() as negbin: # display only
mu = pm.LogNormal("mu", mu=1, sigma=1)
phi = pm.LogNormal("phi", mu=0, sigma=1.5)
pm.NegativeBinomial("y", mu=mu, alpha=phi, observed=y) # alpha is the dispersion
prior = pm.sample_prior_predictive(500)
idata = pm.sample(idata_kwargs={"log_likelihood": True})
pm.sample_posterior_predictive(idata, extend_inferencedata=True)
az.plot_ppc(idata) # observed vs replicated data
print(az.compare({"poisson": idata_poisson, "negbin": idata})) # PSIS-LOO, differences and SEs- 1. To store pointwise log-likelihoods in PyMC use…
- 2. ArviZ's function for model comparison:
- 3. If many Pareto k̂ values are high, use…
Level 3: Advanced
★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.Theory
Why SBC ranks are uniform. Draw θ̃ from the prior and y from p(y given θ̃). Then (θ̃, y) is a draw from the joint distribution, so θ̃ given y is distributed exactly as the posterior. If the L posterior draws are exact, θ̃ and the draws are exchangeable given y. The rank of θ̃ among them is therefore uniform on 0, …, L (Cook, Gelman and Rubin, 2006; Talts et al., 2018). An overconfident sampler places its draws too tightly, so θ̃ often falls outside them and the ranks pile up at 0 and L.
Posterior predictive p-values are not uniform under the true model. They concentrate near 0.5, because the data were used to fit the model. Extreme values are therefore strong evidence of misfit, while moderate values are weak evidence of fit. Choose statistics the model does not fit directly: a Poisson model fits the mean exactly, so check dispersion and zeros.
LOO and WAIC. Both estimate the expected log predictive density for new data, elpd = Σ_i E log p(ỹ_i given y). LOO’s importance weights for point i are 1/p(y_i given θ_s), which can have infinite variance. PSIS fits a generalised Pareto distribution to the largest weights, both to smooth them and to diagnose reliability through its shape k̂. WAIC is asymptotically equivalent to LOO but gives no such diagnostic.
Overdispersion. If counts given a rate are Poisson and the rate varies as Gamma, the counts are negative binomial with variance μ + μ²/φ. The extra term is what the Poisson model cannot produce.
- 1. SBC ranks are uniform because…
- 2. Posterior predictive p-values under a true model tend to be…
- 3. A gamma mixture of Poissons is…
Level 4: Expert
★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.Do posterior predictive checks, LOO and SBC catch a misspecified count model and a broken posterior?
History.
- D. Rubin (1984) posterior predictive checks; A. Gelman, X.-L. Meng and H. Stern (1996) posterior predictive assessment.
- S. Cook, A. Gelman and D. Rubin (2006) software validation; S. Talts et al. (2018) SBC; M. Modrák et al. (2023) SBC in practice.
- S. Watanabe (2010) WAIC; A. Vehtari, A. Gelman and J. Gabry (2017) PSIS-LOO; A. Gelman et al. (2020) “Bayesian workflow”.
Exercise 2. Daily single-event-upset counts for 60 days, simulated as negative binomial with mean 4 and dispersion 2.
- M1, Poisson: Gamma(2, 0.5) prior on the rate; exact conjugate posterior.
- M2, negative binomial: log μ ~ N(1, 1), log φ ~ N(0, 1.5²); posterior on a 100 × 100 grid.
- Draws: 4000 posterior draws each. Posterior predictive p-values for the variance-to-mean ratio, the number of zero days and the maximum; LOO by truncated importance sampling, plus WAIC.
- SBC: for the Poisson–gamma model, 1000 simulations of 20 days with 99 posterior draws each, once with the correct posterior and once with an overconfident one (same mean, half the variance). The statistic is the χ² of 20 rank bins, with a 5% critical value of 30.1.
Observed data: mean 4.35, variance/mean 2.46, 5 zero days. The prior predictive 95% range of daily counts was 0 to 13.
| posterior predictive p-value | variance / mean | zero days | maximum |
|---|---|---|---|
| Poisson | 0.000 | 0.002 | 0.004 |
| negative binomial | 0.484 | 0.439 | 0.491 |
| comparison | LOO elpd | WAIC elpd |
|---|---|---|
| Poisson | −164.2 | −164.2 |
| negative binomial | −149.5 | −149.5 |
| difference | 14.7 ± 7.2 |
| SBC (20 bins, 19 df) | χ² | share of extreme ranks (expected 0.10) |
|---|---|---|
| correct posterior | 22.6 | 0.089 |
| overconfident posterior | 188.4 | 0.222 |
- The Poisson model failed in every respect that mattered: it never produced the observed spread and almost never as many zero days. Its rate estimate (4.35) was fine; the mean is not a useful check.
- The negative binomial passed all three checks and won on LOO by about two standard errors (14.7 ± 7.2). WAIC agreed to one decimal.
- SBC caught the overconfident posterior through a U-shaped rank histogram: 22% of ranks were extreme instead of 10%. It would not have shown up in R-hat, because the draws were independent.
- Seeds 1 and 2: LOO differences 18.0 ± 8.8 and 51.7 ± 15.6; overconfident SBC χ² 156.4 and 293.8. The correct posterior gave χ² 16.6 and 33.9. The latter is above the 5% critical value: a false alarm, as expected in about one run in twenty.
Model checking in AI, robotics and aerospace
- Radiation effects: upset-rate models for memory scrubbing and error-correction sizing are checked against bursty orbit data before they set design margins.
- Reliability growth and fleet data: hierarchical failure models are expanded when posterior predictive checks show unit-to-unit variation the simpler model cannot produce.
- ML systems: the same discipline applies to probabilistic forecasts from neural networks: calibration checks, SBC-style coverage tests and predictive comparisons on held-out data.
References
- A. Gelman et al. (2020). “Bayesian workflow.” arXiv.
- S. Talts, M. Betancourt, D. Simpson, A. Vehtari and A. Gelman (2018). “Validating Bayesian inference algorithms with simulation-based calibration.” arXiv.
- A. Vehtari, A. Gelman and J. Gabry (2017). “Practical Bayesian model evaluation using leave-one-out cross-validation and WAIC.” Statistics and Computing.
- A. Gelman, X.-L. Meng and H. Stern (1996). “Posterior predictive assessment of model fitness via realized discrepancies.” Statistica Sinica.
- M. Modrák et al. (2023). “Simulation-based calibration checking for Bayesian computation: the choice of test quantities shapes sensitivity.” Bayesian Analysis.