Transformations of Random Variables
The CDF method, change-of-variables with Jacobians, and inverse transform sampling.
Assumes you know
Transformations of Random Variables
Intuition first
You know the distribution of and you need the distribution of — a squared value, a logarithm, a reciprocal. This comes up constantly: log-transforming skewed data, converting between units, deriving the chi-squared from the normal, simulating any distribution from a uniform.
There are two reliable methods.
The CDF method always works: write , rearrange the inequality in terms of , then read off or differentiate. Slower, but it never lies to you about monotonicity or about multiple branches.
The change-of-variables formula is faster but only applies when is monotone. Its essential ingredient is the factor, which accounts for the transformation stretching or compressing the axis. Forgetting it is the single most common error, and it produces a "density" that does not integrate to 1.
The CDF method
Three steps, in order:
- Write .
- Rearrange the inequality into a statement about .
- Express in terms of , then differentiate for the density.
Solved problem 1 · The square of a uniform
and . Find the density of .
Step 1 — the support
so as well.
Step 2 — write the CDF and rearrange
Since on the support, taking square roots preserves the inequality:
Step 3 — substitute the uniform CDF
For , , so
Step 4 — differentiate
Step 5 — verify it is a density
Step 6 — interpret
The density blows up as , since , yet the integral is finite. This is the squaring compressing the interval near zero: values of in — 10% of the probability — get squeezed into , just 1% of the range, so the density there must be about ten times larger.
Note also that exceeds 1 on part of its range, which is allowed: densities are not probabilities.
Answer
for . This is .
The change-of-variables formula
If is monotone and differentiable with inverse :
Where the Jacobian factor comes fromAdvanced
Probability must be conserved: the chance of landing in a small interval cannot change just because we relabelled the axis. For a small interval mapping to :
Dividing:
The absolute value is needed because a decreasing has negative derivative, while densities must be non-negative — and because the direction of the interval is irrelevant to its probability.
Why monotonicity is required. If is not monotone, several values map to the same , and probability arrives at from each branch. Take with on : both and contribute, so
Applying the single-branch formula would miss half the probability. In Solved problem 1 the issue did not arise because made the squaring monotone on its support — worth checking every time.
Solved problem 2 · Deriving the lognormal
and . Find the density of , and its mean.
Step 1 — check monotonicity and support
is strictly increasing on all of , so the single-branch formula applies. Its range is , so .
Step 2 — invert and differentiate
Since , the absolute value is just .
Step 3 — apply the formula
This is the lognormal density.
Step 4 — the mean, using the normal MGF
The normal MGF is , so at :
Step 5 — note the trap
The gap is the factor . This is Jensen's inequality: is convex, so .
Concretely, with and : the median of is , but the mean is — 65% higher. The lognormal is right-skewed, so its mean substantially exceeds its median.
Answer
for , with and median .
The standard transformations
| Transformation | Result |
|---|---|
| , normal | Normal: |
| , normal | Standard normal |
| , standard normal | |
| , normal | Lognormal |
| , uniform | Any distribution with CDF |
| , uniform | |
| , exponential |
Solved problem 3 · Z² is chi-squared with one degree of freedom
Show that if then .
Step 1 — recognise the non-monotonicity
is not monotone on : both and map to . Use the CDF method, which handles this automatically.
Step 2 — write the CDF
For :
By the symmetry of the standard normal, :
Step 3 — differentiate
Step 4 — substitute the normal density
With and , so :
Step 5 — match against the chi-squared density
The density is
Set . Then ✓, and the constant is
using . The two expressions are identical.
Answer
. The two branches of the squaring produced the factor of 2 that, combined with , makes the constants match exactly.
import numpy as np
from scipy import stats
rng = np.random.default_rng(0)
# Y = X² for X uniform: density 1/(2√y).
U = rng.random(500_000)
Y = U ** 2
q = np.linspace(0.05, 0.95, 5)
print("quantile empirical theory √q")
for p in q:
print(f"{p:8.2f} {np.quantile(Y, p):9.4f} {p**2:9.4f}")
# Lognormal: mean vs median, the Jensen gap.
mu, sigma = 0.0, 1.0
X = rng.normal(mu, sigma, 1_000_000)
L = np.exp(X)
print(f"\nlognormal mean {L.mean():.4f} theory e^(μ+σ²/2) = {np.exp(mu + sigma**2/2):.4f}")
print(f"lognormal median {np.median(L):.4f} theory e^μ = {np.exp(mu):.4f}")
print(f"exp(E[X]) {np.exp(X.mean()):.4f} <- NOT the mean of Y")
# Z² is chi-squared with 1 df.
Z = rng.standard_normal(1_000_000)
print(f"\nZ²: mean {np.mean(Z**2):.4f} (theory 1) var {np.var(Z**2):.4f} (theory 2)")
print(f"KS test against chi2(1): p = {stats.kstest(Z**2, 'chi2', args=(1,)).pvalue:.3f}")
# Inverse transform sampling for the exponential.
E = -np.log(rng.random(500_000)) / 3.0
print(f"\ninverse transform exponential(3): mean {E.mean():.4f} (theory {1/3:.4f})")Exercise 1
and . Find the density of .
Show solutionHide solution
is increasing on , so the change-of-variables formula applies.
Invert and differentiate:
Apply the formula with :
Check normalisation by substituting , :
This is a Rayleigh distribution with scale . It is the distribution of the magnitude of a two-dimensional vector with independent normal components, which is how it arises in signal processing and wind-speed modelling.
Exercise 2
Explain why a regression fitted to under-predicts total revenue when predictions are exponentiated, and give the correction.
Show solutionHide solution
A least-squares regression on estimates — the conditional mean of the log. Exponentiating gives
which, by the lognormal result, is the conditional median of , not its mean:
where is the variance of the residuals on the log scale. Since , the naive back-transform is always too small.
Because revenue is right-skewed, the mean exceeds the median, and summing medians across customers systematically undershoots the total. The error compounds: with residual standard deviation on the log scale, the correction factor is
so totals are understated by about 20%.
The correction is to multiply predictions by , using the residual variance from the fitted log-scale model. A more robust alternative that does not assume lognormal residuals is Duan's smearing estimator: multiply by the mean of over the residuals.
The cleanest option when you need means rather than medians is to avoid the log transform altogether and fit a generalised linear model with a log link — a gamma or Poisson GLM — which models directly and requires no back-transformation at all.