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.

Change of variables from scratch

The one formula the whole curriculum is built on, in 1-D, 2-D and D-D

00 · Change of variables from scratch

Open In Colab

Every Gaussianization method in this curriculum (RBIG, coupling flows, FFJORD, spline flows) is a way of building an invertible map ff that turns messy data into a standard Gaussian. The one fact that makes such a map useful for density estimation is the change-of-variables formula Rezende & Mohamed, 2015Papamakarios et al., 2021: it says how a probability density changes when we push it through ff. Get this formula and its log-determinant bookkeeping right and the rest of the course is engineering.

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

jax             : 0.12.1
numpy           : 2.4.3
scipy           : 1.17.1
matplotlib      : 3.11.2
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 1-D formula

Let x∈Rx \in \mathbb{R} be a continuous random variable with density pxp_x, and let f:R→Rf: \mathbb{R} \to \mathbb{R} be smooth and strictly monotone, hence invertible. The latent variable is z=f(x)∈Rz = f(x) \in \mathbb{R}, with density pzp_z.

Derivation from conservation of mass

Probability mass is conserved over an infinitesimal interval, so px(x)∣dx∣=pz(z)∣dz∣p_x(x) \vert \mathrm{d}x \vert = p_z(z) \vert \mathrm{d}z \vert. Since dz=f′(x)dx\mathrm{d}z = f'(x) \mathrm{d}x, dividing by ∣dx∣\vert \mathrm{d}x \vert gives (1). Read the other way, the latent density is pz(z)=px(x)/∣f′(x)∣p_z(z) = p_x(x) / \vert f'(x) \vert at the point x=f−1(z)x = f^{-1}(z).

The factor ∣f′(x)∣\vert f'(x) \vert is the local stretch or squeeze of the line under ff, and it is what keeps the transformed density normalised.

Worked example. Take x=ewx = e^w with w∼N(0,1)w \sim \mathcal{N}(0, 1), so xx is log-normal, and the map f(x)=log⁡xf(x) = \log x. Then z=f(x)=wz = f(x) = w is standard normal by construction, so we know the answer and can check the formula. Here f′(x)=1/xf'(x) = 1/x, so (1) predicts pz(z)=px(ez)ezp_z(z) = p_x(e^z) e^z. Figure 1 compares that prediction with a histogram of zz.

The prediction evaluates (1) at the image of each evaluation point.

<Figure size 1200x450 with 2 Axes>

Figure 1:Left: samples of the log-normal xx and its density pxp_x. Right: the histogram of z=f(x)z = f(x), the prediction of the change-of-variables formula and the standard-normal density.

The red prediction lands on both the histogram of zz and the analytic N(0,1)\mathcal{N}(0, 1) curve. The factor ∣f′(x)∣\vert f'(x) \vert does real work: drop it and the prediction no longer integrates to one.

2. The DxD_x-dimensional formula

In DxD_x dimensions the input is x∈RDx\vect{x} \in \mathbb{R}^{D_x}, the map is f:RDx→RDxf: \mathbb{R}^{D_x} \to \mathbb{R}^{D_x} and the latent is z=f(x)∈RDx\vect{z} = f(\vect{x}) \in \mathbb{R}^{D_x}. The derivative becomes the Jacobian matrix Jf(x)∈RDx×Dx\mat{J}_f(\vect{x}) \in \mathbb{R}^{D_x \times D_x}, whose rows are the outputs,

[Jf(x)]ij=∂fi(x)/∂xj[\mat{J}_f(\vect{x})]_{ij} = \partial f_i(\vect{x}) / \partial x_j

and the absolute value of its determinant takes the place of ∣f′(x)∣\vert f'(x) \vert.

The scalar ∣det⁡Jf(x)∣\vert \det \mat{J}_f(\vect{x}) \vert is the local volume change: the factor by which ff inflates or shrinks a small box of probability mass around x\vect{x}.

Linear case. For an affine map f(x)=Ax+bf(\vect{x}) = \mat{A} \vect{x} + \vect{b}, with A∈R2×2\mat{A} \in \mathbb{R}^{2 \times 2} and b∈R2\vect{b} \in \mathbb{R}^{2}, the Jacobian is constant, Jf=A\mat{J}_f = \mat{A}, so ∣det⁡Jf∣=∣det⁡A∣\vert \det \mat{J}_f \vert = \vert \det \mat{A} \vert everywhere. We can read the answer off and check it against autodiff.

At one point x0\vect{x}_0, autodiff gives the Jacobian, and its determinant should match det⁡A\det \mat{A}.

autodiff Jacobian J_f(x0):
 [[ 1.3  0.7]
 [-0.4  0.9]]
|det J_f| (autodiff)     = 1.450000
|det A|   (by hand)      = 1.450000
log|det J_f| (autodiff)  = 0.371564

The determinant is an area change. Lay a grid over the plane, push every cell through ff, and colour each warped cell by ∣det⁡Jf∣\vert \det \mat{J}_f \vert at its centre (Figure 2). For the linear map every cell scales by the same ∣det⁡A∣=1.45\vert \det \mat{A} \vert = 1.45. For the nonlinear map f(x)=x+0.5[sin⁡(1.5x2),sin⁡(1.5x1)]⊤f(\vect{x}) = \vect{x} + 0.5 [\sin(1.5 x_2), \sin(1.5 x_1)]^\top the determinant varies with position, which is why (3) evaluates ∣det⁡Jf(x)∣\vert \det \mat{J}_f(\vect{x}) \vert afresh at every point.

<Figure size 1200x500 with 3 Axes>

Figure 2:A 14 × 14 grid pushed through the linear map (left) and the nonlinear map (right), each cell coloured by ∣det⁡Jf∣\vert \det \mathbf{J}_f \vert at its centre.

On the left the grid becomes identical parallelograms, all scaled by ∣det⁡A∣=1.45\vert \det \mat{A} \vert = 1.45. On the right the cells warp unevenly: expanded regions are larger and brighter, compressed ones smaller and darker. (3) handles both cases the same way because it reads ∣det⁡Jf(x)∣\vert \det \mat{J}_f(\vect{x}) \vert locally.

3. Always work in log space

Densities span many orders of magnitude, and the determinant of a Dx×DxD_x \times D_x matrix is a product of DxD_x numbers, so both overflow or underflow quickly. Flows therefore never form ∣det⁡Jf∣\vert \det \mat{J}_f \vert; they add its logarithm.

jax.numpy.linalg.slogdet returns log⁡∣det⁡∣\log \vert \det \vert stably, with the sign kept apart. We check (4) on a nonlinear map in Dx=5D_x = 5 dimensions,

f(x)=Mtanh⁡(0.7x)+0.5xf(\vect{x}) = \mat{M} \tanh(0.7 \vect{x}) + 0.5 \vect{x}

where M∈R5×5\mat{M} \in \mathbb{R}^{5 \times 5} is a fixed, well-conditioned mixing matrix and tanh⁡\tanh acts elementwise. The Jacobian comes from autodiff, so the check is fully generic.

The log density adds the base log density at the image to the log-determinant.

We evaluate both terms at one standard-normal point.

log p_z(f(x))   = -7.987257
log|det J_f(x)| = -0.126274
log p_x(x)      = -8.113531

The same function, batched with vmap, evaluates a whole sample at once.


batched over 4000 points, all finite: True

4. The bridge: a bijector reports this exact number

Every bijector in gauss_flows has transform_and_log_det(x), which returns (z,log⁡∣det⁡Jf(x)∣)(\vect{z}, \log \vert \det \mat{J}_f(\vect{x}) \vert). The second value is the log-determinant we have been computing. The library works it out analytically and cheaply, with no Dx×DxD_x \times D_x determinant, but it must agree with a brute-force autodiff Jacobian. We confirm that on MixtureGaussianCDF, the marginal bijector at the heart of Gaussianization.

gauss_flows log|det J_f| = 1.7966266902
autodiff    log|det J_f| = 1.7966266902
absolute difference      = 8.88e-16

The two agree to floating-point precision. The formula of sections 1 to 3 is what the package returns, so once you trust the formula you can trust the library, and stop computing Dx×DxD_x \times D_x determinants by hand: the analytic log-determinant is exact and far cheaper.

Recap

ConceptFormulaIn code
1-D change of variables(1)p_x / abs(jax.grad(f)(x))
DxD_x-D change of variables(3)slogdet(jax.jacfwd(f)(x))
log density(4)log_p_z + log_det
the library’s jobanalytic log⁡∣det⁡Jf∣\log \vert \det \mat{J}_f \vertbijector.transform_and_log_det(x)

Next. One bijector is rarely enough. The next notebook stacks maps, f=fK∘…∘f1f = f_K \circ \ldots \circ f_1, and shows that their log-determinants add, which is the rule that lets us build deep flows.

References
  1. Rezende, D. J., & Mohamed, S. (2015). Variational Inference with Normalizing Flows. International Conference on Machine Learning (ICML).
  2. Papamakarios, G., Nalisnick, E., Rezende, D. J., Mohamed, S., & Lakshminarayanan, B. (2021). Normalizing Flows for Probabilistic Modeling and Inference. Journal of Machine Learning Research, 22(57), 1–64.