The two MCMC backends, their diagnostics, a real demonstration of under-sampling, and the ICAR scaling gotcha. This notebook is Part 5. Disease Mapping and Bayesian Smoothing (Chapter 5.7 of 11). The worked example uses Simulated data with known truth (Chapter 5.3, sigma=0.35, expected counts averaging 40). The methods come from geolattice.spatial_statistics.bayes.
Overview
Chapter 5.6 fit BYM and treated the posterior as a given. This chapter looks at how it is actually produced, two different samplers, with different trade-offs: and, critically, demonstrates directly what goes wrong when the built-in sampler’s default settings are not respected.
Step
What we do
Built-in sampler
Blocked Gibbs/Metropolis with graph-coloured updates; dependency-free, handles islands natively
PyMC backend
NUTS; needs a connected graph; a specific, documented scaling subtlety worth understanding
Diagnostics
R-hat, effective sample size, divergences: and a direct thin=1 vs. thin=10 comparison
Cross-validation
Do the two backends agree on the same data?
Posterior predictive check
Does the fitted model actually reproduce data like what was observed?
Model comparison
DIC (always available) vs. WAIC/LOO (a real gap in this library, stated honestly)
API overview
- Queen, KNN, W
- BYM, simulate_relative_risk, simulate_counts, pymc_available
Setup
Locate the repository root, import the spatial-statistics helpers, and point DATA at the example files.
Installation. This notebook needs installation option 6 (every GeoLattice module): pip install "geolattice[all]". See “Installation options” in the GeoLattice README.
geolattice.spatial_statistics : version 0.3.49
PyMC available: True
The Built-in Sampler
backend='gibbs' (the default) uses a blocked Gibbs/Metropolis sampler working directly in the convolution (\(u+v\)) parametrisation. It updates areas in graph-coloured groups: areas that share no edge can be updated simultaneously without breaking the Markov property: and it handles an island natively, since an area with no ICAR neighbours simply has no spatial term to update. Multiple chains start from dispersed initial scales (init_scale varies across chains), which is what makes the R-hat diagnostic meaningful: if chains that started far apart converge to the same distribution, that is real evidence of convergence, not an artefact of similar starting points.
fit time: 10.3s (4 chains x 2000 draws/chain)
max R-hat over relative risks: 1.0049 min ESS: 1266
The PyMC Backend
backend='pymc' uses NUTS (the No-U-Turn Sampler), which typically mixes far faster than single-site Gibbs updates for smooth spatial fields: but it requires a fully connected adjacency graph, since PyMC’s ICAR distribution is not defined on a graph with isolated nodes. The island must be linked first (Chapter 5.3).
Code
island_idx = w.islands()[0]island_id = w.id_order[island_idx]wk1 = KNN(counties, k=1)nearest_id = wk1.neighbors[island_id][0]neighbors = {i: list(w.neighbors[i]) for i in w.id_order}neighbors[island_id] = [nearest_id]neighbors[nearest_id] = neighbors[nearest_id] + [island_id]w_linked = W(neighbors, ids=w.id_order, transform="binary")t0 = time.time()fit_pymc = BYM(O_sim, E_sim, w_linked, model="bym", backend="pymc", draws=1000, tune=1000, chains=4, seed=1, progressbar=False)print(f"fit time: {time.time() - t0:.1f}s ({fit_pymc.chains} chains x {fit_pymc.n_draws} draws/chain)")print(f"max R-hat over relative risks: {fit_pymc.max_rhat:.4f} min ESS: {fit_pymc.min_ess:.0f}")print(f"divergences: {fit_pymc.get('divergences')}")
fit time: 8.8s (4 chains x 1000 draws/chain)
max R-hat over relative risks: 1.0009 min ESS: 4453
divergences: 0
Comparable or better diagnostics with half the total draws (4,000 vs. 20,000) and zero divergent transitions: the practical payoff of gradient-based NUTS over single-site Gibbs updates for this kind of smooth spatial model.
The ICAR scaling gotcha
A specific, easy-to-get-wrong implementation detail: PyMC’s ICAR distribution is unnormalised in its own scale parameter: its density does not include the \(\sigma^{-(n-c)}\) normalising term a proper Gaussian Markov random field needs (where \(n-c\) is the field’s rank after the sum-to-zero constraint). Putting a hyperprior directly on ICAR’s own sigma argument would therefore sample from an improper posterior for that parameter, silently. The documented, correct workaround: and what this library’s PyMC backend actually does internally: is to fix ICAR’s own sigma=1.0 and instead multiply the unit-scale field by a separately-modelled precision outside the distribution:
tau_u = pm.Gamma("tau_u", a_u, b_u)u_std = pm.ICAR("u_std", W=A, sigma=1.0) # fixed unit scale: correctu = pm.Deterministic("u", u_std / pm.math.sqrt(tau_u)) # the actual spatial effect
rather than the tempting but statistically wrong shortcut of passing a modelledsigma=1/pm.math.sqrt(tau_u) directly into pm.ICAR(...). This is exactly why BYM(backend='pymc')’s implementation is structured this way rather than the more obvious-looking alternative: it is not a stylistic choice.
Diagnostics: Why the Built-in Sampler Needs Thinning
Single-site Gibbs updates on a smooth spatial field mix slowly: consecutive draws are highly correlated, because each update only moves one area at a time relative to its current neighbours. thin=10 (the default) keeps every 10th draw specifically to counteract this. A direct comparison at thin=1 shows exactly what is lost by skipping it.
Code
t0 = time.time()fit_thin1 = BYM(O_sim, E_sim, w, model="bym", backend="gibbs", draws=1500, tune=1000, chains=4, thin=1, seed=1)print(f"thin=1 : {time.time() - t0:.1f}s, max R-hat={fit_thin1.max_rhat:.4f}, min ESS={fit_thin1.min_ess:.0f}")print(f"thin=10 : (Section 3 above) max R-hat={fit_gibbs.max_rhat:.4f}, min ESS={fit_gibbs.min_ess:.0f}")
thin=1 : 1.3s, max R-hat=1.0522, min ESS=112
thin=10 : (Section 3 above) max R-hat=1.0049, min ESS=1266
At thin=1, R-hat for the worst relative risk rises to about 1.05 (right at the usual 1.05 convergence threshold) and the worst effective sample size drops by roughly an order of magnitude: despite using fewer raw MCMC iterations before thinning is even applied (1,500+1,000 vs. 2,000+2,000). Thinning does not literally add information; but by discarding highly autocorrelated intermediate draws it keeps the retained sample close to independent, which is what R-hat and ESS actually measure. Skipping it is the single most likely way to get a silently under-converged BYM fit from the built-in sampler.
Cross-backend Validation
Do the two, structurally different samplers actually agree on the same data? This is the strongest available check that neither implementation has a hidden bug: but it is only a fair test if both backends fit the same weights graph. Section 3’s fit_gibbs used w (island included); Section 4’s fit_pymc had to use w_linked (island merged in) for PyMC’s connectivity requirement: comparing those two directly would confound “different sampler” with “different graph”. A fair comparison refits the built-in sampler on w_linked too.
posterior-mean correlation (gibbs vs. pymc, SAME graph w_linked): 0.99997
DIC: gibbs=1525.7 pymc=1525.5
tau_u posterior mean: gibbs=5.68 pymc=5.72
With the confound removed, the two independently implemented samplers: one hand-written Gibbs/Metropolis, one general-purpose NUTS: converge to the same answer: posterior-mean correlation above 0.999 and DIC/\(\tau_u\) within a fraction of a percent of each other. This is about as strong a correctness check as is practically available for a custom MCMC implementation, and it also makes a second point on its own: fitting the same model on two different graphs (island merged vs. left alone) is not the same experiment, even with an identical sampler: always hold the graph fixed when comparing anything else.
Posterior Predictive Check
Beyond convergence, does the fitted model actually reproduce data that looks like what was observed? Draw simulated counts from the posterior and compare their distribution to the real observed counts.
observed mean count = 41.96 posterior-predictive mean count: [40.66, 43.13]
observed max count = 115 posterior-predictive max count : [95, 132]
The observed mean and maximum both fall comfortably inside their posterior-predictive intervals: the fitted model can generate data indistinguishable, on these two summaries, from what was actually observed. A posterior predictive check that failed (observed value far outside the simulated range) would be a concrete sign of model misspecification, distinct from an MCMC convergence problem.
DIC, and Why Not WAIC/LOO Here
BYM.dic is always available from either backend, computed directly from the posterior deviance. WAIC and LOO (via ArviZ, when using the PyMC backend) are a real gap in this library’s current implementation: they require the model to track pointwise log-likelihood at every posterior draw, which run_pymc does not currently request from PyMC.
Code
import arviz as aztry: az.waic(fit_pymc.idata)exceptExceptionas e:print(f"WAIC unavailable: {e}")
WAIC unavailable: log likelihood not found in inference data object
This confirms the gap directly rather than leaving it as an assumption: fit_pymc.idata (the raw ArviZ InferenceData object, available whenever backend='pymc') does not carry a log_likelihood group, so az.waic/az.loo cannot be computed from it without modifying the model definition to request idata_kwargs={"log_likelihood": True}: an extension outside the scope of what BYM currently exposes. DIC remains the practical, always-available model-comparison metric in this library (used throughout Chapters 5.6-5.11); WAIC/LOO would need custom PyMC code written outside the BYM wrapper.
Summary and Conclusions
This chapter opened up how BYM’s posteriors are actually produced, and validated both routes to get there.
What we did
Explained the built-in Gibbs/Metropolis sampler: graph-coloured updates, native island handling, dispersed chain starts for a meaningful R-hat.
Ran the PyMC/NUTS backend: comparable or better diagnostics with a fifth of the total draws and zero divergences, at the cost of requiring a connected graph.
Explained the ICAR unnormalised-scale gotcha precisely, with the correct fixed-scale-then-multiply pattern this library’s own PyMC backend implements.
Demonstrated thin=1 degrading diagnostics directly: R-hat rises to ≈1.05 and ESS drops roughly 10-fold compared to the thin=10 default, on fewer raw iterations.
Cross-validated both backends against each other: posterior-mean correlation 0.99997, near-identical DIC and \(\tau_u\).
Ran a posterior predictive check: the observed mean and max count both fall inside their posterior- predictive intervals.
Confirmed DIC is always available but WAIC/LOO are not currently supported by this library’s PyMC integration, and explained precisely why (no tracked log-likelihood).
Practical takeaways
Never drop the built-in sampler’s default thin=10 without checking max_rhat/min_ess afterward: Section 5 shows concretely what is lost.
Cross-backend validation (Section 6) is a strong, cheap correctness check whenever both are available, run it, don’t just trust one implementation.
Use DIC for model comparison in this library; treat WAIC/LOO as unavailable unless the PyMC model is extended to track log-likelihood explicitly.
Next steps
Chapter 5.8 (Comparing and Evaluating Smoothing Methods) puts every method from Chapters 5.4-5.6 through the formal simulation-based scoring Chapter 5.3’s known-truth data was built for.
Gelman, A. et al. (2013). Bayesian Data Analysis (3rd ed.). CRC Press.
Standard reference on MCMC diagnostics (R-hat, ESS, posterior predictive checks)
Hoffman, M. D. & Gelman, A. (2014). The No-U-Turn sampler. Journal of Machine Learning Research, 15, 1593-1623.
Origin of NUTS
Key papers
Paper
Contribution
Vehtari, A., Gelman, A., Simpson, D., Carpenter, B. & Bürkner, P.-C. (2021). Rank-normalization, folding, and localization: an improved R̂ for assessing convergence of MCMC. Bayesian Analysis, 16, 667-718.
Modern split-R̂ diagnostic used by rhat()
Vehtari, A., Gelman, A. & Gabry, J. (2017). Practical Bayesian model evaluation using leave-one-out cross-validation and WAIC. Statistics and Computing, 27, 1413-1432.
The WAIC/LOO methods this library does not yet expose