Change of Variables in Multiple Integrals
Polar, cylindrical and spherical coordinates are special cases of one theorem: when you change variables in a multiple integral, the area or volume element is multiplied by the absolute value of the Jacobian determinant of the transformation. This lesson explains why, applies it to regions that no standard coordinate system fits, and shows the same formula at work in probability densities and in normalizing flows, a family of generative models in machine learning.
In one variable this is the substitution rule: . The survey treatment of Jacobians is Jacobian and Hessian.
Transformations and the Jacobian
A transformation maps a region in the -plane onto a region in the -plane. We assume is (continuous first partials) and one-to-one, except perhaps on the boundary of . Its Jacobian is the determinant
Why it measures area. Take a tiny rectangle in the -plane with corner and sides , . Its image is a curved quadrilateral, approximately the parallelogram spanned by
whose area is . So stretches small areas by the factor .
Change of variables theorem. With as above and continuous on ,
Note the absolute value: area is never negative. (In one variable no absolute value appears because a reversed interval already supplies the sign.)
Check with polar coordinates, , :
giving . In three variables the Jacobian is a determinant; for spherical coordinates its absolute value is (previous lesson).
Linear maps scale area by
If is linear, for a matrix , its Jacobian is the constant , so every region's area is multiplied by . The sign of says whether preserves orientation (positive) or flips it like a mirror (negative). This is the geometric meaning of the determinant in Linear Algebra.
Example: a parallelogram region
Evaluate where is the parallelogram bounded by , , , . The boundary lines suggest , . Then is the rectangle , , and
Tip: when it is easier to write , in terms of , , use . Here , so directly.
import sympy as sp
import numpy as np
u, v = sp.symbols("u v")
x, y = (u + v) / 2, (u - v) / 2
J = sp.Matrix([x, y]).jacobian([u, v]).det()
I = sp.integrate((x + y) * sp.exp(x - y) * sp.Abs(J), (u, 0, 2), (v, 0, 3))
print("Jacobian:", J, " integral:", I, "≈", round(float(I), 6))
# independent check in x, y: midpoint grid on a bounding box, keeping points inside R
n = 2000
xs = (np.arange(n) + 0.5) * 2.5 / n
ys = -1.5 + (np.arange(n) + 0.5) * 2.5 / n
X, Y = np.meshgrid(xs, ys)
inside = (X + Y >= 0) & (X + Y <= 2) & (X - Y >= 0) & (X - Y <= 3)
print("grid in x, y ≈", round(float(np.sum(((X + Y) * np.exp(X - Y))[inside]) * (2.5 / n)**2), 4))Jacobian: -1/2 integral: -1 + exp(3) ≈ 19.085537
grid in x, y ≈ 19.1102
The grid estimate is off in the second decimal because grid cells straddle the slanted edges of ; the substitution gives the exact value with no effort.
Example: an ellipse
The area of the ellipse : put , , which maps the unit disk onto the ellipse with Jacobian . So the area is . Integrals over ellipsoids reduce to balls in the same way (, volume ).
In probability and ML: densities transform with the Jacobian
If a random vector has density and with invertible and differentiable, then probability mass is preserved, , so
Normalizing flows (RealNVP, Glow, neural spline flows) are generative models built on exactly this formula. They chain simple invertible layers whose Jacobian determinants are cheap (often triangular matrices, whose determinant is the product of the diagonal), push a standard normal through them, and train by maximising the exact log-likelihood above. The design problem of flows is "invertible, expressive, and with an easy ".
import numpy as np
rng = np.random.default_rng(5)
# a tiny 2D "flow": an affine coupling layer x1 = z1, x2 = z2·exp(s(z1)) + t(z1)
s = lambda z1: 0.5 * np.tanh(z1)
t = lambda z1: z1**2
def forward(z):
z1, z2 = z[:, 0], z[:, 1]
return np.stack([z1, z2 * np.exp(s(z1)) + t(z1)], axis=1), s(z1) # log|det J| = s(z1)
def log_px(x):
z1 = x[:, 0]
z2 = (x[:, 1] - t(z1)) * np.exp(-s(z1)) # inverse map
log_pz = -0.5 * (z1**2 + z2**2) - np.log(2 * np.pi)
return log_pz - s(z1) # change of variables
# 1) the transformed density still integrates to 1 (grid sum over a large box)
h = 0.02 # midpoint grid, x2 up to 42 (x2 = z2·e^s + z1²)
g1, g2 = -6 + h * (np.arange(600) + 0.5), -6 + h * (np.arange(2400) + 0.5)
X1, X2 = np.meshgrid(g1, g2, indexing="ij")
dA = h * h
P = np.exp(log_px(np.stack([X1.ravel(), X2.ravel()], axis=1)))
print("∬ p_X dA ≈", round(float(P.sum() * dA), 4))
# 2) samples pushed through the flow match the formula: P(X2 > 2)
x, _ = forward(rng.standard_normal((200_000, 2)))
mask = X2.ravel() > 2
print("P(X2 > 2): samples", round(float(np.mean(x[:, 1] > 2)), 4),
" formula", round(float(P[mask].sum() * dA), 4))∬ p_X dA ≈ 1.0
P(X2 > 2): samples 0.2123 formula 0.2122
Without the term the "density" would not integrate to 1 and the model's likelihoods would be meaningless.
Other places the Jacobian determinant appears in software:
- Sampling. Uniform disk sampling , and Box–Muller for normal random numbers are change-of-variables results (see Triple Integrals in Cylindrical Coordinates).
- Rendering. Converting a light integral from solid angle to surface area uses the Jacobian , and texture-space to world-space integrals use the mesh's UV Jacobian.
- Finite elements and physics. Every element integral is mapped to a reference triangle or cube and multiplied by of the element map; a negative signals an inverted (tangled) element, a classic simulation failure.
Exercises
Exercise 1: A Jacobian
Find for , , and evaluate it at .
Answer
; at it is 20.
Solution
This is the map of complex numbers; .
Exercise 2: A rotated square
Evaluate over the square with vertices , , , .
Answer
.
Solution
The sides lie on , , , . Put , : , .
Exercise 3: Ellipse integral
Compute over the ellipse .
Answer
.
Solution
, maps the unit disk onto , .
Exercise 4: Density of a transformed variable (code)
If is uniform on and with , use the change-of-variables formula to find the density of , then check by simulation.
Answer
for (exponential); .
Solution
This is inverse-transform sampling of the exponential distribution.
import numpy as np
rng = np.random.default_rng(8)
X = -np.log(rng.random(100_000)) / 2
print(round(float(np.mean(X <= 0.5)), 4), round(1 - np.exp(-1), 4))0.6335 0.6321
Test yourself
In the change of variables formula, becomes…
A linear map has matrix . A region of area 4 is mapped to a region of area…
- 4
- 20
- 24
- 28
In a normalizing flow , the log-density is …
- unchanged