Christoph Weniger, University of Amsterdam (GRAPPA)
Three building blocks: simulator, inference loss, embedding network.
Examples:
⚠ Failure mode: wrong model → wrong inference. Misspecification of the simulator propagates into the posterior, often invisibly.
The loss decides what we're solving:
Same simulator, same network, different loss ⇒ different statistical task.
Why we need it:
A raw MLP would have too many parameters and no inductive bias. A CNN (for images), Transformer (for sequences), or GNN (for catalogues) does the compression. Trained jointly with the inference head, so \(T_\phi\) learns features informative for the posterior, not for any predefined label. Architecture choices are the topic of Lec 4.
Minimising NLL on simulator pairs recovers the Bayesian posterior, in two idealisations and two proofs.
| NPE | NRE | NLE | |
|---|---|---|---|
| Models | \(q_\phi(\boldsymbol\theta\mid\mathbf{x})\) | \(\log r_\phi(\boldsymbol\theta, \mathbf{x})\) | \(q_\phi(\mathbf{x}\mid\boldsymbol\theta)\) |
| Loss | NLL | cross-entropy | NLL |
| Sampling | direct, single forward pass | MCMC on \(p(\boldsymbol\theta)\,e^{f_\phi}\) | MCMC on \(q_\phi\,p(\boldsymbol\theta)\) |
| Amortised | yes | yes (re-MCMC per obs) | partial (re-MCMC per obs) |
| High-D \(\boldsymbol\theta\) | flow head needed | scales via marginal targeting (TMNRE) | hard, \(\mathbf{x}\) usually higher-D |
| Production examples | DINGO (GW PE) | saqqara (LISA SGWB) | field models / cosmology |
In Lec 2 we trained \(q_\phi(\boldsymbol\theta\mid\mathbf{x})\) on a fixed dataset of pairs \((\boldsymbol\theta_n, \mathbf{x}_n)\) by minimising the NLL. Today the pairs come from a simulator, \(\boldsymbol\theta_n \sim p(\boldsymbol\theta)\), \(\mathbf{x}_n \sim p(\mathbf{x}\mid\boldsymbol\theta_n)\). Same loss, same network. This is neural posterior estimation (NPE).
The NLL loss is just maximum likelihood: maximise \(\prod_i q_\phi(\boldsymbol\theta^{(i)}\mid\mathbf{x}^{(i)})\), the joint probability of all observed parameters given their data, which is the same as minimising \(-\!\sum_i \log q_\phi(\boldsymbol\theta^{(i)}\mid\mathbf{x}^{(i)})\).
Claim: at the minimum of \(\mathcal{L}\), the learned density \(q_\phi(\boldsymbol\theta\mid\mathbf{x})\) is the Bayesian posterior.
Papamakarios & Murray 2016 (mixture-density); Lueckmann et al. 2017; Greenberg et al. 2019; review: Cranmer, Brehmer, Louppe (PNAS 2020).
The simulator pairs \(\mathcal{D} = \{(\boldsymbol\theta^{(i)}, \mathbf{x}^{(i)})\}_{i=1}^{N}\) define an empirical joint distribution:
\[ \hat p(\boldsymbol\theta, \mathbf{x}) \;=\; \frac{1}{N}\sum_{i=1}^{N}\, \delta(\boldsymbol\theta-\boldsymbol\theta^{(i)})\, \delta(\mathbf{x}-\mathbf{x}^{(i)}) \]
Which factorises into prior and simulator-likelihood:
\[ \hat p(\boldsymbol\theta, \mathbf{x}) \;=\; \hat p(\boldsymbol\theta)\,\hat p(\mathbf{x}\mid\boldsymbol\theta), \qquad \hat p(\boldsymbol\theta) = \frac{1}{N}\sum_i \delta(\boldsymbol\theta-\boldsymbol\theta^{(i)}), \qquad \hat p(\mathbf{x}\mid\boldsymbol\theta^{(i)}) \;=\; \delta(\mathbf{x}-\mathbf{x}^{(i)}) \]
In SBI the data-generating process is known by construction, it is the simulator:
(i) sample \(\boldsymbol\theta \sim p(\boldsymbol\theta)\) from the prior, (ii) run the forward model \(\mathbf{x} \sim p(\mathbf{x}\mid\boldsymbol\theta)\), (iii) store the pair.
In the \(N\to\infty\) idealisation, \(\hat p \to p(\boldsymbol\theta, \mathbf{x}) = p(\boldsymbol\theta)\,p(\mathbf{x}\mid\boldsymbol\theta)\), the delta-combs smooth into a continuous density. This limit is mathematical / conceptual; in practice we always work with finite samples.
Take two idealisations: (i) \(N\to\infty\), so \(\hat p \to p(\boldsymbol\theta, \mathbf{x}) = p(\boldsymbol\theta)\,p(\mathbf{x}\mid\boldsymbol\theta)\); (ii) the network is infinitely flexible, so \(q(\boldsymbol\theta\mid\mathbf{x})\) is any function with \(\int q(\boldsymbol\theta\mid\mathbf{x})\,d\boldsymbol\theta = 1\) for every \(\mathbf{x}\).
\[ \mathcal{L}[q] \;\longrightarrow\; -\!\iint p(\boldsymbol\theta, \mathbf{x})\, \log q(\boldsymbol\theta\mid\mathbf{x})\, d\boldsymbol\theta\, d\mathbf{x} \]
Functional derivative w.r.t. \(q(\boldsymbol\theta\mid\mathbf{x})\), under the constraint \(\int q(\boldsymbol\theta\mid\mathbf{x})\,d\boldsymbol\theta = 1\):
\[ \left.\frac{\delta \mathcal{L}[q]}{\delta q(\boldsymbol\theta\mid\mathbf{x})}\right|_{\int q(\boldsymbol\theta\mid\mathbf{x})\,d\boldsymbol\theta\,=\,1} \;=\; 0 \]
\[ \Longrightarrow\quad q^\star(\boldsymbol\theta\mid\mathbf{x}) \;=\; \frac{p(\boldsymbol\theta, \mathbf{x})}{\int p(\boldsymbol\theta', \mathbf{x})\,d\boldsymbol\theta'} \;=\; p(\boldsymbol\theta\mid\mathbf{x}) \]
The learned \(q_\phi(\boldsymbol\theta\mid\mathbf{x})\) is the Bayesian posterior implied by the simulator. A finite network just interpolates this idealised answer.
Starting from \(\mathcal{L}[q] = -\!\iint p(\boldsymbol\theta, \mathbf{x})\, \log q(\boldsymbol\theta\mid\mathbf{x})\, d\boldsymbol\theta\, d\mathbf{x}\), we want the minimiser \(q^\star\).
\[ -\frac{p(\boldsymbol\theta,\mathbf{x})}{q(\boldsymbol\theta\mid\mathbf{x})} + \lambda(\mathbf{x}) = 0 \]
\(\Rightarrow q^\star(\boldsymbol\theta\mid\mathbf{x}) \propto p(\boldsymbol\theta,\mathbf{x})\). Normalising over \(\boldsymbol\theta\) fixes \(\lambda(\mathbf{x}) = p(\mathbf{x})\) and gives
\[ q^\star(\boldsymbol\theta\mid\mathbf{x}) = p(\boldsymbol\theta\mid\mathbf{x}). \]
\[ \mathcal{L}[q] = \mathbb{E}_{\mathbf{x}}\!\big[\mathrm{KL}\!\left(p(\cdot\mid\mathbf{x})\,\|\,q(\cdot\mid\mathbf{x})\right)\big] + H(\boldsymbol\theta\mid\mathbf{x}) \]
The entropy term does not depend on \(q\). KL \(\ge 0\) with equality iff \(q = p(\cdot\mid\mathbf{x})\), so
\[ q^\star(\boldsymbol\theta\mid\mathbf{x}) = p(\boldsymbol\theta\mid\mathbf{x}). \]
Same answer, two lenses: (a) is mechanical (stationary point of a constrained functional); (b) is geometric (NLL = KL up to a \(q\)-independent constant, so minimising NLL = projecting \(p(\cdot\mid\mathbf{x})\) onto the model family under KL).
The simplest concrete inference head, multivariate \(\boldsymbol\mu_\phi(x)\) with input-dependent \(\sigma_\phi(x)\), and where it breaks.
Our first model baked in a single \(\sigma_\theta\). Make \(\sigma\) a function of \(x\); same network, two heads: \(\mu_\phi(x)\) and \(\log\sigma_\phi(x)\). The NLL trades off mean accuracy against calibrated width.
Toy: noise is small on the left, large on the right. The grey band is the learned \(\sigma(x)\), it tracks the true scale instead of averaging over \(x\).
Density family
\( q_\phi(\boldsymbol\theta\mid x) \;=\; \mathcal{N}\!\bigl(\boldsymbol\theta\,\big|\,\boldsymbol\mu_\phi(x), \boldsymbol\Sigma_\phi(x)\bigr) \)
Both mean and covariance are NN outputs, the network learns where to be confident.
NPE loss on simulator pairs \((\boldsymbol\theta_n, x_n)\):
\( \mathcal{L}(\phi) = -\tfrac{1}{N}\sum_n \log q_\phi(\boldsymbol\theta_n\mid x_n) \)
For diagonal \(\Sigma\): \(\;\tfrac{1}{2}\bigl((\theta-\mu)/\sigma\bigr)^2 + \log\sigma\). Constant \(\Sigma\) reduces to MSE, the linear-regression case from Lec 2.
// Train q_phi(theta|x) on simulator pairs (theta_n, x_n)
for step = 1, 2, ... :
sample mini-batch B of pairs
mu, log_sigma = f_phi(x)
L = mean( 0.5*((theta-mu)/sigma)^2 + log_sigma )
phi <- Adam(phi, grad_phi L)
Forward model: \(t_i = a + b\,x_i + \varepsilon\), 20-point grid, \(\varepsilon\sim\mathcal{N}(0,0.25^2)\). Priors \(a\sim\mathcal U[-1,1]\), \(b\sim\mathcal U[-2,2]\). Network: MLP \([20{\to}32{\to}16{\to}5]\), output \((\mu_a,\mu_b,\log\sigma_a,\log\sigma_b,\rho)\).
Left: a single observation \(\mathbf{x}\). Right: the learned posterior \(q_\phi(a,b\mid\mathbf{x})\) (orange ellipse) vs the true \((a^\star,b^\star)\) (green star). The same trained network handles any new \(\mathbf{x}\), amortised inference.