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 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 into small cells of area , pick a point in each, and add up. As the cells shrink, the sum tends to the double integral:
If , this is the volume under the graph of over , just as a 1-D integral is an area under a curve.
In probability, is a joint density . It is non-negative, its integral over the whole plane is , and the probability that the random point lands in a region is
A density is not a probability. A cell of area near has probability about , so itself may exceed . The uniform density on the rectangle equals : the rectangle has area and the total must be .
The running example of this topic is the density
Outside the square . Points near the corner are more likely than points near the origin. That it really integrates to 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 with held fixed, then over . The reason is already in the Riemann sum: add it column by column. The cells of one column form a Riemann sum in , which tends to , and the sum over the columns is then a Riemann sum in . Adding the rows first gives the other order. On a rectangle ,
Such nested integrals are called iterated integrals. Take on . The inner integral over gives , and then . The other order gives as well.
When the region is not a rectangle, the inner limits depend on the outer variable. The triangle is, for each fixed , the segment :
The order may be swapped whenever is finite, and densities always satisfy this. Without it the two orders can disagree: for on the unit square they give and .
The same theorem works in dimensions: an integral over is 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:
For the uniform density on this is on . The expectation of any function of the random point weighs by the density:
When the density factors, , the variables are independent, and Fubini splits the integral: . For the uniform rectangle, .
This is where losses live. The loss a model really cares about is an expectation over the data distribution,
an integral over every possible input. For a image that is variables. A grid with points per axis would need cells, so nobody integrates this by Fubini. Instead we average over samples:
This is Monte Carlo integration, and a minibatch loss is exactly such an estimate. Its error falls as in any dimension: a million random points in a square estimate to about .
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 sends the unit square to a parallelogram of area . Any region can be cut into small squares, and each is scaled by the same factor, so multiplies every area by .
Example: the map with sends the unit disk to the ellipse with semi-axes and . Its determinant is , so the ellipse has area . For , that is .
A nonlinear map is almost linear near each point. The topic Gradients, Jacobians and Hessians gives the best linear approximation, . So a tiny cell near is scaled by , a factor that now varies from point to point. Adding up the cells gives the change of variables formula:
Two conditions matter. must be a bijection from onto , 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:
Its Jacobian and determinant are
So the area element is . The factor has a picture: a cell with sides and is almost a rectangle of width and length . At , with and , its area is about . The same angle cuts longer arcs further out.
For a check, take the disk of radius centered at the origin:
Polar coordinates pay off for functions that depend only on the distance from the origin. A round Gaussian is one of them, and that is the next block.
The Gaussian integral
The integral has no antiderivative in elementary functions. Polar coordinates still compute it. Call it and square it:
The middle step is Fubini read backwards. In the last integral the factor is exactly what makes integrable by hand: is the derivative of , so . So and
Substituting gives . That is why the standard normal density is .
In dimensions the same tools give the normalizer of a Gaussian with a positive definite covariance :
The proof is a change of variables with . Such an exists: the topic Symmetric matrices and quadratic forms gives . The check below asks you to put the steps in order. For , and the integral is .
Densities of transformed variables
Now turn the formula around. Let have density and let for a bijection . Probability is conserved: a tiny cell near carries mass , and spreads that mass over a cell times larger. So the density of is
or, in logarithms, . In one dimension the determinant is simply .
A linear example: has . With and , the denominator is exactly the factor from the normalizer in the previous block.
A nonlinear example: take two independent standard normals and pass to polar coordinates . Here sends to , and its inverse is the polar map with determinant . The chain rule applied to gives , and , so dividing by means multiplying by :
It factors: the angle is uniform on and independent of the radius, which has density . Read backwards, this is a recipe for sampling Gaussians from uniform numbers.
The formula needs a bijection. If several points map to the same , each of them contributes its own term. For and , every has two preimages , and at both:
This is the chi-squared density with one degree of freedom. The branch alone would integrate to only .
Normalizing flows are built on this formula: an invertible network turns simple noise into data and tracks 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.
Answers are saved on the server and tied to this browser until you sign in.