Course contents 10 / 16

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.

On this page 9

Throw a dart whose two coordinates X1X_1 and X2X_2 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:

P(X12+X22≤1)=∬x12+x22≤112π e−(x12+x22)/2 dx1 dx2.P\big(X_1^2 + X_2^2 \le 1\big) = \iint_{x_1^2 + x_2^2 \le 1} \frac{1}{2\pi}\, e^{-(x_1^2 + x_2^2)/2}\, dx_1\, dx_2 .

Integrating over x2x_2 first leaves the integral of e−x22/2e^{-x_2^2/2} between ±1−x12\pm\sqrt{1 - x_1^2}, which has no elementary antiderivative (Conrad, Example 4.6; the fold in the Gaussian section says why). Yet the answer is exactly 1−e−1/2=0.39351 - e^{-1/2} = 0.3935, 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, ∫abf(x) dx\int_a^b f(x)\, dx is the limit of Riemann sums over finer and finer partitions. In two, cut a rectangle R=[a,b]×[c,d]R = [a, b] \times [c, d] into small cells of area Δx Δy\Delta x\, \Delta y, take a point (xi,yj)(x_i, y_j) in each, and let the cells shrink:

∬Rf dA=lim⁡∑i∑jf(xi,yj) Δx Δy.\iint_R f\, dA = \lim \sum_{i}\sum_{j} f(x_i, y_j)\, \Delta x\, \Delta y .

For f≥0f \ge 0 this is the volume under the surface z=f(x,y)z = f(x, y); for a density p(x,y)p(x, y) 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 x=xix = x_i is a Riemann sum in yy, and the sum over the columns is then a Riemann sum in xx:

∬Rf dA≈∑i(∑jf(xi,yj) Δy) Δx≈∑i(∫cdf(xi,y) dy) Δx≈∫ab(∫cdf(x,y) dy) dx.\begin{aligned} \iint_R f\, dA &\approx \sum_i \Big(\sum_j f(x_i, y_j)\, \Delta y\Big)\, \Delta x \\ &\approx \sum_i \Big(\int_c^d f(x_i, y)\, dy\Big)\, \Delta x \\ &\approx \int_a^b \Big(\int_c^d f(x, y)\, dy\Big)\, dx . \end{aligned}

Adding the rows first gives the other order. For a continuous ff 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 F(x)=∫cdf(x,y) dyF(x) = \int_c^d f(x, y)\, dy. A continuous function on a closed rectangle is uniformly continuous, so ∣F(x)−F(x′)∣≤(d−c)max⁡y∣f(x,y)−f(x′,y)∣\lvert F(x) - F(x')\rvert \le (d - c)\max_y \lvert f(x, y) - f(x', y)\rvert is small when x′x' is close to xx: FF is continuous and has an integral. Cut [a,b][a, b] into intervals IkI_k and [c,d][c, d] into JlJ_l, and let mklm_{kl} and MklM_{kl} be the minimum and maximum of ff on the cell Ik×JlI_k \times J_l. For x∈Ikx \in I_k, integrating mkl≤f(x,y)≤Mklm_{kl} \le f(x, y) \le M_{kl} over each JlJ_l and adding:

∑lmkl ∣Jl∣≤F(x)≤∑lMkl ∣Jl∣.\sum_l m_{kl}\, \lvert J_l\rvert \le F(x) \le \sum_l M_{kl}\, \lvert J_l\rvert .

Integrating over IkI_k and adding over kk puts ∫abF dx\int_a^b F\, dx between the lower sum ∑k,lmkl∣Ik∣∣Jl∣\sum_{k,l} m_{kl}\lvert I_k\rvert\lvert J_l\rvert and the upper sum ∑k,lMkl∣Ik∣∣Jl∣\sum_{k,l} M_{kl}\lvert I_k\rvert\lvert J_l\rvert of the double integral. The double integral lies between the same two sums. By uniform continuity, Mkl−mkl<εM_{kl} - m_{kl} \lt \varepsilon on fine enough cells, so the two sums differ by less than ε (b−a)(d−c)\varepsilon\,(b - a)(d - c), and the two integrals are equal.

The theorem holds for every Riemann-integrable ff 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 f≥0f \ge 0, where the value +∞+\infty is allowed, and for every ff with ∬∣f∣<∞\iint \lvert f\rvert < \infty (Tao, 2011, Theorem 1.7.18 and Corollary 1.7.23). For f≥0f \ge 0 the reason is the one above: a sum of non-negative terms does not depend on the order of summation. A signed ff is split as f=f+−f−f = f^+ - f^-, and both parts need finite integrals.

For f(x,y)=x2+xyf(x, y) = x^2 + xy on [0,1]×[0,2][0, 1] \times [0, 2] the two orders agree:

∫01 ⁣ ⁣∫02(x2+xy) dy dx=∫01(2x2+2x) dx=53,∫02 ⁣ ⁣∫01(x2+xy) dx dy=∫02(13+y2) dy=53.\begin{aligned} \int_0^1 \!\! \int_0^2 (x^2 + xy)\, dy\, dx &= \int_0^1 (2x^2 + 2x)\, dx = \tfrac53 , \\ \int_0^2 \!\! \int_0^1 (x^2 + xy)\, dx\, dy &= \int_0^2 \big(\tfrac13 + \tfrac y2\big)\, dy = \tfrac53 . \end{aligned}

Regions that are not rectangles#

On other regions the inner limits depend on the outer variable. To justify it, extend ff 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 0≤y≤x≤10 \le y \le x \le 1:

∫01 ⁣ ⁣∫0xxy dy dx=∫01x32 dx=18,∫01 ⁣ ⁣∫y1xy dx dy=∫01y(1−y2)2 dy=18.\begin{aligned} \int_0^1 \!\! \int_0^x xy\, dy\, dx &= \int_0^1 \frac{x^3}{2}\, dx = \frac18 , \\ \int_0^1 \!\! \int_y^1 xy\, dx\, dy &= \int_0^1 \frac{y(1 - y^2)}{2}\, dy = \frac18 . \end{aligned}

In probability this is how a marginal is computed, pX(x)=∫p(x,y) dyp_X(x) = \int p(x, y)\, dy, as in the joint distributions note, and how P(Y≤X)P(Y \le X) becomes an integral over a triangle.

When the order matters#

The condition ∬∣f∣<∞\iint \lvert f\rvert < \infty is not a technicality. On the unit square, f(x,y)=(x2−y2)/(x2+y2)2f(x, y) = (x^2 - y^2)/(x^2 + y^2)^2 is the derivative of y/(x2+y2)y/(x^2 + y^2) in yy. So ∫01f dy=1/(1+x2)\int_0^1 f\, dy = 1/(1 + x^2), and the iterated integral in this order is ∫01dx/(1+x2)=π/4\int_0^1 dx/(1 + x^2) = \pi/4. Exchanging xx and yy flips the sign of ff, so the other order gives −π/4-\pi/4. The two orders disagree because ∣f∣\lvert f\rvert is not integrable near the corner (0,0)(0, 0). In the polar coordinates of the next section, x2−y2=r2cos⁡2θx^2 - y^2 = r^2\cos 2\theta and (x2+y2)2=r4(x^2 + y^2)^2 = r^4, so ∣f∣=∣cos⁡2θ∣/r2\lvert f\rvert = \lvert\cos 2\theta\rvert/r^2. Over the quarter disc r≤1r \le 1 inside the square, with the area element r dr dθr\, dr\, d\theta:

∬∣f∣≥∫0π/2∣cos⁡2θ∣ dθ∫01drr.\iint \lvert f\rvert \ge \int_0^{\pi/2} \lvert\cos 2\theta\rvert\, d\theta \int_0^1 \frac{dr}{r} .

The angular integral is 1, and the radial one diverges at r=0r = 0.

Polar coordinates#

In polar coordinates a point of the plane is (x,y)=(rcos⁡θ,rsin⁡θ)(x, y) = (r\cos\theta, r\sin\theta) with r≥0r \ge 0 and θ∈[0,2π)\theta \in [0, 2\pi). A disc or an annulus becomes a rectangle in (r,θ)(r, \theta), and a function of the distance alone, such as e−(x2+y2)e^{-(x^2 + y^2)}, becomes a function of rr alone. The price is the area element. A small polar cell between the radii r±12Δrr \pm \tfrac12\Delta r and the angles θ\theta and θ+Δθ\theta + \Delta\theta is nearly a rectangle with sides Δr\Delta r and r Δθr\,\Delta\theta, the length of an arc of radius rr:

dA=dx dy=r dr dθ.dA = dx\, dy = r\, dr\, d\theta .

For such a cell this is exact: by the sector formula 12ρ2Δθ\tfrac12\rho^2\Delta\theta, its area is 12((r+12Δr)2−(r−12Δr)2)Δθ=r Δr Δθ\tfrac12\big((r + \tfrac12\Delta r)^2 - (r - \tfrac12\Delta r)^2\big)\Delta\theta = r\,\Delta r\,\Delta\theta. The factor rr is the determinant of the Jacobian of the map (r,θ)↦(rcos⁡θ,rsin⁡θ)(r, \theta) \mapsto (r\cos\theta, r\sin\theta):

J=(cos⁡θ−rsin⁡θsin⁡θrcos⁡θ),det⁡J=rcos⁡2θ+rsin⁡2θ=r.\begin{aligned} J &= \begin{pmatrix} \cos\theta & -r\sin\theta \\ \sin\theta & r\cos\theta \end{pmatrix} , \\ \det J &= r\cos^2\theta + r\sin^2\theta = r . \end{aligned}
Figure

The polar change of variables

A rectangular grid in (r, θ) and its image, a polar gridLeft, a rectangular grid with one shaded cell; right, a quarter disc of arcs and rays, where the same cell has become a curved sector of larger area.x = r cos θy = r sin θ012rθ90°12xyarea Δr·Δθ = 0.0873area r·Δr·Δθ = 0.1309= 1.5 · 0.0873A rectangular grid in (r, θ) and its image, a polar gridLeft, a rectangular grid with one shaded cell; right, a quarter disc of arcs and rays, where the same cell has become a curved sector of larger area.012rθ12xyΔr·Δθ = 0.0873r·Δr·Δθ = 0.1309
Left, the rectangle 0 ≤ r ≤ 2, 0° ≤ θ ≤ 90° in (r, θ), with grid steps Δr = 0.25 and Δθ = 10°; right, its image under (r cos θ, r sin θ), a quarter disc. The highlighted cell, centred at r = 1.5, has area 0.0873 on the left and 1.5 · 0.0873 = 0.1309 on the right. Cells farther out stretch more.

The Gaussian integral#

The integral I=∫−∞∞e−x2 dxI = \int_{-\infty}^{\infty} e^{-x^2}\, dx has no elementary antiderivative, yet its value is exact. Its square is easier. Read I2I^2 as a double integral over the plane and switch to polar coordinates, where the extra factor rr makes the integrand an exact derivative, ddr(−12e−r2)=re−r2\tfrac{d}{dr}\big(-\tfrac12 e^{-r^2}\big) = r e^{-r^2}:

I2=∫Re−x2 dx∫Re−y2 dy=∬R2e−(x2+y2) dx dyFubini=∫02π ⁣ ⁣∫0∞e−r2 r dr dθpolar coordinates=2π⋅[−12e−r2]0∞=π.\begin{aligned} I^2 &= \int_{\mathbb R} e^{-x^2}\, dx \int_{\mathbb R} e^{-y^2}\, dy \\ &= \iint_{\mathbb R^2} e^{-(x^2 + y^2)}\, dx\, dy && \text{Fubini} \\ &= \int_0^{2\pi} \!\! \int_0^{\infty} e^{-r^2}\, r\, dr\, d\theta && \text{polar coordinates} \\ &= 2\pi \cdot \Big[-\tfrac12 e^{-r^2}\Big]_0^\infty = \pi . \end{aligned}

Since I>0I > 0, we get I=πI = \sqrt\pi. The polar map is a bijection from (0,∞)×(0,2π)(0, \infty) \times (0, 2\pi) onto the plane without the ray {y=0, x≥0}\{y = 0,\ x \ge 0\}, 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: e−x2e^{-x^2} has an elementary antiderivative only if it has one of the form r(x)e−x2r(x)e^{-x^2} with rr rational, that is, only if r′−2xr=1r' - 2xr = 1. No rational rr solves this. A pole of order kk of rr is a pole of order k+1k + 1 of r′r' and at most of order kk of 2xr2xr, so the left side would have a pole; hence rr is a polynomial. A polynomial r≠0r \ne 0 of degree mm makes the left side a polynomial of degree m+1≥1m + 1 \ge 1, and r=0r = 0 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 [−R,R]2[-R, R]^2 in Cartesian coordinates and over discs of radius RR in polar ones. The integrand is positive, so the integral over the square lies between the integrals over the disc of radius RR inside it and the disc of radius R2R\sqrt 2 around it, and both discs tend to π\pi:

RRdisc of radius RRsquare [−R,R]2[-R, R]^2disc of radius R2R\sqrt 2
23.08413.08413.11233.11233.14053.1405
33.14123.14123.14153.14153.14163.1416

The substitution x=u/2x = u/\sqrt 2 gives ∫e−u2/2 du=2π\int e^{-u^2/2}\, du = \sqrt{2\pi}, which is why the standard normal density carries the factor 1/2π1/\sqrt{2\pi}.

The change-of-variables formula#

Polar coordinates are one case of a general rule. Let T:U→VT: U \to V be a bijection between open sets of Rn\mathbb R^n, continuously differentiable with a continuously differentiable inverse, and let JT(x)J_T(x) be its Jacobian. For f≥0f \ge 0 on VV, or ff with ∫V∣f∣<∞\int_V \lvert f\rvert \lt \infty:

  ∫Vf(y) dy=∫Uf(T(x)) ∣det⁡JT(x)∣ dx  \boxed{\; \int_V f(y)\, dy = \int_U f\big(T(x)\big)\, \big\lvert\det J_T(x)\big\rvert\, dx \;}

To see why, cut UU into small cubes CkC_k with centres xkx_k:

∫Vf(y) dy=∑k∫T(Ck)f(y) dy≈∑kf(T(xk)) vol⁡(T(Ck))≈∑kf(T(xk)) ∣det⁡JT(xk)∣ vol⁡(Ck).\begin{aligned} \int_V f(y)\, dy &= \sum_k \int_{T(C_k)} f(y)\, dy \\ &\approx \sum_k f\big(T(x_k)\big)\, \operatorname{vol}\big(T(C_k)\big) \\ &\approx \sum_k f\big(T(x_k)\big)\, \big\lvert\det J_T(x_k)\big\rvert\, \operatorname{vol}(C_k) . \end{aligned}
  1. The images T(Ck)T(C_k) tile VV.
  2. On a small region ff is nearly constant.
  3. On a small cube TT is close to the affine map xk+JT(xk)(x−xk)x_k + J_T(x_k)(x - x_k), 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 S⊂US \subset U whose boundary has zero volume (a box, a ball, a polygon) and a Riemann-integrable ff on T(S)T(S), and states ∫T(S)f=∫S(f∘T) ∣det⁡JT∣\int_{T(S)} f = \int_S (f \circ T)\, \lvert\det J_T\rvert. For f≥0f \ge 0 the boxed form follows by letting SS grow to UU, balls of radius R→∞R \to \infty for example, and passing to the limit on both sides; a signed ff is split into f+−f−f^+ - f^-. The proof turns the second approximation into two inequalities.

Proof outline: why the approximations become exact
  1. On a cube CC of side ss around xCx_C, write T=A∘T~T = A \circ \tilde T with A=JT(xC)A = J_T(x_C). Then T~\tilde T has Jacobian A−1JTA^{-1}J_T, equal to II at xCx_C. Because JTJ_T is uniformly continuous on SS and ∥A−1∥\lVert A^{-1}\rVert is bounded there, small enough cubes keep ∥A−1JT(y)−I∥≤ε\lVert A^{-1}J_T(y) - I\rVert \le \varepsilon on all of CC.
  2. The fundamental theorem of calculus along the segment from xCx_C to yy gives ∥T~(y)−T~(xC)−(y−xC)∥≤ε∥y−xC∥≤εn s/2\lVert \tilde T(y) - \tilde T(x_C) - (y - x_C)\rVert \le \varepsilon\lVert y - x_C\rVert \le \varepsilon\sqrt n\, s/2. So T~(C)\tilde T(C) lies in the cube of side s(1+n ε)s(1 + \sqrt n\,\varepsilon) around T~(xC)\tilde T(x_C).
  3. The linear map AA multiplies volumes by ∣det⁡A∣\lvert\det A\rvert, so vol⁡T(C)≤(1+n ε)n∣det⁡JT(xC)∣vol⁡(C)\operatorname{vol} T(C) \le (1 + \sqrt n\,\varepsilon)^n \lvert\det J_T(x_C)\rvert \operatorname{vol}(C).
  4. For f≥0f \ge 0, with xCx_C chosen where ∣det⁡JT∣\lvert\det J_T\rvert is smallest on CC, summing ∫T(C)f≤sup⁡T(C)f⋅vol⁡T(C)\int_{T(C)} f \le \sup_{T(C)} f \cdot \operatorname{vol} T(C) over the cubes bounds ∫T(S)f\int_{T(S)} f by (1+n ε)n(1 + \sqrt n\,\varepsilon)^n times an upper Riemann sum of (f∘T)∣det⁡JT∣(f \circ T)\lvert\det J_T\rvert.
  5. The same argument for T−1T^{-1} gives the reverse inequality, and ε→0\varepsilon \to 0 closes the gap (Lebl carries out these steps).

In one variable the formula is the substitution rule. If F′=fF' = f, the chain rule gives (F∘g)′=f(g) g′(F \circ g)' = f(g)\,g', and the fundamental theorem of calculus gives ∫abf(g(x)) g′(x) dx=F(g(b))−F(g(a))=∫g(a)g(b)f(y) dy\int_a^b f(g(x))\,g'(x)\, dx = F(g(b)) - F(g(a)) = \int_{g(a)}^{g(b)} f(y)\, dy. For a decreasing gg the limits on the right come in the wrong order; putting them in order flips the sign, and g′<0g' \lt 0 absorbs it, so ∫g([a,b])f(y) dy=∫abf(g(x)) ∣g′(x)∣ dx\int_{g([a, b])} f(y)\, dy = \int_a^b f(g(x))\,\lvert g'(x)\rvert\, dx. The absolute value records that an interval run backwards still has a positive length.

The ellipse x2/a2+y2/b2≤1x^2/a^2 + y^2/b^2 \le 1 is the image of the unit disc under T(u,v)=(au,bv)T(u, v) = (au, bv), whose Jacobian diag⁡(a,b)\operatorname{diag}(a, b) has determinant abab. Its area is therefore abab times the area π\pi of the disc, πab\pi ab; for a=3a = 3 and b=2b = 2 that is 6π=18.8506\pi = 18.850.

The same move normalizes every Gaussian. For a symmetric positive definite Σ\Sigma with Cholesky factorization Σ=LL⊤\Sigma = LL^\top, substitute x=Lzx = Lz, a linear map with Jacobian LL. Then Σ−1=L−⊤L−1\Sigma^{-1} = L^{-\top}L^{-1} gives x⊤Σ−1x=(L−1x)⊤(L−1x)=z⊤zx^\top \Sigma^{-1}x = (L^{-1}x)^\top(L^{-1}x) = z^\top z, and det⁡Σ=det⁡Ldet⁡L⊤=(det⁡L)2\det\Sigma = \det L \det L^\top = (\det L)^2 gives ∣det⁡L∣=det⁡Σ\lvert\det L\rvert = \sqrt{\det\Sigma}. The integrand e−z⊤z/2e^{-z^\top z/2} is the product of the e−zi2/2e^{-z_i^2/2}, so Tonelli splits the integral into nn one-variable Gaussians:

∫Rne−12x⊤Σ−1x dx=det⁡Σ∫Rne−12z⊤z dz=det⁡Σ ∏i=1n∫Re−zi2/2 dzi=(2π)n/2det⁡Σ.\begin{aligned} \int_{\mathbb R^n} e^{-\frac12 x^\top \Sigma^{-1}x}\, dx &= \sqrt{\det\Sigma} \int_{\mathbb R^n} e^{-\frac12 z^\top z}\, dz \\ &= \sqrt{\det\Sigma}\, \prod_{i=1}^n \int_{\mathbb R} e^{-z_i^2/2}\, dz_i \\ &= (2\pi)^{n/2} \sqrt{\det\Sigma} . \end{aligned}

For Σ\Sigma with rows (2,1.2)(2, 1.2) and (1.2,1)(1.2, 1), det⁡Σ=0.56\det\Sigma = 0.56 and the integral is 2π0.56=4.7022\pi\sqrt{0.56} = 4.702, which numerical integration confirms. This is the normalizing constant of the multivariate Gaussian density.

Densities under a transform#

If a random vector XX has density pXp_X and TT is a change of variables as above, what is the density of Y=T(X)Y = T(X)? The probability of a set BB of values of YY is the probability of its preimage, an integral over xx. Changing variables with the map T−1T^{-1} turns it into an integral over BB:

P(Y∈B)=P(X∈T−1(B))=∫T−1(B)pX(x) dx=∫BpX(T−1(y)) ∣det⁡JT−1(y)∣ dy.\begin{aligned} P(Y \in B) &= P\big(X \in T^{-1}(B)\big) \\ &= \int_{T^{-1}(B)} p_X(x)\, dx \\ &= \int_B p_X\big(T^{-1}(y)\big)\, \big\lvert\det J_{T^{-1}}(y)\big\rvert\, dy . \end{aligned}

This holds for every set BB, so the last integrand is the density of YY. The chain rule applied to T−1(T(x))=xT^{-1}(T(x)) = x gives JT−1(y)=JT(x)−1J_{T^{-1}}(y) = J_T(x)^{-1}, and det⁡(A−1)=1/det⁡A\det(A^{-1}) = 1/\det A, so

pY(y)=pX(x)∣det⁡JT(x)∣,p_Y(y) = \frac{p_X(x)}{\big\lvert\det J_T(x)\big\rvert} ,

where x=T−1(y)x = T^{-1}(y). Where TT stretches volumes, ∣det⁡JT∣>1\lvert\det J_T\rvert > 1, the same probability is spread over more volume and the density falls; where it compresses, the density rises. An integral picks up ∣det⁡J∣\lvert\det J\rvert and a density divides by it, because their product, probability, is what is conserved (Deisenroth, 2025, Appendix B).

Now the opening question. Let (R,Θ)(R, \Theta) be the polar coordinates of the two independent standard normals (X1,X2)(X_1, X_2). Apply the formula to the map TT from (x1,x2)(x_1, x_2) to (r,θ)(r, \theta). Its inverse, the polar map, has ∣det⁡JT−1∣=r\lvert\det J_{T^{-1}}\rvert = r, so by the last line of the derivation above the density of (R,Θ)(R, \Theta) is the joint density of (X1,X2)(X_1, X_2) times rr:

pR,Θ(r,θ)=12π⋅re−r2/2.p_{R,\Theta}(r, \theta) = \frac{1}{2\pi} \cdot r e^{-r^2/2} .

The density factorizes, so the angle and the radius are independent: the angle is uniform on [0,2π)[0, 2\pi), and the radius has density re−r2/2r e^{-r^2/2}. The dart lands within distance 1 with probability

P(R≤1)=∫01re−r2/2 dr=1−e−1/2=0.3935,P(R \le 1) = \int_0^1 r e^{-r^2/2}\, dr = 1 - e^{-1/2} = 0.3935 ,

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 det⁡J\det J 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 x↦rxx \mapsto rx has Jacobian rIrI and determinant rnr^n, so the ball of radius rr has volume VnrnV_n r^n, where VnV_n 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 rr and r+drr + dr has volume d(Vnrn)=nVnrn−1 drd(V_n r^n) = nV_n r^{n-1}\, dr, and summing over shells is a Riemann sum in rr. For a continuous gg, the fold turns this into ∫∥x∥≤Rg(∥x∥) dx=∫0Rg(r) nVnrn−1 dr\int_{\lVert x\rVert \le R} g(\lVert x\rVert)\, dx = \int_0^R g(r)\, nV_n r^{n-1}\, dr.

Derivation: integrating over shells

Let Φ(r)=∫∥x∥≤rg(∥x∥) dx\Phi(r) = \int_{\lVert x\rVert \le r} g(\lVert x\rVert)\, dx. For h>0h > 0, Φ(r+h)−Φ(r)\Phi(r + h) - \Phi(r) is the integral over the shell r<∥x∥≤r+hr \lt \lVert x\rVert \le r + h, of volume Vn((r+h)n−rn)V_n\big((r + h)^n - r^n\big). On the shell g(∥x∥)g(\lVert x\rVert) lies between the minimum and the maximum of gg on [r,r+h][r, r + h], so

min⁡[r,r+h]g≤Φ(r+h)−Φ(r)Vn((r+h)n−rn)≤max⁡[r,r+h]g.\min_{[r, r + h]} g \le \frac{\Phi(r + h) - \Phi(r)}{V_n\big((r + h)^n - r^n\big)} \le \max_{[r, r + h]} g .

As h→0h \to 0 both bounds tend to g(r)g(r) by continuity, and Vn((r+h)n−rn)/h→nVnrn−1V_n\big((r + h)^n - r^n\big)/h \to nV_n r^{n-1}. So Φ′(r)=g(r) nVnrn−1\Phi'(r) = g(r)\, nV_n r^{n-1}, and the fundamental theorem of calculus with Φ(0)=0\Phi(0) = 0 gives the formula. For g(r)=e−r2g(r) = e^{-r^2}, let R→∞R \to \infty.

Compute the nn-dimensional Gaussian integral both ways. By Fubini it is a product of nn one-dimensional integrals, each equal to π\sqrt\pi; by shells it is an integral over rr:

πn/2=∫Rne−∥x∥2 dx=∫0∞e−r2 nVnrn−1 dr=n2 Vn Γ(n2).\begin{aligned} \pi^{n/2} &= \int_{\mathbb R^n} e^{-\lVert x\rVert^2}\, dx \\ &= \int_0^\infty e^{-r^2}\, nV_n r^{n-1}\, dr \\ &= \frac n2\, V_n\, \Gamma\big(\tfrac n2\big) . \end{aligned}

The last step substitutes u=r2u = r^2, which turns ∫0∞rn−1e−r2 dr\int_0^\infty r^{n-1}e^{-r^2}\, dr into 12Γ(n/2)\tfrac12\Gamma(n/2), where Γ(s)=∫0∞us−1e−u du\Gamma(s) = \int_0^\infty u^{s-1}e^{-u}\, du is the gamma function. Integrating by parts, Γ(s+1)=[−use−u]0∞+s∫0∞us−1e−u du=s Γ(s)\Gamma(s + 1) = \big[-u^s e^{-u}\big]_0^\infty + s\int_0^\infty u^{s-1}e^{-u}\, du = s\,\Gamma(s) for s>0s > 0. With n2Γ(n2)=Γ(n2+1)\tfrac n2\Gamma(\tfrac n2) = \Gamma(\tfrac n2 + 1),

Vn=πn/2Γ(n2+1).V_n = \frac{\pi^{n/2}}{\Gamma\big(\tfrac n2 + 1\big)} .

From Γ(1)=1\Gamma(1) = 1 and the recursion, Γ(k+1)=k!\Gamma(k + 1) = k!; the substitution u=x2u = x^2 turns Γ(12)\Gamma(\tfrac12) into 2∫0∞e−x2 dx=π2\int_0^\infty e^{-x^2}\, dx = \sqrt\pi. For n=2n = 2 this gives π\pi, and for n=3n = 3, with Γ(52)=32⋅12π\Gamma(\tfrac52) = \tfrac32 \cdot \tfrac12 \sqrt\pi, it gives 4π/34\pi/3. For n=10n = 10, Γ(6)=5!=120\Gamma(6) = 5! = 120 and V10=π5/120=2.550V_{10} = \pi^5/120 = 2.550. The ball sits inside the cube [−1,1]n[-1, 1]^n of volume 2n2^n:

nnVnV_nshare of the cube [−1,1]n[-1, 1]^n
23.1423.14278.5%78.5\%
34.1894.18952.4%52.4\%
55.2645.26416.4%16.4\%
102.5502.5500.25%0.25\%
200.02580.02582.5⋅10−82.5 \cdot 10^{-8}

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 nn-dimensional integral to nn nested one-dimensional ones, and a numerical rule with kk points per axis then needs knk^n evaluations of the integrand. A million evaluations buy 106/10=3.9810^{6/10} = 3.98 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 N−1/nN^{-1/n} for NN points, so the error falls like N−2/nN^{-2/n}: in ten dimensions a hundred times more points cut it by a factor of only 1000.2=2.5100^{0.2} = 2.5. 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 hh with centre mm, and let MM bound the norm of the Hessian of ff. The second-order Taylor expansion with its remainder gives f(x)=f(m)+∇f(m)⊤(x−m)+ρ(x)f(x) = f(m) + \nabla f(m)^\top(x - m) + \rho(x) with ∣ρ(x)∣≤M2∥x−m∥2\lvert\rho(x)\rvert \le \tfrac M2\lVert x - m\rVert^2. The linear term integrates to zero over the cell, which is symmetric about mm. Each coordinate contributes hn−1∫−h/2h/2t2 dt=hn+2/12h^{n-1}\int_{-h/2}^{h/2} t^2\, dt = h^{n+2}/12 to the integral of ∥x−m∥2\lVert x - m\rVert^2, so

∣∫cellf dx−hnf(m)∣≤M2⋅n hn+212.\Big\lvert \int_{\text{cell}} f\, dx - h^n f(m)\Big\rvert \le \frac M2 \cdot \frac{n\,h^{n+2}}{12} .

Adding over the vol⁡(D)/hn\operatorname{vol}(D)/h^n cells, the total error is at most nM24vol⁡(D) h2\tfrac{nM}{24}\operatorname{vol}(D)\, h^2.

Monte Carlo integration writes the integral as an expectation, ∫Df(x) dx=vol⁡(D) E[f(U)]\int_D f(x)\, dx = \operatorname{vol}(D)\, \mathbb E[f(U)] with UU uniform on DD, and averages ff over NN random points. By the central limit theorem its error is about σ/N\sigma/\sqrt N, where σ\sigma is the standard deviation of vol⁡(D)f(U)\operatorname{vol}(D) f(U), and the rate 1/N1/\sqrt N 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 V10V_{10} from points in the cube sees the ball once in 400 draws, so σ\sigma is large next to the answer. The estimate is 2102^{10} times the share q^\hat q of hits, and the share is a mean of NN indicators with success probability q=0.00249q = 0.00249. Its standard deviation is q(1−q)/N\sqrt{q(1 - q)/N}, so the relative error is (1−q)/(qN)=0.020\sqrt{(1 - q)/(qN)} = 0.020 for N=106N = 10^6: the 2% that the law of large numbers note finds.

Where it breaks#

The change of variables needs a bijection. Let XX be standard normal and Y=X2Y = X^2. Applied to the branch x=yx = \sqrt y alone, the formula gives pX(y)/(2y)p_X(\sqrt y)/(2\sqrt y), the Jacobian of x↦x2x \mapsto x^2 being 2x2x. That function integrates to 12\tfrac12 over y>0y > 0, since it is the density of XX restricted to x>0x > 0. Every y>0y > 0 has two preimages, ±y\pm\sqrt y, and the density of YY adds their contributions:

pY(y)=pX(y)+pX(−y)2y=e−y/22πy,p_Y(y) = \frac{p_X(\sqrt y) + p_X(-\sqrt y)}{2\sqrt y} = \frac{e^{-y/2}}{\sqrt{2\pi y}} ,

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 kk into an image space of dimension n>kn > k, and its image has zero volume there. Cut a noise cube [0,1]k[0, 1]^k into NkN^k small cubes of diameter k/N\sqrt k/N. A generator built from linear maps and Lipschitz activations is Lipschitz with some constant ℓ\ell, so it sends each small cube into a ball of radius ℓk/N\ell\sqrt k/N. The total volume is at most NkVn(ℓk/N)nN^k V_n(\ell\sqrt k/N)^n, which tends to 0 as N→∞N \to \infty because k<nk \lt n. A density pp would give the image probability ∫imagep=0\int_{\text{image}} p = 0, yet every generated image lies in it, so the images have no density in the ordinary sense.

The determinant itself costs O(n3)O(n^3) operations for a general map of nn 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 rr 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 ∬∣f∣<∞\iint\lvert f\rvert < \infty.
  • Polar coordinates: dx dy=r dr dθdx\, dy = r\, dr\, d\theta, and r=det⁡Jr = \det J of the polar map.
  • ∫e−x2dx=π\int e^{-x^2} dx = \sqrt\pi; hence 1/2π1/\sqrt{2\pi} and (2π)n/2det⁡Σ(2\pi)^{n/2}\sqrt{\det\Sigma} in Gaussian densities.
  • Integrals pick up ∣det⁡JT∣\lvert\det J_T\rvert; densities divide by it: pY(y)=pX(x)/∣det⁡JT(x)∣p_Y(y) = p_X(x)/\lvert\det J_T(x)\rvert.
  • A many-to-one map needs a sum over all preimages.
  • Grids need knk^n 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 X2X^2. 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#