Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Mixture CDF as a learnable bijection

Train the marginal end to end by maximum likelihood

03 · Mixture CDF as a learnable bijection

Open In Colab

Notebooks 01 and 02 fit a marginal from quantiles, EM or a plug-in rule. A normalizing flow asks for something else: every layer must be differentiable in its parameters, so that a deep stack can be trained jointly by gradient descent. This notebook treats the mixture CDF as that kind of layer and trains it end to end by maximum likelihood.

Python implementation: CPython
Python version       : 3.13.12
IPython version      : 9.12.0

jax             : 0.12.1
numpy           : 2.4.3
matplotlib      : 3.11.2
flowjax         : 19.1.0
pipekit_train   : 0.3.4
gauss_flows_maps: 0.3.0

Compiler    : Clang 21.1.4 
OS          : Linux
Release     : 6.8.0-1044-azure
Machine     : x86_64
Processor   : x86_64
CPU cores   : 16
Architecture: 64bit

1. The maximum-likelihood objective

Take one coordinate x∈Rx \in \mathbb{R} and a marginal map z=fθ(x)z = f_\theta(x) with parameters θ\theta, which sends xx through a mixture CDF (2) and then the probit (3). With a standard-normal base, the change of variables (4) gives each point a log-density log⁡px(x;θ)\log p_x(x; \theta). Training maximises the mean log-density of NN samples x1,…,xNx_1, \ldots, x_N, which is the same as minimising the negative log-likelihood

L(θ)=−1N∑n=1Nlog⁡px(xn;θ)\mathcal{L}(\theta) = -\frac{1}{N} \sum_{n=1}^{N} \log p_x(x_n; \theta)

Both terms of (4) are closed form for a mixture CDF: fθ(x)f_\theta(x) is a sum of normal CDFs followed by Φ−1\Phi^{-1}, and its log-derivative is the marginal log-determinant (5). So L\mathcal{L} is an ordinary differentiable function of θ\theta, and jax.grad gives its gradient with no root find anywhere.

2. Unconstrained parameters

A mixture needs weights on the simplex and positive scales, but an optimiser like Adam moves parameters anywhere in R\mathbb{R}. MixtureGaussianCDF stores unconstrained logits αm∈R\alpha_m \in \mathbb{R} and raw scales ρm∈R\rho_m \in \mathbb{R}, one per component, and maps them to valid values on every call:

wm=exp⁡(αm)/∑j=1Mexp⁡(αj), and σm=0.005+log⁡(1+exp⁡(ρm))w_m = \exp(\alpha_m) / \sum_{j=1}^{M} \exp(\alpha_j), \text{ and } \sigma_m = 0.005 + \log(1 + \exp(\rho_m))

The softmax keeps the weights positive and summing to one, and the softplus with a floor of 0.005 keeps every scale away from zero, where the mixture density would collapse onto a point. The means μm\mu_m need no constraint. The parameters are then θ=(αm,μm,ρm)m=1M\theta = (\alpha_m, \mu_m, \rho_m)_{m=1}^{M}, and any gradient step on them leaves a valid mixture.

3. Training end to end

The data are 3000 draws from two well-separated Gaussians. We start from a deliberately rough fit: MixtureGaussianCDF.from_data places M=6M = 6 components at quantiles of every twentieth point, with equal weights. Wrapping the marginal as Transformed(StandardNormal((1,)), Invert(marginal)) makes it a FlowJax distribution whose log_prob runs the closed-form direction x↦zx \mapsto z, so negative_log_likelihood from gauss_flows_models.inference evaluates (1) on a batch.

The warm start is a mixture of M=6M = 6 components placed at quantiles of every twentieth point, wrapped as a flow whose log_prob runs x↦zx \mapsto z.

We evaluate L\mathcal{L} at the warm start and check that its gradient in θ\theta, which runs through the forward map only, is finite.

initial NLL: 1.7146
gradient leaves: 3, all finite: True

pipekit-train runs the loop. Its task is the batch negative log-likelihood, a fifth of the points is held out, and training stops once the held-out loss has not improved for ten epochs, keeping the best epoch, whose parameters we call θ^\hat{\theta}.

The rows are split at random into Nt=2400N_t = 2400 training rows and Nv=600N_v = 600 held-out rows, and an epoch is ⌊Nt/Nb⌋\lfloor N_t / N_b \rfloor steps of Nb=256N_b = 256 points.

The loop trains every array leaf of the flow except those wrapped in paramax.NonTrainable, keeps the per-step losses, and stops early on the held-out loss.

The history holds the loss of every step; we average it per epoch to compare with the held-out loss, which is evaluated once per epoch.

epochs run: 54

We compare four negative log-likelihoods on all 3000 points: the warm start, the trained flow, an EM fit of the same six components, and the true two-Gaussian density p(x)=12N(x;−2,0.52)+12N(x;1.5,0.82)p(x) = \frac{1}{2} \mathcal{N}(x; -2, 0.5^2) + \frac{1}{2} \mathcal{N}(x; 1.5, 0.8^2).

NLL on all 3000 points,      initial: 1.7146
NLL on all 3000 points,      trained: 1.6372
NLL on all 3000 points,           EM: 1.6397
NLL on all 3000 points, true density: 1.6379

Gradient descent closes the gap from the warm start (1.7146 nats) and lands on the EM fit with the same six components: 1.6372 against EM’s 1.6397. Both are within a few thousandths of a nat of the true density’s 1.6379. The trained mixture even dips slightly below it, because it was fitted to these very points; the true density is the floor only on average over fresh draws. The curves below show the descent, then the learned density and the latent it produces.

<Figure size 1200x450 with 2 Axes>

Figure 1:Left: the mean negative log-likelihood (1) per epoch, on the training points and on the held-out fifth, against the EM fit and the true density (dashed). Right: the density of the trained mixture-CDF marginal over a histogram of the data.

The density is not the only output. The same trained layer is a Gaussianizing map: pushing the data through fθf_\theta gives a latent that should be standard normal, which is what a deeper flow relies on when it stacks this layer between rotations.

latent initial: mean +0.019, std 1.132
latent trained: mean -0.016, std 1.001
<Figure size 1200x450 with 2 Axes>

Figure 2:The data pushed through the initial and the trained marginal, against the standard-normal density (dashed). Training moves the latent onto N(0,1)\mathcal{N}(0, 1), which is what the layer is for inside a deep flow.

The latent standard deviation falls from 1.132 at the warm start to 1.001 after training. The fit itself is the EM fit, so the gain is not accuracy. It is that the layer is differentiable in θ\theta: it drops into a deep flow and trains jointly with every rotation and coupling around it, instead of being fit on its own and frozen.

Every gradient here came from the closed-form forward map. The moment we train through the inverse instead, for example with a sampling or variational objective, the gradient has to pass through a root find, and notebook 04 answers how.

Recap

ConceptEquationIn code
log-density of a point(4), (5)Transformed(StandardNormal, Invert(marginal)).log_prob
maximum-likelihood objective(1)negative_log_likelihood
unconstrained parameters(2)MixtureGaussianCDF leaves
end-to-end traininggradient descent in θ\thetapipekit_train.TrainingLoop
forward gradientclosed form, all maximum likelihood needseqx.filter_grad
inverse gradientunrolling, one step or the adjointnotebook 04

Next. Training leaned on the closed-form forward map. The inverse is a root find, and 04 · Inversion strategies covers it end to end: bisection against Newton, the safeguarded hybrid, how to differentiate the inverse and why unrolling can fail, and how to vectorise it across a batch.