Foundations to Frontier
Tests
A. Mathematical Foundations › A4 Probability Theory › Theory

Transformations of Random Variables and the Change-of-Variables Formula

If you know how X is distributed, how is g(X) distributed? Through the CDF, then the change-of-variables formula: densities pick up a Jacobian factor, because a map that stretches space thins out probability. You will use it for inverse-transform sampling, Box–Muller, sums by convolution, the reparameterization trick, normalizing flows, and the 'banana' that makes linearized range-bearing filters biased.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Stretching a rubber sheet

Sprinkle sand evenly on a rubber sheet, then stretch one end. The sand there spreads out and gets thinner. Where the sheet is squashed, the sand piles up thicker. No sand appears or disappears.

Probability behaves the same way. If XX is random and you compute Y=g(X)Y = g(X), the total probability stays 1, but its density changes wherever gg stretches or squashes. The amount of stretching is the derivative (or, in several dimensions, the Jacobian). That’s the change-of-variables formula.

Push samples through a function: does the density formula match?
at y = 1.310: f_X(0.450) × |h′| 1.272 = 0.4586
share of samples in the window 0.985 · formula's mass over the window 0.986
Blue: histogram of 6000 transformed samples. Orange: the change-of-variables formula f_Y(y) = Σ f_X(h(y))·|h′(y)|, where h = g⁻¹ on each branch. The Jacobian factor |h′| is what you would forget by just plugging h(y) into f_X: try it and the curve no longer matches, nor integrates to 1.
Why engineers care

A lidar measures range and bearing, but a map wants x and y. A neural network outputs a number, but you need a probability between 0 and 1. A simulator needs Gaussian noise but the computer only makes uniform random numbers. Each time, you’re transforming a random variable and need to know its new distribution.

📝 Quick check (Level 0)
0/3 answered
  1. 1. Where a map stretches space, the density…
  2. 2. After transforming, the total probability is…
  3. 3. If Y = 2X, Y's spread is…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

Method 1: through the CDF

For increasing gg, FY(y)=P(g(X)≤y)=P(X≤g−1(y))=FX(g−1(y))F_Y(y) = P(g(X)\le y) = P(X\le g^{-1}(y)) = F_X(g^{-1}(y)). Differentiate to get the density. This works for any gg if you’re careful with the event {g(X)≤y}\{g(X)\le y\}.

Example. Y=X2Y = X^2 with X∼N(0,1)X\sim\mathcal N(0,1): FY(y)=P(−y≤X≤y)=2Φ(y)−1F_Y(y) = P(-\sqrt y\le X\le\sqrt y) = 2\Phi(\sqrt y) - 1, so fY(y)=12πye−y/2f_Y(y) = \frac{1}{\sqrt{2\pi y}}e^{-y/2}. This is the χ12\chi^2_1 density.

Method 2: the change-of-variables formula

If gg is smooth and one-to-one with inverse hh,

fY(y)=fX(h(y)) ∣h′(y)∣=fX(x)∣g′(x)∣∣x=h(y)f_Y(y) = f_X(h(y))\,\lvert h'(y)\rvert = \frac{f_X(x)}{\lvert g'(x)\rvert}\Big\rvert_{x = h(y)}

If several xx map to the same yy, sum over the branches.

  • Affine: Y=aX+b⇒fY(y)=1∣a∣fX(y−ba)Y = aX + b\Rightarrow f_Y(y) = \frac1{\lvert a\rvert}f_X\big(\frac{y - b}a\big).
  • Log-normal: Y=eXY = e^X, X∼N(μ,σ2)⇒fY(y)=1yσ2πe−(ln⁡y−μ)2/2σ2X\sim\mathcal N(\mu,\sigma^2)\Rightarrow f_Y(y) = \frac{1}{y\sigma\sqrt{2\pi}}e^{-(\ln y - \mu)^2/2\sigma^2}.

Inverse-transform sampling

If U∼Uniform(0,1)U\sim\text{Uniform}(0,1) then F−1(U)F^{-1}(U) has CDF FF. For example, −ln⁡(1−U)/λ∼Exp(λ)-\ln(1 - U)/\lambda\sim\text{Exp}(\lambda). Conversely, FX(X)∼Uniform(0,1)F_X(X)\sim\text{Uniform}(0,1) for continuous XX: this is the probability integral transform, used to check calibration.

📝 Quick check (Level 1)
0/3 answered
  1. 1. Y = 3X + 1. f_Y(y) =
  2. 2. To sample Exp(λ) from U ~ Uniform…
  3. 3. Y = X² needs how many inverse branches?

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 45 minUndergraduate students and practising engineers.

Several dimensions: the Jacobian

For a smooth one-to-one y=g(x)\mathbf y = g(\mathbf x) on Rd\mathbb R^d with inverse hh,

fY(y)=fX(h(y)) ∣det⁡Jh(y)∣=fX(x)∣det⁡Jg(x)∣f_{\mathbf Y}(\mathbf y) = f_{\mathbf X}(h(\mathbf y))\,\lvert\det J_h(\mathbf y)\rvert = \frac{f_{\mathbf X}(\mathbf x)}{\lvert\det J_g(\mathbf x)\rvert}

This is the multiple-integral change of variables (A3.2-8) read backwards. The determinant measures how much a tiny box’s volume changes.

  • Linear maps: Y=AX+b⇒fY(y)=fX(A−1(y−b))/∣det⁡A∣\mathbf Y = A\mathbf X + \mathbf b\Rightarrow f_{\mathbf Y}(\mathbf y) = f_{\mathbf X}(A^{-1}(\mathbf y - \mathbf b))/\lvert\det A\rvert. For Gaussians this gives N(Aμ+b,AΣA⊤)\mathcal N(A\mu + \mathbf b, A\Sigma A^\top) (A4.2-4).
  • Polar coordinates: (x,y)=(rcos⁡θ,rsin⁡θ)(x, y) = (r\cos\theta, r\sin\theta) has ∣det⁡J∣=r\lvert\det J\rvert = r.
  • Box–Muller: R=−2ln⁡U1R = \sqrt{-2\ln U_1} and Θ=2πU2\Theta = 2\pi U_2 make (Rcos⁡Θ,Rsin⁡Θ)(R\cos\Theta, R\sin\Theta) two independent standard normals.

Sums: convolution

If X,YX, Y are independent, fX+Y(s)=∫fX(x)fY(s−x) dxf_{X+Y}(s) = \int f_X(x)f_Y(s - x)\,dx. Use the transformation (X,Y)↦(X,X+Y)(X, Y)\mapsto(X, X + Y), whose Jacobian is 1, then integrate out XX. Two uniforms give a triangle, and two Exp(1) variables give a Gamma(2, 1).

The reparameterization trick

To differentiate EX∼N(μ,σ2)[f(X)]\mathbb E_{X\sim\mathcal N(\mu,\sigma^2)}[f(X)] with respect to μ\mu and σ\sigma, write X=μ+σεX = \mu + \sigma\varepsilon with ε∼N(0,1)\varepsilon\sim\mathcal N(0,1). Then ∇E[f(X)]=E[∇f(μ+σε)]\nabla\mathbb E[f(X)] = \mathbb E[\nabla f(\mu + \sigma\varepsilon)], which you can backpropagate through. It usually has much lower variance than the score-function (REINFORCE) estimator E[f(X)∇log⁡p(X)]\mathbb E[f(X)\nabla\log p(X)] (Kingma and Welling, 2014; Practice Code 5).

🐍 Exercise 1: densities of transformed variables, and two samplers
Implement the change-of-variables density with several inverse branches, inverse-CDF sampling and Box–Muller. Check the log-normal against a histogram, Z² against chi-square(1), and that Box–Muller's outputs are independent standard normals.
📝 Quick check (Level 2)
0/3 answered
  1. 1. |det J| of the polar map (r, θ) → (x, y) is…
  2. 2. The density of X + Y (independent) is…
  3. 3. The reparameterization trick writes X ~ N(μ, σ²) as…

Level 3: Advanced

★★★★☆ Advanced⏱ 40 minGraduate level, with full derivations and proofs.

Proof of the one-dimensional formula

Let gg be strictly increasing and differentiable, with inverse hh.

  1. CDF. FY(y)=P(X≤h(y))=FX(h(y))F_Y(y) = P(X\le h(y)) = F_X(h(y)).
  2. Differentiate. By the chain rule, fY(y)=fX(h(y))h′(y)f_Y(y) = f_X(h(y))h'(y).
  3. Decreasing case. If gg is decreasing, FY(y)=1−FX(h(y))F_Y(y) = 1 - F_X(h(y)), so fY=−fX(h)h′=fX(h)∣h′∣f_Y = -f_X(h)h' = f_X(h)\lvert h'\rvert.
  4. Several branches. If gg is piecewise monotone, {g(X)≤y}\{g(X)\le y\} splits into disjoint pieces, and the densities add. ∎

Why Box–Muller works

Map (u1,u2)↦(z1,z2)(u_1, u_2)\mapsto(z_1, z_2). The inverse is u1=e−(z12+z22)/2u_1 = e^{-(z_1^2 + z_2^2)/2} and u2=12πatan2⁡(z2,z1)u_2 = \frac1{2\pi}\operatorname{atan2}(z_2, z_1). Its Jacobian determinant has absolute value 12πe−(z12+z22)/2\frac1{2\pi}e^{-(z_1^2 + z_2^2)/2}. The uniform density is 1, so

f(z1,z2)=12πe−z12/2e−z22/2=φ(z1)φ(z2)f(z_1, z_2) = \frac1{2\pi}e^{-z_1^2/2}e^{-z_2^2/2} = \varphi(z_1)\varphi(z_2)

The density factors, so the two outputs are independent standard normals. ∎

Normalizing flows

Compose invertible maps x=gK∘⋯∘g1(z)\mathbf x = g_K\circ\cdots\circ g_1(\mathbf z) with z∼N(0,I)\mathbf z\sim\mathcal N(0, I). Then

log⁡p(x)=log⁡p(z)−∑klog⁡∣det⁡Jgk∣\log p(\mathbf x) = \log p(\mathbf z) - \sum_k\log\lvert\det J_{g_k}\rvert

Coupling layers (Dinh et al., 2017) keep half the coordinates and scale/shift the rest, x2=z2es(z1)+t(z1)x_2 = z_2e^{s(z_1)} + t(z_1). The Jacobian is triangular, so its log-determinant is just ∑s(z1)\sum s(z_1), at no extra cost.

Exercise 3.1: Prove the probability integral transform

For continuous strictly increasing FF, P(F(X)≤u)=P(X≤F−1(u))=F(F−1(u))=uP(F(X)\le u) = P(X\le F^{-1}(u)) = F(F^{-1}(u)) = u for u∈(0,1)u\in(0,1). That is the Uniform(0,1) CDF.

📝 Quick check (Level 3)
0/3 answered
  1. 1. A decreasing g gives f_Y = f_X(h)·…
  2. 2. Box–Muller's outputs are independent because…
  3. 3. A coupling layer's log |det J| is…

Level 4: Expert

★★★★★ Expert⏱ 45 minResearchers: original papers and open problems.

Range-bearing sensors and the banana

A lidar or radar measures range rr and bearing θ\theta with roughly independent Gaussian errors. In Cartesian coordinates the uncertainty is a curved banana, not an ellipse: its density is fr(r)fθ(θ)/rf_r(r)f_\theta(\theta)/r. An EKF linearizes the map at the mean, which gives an ellipse centred at (r0,0)(r_0, 0). But the true mean is r0e−σθ2/2r_0e^{-\sigma_\theta^2/2} along the boresight: closer to the sensor. With large bearing noise, the linearized filter is biased and overconfident. That’s why the unscented transform, particle filters and “debiased converted measurements” exist (Julier and Uhlmann, 2004; Lerro and Bar-Shalom, 1993).

🐍 Exercise 2 (research): the range-bearing banana and a flow's log-det
Implement the exact density of (r cos θ, r sin θ) with the 1/r Jacobian and check it integrates to 1 and matches samples. Measure the bias of the linearized (EKF) mean. Then build a RealNVP-style affine coupling layer: check its log-det against a numerical Jacobian, that its density integrates to 1, and that it inverts exactly.

Transformations in AI, robotics and aerospace

  • Unscented transform. Instead of linearizing gg, push 2d+12d + 1 sigma points through it and refit the mean and covariance. This captures second-order effects like the banana’s bias (Julier and Uhlmann, 2004).
  • Normalizing flows and diffusion models are change-of-variables machines. Flows learn invertible maps with tractable Jacobians (Rezende and Mohamed, 2015; Dinh et al., 2017). Continuous-time flows replace log⁡∣det⁡J∣\log\lvert\det J\rvert with the integral of a trace (Chen et al., 2018).
  • Squashed Gaussian policies. Soft actor-critic samples u=tanh⁡(μ+σε)u = \tanh(\mu + \sigma\varepsilon) to keep actions within limits. Its log-probability needs the correction −∑log⁡(1−ui2)-\sum\log(1 - u_i^2), a Jacobian term (Haarnoja et al., 2018).
  • Orbit uncertainty propagation. Gaussian uncertainty in Cartesian position stops being Gaussian after a few orbits. Mapping to orbital elements, which evolve nearly linearly, keeps it close to Gaussian far longer (Junkins et al., 1996).
  • Calibration checks for probabilistic forecasts use the probability integral transform. If the forecasts are calibrated, F(yobs)F(y_{\text{obs}}) is uniform.

References

  1. G. Casella, R. Berger (2002). Statistical Inference, 2nd ed., Ch. 2 and 4. Duxbury.
  2. G. Box, M. Muller (1958). “A note on the generation of random normal deviates.” Annals of Mathematical Statistics, 29(2).
  3. L. Devroye (1986). Non-Uniform Random Variate Generation. Springer.
  4. D. Kingma, M. Welling (2014). “Auto-encoding variational Bayes.” ICLR.
  5. D. Rezende, S. Mohamed (2015). “Variational inference with normalizing flows.” ICML.
  6. L. Dinh, J. Sohl-Dickstein, S. Bengio (2017). “Density estimation using Real NVP.” ICLR.
  7. R. Chen, Y. Rubanova, J. Bettencourt, D. Duvenaud (2018). “Neural ordinary differential equations.” NeurIPS.
  8. S. Julier, J. Uhlmann (2004). “Unscented filtering and nonlinear estimation.” Proceedings of the IEEE, 92(3).
  9. D. Lerro, Y. Bar-Shalom (1993). “Tracking with debiased consistent converted measurements versus EKF.” IEEE Transactions on Aerospace and Electronic Systems, 29(3).
  10. T. Haarnoja, A. Zhou, P. Abbeel, S. Levine (2018). “Soft actor-critic.” ICML.
  11. J. Junkins, M. Akella, K. Alfriend (1996). “Non-Gaussian error propagation in orbital mechanics.” Journal of the Astronautical Sciences, 44(4).