10 / 16 · Calculus
Multiple integrals and change of variables
Integrals over several variables, the tool behind densities, expectations and marginals: iterated integrals, polar coordinates and the Gaussian integral, the change of variables with the Jacobian determinant, and why grids fail in high dimensions.
Gradients, Jacobians and Hessians · Determinants and eigenvalues
On this page 9
Throw a dart whose two coordinates and are independent standard normal numbers. How often does it land within distance 1 of the centre? The answer is the integral of the joint density over the unit disc:
Integrating over first leaves the integral of between , which has no elementary antiderivative (Conrad, Example 4.6; the fold in the Gaussian section says why). Yet the answer is exactly , and it takes two lines once the disc is described by a radius and an angle.
Probability is written in integrals of this kind. A density must integrate to 1, an expectation is an integral, a marginal integrates a variable out, and a generative model that transforms random noise needs to know what the transform does to the density. Two tools compute them: reducing an integral over several variables to integrals over one, and changing the coordinates to ones that fit the region or the integrand.
Fubini: one variable at a time#
In one variable, is the limit of Riemann sums over finer and finer partitions. In two, cut a rectangle into small cells of area , take a point in each, and let the cells shrink:
For this is the volume under the surface ; for a density it is the probability of the rectangle. The definition says what the integral is, not how to compute it.
To compute it, add the cells column by column. The sum over a column is a Riemann sum in , and the sum over the columns is then a Riemann sum in :
Adding the rows first gives the other order. For a continuous on a rectangle the approximations become equalities in the limit, which is Fubini’s theorem: a double integral equals the iterated integral in either order. The proof squeezes the iterated integral between the lower and upper sums of the double one (HSE, 2018, Theorem 1), and the fold below gives it.
Proof: Fubini for a continuous function on a rectangle
Let . A continuous function on a closed rectangle is uniformly continuous, so is small when is close to : is continuous and has an integral. Cut into intervals and into , and let and be the minimum and maximum of on the cell . For , integrating over each and adding:
Integrating over and adding over puts between the lower sum and the upper sum of the double integral. The double integral lies between the same two sums. By uniform continuity, on fine enough cells, so the two sums differ by less than , and the two integrals are equal.
The theorem holds for every Riemann-integrable on a rectangle (HSE, 2018, Theorem 2). Over unbounded regions it becomes the Fubini–Tonelli theorem of measure theory: the iterated integrals in either order equal the double integral for every , where the value is allowed, and for every with (Tao, 2011, Theorem 1.7.18 and Corollary 1.7.23). For the reason is the one above: a sum of non-negative terms does not depend on the order of summation. A signed is split as , and both parts need finite integrals.
For on the two orders agree:
Regions that are not rectangles#
On other regions the inner limits depend on the outer variable. To justify it, extend by zero to a rectangle around the region. The extension is bounded and discontinuous only on the boundary curve, a set of zero area, so it is Riemann integrable (Lebl, Theorem 10.4.3) and Fubini applies to it. On the triangle :
In probability this is how a marginal is computed, , as in the joint distributions note, and how becomes an integral over a triangle.
When the order matters#
The condition is not a technicality. On the unit square, is the derivative of in . So , and the iterated integral in this order is . Exchanging and flips the sign of , so the other order gives . The two orders disagree because is not integrable near the corner . In the polar coordinates of the next section, and , so . Over the quarter disc inside the square, with the area element :
The angular integral is 1, and the radial one diverges at .
Polar coordinates#
In polar coordinates a point of the plane is with and . A disc or an annulus becomes a rectangle in , and a function of the distance alone, such as , becomes a function of alone. The price is the area element. A small polar cell between the radii and the angles and is nearly a rectangle with sides and , the length of an arc of radius :
For such a cell this is exact: by the sector formula , its area is . The factor is the determinant of the Jacobian of the map :
The polar change of variables
The Gaussian integral#
The integral has no elementary antiderivative, yet its value is exact. Its square is easier. Read as a double integral over the plane and switch to polar coordinates, where the extra factor makes the integrand an exact derivative, :
Since , we get . The polar map is a bijection from onto the plane without the ray , and a ray has zero area, so leaving it out changes no integral.
Proof outline: no elementary antiderivative for the Gaussian
An elementary function is built from rational functions, exponentials, logarithms and roots by finitely many operations and compositions. Liouville’s theorem, in the form of Conrad, Theorem 4.4, reduces the question to algebra: has an elementary antiderivative only if it has one of the form with rational, that is, only if . No rational solves this. A pole of order of is a pole of order of and at most of order of , so the left side would have a pole; hence is a polynomial. A polynomial of degree makes the left side a polynomial of degree , and makes it 0. Neither is the constant 1 (Example 4.6 there).
The plane is unbounded, so the second and third lines are limits: over squares in Cartesian coordinates and over discs of radius in polar ones. The integrand is positive, so the integral over the square lies between the integrals over the disc of radius inside it and the disc of radius around it, and both discs tend to :
| disc of radius | square | disc of radius | |
|---|---|---|---|
| 2 | |||
| 3 |
The substitution gives , which is why the standard normal density carries the factor .
The change-of-variables formula#
Polar coordinates are one case of a general rule. Let be a bijection between open sets of , continuously differentiable with a continuously differentiable inverse, and let be its Jacobian. For on , or with :
To see why, cut into small cubes with centres :
- The images tile .
- On a small region is nearly constant.
- On a small cube is close to the affine map , and an affine map multiplies volumes by the absolute value of its determinant, as in the determinants note and the Jacobian of a small square.
The last sum is a Riemann sum for the right side of the formula. The precise theorem (Lebl, Theorem 10.7.2) takes a compact set whose boundary has zero volume (a box, a ball, a polygon) and a Riemann-integrable on , and states . For the boxed form follows by letting grow to , balls of radius for example, and passing to the limit on both sides; a signed is split into . The proof turns the second approximation into two inequalities.
Proof outline: why the approximations become exact
- On a cube of side around , write with . Then has Jacobian , equal to at . Because is uniformly continuous on and is bounded there, small enough cubes keep on all of .
- The fundamental theorem of calculus along the segment from to gives . So lies in the cube of side around .
- The linear map multiplies volumes by , so .
- For , with chosen where is smallest on , summing over the cubes bounds by times an upper Riemann sum of .
- The same argument for gives the reverse inequality, and closes the gap (Lebl carries out these steps).
In one variable the formula is the substitution rule. If , the chain rule gives , and the fundamental theorem of calculus gives . For a decreasing the limits on the right come in the wrong order; putting them in order flips the sign, and absorbs it, so . The absolute value records that an interval run backwards still has a positive length.
The ellipse is the image of the unit disc under , whose Jacobian has determinant . Its area is therefore times the area of the disc, ; for and that is .
The same move normalizes every Gaussian. For a symmetric positive definite with Cholesky factorization , substitute , a linear map with Jacobian . Then gives , and gives . The integrand is the product of the , so Tonelli splits the integral into one-variable Gaussians:
For with rows and , and the integral is , which numerical integration confirms. This is the normalizing constant of the multivariate Gaussian density.
Densities under a transform#
If a random vector has density and is a change of variables as above, what is the density of ? The probability of a set of values of is the probability of its preimage, an integral over . Changing variables with the map turns it into an integral over :
This holds for every set , so the last integrand is the density of . The chain rule applied to gives , and , so
where . Where stretches volumes, , the same probability is spread over more volume and the density falls; where it compresses, the density rises. An integral picks up and a density divides by it, because their product, probability, is what is conserved (Deisenroth, 2025, Appendix B).
Now the opening question. Let be the polar coordinates of the two independent standard normals . Apply the formula to the map from to . Its inverse, the polar map, has , so by the last line of the derivation above the density of is the joint density of times :
The density factorizes, so the angle and the radius are independent: the angle is uniform on , and the radius has density . The dart lands within distance 1 with probability
the share of a two-dimensional Gaussian inside its “one sigma” circle quoted in the multivariate Gaussian note. Read backwards, the same computation is the Box–Muller method that produces normal samples from uniform ones. In one dimension the formula is the change-of-variables formula of the probability course. Stacked over a chain of learned invertible maps, it gives the exact likelihood of a normalizing flow, whose architectures are designed so that is cheap.
The volume of a ball in n dimensions#
How large is the unit ball in ten dimensions? Two uses of the change of variables answer it. The scaling has Jacobian and determinant , so the ball of radius has volume , where is the volume of the unit ball. A function of the distance alone can then be integrated over thin shells: the shell between the radii and has volume , and summing over shells is a Riemann sum in . For a continuous , the fold turns this into .
Derivation: integrating over shells
Let . For , is the integral over the shell , of volume . On the shell lies between the minimum and the maximum of on , so
As both bounds tend to by continuity, and . So , and the fundamental theorem of calculus with gives the formula. For , let .
Compute the -dimensional Gaussian integral both ways. By Fubini it is a product of one-dimensional integrals, each equal to ; by shells it is an integral over :
The last step substitutes , which turns into , where is the gamma function. Integrating by parts, for . With ,
From and the recursion, ; the substitution turns into . For this gives , and for , with , it gives . For , and . The ball sits inside the cube of volume :
| share of the cube | ||
|---|---|---|
| 2 | ||
| 3 | ||
| 5 | ||
| 10 | ||
| 20 |
In ten dimensions almost all of the cube’s volume lies in its corners, outside the inscribed ball.
High dimensions: why grids fail#
Fubini reduces an -dimensional integral to nested one-dimensional ones, and a numerical rule with points per axis then needs evaluations of the integrand. A million evaluations buy points per axis in ten dimensions. The midpoint rule has error proportional to the squared spacing (the fold derives it), and the spacing shrinks like for points, so the error falls like : in ten dimensions a hundred times more points cut it by a factor of only . Deisenroth (2025), Table 3, gives a rule of thumb: Gaussian quadrature up to about three dimensions, Bayesian quadrature up to about ten, and Monte Carlo beyond.
Derivation: the error of the midpoint rule
Take a cell of side with centre , and let bound the norm of the Hessian of . The second-order Taylor expansion with its remainder gives with . The linear term integrates to zero over the cell, which is symmetric about . Each coordinate contributes to the integral of , so
Adding over the cells, the total error is at most .
Monte Carlo integration writes the integral as an expectation, with uniform on , and averages over random points. By the central limit theorem its error is about , where is the standard deviation of , and the rate does not depend on the dimension. The estimator and its error bar belong to the Monte Carlo note of the probability course.
The table above shows the catch. A hit-or-miss estimate of from points in the cube sees the ball once in 400 draws, so is large next to the answer. The estimate is times the share of hits, and the share is a mean of indicators with success probability . Its standard deviation is , so the relative error is for : the 2% that the law of large numbers note finds.
Where it breaks#
The change of variables needs a bijection. Let be standard normal and . Applied to the branch alone, the formula gives , the Jacobian of being . That function integrates to over , since it is the density of restricted to . Every has two preimages, , and the density of adds their contributions:
the chi-squared density with one degree of freedom. For a map between spaces of different dimensions the Jacobian is not square and the formula has no determinant at all. The generator of a GAN maps a noise vector of dimension into an image space of dimension , and its image has zero volume there. Cut a noise cube into small cubes of diameter . A generator built from linear maps and Lipschitz activations is Lipschitz with some constant , so it sends each small cube into a ball of radius . The total volume is at most , which tends to 0 as because . A density would give the image probability , yet every generated image lies in it, so the images have no density in the ordinary sense.
The determinant itself costs operations for a general map of variables, the cost of elimination. That is why normalizing flows restrict themselves to maps with triangular or otherwise structured Jacobians, whose determinant is the product of the diagonal. And closed forms are the exception. The Gaussian integral works because its integrand depends on the radius alone, so the polar factor makes it an exact derivative; the evidence of a Bayesian model or the normalizer of an energy-based model has no closed form and is approximated by sampling or by variational bounds.
What you should remember#
- Fubini: a double integral is two single ones, in either order, if .
- Polar coordinates: , and of the polar map.
- ; hence and in Gaussian densities.
- Integrals pick up ; densities divide by it: .
- A many-to-one map needs a sum over all preimages.
- Grids need points; the unit ball fills 0.25% of the cube in ten dimensions.
Code#
Fubini in both orders and its counterexample, the polar Jacobian and one polar cell, the Gaussian integral squeezed between discs and squares, the Gaussian normalizer and the ellipse by numerical integration, the polar coordinates of two normals, the volume of the unit ball, and the density of . Random numbers come from a fixed seed; exact digits may vary across platforms and library versions.
import numpy as np
from math import gamma, pi
from scipy import integrate, special
rng = np.random.default_rng(0)
# Fubini: the same integral in both orders, on a rectangle and on a triangle
f = lambda x, y: x ** 2 + x * y
print(round(integrate.dblquad(lambda y, x: f(x, y), 0, 1, 0, 2)[0], 6), # dy inside, x in [0, 1]
round(integrate.dblquad(lambda x, y: f(x, y), 0, 2, 0, 1)[0], 6)) # dx inside, y in [0, 2]
print(round(integrate.dblquad(lambda y, x: x * y, 0, 1, 0, lambda x: x)[0], 6),
round(integrate.dblquad(lambda x, y: x * y, 0, 1, lambda y: y, 1)[0], 6))
# 1.666667 1.666667 | 0.125 0.125
# where Fubini fails: (x^2 - y^2) / (x^2 + y^2)^2 on the unit square, not absolutely integrable
g = lambda x, y: (x * x - y * y) / (x * x + y * y) ** 2
dy_first = integrate.quad(lambda x: integrate.quad(lambda y: g(x, y), 0, 1)[0], 0, 1, limit=200)[0]
dx_first = integrate.quad(lambda y: integrate.quad(lambda x: g(x, y), 0, 1)[0], 0, 1, limit=200)[0]
print(round(dy_first, 6), round(dx_first, 6), round(np.pi / 4, 6)) # 0.785398 -0.785398 0.785398
# the polar Jacobian, and the area of one polar cell
r, th = 1.5, np.radians(30)
J = np.array([[np.cos(th), -r * np.sin(th)], [np.sin(th), r * np.cos(th)]])
dr, dth = 0.25, np.radians(20)
print(round(np.linalg.det(J), 6), round(0.5 * ((r + dr / 2) ** 2 - (r - dr / 2) ** 2) * dth, 4), round(r * dr * dth, 4))
# 1.5 0.1309 0.1309
# the Gaussian integral: discs and squares squeeze the limit to pi
for R in (1, 2, 3):
print(R, round(np.pi * (1 - np.exp(-R * R)), 4), round(np.pi * special.erf(R) ** 2, 4), round(np.pi * (1 - np.exp(-2 * R * R)), 4))
# 1 1.9859 2.231 2.7164 | 2 3.0841 3.1123 3.1405 | 3 3.1412 3.1415 3.1416
print(round(integrate.quad(lambda x: np.exp(-x * x), -np.inf, np.inf)[0] ** 2, 10), round(np.pi, 10)) # 3.1415926536 3.1415926536
# the Gaussian normalizer in two dimensions: (2 pi) sqrt(det Sigma), by the substitution x = L z
Sig = np.array([[2, 1.2], [1.2, 1]]); P = np.linalg.inv(Sig)
num = integrate.dblquad(lambda y, x: np.exp(-0.5 * np.array([x, y]) @ P @ np.array([x, y])), -12, 12, -9, 9)[0]
print(round(num, 4), round(2 * np.pi * np.sqrt(np.linalg.det(Sig)), 4)) # 4.7019 4.7019
# an ellipse: area pi a b through the map (u, v) -> (a u, b v)
a_, b_ = 3, 2
ell = integrate.dblquad(lambda y, x: 1.0, -a_, a_, lambda x: -b_ * np.sqrt(1 - (x / a_) ** 2), lambda x: b_ * np.sqrt(1 - (x / a_) ** 2))[0]
print(round(ell, 4), round(np.pi * a_ * b_, 4)) # 18.8496 18.8496
# densities under a transform: two standard normals in polar coordinates
Z = rng.normal(size=(1_000_000, 2)); Rr = np.hypot(Z[:, 0], Z[:, 1]); Th = np.arctan2(Z[:, 1], Z[:, 0])
print(round((Rr <= 1).mean(), 4), round(1 - np.exp(-0.5), 4), round((Rr ** 2).mean(), 3), round(np.corrcoef(Rr, Th)[0, 1], 4))
# 0.3934 0.3935 1.999 -0.0011: P(R <= 1) and its formula, E[R^2] = 2, no correlation between radius and angle
# the volume of the unit ball, pi^(n/2) / Gamma(n/2 + 1), and its share of the cube [-1, 1]^n
for n in (2, 3, 5, 10, 20):
V = pi ** (n / 2) / gamma(n / 2 + 1)
print(n, round(V, 4), f'{V / 2 ** n:.3g}', end=' | ')
print()
# 2 3.1416 0.785 | 3 4.1888 0.524 | 5 5.2638 0.164 | 10 2.5502 0.00249 | 20 0.0258 2.46e-08
print(round(1e6 ** 0.1, 2), round(100 ** 0.2, 2)) # 3.98 2.51: points per axis, error reduction for 100 times more points
# a many-to-one map: Y = X^2; one branch integrates to 1/2, both branches give the chi-squared density
pX = lambda x: np.exp(-x * x / 2) / np.sqrt(2 * np.pi)
one = integrate.quad(lambda y: pX(np.sqrt(y)) / (2 * np.sqrt(y)), 0, np.inf)[0]
both = integrate.quad(lambda y: (pX(np.sqrt(y)) + pX(-np.sqrt(y))) / (2 * np.sqrt(y)), 0, np.inf)[0]
print(round(one, 6), round(both, 6)) # 0.5 1.0
Next#
The calculus section is complete: derivatives in one and many variables, matrix derivatives, and integrals with their change of variables. The optimization section puts the derivatives to work. A zero gradient is only a candidate for a minimum, and the Hessian test is only local. The next note identifies the functions for which every local minimum is global, the convex ones, and gives the tests for convexity that the gradient and the Hessian make possible.
References#
- M. P. Deisenroth. Modern Integration Methods in Machine Learning, draft additional chapter to Mathematics for Machine Learning, 2025. Section 2 (quadrature and its dimension limits), Section 3 (Monte Carlo integration), Appendix B (change of variables).
- Faculty of Mathematics, HSE University. Lecture 26-18: Fubini’s theorem (in Russian), 2018. Theorem 1 (continuous functions, with the proof by lower and upper sums) and Theorem 2 (Riemann-integrable functions).
- J. Lebl. Basic Analysis II: Introduction to Real Analysis, Volume II, version 6.3, 2026. Theorem 10.4.3 (Riemann integrability and discontinuities of measure zero), Theorem 10.7.2 (change of variables, with the proof outlined above).
- T. Tao. An Introduction to Measure Theory. American Mathematical Society, 2011. Theorem 1.7.18 (Tonelli), Theorem 1.7.21 (Fubini), Corollary 1.7.23 (Fubini–Tonelli).
- B. Conrad. Impossibility theorems for elementary integration. Theorem 4.4 (Liouville’s criterion for ) and Example 4.6 ().