Table of Contents
Learning Objectives
Notation
Core Intuition
The Change of Variables Formula
The Jacobian Matrix
Computing Determinants Efficiently
Triangular Jacobians
The Matrix Determinant Lemma
Hutchinson's Trace Estimator
Composition of Flows
When Exact Likelihood Is Tractable
Common Pitfalls
Summary
Exercises
Learning Objectives
Derive the multivariate change of variables formula from first principles.
Explain the geometric meaning of the Jacobian determinant (volume change).
Identify architectures that yield efficient (triangular, low-rank) Jacobians.
Apply the matrix determinant lemma to residual connections.
Use Hutchinson's estimator for stochastic log-det computation.
Notation
f : R d → R d f: \mathbb{R}^d \to \mathbb{R}^d f : R d → R d — diffeomorphism (smooth, invertible)
J f ( x ) = ∂ f / ∂ x ∈ R d × d \mathbf{J}_f(\mathbf{x}) = \partial f / \partial\mathbf{x} \in \mathbb{R}^{d \times d} J f ( x ) = ∂ f / ∂ x ∈ R d × d — Jacobian matrix
∣ det J f ∣ |\det\mathbf{J}_f| ∣ det 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 f f f locally stretches or compresses volume. If f f f doubles the volume at some point, the density must halve there. This fundamental principle connects invertible transformations to density estimation.
p(z) p(x) = p(z)|det ∂z/∂x| x = 1.50·z · log|det J| = 0.41 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.
One dimension: If x = f ( z ) x = f(z) x = f ( z ) and f f f is monotonic:
p X ( x ) = p Z ( z ) ⋅ ∣ d z d x ∣ = p Z ( f − 1 ( x ) ) ⋅ ∣ f ′ − 1 ( 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} p X ( x ) = p Z ( z ) ⋅ d x d z = p Z ( f − 1 ( x )) ⋅ ∣ f ′ − 1 ( x ) ∣. ( 1 )
Multiple dimensions: If x = f ( z ) \mathbf{x} = f(\mathbf{z}) x = f ( z ) where f f f is a diffeomorphism:
p X ( x ) = p Z ( f − 1 ( x ) ) ⋅ ∣ det J f − 1 ( 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} p X ( x ) = p Z ( f − 1 ( x )) ⋅ det J f − 1 ( x ) . ( 2 )
Equivalently, in the "forward" direction:
p X ( f ( z ) ) = p Z ( z ) ⋅ ∣ det J f ( z ) ∣ − 1 . (3) p_X(f(\mathbf{z})) = p_Z(\mathbf{z}) \cdot \left|\det\mathbf{J}_f(\mathbf{z})\right|^{-1}.
\tag{3} p X ( f ( z )) = p Z ( z ) ⋅ ∣ det J f ( z ) ∣ − 1 . ( 3 )
Proof sketch: Consider a small volume element d z d\mathbf{z} d z around z \mathbf{z} z . Under f f f , it maps to a volume element d x = ∣ det J f ∣ ⋅ d z d\mathbf{x} = |\det\mathbf{J}_f| \cdot d\mathbf{z} d x = ∣ det J f ∣ ⋅ d z . Since probability is conserved: p X ( x ) d x = p Z ( z ) d z p_X(\mathbf{x})d\mathbf{x} = p_Z(\mathbf{z})d\mathbf{z} p X ( x ) d x = p Z ( z ) d z .
The Jacobian Matrix
For f : R d → R d f: \mathbb{R}^d \to \mathbb{R}^d f : R d → R d :
J f ( x ) = ( ∂ f 1 ∂ x 1 ⋯ ∂ f 1 ∂ x d ⋮ ⋱ ⋮ ∂ f d ∂ x 1 ⋯ ∂ f d ∂ x d ) . (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} J f ( x ) = ∂ x 1 ∂ f 1 ⋮ ∂ x 1 ∂ f d ⋯ ⋱ ⋯ ∂ x d ∂ f 1 ⋮ ∂ x d ∂ f d . ( 4 )
Geometric interpretation:
∣ det J ∣ > 1 |\det\mathbf{J}| > 1 ∣ det J ∣ > 1 : local volume expansion.
∣ det J ∣ < 1 |\det\mathbf{J}| < 1 ∣ det J ∣ < 1 : local volume contraction.
∣ det J ∣ = 1 |\det\mathbf{J}| = 1 ∣ det J ∣ = 1 : volume-preserving (like rotations).
∣ det J ∣ = 0 |\det\mathbf{J}| = 0 ∣ det J ∣ = 0 : degenerate (not invertible at this point).
Computational cost: Naively O ( d 3 ) O(d^3) O ( d 3 ) to compute the determinant. For d = 4096 d = 4096 d = 4096 (typical hidden dimension), this is prohibitive. Flow architectures are designed to make this cheaper.
Computing Determinants Efficiently
Strategy 1: Triangular matrices. det ( T ) = ∏ i T i i \det(\mathbf{T}) = \prod_i T_{ii} det ( T ) = ∏ i T ii . Cost: O ( d ) O(d) O ( d ) .
Strategy 2: Block-triangular.
det ( A 0 C D ) = 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} det ( A C 0 D ) = det ( A ) det ( D ) . ( 5 )
Strategy 3: Rank-1 updates (Matrix determinant lemma).
det ( A + u v T ) = ( 1 + v T A − 1 u ) det ( A ) . (6) \det(\mathbf{A} + \mathbf{uv}^T) = (1 + \mathbf{v}^T\mathbf{A}^{-1}\mathbf{u})\det(\mathbf{A}).
\tag{6} det ( A + uv T ) = ( 1 + v T A − 1 u ) det ( A ) . ( 6 )
Strategy 4: Trace (for log-det of near-identity). If J = I + ϵ A \mathbf{J} = \mathbf{I} + \epsilon\mathbf{A} J = I + ϵ A :
log det ( I + ϵ A ) ≈ ϵ ⋅ tr ( A ) − ϵ 2 2 tr ( A 2 ) + … (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} log det ( I + ϵ A ) ≈ ϵ ⋅ tr ( A ) − 2 ϵ 2 tr ( A 2 ) + … ( 7 )
Triangular Jacobians
Autoregressive transformations naturally yield triangular Jacobians:
y i = f i ( x 1 , … , x i ) ⟹ ∂ y i ∂ x j = 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} y i = f i ( x 1 , … , x i ) ⟹ ∂ x j ∂ y i = 0 for j > i . ( 8 )
The Jacobian is lower-triangular. Determinant = product of diagonal:
det J = ∏ i = 1 d ∂ f i ∂ x i . (9) \det\mathbf{J} = \prod_{i=1}^d \frac{\partial f_i}{\partial x_i}.
\tag{9} det J = i = 1 ∏ d ∂ x i ∂ f i . ( 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}) f ( x ) = x + g ( x ) :
J f = I + J g . (10) \mathbf{J}_f = \mathbf{I} + \mathbf{J}_g.
\tag{10} J f = I + J g . ( 10 )
If J g \mathbf{J}_g J g has low rank: J g = U V T \mathbf{J}_g = \mathbf{UV}^T J g = UV T where U , V ∈ R d × r \mathbf{U}, \mathbf{V} \in \mathbb{R}^{d \times r} U , V ∈ R d × r :
det ( I + U V T ) = det ( I r + V T U ) . (11) \det(\mathbf{I} + \mathbf{UV}^T) = \det(\mathbf{I}_r + \mathbf{V}^T\mathbf{U}).
\tag{11} det ( I + UV T ) = det ( I r + V T U ) . ( 11 )
Cost: O ( r 2 d + r 3 ) O(r^2 d + r^3) O ( r 2 d + r 3 ) instead of O ( d 3 ) O(d^3) O ( d 3 ) . Practical when r ≪ d r \ll d r ≪ d .
Hutchinson's Trace Estimator
When J \mathbf{J} J is not structured, use stochastic estimation:
tr ( A ) = E v ∼ N ( 0 , I ) [ v T A v ] . (12) \text{tr}(\mathbf{A}) = \mathbb{E}_{\mathbf{v} \sim \mathcal{N}(0,\mathbf{I})}[\mathbf{v}^T\mathbf{A}\mathbf{v}].
\tag{12} tr ( A ) = E v ∼ N ( 0 , I ) [ v T Av ] . ( 12 )
For log-det via power series:
log det ( I + J g ) = ∑ k = 1 ∞ ( − 1 ) k + 1 k tr ( J g k ) ≈ ∑ k = 1 K ( − 1 ) k + 1 k v T J g k v . (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} log det ( I + J g ) = k = 1 ∑ ∞ k ( − 1 ) k + 1 tr ( J g k ) ≈ k = 1 ∑ K k ( − 1 ) k + 1 v T J g k v . ( 13 )
Cost: O ( K d ) O(Kd) O ( K d ) per sample (each J g v \mathbf{J}_g\mathbf{v} J g v is a JVP, computable via autodiff). Biased estimate unless K → ∞ K \to \infty K → ∞ .
Composition of Flows
For f = f K ∘ ⋯ ∘ f 1 f = f_K \circ \cdots \circ f_1 f = f K ∘ ⋯ ∘ f 1 :
log ∣ det J f ∣ = ∑ k = 1 K log ∣ det J f k ( z k − 1 ) ∣ . (14) \log|\det\mathbf{J}_f| = \sum_{k=1}^K \log|\det\mathbf{J}_{f_k}(\mathbf{z}_{k-1})|.
\tag{14} log ∣ det J f ∣ = k = 1 ∑ K log ∣ det J f k ( z k − 1 ) ∣. ( 14 )
Log-likelihood:
log p ( x ) = log p 0 ( f − 1 ( x ) ) + ∑ k = 1 K log ∣ det J f k − 1 ( z k ) ∣ . (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} log p ( x ) = log p 0 ( f − 1 ( x )) + k = 1 ∑ K log ∣ det J f k − 1 ( z k ) ∣. ( 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:
Invertibility of f f f (to compute f − 1 ( x ) f^{-1}(\mathbf{x}) f − 1 ( x ) for the base density term).
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: ∣ det J ∣ |\det\mathbf{J}| ∣ det J ∣ . Without it, negative determinants (orientation-reversing maps) give negative "probabilities."
Pitfall 2. Computing the full Jacobian matrix then taking determinant. This is O ( d 2 ) O(d^2) O ( d 2 ) memory + O ( d 3 ) O(d^3) O ( d 3 ) compute. Always exploit structure.
Pitfall 3. Confusing the forward and inverse Jacobians. det J f − 1 = 1 / det J f \det\mathbf{J}_{f^{-1}} = 1/\det\mathbf{J}_f det J f − 1 = 1/ det J f . Using the wrong one flips the sign of the log-likelihood.
Summary
Change of variables: p X ( x ) = p Z ( f − 1 ( x ) ) ⋅ ∣ det J f − 1 ∣ p_X(\mathbf{x}) = p_Z(f^{-1}(\mathbf{x})) \cdot |\det\mathbf{J}_{f^{-1}}| p X ( x ) = p Z ( f − 1 ( x )) ⋅ ∣ det J f − 1 ∣ .
Jacobian determinant measures local volume change.
Efficient computation: triangular (O ( d ) 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 θ x f(\mathbf{x}) = \mathbf{R}_\theta\mathbf{x} f ( x ) = R θ x . Show that ∣ det J ∣ = 1 |\det\mathbf{J}| = 1 ∣ det 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 + u v T ) \det(\mathbf{I} + \mathbf{uv}^T) det ( I + 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 u = [ 1 , 0 , 0 ] T , v = [ 2 , 3 , 1 ] T .
Exercise 4. Estimate tr ( A ) \text{tr}(\mathbf{A}) tr ( A ) using Hutchinson's estimator with 5 random vectors for a known 3 × 3 3 \times 3 3 × 3 matrix. Compute the variance.
Exercise 5. Prove that the Jacobian determinant of the composition f 2 ∘ f 1 f_2 \circ f_1 f 2 ∘ f 1 equals det J f 2 ⋅ det J f 1 \det\mathbf{J}_{f_2} \cdot \det\mathbf{J}_{f_1} det J f 2 ⋅ det J f 1 .