Normalizing Flows as Conditional Density Estimators
Summary
A normalizing flow defines a flexible density by pushing a simple base variable through an invertible, differentiable map with a cheap Jacobian determinant, so that is exact, evaluable, and sampleable. Making the map depend on side information turns it into a conditional density estimator: for NPE or for NLE. Papamakarios et al. call flows “a natural fit for likelihood-free inference.” Two facts from the review govern their use in SBI: fitting from samples is forward-KL minimization, i.e. maximum likelihood, and masked autoregressive flows are fast in one direction and times slower in the other, which dictates which architecture suits which SBI target.
Overview
Neural SBI needs a density model that (a) conditions on a vector, (b) can represent multimodal, skewed, truncated shapes, (c) gives exact log-densities for a maximum-likelihood loss, and (d) can be sampled. Mixture density networks (the original choice of Papamakarios & Murray 2016) satisfy these but scale poorly in the number of components. Flows satisfy all four by construction and have displaced them; the SBI benchmark uses Masked Autoregressive Flows and Neural Spline Flows throughout.
The name: “flow” is the trajectory samples follow through the chain ; “normalizing” is the inverse direction, which takes data and “normalizes” it into the base density, usually a standard normal (Sec. 2.1). The review also makes a terminological point worth keeping: is not a latent variable, because “upon observing , the corresponding is uniquely determined.”
Main Content
Flow-based model (Sec. 2.1, Eqs. 1-3) ^def-flow
Let and with , where is a diffeomorphism (invertible, with and differentiable; must also be -dimensional). Then
is the local volume change: where expands a neighbourhood the density falls, where it contracts the density rises.
Composition (Sec. 2.1, Eqs. 5-6) ^thm-composition
Diffeomorphisms are closed under composition, with
So log-determinants add across layers, and a deep flow’s log-density is a sum of cheap per-layer terms.
Two operations, two costs. “Sampling from the model requires the ability to sample from and to compute the forward transformation . Evaluating the model’s density requires computing the inverse transformation and its Jacobian determinant.” “The application will dictate which of these operations need to be implemented and how efficient they need to be.”
Universality (Sec. 2.2) ^thm-universality
If everywhere and the conditional CDFs are differentiable, the map is a diffeomorphism with triangular Jacobian taking to the uniform distribution on . Composing one such map with the inverse of another shows that a flow can turn any well-behaved base into any well-behaved target. Autoregressive flows implement this construction directly, so they “are universal approximators … provided the transformer and the conditioner are flexible enough”; the authors caution that this “is just a statement of representational power and makes no guarantees about the flow’s behavior in practice.”
Training: forward KL is what SBI uses
Forward KL = maximum likelihood (Sec. 2.3.1, Eqs. 13-14) ^def-forward-kl
With samples from the target but no ability to evaluate it,
This needs only , its Jacobian determinant, and : “we can train a flow model with maximum likelihood even if we are not able to compute or sample from .”
The reverse KL (Sec. 2.3.2, Eqs. 17-19) is the mirror image: it needs an evaluable (possibly unnormalized) target and a sampleable flow, and is the objective of variational inference with flows. This is the cleanest statement of the difference between VI and neural SBI: VI has the density and lacks samples, so it uses reverse KL; SBI has samples from the joint and lacks the density, so it uses forward KL. (A standard observation that the review itself does not make: the forward KL penalizes missing mass and so tends to over-cover, whereas the reverse KL tends to lock onto one mode; for a posterior approximation, over-dispersion is usually the less harmful error.)
Autoregressive flows: transformer plus conditioner
Autoregressive flow (Sec. 3.1, Eqs. 29-32) ^def-ar-flow
where the transformer is strictly monotonic in and the conditioner may be any function of the preceding dimensions (it “does not need to be a bijection”). The Jacobian is lower triangular, so
computable in .
Transformers. Affine: with , giving (Eqs. 33-34). Simple, but a single affine autoregressive layer applied to a Gaussian yields conditionals that “will necessarily be Gaussian”, and “it’s unknown whether affine autoregressive flows with multiple layers are universal approximators or not.” Monotonic splines: segments with learned knots and slopes; rational-quadratic splines (Durkan et al. 2019, the Neural Spline Flow) “are as fast to invert as to evaluate, while maintaining exact analytical invertibility”, located by binary search and arbitrarily flexible as grows.
Conditioners.
- Masked (MADE-style): one feed-forward network whose weight matrices are multiplied by binary masks so output sees only . All come out of one pass, so the forward direction is parallel; but inversion must proceed dimension by dimension and is “about times more expensive.” This is the conditioner of MAF and IAF, which differ only in which direction is the fast one.
- Coupling layers (NICE, Real NVP, Glow): split , pass the first part through unchanged, and transform the second elementwise with parameters . “Equally fast to evaluate or invert”, but “the efficiency of coupling layers comes at the cost of reduced expressive power”: a single coupling layer is not universal, so layers are stacked with permutations in between.
Which direction must be fast in SBI?
| Use | Hot operation | Suitable flow |
|---|---|---|
| NLE: inside an MCMC loop | density evaluation, thousands of times; sampling only for the goodness-of-fit diagnostic | MAF (fast evaluation, slow sampling), as chosen by the SNL paper |
| NPE: draw posterior samples per observation and evaluate in training | both | coupling or spline flows; since is small (tens at most), the penalty of MAF is also tolerable |
Conditioning
In the SBI setting the conditioning vector (the data for NPE, the parameters for NLE) is fed as an extra input to every conditioner network, so the map is a different diffeomorphism for each context while remaining invertible in its main argument. Sec. 6.2.4 states the recipe: generate from , then “fit a flow-based model conditioned on … in order to approximate the posterior”, or “fit a flow-based model conditioned on in order to approximate the intractable likelihood.” For high-dimensional the context is first compressed by an embedding network trained jointly with the flow.
Practicalities (Sec. 3.4). Insert batch normalization between layers (it is itself an elementwise affine bijection with a trivial log-determinant, used in the SNL defaults) or activation normalization when batches are small. Because a flow is a diffeomorphism of with a base density supported everywhere, a flow over a bounded parameter places some mass outside a uniform prior’s support unless the parameter is first mapped to an unbounded space. The SBI benchmark reports the related finding for the MCMC-based methods: “(S)NLE and (S)NRE improved by transforming parameters to be unbounded: Without transformations, runs on some tasks can get stuck during MCMC sampling (e.g., Lotka-Volterra)” (Lueckmann et al. 2021, finding 6).
Examples
A one-layer conditional affine flow is heteroscedastic regression. Let , base , and with neural networks of the data. Then
a Gaussian posterior approximation whose mean and scale are learned functions of , exactly the single-component MDN of Papamakarios & Murray’s Algorithm 1. Stacking such layers with autoregressive conditioners and swapping the affine for a rational-quadratic spline gives the Neural Spline Flow used for (S)NPE in the benchmark: “five flow transforms, two residual blocks of 50 hidden units each, ReLU non-linearity, and 10 bins” (Lueckmann et al. 2021, App. A.5). That benchmark found that “higher capacity density estimators were beneficial for posterior but not likelihood estimation.”
# conditional affine autoregressive layer (MAF-style), density direction
def log_prob(theta, context):
u, logdet = theta, 0.0
for layer in layers: # each layer: masked MLP -> (shift, log_scale)
shift, log_scale = layer(u, context) # h_i depends on u_{<i} and on the context only
u = (u - shift) * torch.exp(-log_scale) # inverse transform, parallel over dimensions
logdet = logdet - log_scale.sum(-1)
return standard_normal.log_prob(u).sum(-1) + logdetConnections
- Neural Posterior Estimation (NPE) - flows as ; SNPE-C was designed specifically so flows could be used with proposals.
- Neural Likelihood Estimation and Sequential Neural Likelihood - conditional MAF as .
- Neural Ratio Estimation - the alternative that avoids density estimation and the invertibility constraint altogether.
- Variational Inference and Pathfinder - flows as variational families under the reverse KL; same architecture, opposite divergence, opposite requirement (density vs samples).
- Variational Posterior Estimator (Barber-Agakov) and Implicit Likelihood Estimator - amortized and in experimental design, for which a conditional flow is the natural high-capacity family.
- Synthetic Likelihood Construction - the zero-layer case: a Gaussian with -dependent mean and covariance.
See Also
- Amortized vs Sequential Inference - the conditional flow over all contexts is what amortization means operationally.
- The Typical Set and the Log Posterior Density - why density estimation in high dimensions is hard, and why NLE struggles with raw high-dimensional .
- Approximation Methods - the BDA3 family of distributional approximations that flows generalize.
- Simulation-Based and Amortized Inference - where “neural density estimators” are named in the Bayesian Workflow book.
- Normalizing Flows for Variational Inference — reverse-KL vs forward-KL training of flows