Change of Variables & Jacobian Determinants

The mathematical backbone of all flow models: the change of variables formula, Jacobian matrices, determinant computation tricks, and when exact likelihood is tractable.

Intermediate

Table of Contents

  1. Learning Objectives
  2. Notation
  3. Core Intuition
  4. The Change of Variables Formula
  5. The Jacobian Matrix
  6. Computing Determinants Efficiently
  7. Triangular Jacobians
  8. The Matrix Determinant Lemma
  9. Hutchinson's Trace Estimator
  10. Composition of Flows
  11. When Exact Likelihood Is Tractable
  12. Common Pitfalls
  13. Summary
  14. Exercises

Learning Objectives

  1. Derive the multivariate change of variables formula from first principles.
  2. Explain the geometric meaning of the Jacobian determinant (volume change).
  3. Identify architectures that yield efficient (triangular, low-rank) Jacobians.
  4. Apply the matrix determinant lemma to residual connections.
  5. Use Hutchinson's estimator for stochastic log-det computation.

Notation

  • f:RdRdf: \mathbb{R}^d \to \mathbb{R}^d — diffeomorphism (smooth, invertible)
  • Jf(x)=f/xRd×d\mathbf{J}_f(\mathbf{x}) = \partial f / \partial\mathbf{x} \in \mathbb{R}^{d \times d} — Jacobian matrix
  • detJf|\det\mathbf{J}_f| — absolute value of Jacobian determinant

Core Intuition

When we stretch, rotate, or warp space, probability density must change to keep total probability equal to 1. The Jacobian determinant measures exactly how much a transformation ff locally stretches or compresses volume. If ff doubles the volume at some point, the density must halve there. This fundamental principle connects invertible transformations to density estimation.

Change of Variables

p(z)p(x) = p(z)|det ∂z/∂x|x = 1.50·z · log|det J| = 0.41
Scale a
1.50
Source p(z)Target p(x)
Explore: log p(x) = log p(z) − log|det(∂f/∂z)|. Scaling stretches the density inversely. Normalizing flows accumulate log-determinants across layers.

The Change of Variables Formula

One dimension: If x=f(z)x = f(z) and ff is monotonic:

pX(x)=pZ(z)dzdx=pZ(f1(x))f1(x).(1)p_X(x) = p_Z(z) \cdot \left|\frac{dz}{dx}\right| = p_Z(f^{-1}(x)) \cdot |f'^{-1}(x)|. \tag{1}

Multiple dimensions: If x=f(z)\mathbf{x} = f(\mathbf{z}) where ff is a diffeomorphism:

pX(x)=pZ(f1(x))detJf1(x).(2)\boxed{p_X(\mathbf{x}) = p_Z(f^{-1}(\mathbf{x})) \cdot \left|\det\mathbf{J}_{f^{-1}}(\mathbf{x})\right|.} \tag{2}

Equivalently, in the "forward" direction:

pX(f(z))=pZ(z)detJf(z)1.(3)p_X(f(\mathbf{z})) = p_Z(\mathbf{z}) \cdot \left|\det\mathbf{J}_f(\mathbf{z})\right|^{-1}. \tag{3}

Proof sketch: Consider a small volume element dzd\mathbf{z} around z\mathbf{z}. Under ff, it maps to a volume element dx=detJfdzd\mathbf{x} = |\det\mathbf{J}_f| \cdot d\mathbf{z}. Since probability is conserved: pX(x)dx=pZ(z)dzp_X(\mathbf{x})d\mathbf{x} = p_Z(\mathbf{z})d\mathbf{z}.


The Jacobian Matrix

For f:RdRdf: \mathbb{R}^d \to \mathbb{R}^d:

Jf(x)=(f1x1f1xdfdx1fdxd).(4)\mathbf{J}_f(\mathbf{x}) = \begin{pmatrix} \frac{\partial f_1}{\partial x_1} & \cdots & \frac{\partial f_1}{\partial x_d} \\ \vdots & \ddots & \vdots \\ \frac{\partial f_d}{\partial x_1} & \cdots & \frac{\partial f_d}{\partial x_d} \end{pmatrix}. \tag{4}

Geometric interpretation:

  • detJ>1|\det\mathbf{J}| > 1: local volume expansion.
  • detJ<1|\det\mathbf{J}| < 1: local volume contraction.
  • detJ=1|\det\mathbf{J}| = 1: volume-preserving (like rotations).
  • detJ=0|\det\mathbf{J}| = 0: degenerate (not invertible at this point).

Computational cost: Naively O(d3)O(d^3) to compute the determinant. For d=4096d = 4096 (typical hidden dimension), this is prohibitive. Flow architectures are designed to make this cheaper.


Computing Determinants Efficiently

Strategy 1: Triangular matrices. det(T)=iTii\det(\mathbf{T}) = \prod_i T_{ii}. Cost: O(d)O(d).

Strategy 2: Block-triangular.

det(A0CD)=det(A)det(D).(5)\det\begin{pmatrix}\mathbf{A} & \mathbf{0} \\ \mathbf{C} & \mathbf{D}\end{pmatrix} = \det(\mathbf{A})\det(\mathbf{D}). \tag{5}

Strategy 3: Rank-1 updates (Matrix determinant lemma).

det(A+uvT)=(1+vTA1u)det(A).(6)\det(\mathbf{A} + \mathbf{uv}^T) = (1 + \mathbf{v}^T\mathbf{A}^{-1}\mathbf{u})\det(\mathbf{A}). \tag{6}

Strategy 4: Trace (for log-det of near-identity). If J=I+ϵA\mathbf{J} = \mathbf{I} + \epsilon\mathbf{A}:

logdet(I+ϵA)ϵtr(A)ϵ22tr(A2)+(7)\log\det(\mathbf{I} + \epsilon\mathbf{A}) \approx \epsilon\cdot\text{tr}(\mathbf{A}) - \frac{\epsilon^2}{2}\text{tr}(\mathbf{A}^2) + \ldots \tag{7}

Triangular Jacobians

Autoregressive transformations naturally yield triangular Jacobians:

yi=fi(x1,,xi)    yixj=0 for j>i.(8)y_i = f_i(x_1, \ldots, x_i) \implies \frac{\partial y_i}{\partial x_j} = 0 \text{ for } j > i. \tag{8}

The Jacobian is lower-triangular. Determinant = product of diagonal:

detJ=i=1dfixi.(9)\det\mathbf{J} = \prod_{i=1}^d \frac{\partial f_i}{\partial x_i}. \tag{9}

Examples: MAF, IAF, autoregressive neural spline flows.

Coupling layers (RealNVP/Glow): Split dimensions; Jacobian is block-triangular with identity blocks. Determinant = product of scale factors.


The Matrix Determinant Lemma

For residual connections f(x)=x+g(x)f(\mathbf{x}) = \mathbf{x} + g(\mathbf{x}):

Jf=I+Jg.(10)\mathbf{J}_f = \mathbf{I} + \mathbf{J}_g. \tag{10}

If Jg\mathbf{J}_g has low rank: Jg=UVT\mathbf{J}_g = \mathbf{UV}^T where U,VRd×r\mathbf{U}, \mathbf{V} \in \mathbb{R}^{d \times r}:

det(I+UVT)=det(Ir+VTU).(11)\det(\mathbf{I} + \mathbf{UV}^T) = \det(\mathbf{I}_r + \mathbf{V}^T\mathbf{U}). \tag{11}

Cost: O(r2d+r3)O(r^2 d + r^3) instead of O(d3)O(d^3). Practical when rdr \ll d.


Hutchinson's Trace Estimator

When J\mathbf{J} is not structured, use stochastic estimation:

tr(A)=EvN(0,I)[vTAv].(12)\text{tr}(\mathbf{A}) = \mathbb{E}_{\mathbf{v} \sim \mathcal{N}(0,\mathbf{I})}[\mathbf{v}^T\mathbf{A}\mathbf{v}]. \tag{12}

For log-det via power series:

logdet(I+Jg)=k=1(1)k+1ktr(Jgk)k=1K(1)k+1kvTJgkv.(13)\log\det(\mathbf{I} + \mathbf{J}_g) = \sum_{k=1}^\infty \frac{(-1)^{k+1}}{k}\text{tr}(\mathbf{J}_g^k) \approx \sum_{k=1}^K \frac{(-1)^{k+1}}{k}\mathbf{v}^T\mathbf{J}_g^k\mathbf{v}. \tag{13}

Cost: O(Kd)O(Kd) per sample (each Jgv\mathbf{J}_g\mathbf{v} is a JVP, computable via autodiff). Biased estimate unless KK \to \infty.


Composition of Flows

For f=fKf1f = f_K \circ \cdots \circ f_1:

logdetJf=k=1KlogdetJfk(zk1).(14)\log|\det\mathbf{J}_f| = \sum_{k=1}^K \log|\det\mathbf{J}_{f_k}(\mathbf{z}_{k-1})|. \tag{14}

Log-likelihood:

logp(x)=logp0(f1(x))+k=1KlogdetJfk1(zk).(15)\log p(\mathbf{x}) = \log p_0(f^{-1}(\mathbf{x})) + \sum_{k=1}^K \log|\det\mathbf{J}_{f_k^{-1}}(\mathbf{z}_k)|. \tag{15}

Each layer contributes independently to the log-determinant. This is why flows are composed of many simple layers.


When Exact Likelihood Is Tractable

Exact likelihood requires:

  1. Invertibility of ff (to compute f1(x)f^{-1}(\mathbf{x}) for the base density term).
  2. Efficient Jacobian determinant (triangular, low-rank, or estimable).

Always tractable: Coupling flows, autoregressive flows, linear flows. Approximately tractable: Residual flows (Hutchinson's estimator), continuous flows (trace estimator). Not tractable: GANs, standard VAE decoders, diffusion with stochastic sampling.


Common Pitfalls

Pitfall 1. Forgetting the absolute value: detJ|\det\mathbf{J}|. Without it, negative determinants (orientation-reversing maps) give negative "probabilities."

Pitfall 2. Computing the full Jacobian matrix then taking determinant. This is O(d2)O(d^2) memory + O(d3)O(d^3) compute. Always exploit structure.

Pitfall 3. Confusing the forward and inverse Jacobians. detJf1=1/detJf\det\mathbf{J}_{f^{-1}} = 1/\det\mathbf{J}_f. Using the wrong one flips the sign of the log-likelihood.


Summary

  • Change of variables: pX(x)=pZ(f1(x))detJf1p_X(\mathbf{x}) = p_Z(f^{-1}(\mathbf{x})) \cdot |\det\mathbf{J}_{f^{-1}}|.
  • Jacobian determinant measures local volume change.
  • Efficient computation: triangular (O(d)O(d)), low-rank (MDL), stochastic (Hutchinson's).
  • Composition: log-det sums over layers.
  • Architecture design is driven by making the Jacobian tractable.

Exercises

Exercise 1. Derive the change of variables for the 2D rotation f(x)=Rθxf(\mathbf{x}) = \mathbf{R}_\theta\mathbf{x}. Show that detJ=1|\det\mathbf{J}| = 1.

Exercise 2. For a coupling layer with affine transform: compute the full Jacobian matrix and verify it's triangular.

Exercise 3. Use the matrix determinant lemma to compute det(I+uvT)\det(\mathbf{I} + \mathbf{uv}^T) for u=[1,0,0]T,v=[2,3,1]T\mathbf{u} = [1, 0, 0]^T, \mathbf{v} = [2, 3, 1]^T.

Exercise 4. Estimate tr(A)\text{tr}(\mathbf{A}) using Hutchinson's estimator with 5 random vectors for a known 3×33 \times 3 matrix. Compute the variance.

Exercise 5. Prove that the Jacobian determinant of the composition f2f1f_2 \circ f_1 equals detJf2detJf1\det\mathbf{J}_{f_2} \cdot \det\mathbf{J}_{f_1}.