Search

Search papers and lessons

Change of Variables Formula

Level 2 · 13 min read

The change of variables formula rewrites an integral in a new variable and multiplies by a stretch factor, the derivative in one dimension and the Jacobian determinant in several.

On this page

  • Colour = topic mix
  • Calculus
  • Discrete math
  • Linear algebra
  • ReadReady to readNot yet

TL;DR

The change of variables formula lets you swap the input of an integral for a new one and still get the same answer. You rewrite the integrand in the new variable, multiply by a stretch factor that says how much the new variable stretches space, and integrate over the matching new region. In one dimension the stretch factor is the derivative of the substitution. In several dimensions it is the absolute value of the Determinant of the Jacobian matrix. It matters because it turns hard integrals into easy ones, and it tells you how a probability density changes when a random quantity is transformed.

Builds on: Definite Integral, Jacobian Matrix, Determinant · Leads to: Normalizing Flow

Where it's used

Area of a round region. The area of a disk is hard to write with x and y alone, because its edge is a curve. Switching to polar coordinates, which name each point by its distance r from the centre and its angle θ, turns the disk into a rectangle in (r, θ). The change of variables formula supplies the factor r that the new area element needs, and the disk integral becomes two simple one-dimensional integrals.

Density of a transformed random quantity. A model may describe a random quantity X with a density, then use Y = g(X) as the thing it actually reports, for example a power computed from a measured current. The density of Y is not the same function as the density of X. The change of variables formula gives the new density, and it is the reason a density can be moved from one space to another without losing the property that it adds up to 1.

Exact likelihoods in generative image models. Normalizing flows, including the RealNVP image models of 2016, map an image through a chain of invertible transformations to a simple distribution. To score an image exactly, the model needs the density after the mapping, and the Jacobian determinant of each step supplies the correction term. The formula is the reason those models can compute exact likelihoods at all.

Volume in physics. A ball of radius R has a simple volume in spherical coordinates (radius, polar angle, azimuth), and the stretch factor there is r squared times the sine of the polar angle. Gravity and charge distributions with spherical symmetry are integrated this way, which is much easier than working in ordinary x, y, z.

How it works

Trace one tiny integral from start to finish: the integral of 2x times e raised to x squared, from x = 0 to x = 1.

  1. Pick a new variable. The exponent is x squared, and the factor 2x is its derivative. So let u = x squared. Check: the integrand already contains the derivative of u, which is the signal that this substitution will simplify the problem.
  2. Find the stretch. Differentiate (take the derivative): du/dx = 2x, so du = 2x dx. Check: the 2x in the integrand is exactly this stretch, with no leftover factor.
  3. Rewrite the integrand. Replace x squared by u, and 2x dx by du. The integrand 2x e to the x squared, dx, becomes e to the u, du. Check: no x remains on either side.
  4. Move the limits. The region from x = 0 to x = 1 maps to u = 0 to u = 1, because x squared is 0 at x = 0 and 1 at x = 1. Check: the new limits are the values of u at the old limits, not the old limits themselves.
  5. Integrate in the new variable. The integral of e to the u, from 0 to 1, is e minus 1, about 1.718. Check: on [0, 1] the function e to the u lies between 1 and e, so its area must lie between 1 and about 2.718, and 1.718 does.

The mistake to avoid is step 2. The factor du/dx must come out of the integrand, not be dropped. Replacing x by u without it gives a wrong answer that looks just as tidy.

The same logic has a second version for many variables. Replace the one stretch factor by the amount a small patch of space is scaled, which is what the Jacobian determinant measures.

flowchart LR
    A["Hard integral in x"] --> B["Substitute u = g(x)"]
    B --> C["Multiply by the stretch factor"]
    C --> D["Easier integral in u"]

The math

Definition: Change of variables in one dimension

Let gg have a continuous derivative on the interval from aa to bb, and let ff be continuous on the range of gg. Then

∫abf(g(x)) g′(x) dx=∫g(a)g(b)f(u) du.\int_a^b f\big(g(x)\big)\, g'(x)\, dx = \int_{g(a)}^{g(b)} f(u)\, du.

The left side is the integral in the old variable x. The right side is the same integral written in the new variable u = g(x).

Steps 1 and 2 in symbols

Let u = g(x). Then the small change du in u is the derivative times the small change dx in x:

du=g′(x) dx.du = g'(x)\, dx.

The factor g'(x) says how much one unit of x becomes units of u, locally. In the tiny example, g(x) = x squared, so g'(x) = 2x.

Steps 3 and 4 in symbols

Replace f(g(x)) by f(u) and g'(x) dx by du. The limits move with the substitution: x = a becomes u = g(a), and x = b becomes u = g(b).

Why the formula is true: let F be an Antiderivative of f, so F' = f. The Chain Rule says the derivative of F(g(x)) is f(g(x)) g'(x). By the fundamental theorem of calculus, the left side equals F(g(b)) minus F(g(a)), and the right side equals F(g(b)) minus F(g(a)) as well. The two sides agree.

Several variables (the second version in the walkthrough)

Let T map a region U in the (u) plane one-to-one onto a region V, with continuous partial derivatives and an invertible Jacobian matrix J_T. A linear map has one constant determinant; a curved map has a determinant that changes from point to point. The entries of J_T are the partial derivatives of each output of T with respect to each input. Then

∫Vf(y) dy=∫Uf(T(u)) ∣det⁡JT(u)∣ du.\int_V f(y)\, dy = \int_U f\big(T(u)\big)\, \left| \det J_T(u) \right|\, du.

Here the absolute value means the size of the number, so area and volume come out positive. The determinant is the local area or volume scaling of T, so |det J_T| plays the role that g'(x) played in one dimension. In one dimension the Jacobian is the single number g'(x), and the formula reduces to the one above.

Polar coordinates

Take T(r, θ) = (r cos θ, r sin θ). Its Jacobian is

JT=(cos⁡θ−rsin⁡θsin⁡θrcos⁡θ),J_T = \begin{pmatrix} \cos\theta & -r\sin\theta \\ \sin\theta & r\cos\theta \end{pmatrix},

with determinant r cos squared θ plus r sin squared θ, which is r. So each small area element becomes dA=r dr dθdA = r\, dr\, d\theta.

Density transformation

Let X have a density p_X, and let Y = T(X) where T is one-to-one and differentiable with inverse h. Then Y has density

pY(y)=pX(h(y)) ∣det⁡Jh(y)∣.p_Y(y) = p_X\big(h(y)\big)\, \left| \det J_h(y) \right|.

The density of Y at y is the density of X at the matching point, scaled by how much the inverse map stretches space. This is what keeps the total probability at 1.

Worked examples

Example
Tiny: substitution in a polynomial

Compute the integral of 6 times (2x + 1) squared, from x = 0 to x = 1.

  1. Let u = 2x + 1. Then du = 2 dx, so dx = du divided by 2.
  2. Move the limits. At x = 0, u = 1. At x = 1, u = 3.
  3. Rewrite the integrand: 6 u squared times du/2 is 3 u squared du.
  4. Integrate: the integral of 3 u squared from 1 to 3 is u cubed evaluated from 1 to 3, which is 27 minus 1, or 26.
  5. Check by expanding directly: 6 times (2x + 1) squared is 24 x squared + 24 x + 6. Its integral from 0 to 1 is 8 + 12 + 6 = 26. The two methods agree.
Example
Standard: area of a disk

Find the area of a disk of radius 2 using polar coordinates.

  1. The disk is the set of points with r between 0 and 2, and angle θ between 0 and 2π.
  2. The area element is r dr dθ, because the Jacobian determinant of polar coordinates is r.
  3. Integrate the inner part first: the integral of r from 0 to 2 is r squared over 2, evaluated from 0 to 2, which is 2.
  4. Integrate the outer part: the integral of 2 over θ from 0 to 2π is 4π.
  5. Interpret the answer: the area is 4π, about 12.57. This matches the familiar formula π times radius squared, which gives π times 4.
Example
Realistic: the density of a log-normal quantity

Let X follow the standard normal distribution, with density p_X(x) = e to the power of minus x squared over 2, divided by the square root of 2π. Let Y = e to the X. Find the density of Y for y greater than 0.

  1. The inverse map is h(y) = ln y, so the derivative h'(y) = 1 divided by y.
  2. Apply the density formula: p_Y(y) = p_X(ln y) times |1/y|.
  3. Since y is positive, |1/y| = 1/y. Substitute the density of X at ln y: p_Y(y) = e to the power of minus (ln y) squared over 2, divided by y times the square root of 2π.
  4. Check at y = 1: ln 1 = 0, so p_Y(1) = 1 divided by the square root of 2π, about 0.399. The exponent is 0 there, which is the peak of the standard normal, so the value is the largest one the normal density takes.
  5. The result is the log-normal density, a standard model for quantities that are the exponential of a normal quantity, such as some incomes and particle sizes.

Try it: find p_Y(e), the density of Y at y = e. Answer: the exponent is minus (ln e) squared over 2, which is minus 1/2. The denominator is e times the square root of 2π, about 6.813. So p_Y(e) is e to the minus 0.5, divided by 6.813, which is about 0.089.

Common mistakes and tradeoffs

It is tempting to think you can replace x by u and leave dx alone. In fact dx must be replaced by du divided by g'(x), because the substitution stretches the axis. Dropping this factor changes the answer, and the error is easy to miss when the rest of the algebra looks clean.

It is tempting to think the limits can stay as they were after the substitution. In fact the limits must be the values of u at the old endpoints, because the new integral runs over the new variable. Mixing old limits with a new integrand gives a meaningless number.

It is tempting to think the Jacobian determinant can be used with its sign. In fact the absolute value is required, because area and volume are positive and a reflection gives a negative determinant. A density must also be non-negative, so the absolute value belongs in the density formula too.

It is tempting to think the density of Y uses the Jacobian of T. In fact it uses the Jacobian of the inverse map h. Equivalently, |det J_h(y)| equals 1 divided by |det J_T| evaluated at h(y). Confusing the two gives densities that do not integrate to 1.

It is tempting to think any substitution works. In fact the map must be one-to-one on the region. The map x squared on the interval from minus 1 to 1 folds the line onto itself, so the formula counts each value twice unless you split the region.

The formula is worth using when the integrand contains a function and its derivative, when the region has a natural shape such as a disk or a ball, or when a density must be carried through a transformation. It is not worth the effort when no substitution simplifies the integrand. Two alternatives are numerical integration, which approximates the integral with weighted sums, and Monte Carlo sampling, which estimates it from random points and never needs the Jacobian.

History

Substitution was used in integral calculus from its beginnings in the late 1600s, when Leibniz and Newton developed the subject, because it is the reverse of the chain rule. Writing the rule with differentials, as du and dx, made the bookkeeping easy to follow.

The multi-dimensional version needed determinants, which were studied in the 1800s. Carl Gustav Jacob Jacobi studied these functional determinants in the 1830s and 1840s, and the Jacobian is named after him. The area scaling of a map became the central idea behind the change of variables in several dimensions.

Transforming probability densities became routine in statistics as probability theory matured in the 20th century. It moved into machine learning in the mid-2010s, when normalizing flows used invertible maps to compute exact likelihoods, which is why the determinant term appears in modern generative models.

Recap

  • A substitution u = g(x) rewrites an integral, and the factor g'(x) dx becomes du.
  • The limits of integration move with the substitution, from x = a and x = b to u = g(a) and u = g(b).
  • In several variables the stretch factor is the absolute value of the Jacobian determinant, |det J_T|.
  • Polar coordinates use the stretch factor r, so the area element is r dr dθ.
  • A probability density transforms with the stretch factor of the inverse map, so it still integrates to 1.

Read next: Normalizing Flow, which uses the density formula to build models with exact likelihoods.

Check yourself

QuestionCompute the integral of 3 times (3x + 1) squared, from x = 0 to x = 1, using the substitution u = 3x + 1. Give a number.Show answer

Let u = 3x + 1, so du = 3 dx. The limits become u = 1 at x = 0 and u = 4 at x = 1. The integrand 3 (3x + 1) squared dx becomes u squared du. The integral of u squared from 1 to 4 is (64 minus 1) divided by 3, which is 21. Direct expansion gives 27x squared + 18x + 3, whose integral from 0 to 1 is 9 + 9 + 3 = 21.

QuestionIn polar coordinates, what is the stretch factor that appears in the area element, and why does it appear?Show answer

The stretch factor is r, the distance from the origin. It is the determinant of the Jacobian of (r, θ) mapped to (r cos θ, r sin θ). Circles at larger r enclose more area per unit change in angle, so each small patch of area is scaled by r.

QuestionWhy does the density formula for Y = T(X) use the absolute value of the Jacobian determinant rather than the determinant itself?Show answer

A density is a height that must be non-negative, and the absolute value keeps it so when T reverses orientation and the determinant is negative. The absolute value also measures the true local scaling of space, which is what determines how probability spreads out. Without it, the formula could give negative densities.

QuestionA map stretches the x axis by a factor of 5 and leaves y unchanged, so T(x, y) = (5x, y). If X has density p_X, what is the density of Y = T(X) at a point (y1, y2)?Show answer

The Jacobian of T is diagonal with entries 5 and 1, so its determinant is 5. The inverse map divides the first coordinate by 5, so its Jacobian determinant has absolute value 1/5. The density of Y is therefore p_Y(y1, y2) = p_X(y1/5, y2) times 1/5. The factor 1/5 keeps the total probability at 1, because the same probability is spread over a region five times wider.

QuestionName one situation where you should not use a substitution, and one alternative.Show answer

A substitution does not help when the integrand has no function-and-derivative pair that a substitution can simplify, or when the map is not one-to-one on the region. An alternative is numerical integration, which approximates the integral with weighted sums, or Monte Carlo sampling, which estimates the integral from random points and does not need a Jacobian.

Sources