Machine Learning for Astroparticle Physics:
A Crash-course in SBI

Lecture 5a - Rank-based diagnostics and failure modes

Christoph Weniger — University of Amsterdam (GRAPPA)

Where can our inference go wrong?

A taxonomy of epistemic uncertainties in SBI: three failure modes, each with its own diagnostic strategy.

CategoryTypeDescription
Epistemic Type A Misspecified model
"I'm simulating the wrong world"
Type B Lossy summary
"I'm discarding relevant information"
Type C Inexact inference
"I can't compute the posterior exactly"
AleatoricIrreducible randomness in the data

Lossy summaries

When the embedding network fails to extract all relevant information from the data.

Overdispersion vs lossy summaries

Approximate posteriors $q_\phi(\theta \mid x)$ are overdispered (underdispersed) when they overestimate (underestimate) their coverage of the data generating parameters.

Posteriors that are exact but based on lossy summaries, $q_\phi(\theta \mid x) = p(\theta \mid T(x))$, are wider but not overdispersed. Their width correctly represents information loss and additional scatter in the posterior position.

Information Content: A Venn Diagram View

Entropy \[ H(X) = -\sum_x p(x)\log p(x) \] Average uncertainty in \(X\). How many bits does it take to describe a draw from \(p(x)\)?
Conditional Entropy \[ H(X\mid Y) = -\sum_{x,y} p(x,y)\log p(x\mid y) \] Remaining uncertainty in \(X\) after observing \(Y\).
Mutual Information \[ I(X;Y) = H(X) - H(X\mid Y) = \sum_{x,y} p(x,y)\log\frac{p(x,y)}{p(x)p(y)} \] How much knowing \(Y\) reduces uncertainty about \(X\). Zero iff \(X \perp Y\).

Information Content: A Venn Diagram View

Data Processing Inequality \[ I(\theta;\, T(x)) \;\le\; I(\theta;\, x) \]

Any deterministic function \(T(x)\) can only destroy — never create — information about \(\theta\).

Sufficient Statistic

\[ T(x)\ \text{sufficient} \;\Leftrightarrow\; I(\theta;\,T(x)) = I(\theta;\,x) \]

Equality holds iff \(\theta \perp x \mid T(x)\): the statistic captures everything about \(\theta\).

Compressing \(x \to T(x)\) before SBI can only widen posteriors. The ideal learned summary is a sufficient statistic.

What Does the NPE Loss Optimize?

Maximize \(\log q_\phi(\theta\mid x)\) on the training set:

\[ \mathcal{L}_\text{NPE} = -\frac{1}{|\mathcal{D}|}\sum_{(\theta,x)\in\mathcal{D}} \log q_\phi(\theta\mid x) \]

As \(N\to\infty\), the empirical loss becomes an expectation over the joint:

\[ \mathcal{L}_\text{NPE} \longrightarrow \mathbb{E}_{x\sim p(x)}\!\Big[ D_\text{KL}\big(p(\theta\mid x)\,\|\,q_\phi(\theta\mid x)\big) + H\big(p(\theta\mid x)\big) \Big] \]

where the KL divergence measures the (non-symmetric) "distance" between distributions, with \(D_\text{KL}\ge 0\) and equality iff \(p=q\):

\[ D_\text{KL}(p\,\|\,q) = \mathbb{E}_{p}\!\left[\log\frac{p(\theta)}{q(\theta)}\right] = \sum_\theta p(\theta)\log\frac{p(\theta)}{q(\theta)} \]

So the loss is minimized exactly when \(q_\phi(\theta\mid x) = p(\theta\mid x)\) for all \(x\). Requires a normalized density estimator (e.g. a normalizing flow).

NPE Learns Information-Maximizing Summaries

Add a summary network \(q_\phi(\theta\mid x)\to q_\phi(\theta\mid T_\phi(x))\). Then

\[ \mathcal{L}_\text{NPE} \;\ge\; \mathbb{E}_{p(x)} H\big(p(\theta\mid T_\phi(x))\big) \;\ge\; \mathbb{E}_{p(x)} H\big(p(\theta\mid x)\big) \]

Tight when the estimator is exact. Training \(T_\phi\) end-to-end pushes summaries toward minimal posterior entropy.


Using the identity

\[ \mathbb{E}_{p(x)} H\big(p(\theta\mid T_\phi(x))\big) = H\big(p(\theta)\big) - I\big(\theta; T_\phi(x)\big) \]

we find that

minimizing posterior entropy \(\;\Leftrightarrow\;\) maximizing \(I(\theta; T_\phi(x))\).

Summaries learned in the context of NPE (but also NRE, score matching, ..., not NLE) are information-maximizing — a key strength. But it brings the challenge of knowing when they are sufficient.

Rank Diagnostics

Letting the inference "walk on its own": forward vs backward, judged by ranks.

The forward–backward consistency principle

Compare samples from two joint distributions on \((\theta, x)\), derived from the forward model and the backward model, respectively.

\[ \underbrace{q_\phi(\theta\mid x)\,p(x)}_{\text{backward (inference)}} \;\;\overset{?}{=}\;\; \underbrace{p(x\mid\theta)\,p(\theta)}_{\text{forward (simulator)}} \]

Key observation: If \(q_\phi(\theta\mid x) = p(\theta\mid x)\), the two distributions coincide. Vice versa, any discrepancy flags inference error.

The forward–backward consistency principle

Simulation-based calibration (SBC). Given an observation \(x\sim p(x)\) drawn from the generative model, draw one parameter from the true posterior, \(\theta^* \sim p(\theta\mid x)\), and \(L\) further draws from the learned posterior, \(\theta_{1:L} \sim q_\phi(\theta\mid x)\).

\[ x \sim p(x),\quad \theta^* \sim p(\theta\mid x),\quad \theta_{1:L} \sim q_\phi(\theta\mid x) \]
However, we cannot draw from the true posterior. Luckily, by Bayes' theorem this is equivalent to:

\[ \theta^* \sim p(\theta),\quad x \sim p(x\mid\theta^*),\quad \theta_{1:L} \sim q_\phi(\theta\mid x) \]
And this can we draw quite easily once $q_\phi(\theta \mid x)$ is trained.

What SBC actually does, in one picture

For each of \(N\) simulated experiments we get one rank between \(0\) and \(L\). Aggregate them into a histogram.

  1. Draw \(\theta^*\) from the prior.
  2. Run the simulator to get \(x\).
  3. Draw \(L\) samples \(\theta_{1:L}\) from the learned posterior \(q_\phi(\theta\mid x)\).
  4. Count how many of the \(\theta_i\) are below \(\theta^*\).
  5. Repeat \(N\) times; histogram the ranks.

Under \(q_\phi=p\), the histogram is flat; any structure is a calibration signature.

trials: 0

Top: one trial — the true value (red ×) among the approx-posterior samples (orange). Bottom: the trial's rank drops into the histogram.

The forward–backward consistency principle

Now, given rank test samples

\[ \theta^* \sim p(\theta),\quad x \sim p(x\mid\theta^*),\quad \theta_{1:L} \sim q_\phi(\theta\mid x) \]

we can use some rank statistics arguments to construct a test.

The position (rank) of the true posterior sample \(\theta^*\) among the approximate posterior samples \(\theta_{1:L}\), is given by

\(F(\theta^*) = \tfrac{1}{L}\sum_i \mathbb{1}(\theta_i \le \theta^*)\).


Assuming that \(q_\phi = p\), all \(L+1\) positions are equally likely, and hence we expect:

\[ F(\theta^*) \;\sim\; \text{Uniform}\bigl(\{0, \tfrac{1}{L}, \ldots, 1\}\bigr) \]

Any deviations from this uniform distribution are a sign of miscalibration. This is the core idea of SBC.

Demo: reading SBC rank histograms

Simulator: \(\theta\sim\mathcal{U}(-1,1)\), \(x\mid\theta\sim\mathcal{N}(\theta, \sigma^2)\) with \(\sigma=0.1\).

Learned posterior:

\[ q_\phi(\theta\mid x) = \mathcal{N}(\theta;\,x+b,\,(s\sigma)^2) \]
  • Bias \(b\): shifts the mean
  • Scale \(s\): \(s<1\) underdispersed; \(s>1\) overdispersed
Watch for: bias → skew; underdispersion → U-shape; overdispersion → inverse U-shape.
trials: 0

Each trial draws \(\theta^*\) and \(x\) afresh, so both posteriors recentre on \(x\). Red × is \(\theta^*\); orange dots are the \(q\)-samples whose rank we record.

HPDR coverage: same machinery, different ordering

The highest posterior density region at level \(1-\alpha\):

\[ \mathcal{C}_{1-\alpha}(x) = \{\theta : q_\phi(\theta\mid x) \ge \gamma_{1-\alpha}\}, \quad \int_{\mathcal{C}_{1-\alpha}(x)} q_\phi(\theta\mid x)\,d\theta = 1 - \alpha \]

If \(q_\phi = p\), a fraction \(1-\alpha\) of true parameters \(\theta^*\) should fall inside. Plot empirical vs nominal coverage: the pp-plot.

Equivalently, this is a rank test with ordering function \(q_\phi(\theta\mid x)\):

\[ F_x(\theta^*) = \tfrac{1}{L}\sum_{i=1}^{L} \mathbb{1}\bigl(q_\phi(\theta_i\mid x) \ge q_\phi(\theta^*\mid x)\bigr) \]

Demo: reading HPDR rank histograms

Same simulator and learned posterior as before — only the ordering changes.

Now we rank by density \(q_\phi(\theta\mid x)\):

\[ F_x(\theta^*) = \tfrac{1}{L}\sum_i \mathbb{1}\bigl(q_\phi(\theta_i\mid x) \ge q_\phi(\theta^*\mid x)\bigr) \]

The dots now sit on the orange curve at their density height; the red line marks \(q_\phi(\theta^*\mid x)\). The rank counts the orange dots above the red line.

Watch for: pure bias → barely moves; underdispersion → mass at high \(F_x\) (the truth sits in the tails); overdispersion → mass at low \(F_x\).
trials: 0

Horizontal lines carry each density to the axis. Orange lines above the red one are the samples ranked higher than \(\theta^*\).

Demo: SBC vs HPDR, side-by-side

Same Gaussian setup as before; now we look at both diagnostics at once.

SBC orders by \(\theta_i\). Picks up bias, dispersion, and the symmetry-broken kind of asymmetric mismatch.
HPDR orders by \(q_\phi(\theta_i\mid x)\). Tracks dispersion well, only mildly responds to pure bias.

Failure Modes & Generalization

Where rank tests stay blind, and the framework that ties them together.

Failure 1: spatially varying bias hides in the average

SBC averages over \(x \sim p(x)\). A bias whose sign changes across data space partially cancels.

\[ q_\phi(\theta\mid x) = \mathcal{N}\bigl(\theta;\, x + b\,\mathrm{sgn}(x),\, \sigma^2\bigr) \]

Global rank histogram (dashed black) looks nearly flat. Stratify by sign of \(x\) and the opposite biases appear at once.

Lesson: stratify whenever miscalibration could depend on \(x\). The cost is statistical power per stratum.

Failure 2: SBC is blind to information loss

Two observations \(x_1, x_2 \sim \mathcal{N}(\theta, \sigma^2)\). The Bayes-optimal posterior uses both:

\[ p(\theta\mid x_1, x_2) = \mathcal{N}\!\Bigl(\tfrac{x_1+x_2}{2},\, \tfrac{\sigma^2}{2}\Bigr) \]

Compare two flawed posteriors, both wider than optimal:

  • Overdispersed: correct mean, scale \(s>1\)
  • Lossy: uses only \(x_1\), \(q = \mathcal{N}(x_1, \sigma^2)\)
SBC catches overdispersion. The lossy case looks calibrated: a valid posterior, just based on less information (Type B).

Model-based ranks: recovering Type B sensitivity

If we can evaluate the simulator's likelihood \(p(x\mid\theta)\) or score \(\nabla_\theta \log p(x\mid\theta)\), use those as ordering functions instead.

Likelihood ranks \[ F_x(\theta^*) = \tfrac{1}{L}\sum_i \mathbb{1}\bigl(p(x\mid\theta_i) \ge p(x\mid\theta^*)\bigr) \] Sensitive to Type B: the ranking depends on the full \(x\), not on the learned summary.
True HPDR coverage \[ F_x(\theta^*) = \tfrac{1}{L}\sum_i \mathbb{1}\bigl(\pi(\theta_i\mid x) \ge \pi(\theta^*\mid x)\bigr) \] with \(\pi \propto p(x\mid\theta)p(\theta)\). Probes the true HPDRs, not the learned ones.

Same rank machinery, ordering function changed. In the lossy-summary case above, the likelihood-rank histogram would no longer be flat.

Requires a tractable model. When you have one, use it — it is the only rank test that sees information loss.

Failure 3: HPDR coverage is blind to shape

HPDR tests check only the mass inside super-level sets. Distorted shapes that preserve mass-vs-level go undetected.

Construct \(q(\theta) = g(|\theta - \theta_0|)\) so every symmetric interval matches the true posterior mass. The pp-plot stays perfectly diagonal, even when the shape is bimodal.

Mitigation: combine ordering functions (HPDR, central interval, lower/upper limits, SBC). Each closes a different blind direction.

Left: true posterior (dashed) vs distorted \(q\). Right: pp-plot stays on the diagonal regardless of distortion.

Beyond marginals: probing the joint

SBC applied separately to each component \(\theta_i\) tests only the marginals. It can pass while the joint posterior is wrong (correlations distorted).

Cramér–Wold theorem. A distribution is determined by all its one-dimensional projections. \[ q_\phi(\mathbf{w}^\top\boldsymbol\theta \mid x) = p(\mathbf{w}^\top\boldsymbol\theta \mid x) \quad \text{for all } \mathbf{w} \in \mathbb{R}^N \;\;\Rightarrow\;\; q_\phi = p \]
  • Practical recipe: SBC on a random ensemble of projections \(\mathbf{w}^\top\boldsymbol\theta\).
  • Cheap (rank-test samples reused), and much more revealing than per-coordinate SBC.
  • This is the bridge from SBC to the generalized rank framework: arbitrary ordering functions \(f(\boldsymbol\theta, x)\).

Generalized rank diagnostics: one framework

All forward–backward tests share a structure: choose an ordering function \(f(\boldsymbol\theta, x)\) and rank \(\theta^*\) among the \(\theta_{1:L}\) under that ordering.

MethodRanks byType BOrdering \(f(\boldsymbol\theta, x)\)
SBCparameter (\(d\) tests)No\(\theta_i\) for \(i=1,\ldots,d\)
HPDR coveragelearned posteriorNo\(q_\phi(\boldsymbol\theta\mid x)\)
TARPdistance to referenceMaybe\(\|\boldsymbol\theta - \boldsymbol\theta'\|^2\), \(\boldsymbol\theta' \sim p(\boldsymbol\theta)\)
True HPDRtrue posteriorYes\(\pi(\boldsymbol\theta\mid x) \propto p(x\mid\boldsymbol\theta)p(\boldsymbol\theta)\)
Likelihood rankslikelihoodYes\(p(x\mid\boldsymbol\theta)\)
Score ranksscore (\(d\) tests)Yes\(\nabla_{\theta_i} \log p(x\mid\boldsymbol\theta)\)
Completeness. If the rank statistic is uniform for every ordering \(f(\boldsymbol\theta, x)\), then \(q_\phi = p\) almost everywhere (Lemos et al. 2023). Any finite set of orderings is necessary but not sufficient.

Building complex systems

Any complex system, really.

Building Systems That Work

"A complex system that works is invariably found to have evolved from a simple system that worked. A complex system designed from scratch never works and cannot be patched up to make it work. You have to start over with a working simple system."
— John Gall, The Systems Bible, 1977

A clockmaker does not assemble every gear and escapement at once and expect it to tick. Start with the gear train — verify it rotates. Add the escapement, check regulation. Add the pendulum, verify synchronization. At each step, the mechanism works.


SBI pipelines (and anything deep learning related) obey the same law. The diagnostics ahead are designed to pinpoint failures in nearly-working systems. Applied to a completely broken pipeline they generate noise; applied to an incrementally refined system they can tell you exactly what broke.

Building Systems That Work

The Iterative Refinement Workflow
  1. Start minimal — toy data, known sufficient statistics, simple network
  2. Validate thoroughly — SBC, coverage, posterior predictive checks
  3. Add one component — learned summaries or realistic noise or more parameters; not all at once
  4. Re-validate — pass → proceed; fail → you know what broke
  5. Iterate — repeat until full problem complexity
Rules of thumb: trust validated components when progressing — question everything when stuck — if you can't describe the failure in 5 sentences, the example isn't minimal enough.

Key Takeaways

  • Rank Diagnostics: the uniformity of ranks under \(q_\phi = p\) turns the forward–backward principle into a sharp, distribution-free test. SBC histograms and HPDR pp-plots are two readings of the same machinery.
  • Failure Modes: SBC averages over \(x\) and can hide spatially varying bias; SBC and HPDR are blind to information loss; HPDR is blind to shape distortions that preserve mass-vs-level.
  • Generalization: all of these are special cases of generalized rank diagnostics with different ordering functions. Model-based ranks (likelihood, score, true HPDR) recover Type B sensitivity when the model is tractable.
  • The conceptual jump: no single diagnostic is sufficient. Comprehensive validation means stacking orderings, projections, and stratifications, each closing a different blind spot.