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 bijector

Train the marginal end-to-end by maximum likelihood

03 — Mixture-CDF as a learnable bijector

Notebooks 01–02 fit marginals by quantiles, EM, or plug-in rules. But the whole point of a normalizing flow is that every layer is differentiable in its parameters, so a deep stack can be trained jointly by gradient descent. This notebook treats the mixture-CDF as that learnable layer and trains it end-to-end by maximum likelihood.

What you will see

1. The maximum-likelihood objective

A marginal bijector z=Tθ(x)z = T_\theta(x) with base N(0,1)\mathcal{N}(0,1) assigns each point the log-density (change of variables, Part 0 notebook 00)

log⁡pθ(x)=log⁡ϕ(Tθ(x))+log⁡∣Tθ′(x)∣,\log p_\theta(x) = \log \phi\big(T_\theta(x)\big) + \log\big|T_\theta'(x)\big|,

and training maximises the average log⁡pθ\log p_\theta — equivalently minimises the negative log-likelihood L(θ)=−Ex[log⁡pθ(x)]\mathcal{L}(\theta) = -\mathbb{E}_x[\log p_\theta(x)]. For gauss_flows.MixtureGaussianCDF the parameters θ are the (unconstrained) mixture weights, means, and scales, and both terms above are returned by transform_and_log_det — so L\mathcal{L} is a plain differentiable function of θ that optax can descend.

NLL: 1.7441 (init) -> 1.6490 (trained)
<Figure size 1100x420 with 2 Axes>

The NLL falls and the learned density (red) locks onto the bimodal data — the same fit EM gave in notebook 01, but reached by gradient descent. That difference is the whole point: because the layer is differentiable in θ, it drops into a deep flow and trains jointly with every rotation and coupling around it, instead of being fit in isolation.

Training used only the forward gradient (jax.grad of transform_and_log_det, all closed-form). The moment you instead train through the layer’s inverse — e.g. a sampling- or variational objective — you hit the root-find gradient question, which notebook 04 answers.

Recap

piecetakeawayin code
log-densitylog⁡ϕ(Tθx)+log⁡∣Tθ′∣\log\phi(T_\theta x) + \log\lvert T_\theta'\rverttransform_and_log_det
MLE objectiveminimise NLL of the Gaussianized outputnll
end-to-end traininggradient descent in θ; stacks into deep flowsoptax + eqx.filter_value_and_grad
forward gradientclosed form — all MLE needsplain jax.grad
inverse gradientunroll / one-step / adjointnotebook 04

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