Skip to content
BIASED

Multiple integrals and change of variables

Probabilities and losses are integrals over many variables. The double integral as a limit of sums, Fubini's theorem, expectations and Monte Carlo, the change of variables with the absolute Jacobian determinant, polar coordinates, the Gaussian integral and densities of transformed variables.

The integral as a limit of sums

The topic Matrix calculus gave us gradients of losses such as ∥Ax−b∥2\lVert Ax - b\rVert^2 and softmax cross-entropy in a few lines. But a loss is an average over data. When the data come from a distribution, that average is an integral, taken over as many variables as a data point has coordinates. This topic learns to compute such integrals.

Start in the plane. Cut a rectangle RR into small cells of area ΔA\Delta A, pick a point (xi,yj)(x_i, y_j) in each, and add up. As the cells shrink, the sum tends to the double integral:

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

If f≥0f \ge 0, this is the volume under the graph of ff over RR, just as a 1-D integral is an area under a curve.

In probability, ff is a joint density p(x,y)p(x, y). It is non-negative, its integral over the whole plane is 11, and the probability that the random point (X,Y)(X, Y) lands in a region RR is

P((X,Y)∈R)=∬Rp(x,y) dA.P\big((X, Y) \in R\big) = \iint_R p(x, y)\, dA .

A density is not a probability. A cell of area ΔA\Delta A near (x,y)(x, y) has probability about p(x,y) ΔAp(x, y)\,\Delta A, so pp itself may exceed 11. The uniform density on the rectangle [0,2]×[0,1][0, 2] \times [0, 1] equals 12\tfrac12: the rectangle has area 22 and the total must be 11.

The running example of this topic is the density

p(x,y)=x+y,(x,y)∈[0,1]2.p(x, y) = x + y, \qquad (x, y) \in [0, 1]^2 .

Outside the square p=0p = 0. Points near the corner (1,1)(1, 1) are more likely than points near the origin. That it really integrates to 11 we will check once we can compute double integrals.

Fubini's theorem: one variable at a time

Riemann sums define the integral but are a poor way to compute it. Fubini's theorem reduces a double integral to two ordinary ones: integrate over yy with xx held fixed, then over xx. The reason is already in the Riemann sum: add it column by column. The cells of one column x=xix = x_i form a Riemann sum in yy, which tends to ∫f(xi,y) dy\int f(x_i, y)\, dy, and the sum over the columns is then a Riemann sum in xx. Adding the rows first gives the other order. On a rectangle [a,b]×[c,d][a, b] \times [c, d],

∬Rf dA=∫ab(∫cdf(x,y) dy)dx=∫cd(∫abf(x,y) dx)dy.\iint_R f\, dA = \int_a^b \left( \int_c^d f(x, y)\, dy \right) dx = \int_c^d \left( \int_a^b f(x, y)\, dx \right) dy .

Such nested integrals are called iterated integrals. Take f=x2+xyf = x^2 + xy on [0,1]×[0,2][0, 1] \times [0, 2]. The inner integral over yy gives 2x2+2x2x^2 + 2x, and then ∫01(2x2+2x) dx=23+1=53\int_0^1 (2x^2 + 2x)\, dx = \tfrac23 + 1 = \tfrac53. The other order gives ∫02(13+y2) dy=23+1=53\int_0^2 \big(\tfrac13 + \tfrac y2\big)\, dy = \tfrac23 + 1 = \tfrac53 as well.

When the region is not a rectangle, the inner limits depend on the outer variable. The triangle D={0≤y≤x≤1}D = \{0 \le y \le x \le 1\} is, for each fixed xx, the segment 0≤y≤x0 \le y \le x:

∬Dxy dA=∫01(∫0xxy dy)dx=∫01x32 dx=18.\iint_D xy\, dA = \int_0^1 \left( \int_0^x xy\, dy \right) dx = \int_0^1 \frac{x^3}{2}\, dx = \frac18 .

The order may be swapped whenever ∬∣f∣ dA\iint \lvert f\rvert\, dA is finite, and densities always satisfy this. Without it the two orders can disagree: for (x2−y2)/(x2+y2)2(x^2 - y^2)/(x^2 + y^2)^2 on the unit square they give π/4\pi/4 and −π/4-\pi/4.

The same theorem works in nn dimensions: an integral over Rn\mathbb R^n is nn nested 1-D integrals, in any order.

Marginals, expectations and Monte Carlo

With Fubini, the basic objects of probability become computable. Integrating a joint density over one variable gives the marginal density of the other:

pX(x)=∫p(x,y) dy.p_X(x) = \int p(x, y)\, dy .

For the uniform density on [0,2]×[0,1][0, 2] \times [0, 1] this is ∫0112 dy=12\int_0^1 \tfrac12\, dy = \tfrac12 on [0,2][0, 2]. The expectation of any function gg of the random point weighs gg by the density:

E[g(X,Y)]=∬g(x,y) p(x,y) dA.\mathbb E\big[g(X, Y)\big] = \iint g(x, y)\, p(x, y)\, dA .

When the density factors, p(x,y)=pX(x) pY(y)p(x, y) = p_X(x)\, p_Y(y), the variables are independent, and Fubini splits the integral: E[XY]=E[X] E[Y]\mathbb E[XY] = \mathbb E[X]\,\mathbb E[Y]. For the uniform rectangle, E[XY]=1⋅12=12\mathbb E[XY] = 1 \cdot \tfrac12 = \tfrac12.

This is where losses live. The loss a model really cares about is an expectation over the data distribution,

L(θ)=E(x,y)∼p[ℓ(θ; x,y)],L(\theta) = \mathbb E_{(x, y) \sim p}\big[\ell(\theta;\, x, y)\big],

an integral over every possible input. For a 28×2828 \times 28 image that is 784784 variables. A grid with 1010 points per axis would need 1078410^{784} cells, so nobody integrates this by Fubini. Instead we average over samples:

E[g(X)]≈1N∑i=1Ng(xi),xi∼p.\mathbb E\big[g(X)\big] \approx \frac1N \sum_{i=1}^N g(x_i), \qquad x_i \sim p .

This is Monte Carlo integration, and a minibatch loss is exactly such an estimate. Its error falls as σ/N\sigma/\sqrt N in any dimension: a million random points in a square estimate π\pi to about 0.0020.002.

The change of variables formula

Many integrals become easy in other coordinates. To switch coordinates we need to know how a map changes areas.

Start with a linear map. The topic Determinants and eigenvalues showed that AA sends the unit square to a parallelogram of area ∣det⁡A∣\lvert\det A\rvert. Any region can be cut into small squares, and each is scaled by the same factor, so AA multiplies every area by ∣det⁡A∣\lvert\det A\rvert.

Example: the map (u,v)↦(au,bv)(u, v) \mapsto (au, bv) with a,b>0a, b > 0 sends the unit disk to the ellipse with semi-axes aa and bb. Its determinant is abab, so the ellipse has area πab\pi ab. For a=3a = 3, b=2b = 2 that is 6π≈18.856\pi \approx 18.85.

A nonlinear map TT is almost linear near each point. The topic Gradients, Jacobians and Hessians gives the best linear approximation, T(x+δ)≈T(x)+JT(x) δT(x + \delta) \approx T(x) + J_T(x)\,\delta. So a tiny cell near xx is scaled by ∣det⁡JT(x)∣\lvert\det J_T(x)\rvert, a factor that now varies from point to point. Adding up the cells gives the change of variables formula:

∫T(U)f(y) dy=∫Uf(T(x)) ∣det⁡JT(x)∣ dx.\int_{T(U)} f(y)\, dy = \int_U f\big(T(x)\big)\, \big\lvert\det J_T(x)\big\rvert\, dx .

Two conditions matter. TT must be a bijection from UU onto T(U)T(U), so that no cell is counted twice. And the factor is an absolute value: a negative determinant only means the map flips orientation, like a mirror, while areas stay positive.

Polar coordinates

The most useful change of variables in the plane is to polar coordinates:

T(r,θ)=(rcos⁡θ, rsin⁡θ),r≥0,  0≤θ<2π.T(r, \theta) = (r\cos\theta,\ r\sin\theta), \qquad r \ge 0,\ \ 0 \le \theta < 2\pi .

Its Jacobian and determinant are

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

So the area element is dA=r dr dθdA = r\, dr\, d\theta. The factor rr has a picture: a cell with sides Δr\Delta r and Δθ\Delta\theta is almost a rectangle of width Δr\Delta r and length r Δθr\,\Delta\theta. At r=1.5r = 1.5, with Δr=0.25\Delta r = 0.25 and Δθ=20∘≈0.349\Delta\theta = 20^\circ \approx 0.349, its area is about 0.1310.131. The same angle cuts longer arcs further out.

For a check, take the disk BRB_R of radius RR centered at the origin:

∬BRdA=∫02π ⁣ ⁣∫0Rr dr dθ=2π⋅R22=πR2.\iint_{B_R} dA = \int_0^{2\pi}\!\! \int_0^R r\, dr\, d\theta = 2\pi \cdot \frac{R^2}{2} = \pi R^2 .

Polar coordinates pay off for functions that depend only on the distance ∥x∥\lVert x\rVert from the origin. A round Gaussian is one of them, and that is the next block.

The Gaussian integral

The integral ∫−∞∞e−x2 dx\int_{-\infty}^{\infty} e^{-x^2}\, dx has no antiderivative in elementary functions. Polar coordinates still compute it. Call it II and square it:

I2=∫e−x2dx∫e−y2dy=∬R2e−(x2+y2) dA=∫02π ⁣ ⁣∫0∞e−r2 r dr dθ.I^2 = \int e^{-x^2} dx \int e^{-y^2} dy = \iint_{\mathbb R^2} e^{-(x^2 + y^2)}\, dA = \int_0^{2\pi}\!\! \int_0^\infty e^{-r^2}\, r\, dr\, d\theta .

The middle step is Fubini read backwards. In the last integral the factor rr is exactly what makes e−r2e^{-r^2} integrable by hand: re−r2r e^{-r^2} is the derivative of −12e−r2-\tfrac12 e^{-r^2}, so ∫0∞e−r2r dr=12\int_0^\infty e^{-r^2} r\, dr = \tfrac12. So I2=2π⋅12=πI^2 = 2\pi \cdot \tfrac12 = \pi and

∫−∞∞e−x2 dx=π.\int_{-\infty}^{\infty} e^{-x^2}\, dx = \sqrt\pi .

Substituting x=t/2x = t/\sqrt2 gives ∫e−t2/2 dt=2π\int e^{-t^2/2}\, dt = \sqrt{2\pi}. That is why the standard normal density is 12π e−t2/2\tfrac{1}{\sqrt{2\pi}}\, e^{-t^2/2}.

In nn dimensions the same tools give the normalizer of a Gaussian with a positive definite covariance Σ\Sigma:

∫Rnexp⁡ ⁣(−12 x⊤Σ−1x) dx=(2π)n/2det⁡Σ.\int_{\mathbb R^n} \exp\!\Big(-\tfrac12\, x^\top \Sigma^{-1} x\Big)\, dx = (2\pi)^{n/2} \sqrt{\det\Sigma} .

The proof is a change of variables x=Lzx = Lz with Σ=LL⊤\Sigma = LL^\top. Such an LL exists: the topic Symmetric matrices and quadratic forms gives L=Σ1/2L = \Sigma^{1/2}. The check below asks you to put the steps in order. For Σ=(21.21.21)\Sigma = \begin{pmatrix} 2 & 1.2 \\ 1.2 & 1 \end{pmatrix}, det⁡Σ=0.56\det\Sigma = 0.56 and the integral is 2π0.56≈4.7022\pi\sqrt{0.56} \approx 4.702.

Densities of transformed variables

Now turn the formula around. Let XX have density pXp_X and let Y=T(X)Y = T(X) for a bijection TT. Probability is conserved: a tiny cell near xx carries mass pX(x) dxp_X(x)\,dx, and TT spreads that mass over a cell ∣det⁡JT(x)∣\lvert\det J_T(x)\rvert times larger. So the density of YY is

pY(y)=pX(x)∣det⁡JT(x)∣,x=T−1(y),p_Y(y) = \frac{p_X(x)}{\big\lvert\det J_T(x)\big\rvert}, \qquad x = T^{-1}(y),

or, in logarithms, log⁡pY(y)=log⁡pX(x)−log⁡∣det⁡JT(x)∣\log p_Y(y) = \log p_X(x) - \log\lvert\det J_T(x)\rvert. In one dimension the determinant is simply ∣T′(x)∣\lvert T'(x)\rvert.

A linear example: Y=AX+bY = AX + b has pY(y)=pX(A−1(y−b))/∣det⁡A∣p_Y(y) = p_X\big(A^{-1}(y - b)\big)/\lvert\det A\rvert. With X∼N(0,I)X \sim \mathcal N(0, I) and A=LA = L, the denominator ∣det⁡L∣=det⁡Σ\lvert\det L\rvert = \sqrt{\det\Sigma} is exactly the factor from the normalizer in the previous block.

A nonlinear example: take two independent standard normals (X1,X2)(X_1, X_2) and pass to polar coordinates (R,Θ)(R, \Theta). Here TT sends xx to (r,θ)(r, \theta), and its inverse is the polar map with determinant rr. The chain rule applied to T−1(T(x))=xT^{-1}(T(x)) = x gives JT−1=JT−1J_{T^{-1}} = J_T^{-1}, and det⁡(A−1)=1/det⁡A\det(A^{-1}) = 1/\det A, so dividing by ∣det⁡JT∣\lvert\det J_T\rvert means multiplying by rr:

p(r,θ)=12π e−r2/2⋅r.p(r, \theta) = \frac{1}{2\pi}\, e^{-r^2/2} \cdot r .

It factors: the angle is uniform on [0,2π)[0, 2\pi) and independent of the radius, which has density re−r2/2r e^{-r^2/2}. Read backwards, this is a recipe for sampling Gaussians from uniform numbers.

The formula needs a bijection. If several points xx map to the same yy, each of them contributes its own term. For X∼N(0,1)X \sim \mathcal N(0, 1) and Y=X2Y = X^2, every y>0y > 0 has two preimages ±y\pm\sqrt y, and ∣T′(x)∣=2y\lvert T'(x)\rvert = 2\sqrt y at both:

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}} .

This is the chi-squared density with one degree of freedom. The branch x=yx = \sqrt y alone would integrate to only 12\tfrac12.

Normalizing flows are built on this formula: an invertible network turns simple noise into data and tracks log⁡∣det⁡J∣\log\lvert\det J\rvert layer by layer. The topic The change of variables in the Generative Models course develops this.

We can now write a loss as an expectation, estimate it from samples, and differentiate it. What we cannot yet say is when minimizing it is easy: when a local minimum is the global one. That is the topic Convex sets and functions.

Check your understanding

The same questions as in Pulse. Answers are shared between modes.

Full notes

Answers are saved on the server and tied to this browser until you sign in.