Skip to content

Probabilistic inference, Monte Carlo, and posterior computation · Probabilistic computation and inference foundations · INF020 · Training method

Neural posterior estimation and sequential NPE (SNPE)

Trains a conditional density estimator to approximate parameter posteriors from simulated observations when the simulator likelihood is intractable.

Partly runs in CodeSplash (Experimental) Component demo, not a trained model CodeSplash's run and the reference's record

In CodeSplash

A CodeSplash method covers part of this entry; some options or variants aren't built yet. The methods that run it are Experimental.

Do it in CodeSplash Run it on your own data. It's in the Experimental lane in the app.

Availability
Partly runs in CodeSplash (Experimental)
Code example
Component demo, not a trained model
Who ran it
CodeSplash's run and the reference's record
CodeSplash's run
Verified in Python and R through simulate.calibrate. The reference's example ran in Python and R here, and they agree.
The reference's record
Python: Ran in the reference's review. The report doesn't record which version of the code ran. R: Reviewed but not run: R wasn't available to the reference's authors.

Purpose

Trains a conditional density estimator to approximate parameter posteriors from simulated observations when the simulator likelihood is intractable. Sequential versions concentrate simulations near a particular observation while correcting the changing proposal; a reusable amortized estimator has a different coverage scope.

Question / hypotheses

Given simulator-generated parameter/observation pairs, can a flexible conditional density approximate p(theta|x), including uncertainty and multiple solutions?

Architecture / computational structure

Sample parameters, simulate data, encode observations, and fit a normalized conditional density such as a mixture density network or conditional flow. Sequential inference updates the simulation proposal around x_observed. Keep proposal provenance for every round so correction matches the actual simulation distribution.

Assumptions

The simulator and prior must define a meaningful inferential target, simulated observations must cover the observed case, and the conditional density must have adequate support/capacity. Sequential proposals require the correction appropriate to the chosen SNPE variant. Inference can be precise yet scientifically wrong under simulator misspecification.

Formula / computational definition

LNPE=−Ep(θ)p(x∣θ)log⁡qϕ(θ∣x);LSNPE−B=−Er(θ)p(x∣θ)[p(θ)r(θ)log⁡qϕ(θ∣x)].\mathcal L_{NPE}=-E_{p(\theta)p(x\mid\theta)}\log q_\phi(\theta\mid x);\quad \mathcal L_{SNPE-B}=-E_{r(\theta)p(x\mid\theta)}\left[\frac{p(\theta)}{r(\theta)}\log q_\phi(\theta\mid x)\right].

Parameters and notation

Learned phi includes conditional-density and optional observation-embedding weights. Theta is the unknown simulator parameter; it is not a classifier label. Hyperparameters include simulator budget, prior/proposal, rounds, flow/mixture architecture, optimizer, batch size, learning rate, and stopping rule. SNPE-C/APT uses different proposal correction, not the displayed SNPE-B loss.

Fitting, fine-tuning, and adaptation

An amortized network can be fine-tuned for a local observation with additional simulations and valid proposal correction. Freeze an observation encoder only after checking that its summaries preserve required information; otherwise unfreeze it and use a cautiously smaller learning rate relative to a newly initialized density head. Reassess calibration after adaptation.

How to use and calibrate

Hold out simulated pairs, check posterior predictive simulations, perform simulation-based calibration or appropriate local coverage checks, and compare to known posteriors on a reduced model. Plot joint posterior structure and recovery of simulated parameters. Assess robustness to prior, summaries, and simulator assumptions.

Example application

Infer latent physical or neuronal parameters from a simulator-generated time series when evaluating p(x|theta) is difficult but simulation at chosen theta is feasible.

Interpretation / pitfalls

Watch out

Sequential training without proposal correction can learn the proposal posterior rather than the intended prior posterior. Weight ratios can have severe variance, motivating alternative SNPE variants. Libraries may self-normalize minibatch weights, changing scaling and finite-sample behavior from the displayed expectation. One local estimator is not automatically amortized over all future observations.

Python and R code examples

Scope: One importance-weighted Gaussian conditional-head loss and gradient step on supplied simulated pairs; not a neural flow, simulator, or complete sequential inference run.

Implementation route: Both examples compute the head component natively in NumPy/base R. Python sbi implements NPE variants and diagnostics. Full sbi workflows from R require an explicit reticulate bridge or a custom R neural-density implementation; this entry does not claim a native equivalent full SNPE package.

Dependencies and validation: Python — executed successfully on the supplied synthetic inputs; imports identify NumPy/SciPy dependencies. R — base R, reviewed but not executed because Rscript is unavailable. Random-number streams differ between languages; matching seed integers do not imply matching draws.

Python and R

The reference's own programs, shown as written. A component demo shows one central piece of the method; it doesn't train a full model.

Python

Component demo, not a trained model — One importance-weighted Gaussian conditional-head loss and gradient step on supplied simulated pairs; not a neural flow, simulator, or complete sequential inference run. · needs numpy

import numpy as np
theta=np.array([0.,.5,1.]); x=np.array([.1,.6,.8]); weight=np.array([1.,.5,2.])
a=.7; b=0.; sigma=.5; mu=a*x+b
nll=.5*((theta-mu)/sigma)**2+np.log(sigma*np.sqrt(2*np.pi))
loss=np.mean(weight*nll)
grad_a=np.mean(weight*(mu-theta)*x/sigma**2); grad_b=np.mean(weight*(mu-theta)/sigma**2)
print('weighted loss',loss,'new head',a-.01*grad_a,b-.01*grad_b)
# Weights are supplied prior/proposal ratios, not normalized class weights.
R

Component demo, not a trained model — One importance-weighted Gaussian conditional-head loss and gradient step on supplied simulated pairs; not a neural flow, simulator, or complete sequential inference run.

theta <- c(0,.5,1); x <- c(.1,.6,.8); weight <- c(1,.5,2)
a <- .7; b <- 0; sigma <- .5; mu <- a*x+b
nll <- .5*((theta-mu)/sigma)^2+log(sigma*sqrt(2*pi)); loss <- mean(weight*nll)
grad.a <- mean(weight*(mu-theta)*x/sigma^2); grad.b <- mean(weight*(mu-theta)/sigma^2)
print(c(weighted.loss=loss,new.a=a-.01*grad.a,new.b=b-.01*grad.b))
# Supplied prior/proposal ratios; this is not full SNPE training.

Sources

From the CodeSplash reference, entry INF020.