Skip to content

Transforming random variables

The density of a function of a random variable stretches with the map, and the inverse distribution function turns even dots into any shape.

Helps to know firstContinuous random variables

Helps to know first
This topic

First, the picture

Drop dots evenly down the side of a box, bounce them off a curve, and let them fall. Watch them pile into a new shape.

Turning randomness into any shape

Even random heights from 0 to 1, sent through a distribution function (seed 2600 + target).

Exponential: each dot starts at a random height u between 0 and 1, runs across to the curve F, and falls to the value x where F(x) = u.

shape
exponential
dots
0
x for the level u
–
0.00 / 18.20 s
Describe this picture

Two panels on one horizontal axis. On top, a rising curve, a distribution function, from 0 to 1 up the vertical axis, labelled u=F(x)u=F(x). Below, a histogram. Seeded dots (400 for each curve; seeds 2600, 2601 and 2602) start at random heights spread evenly between 0 and 1 on the vertical axis, travel across until they meet the curve, then fall straight down into the histogram: the first 12 one by one, stacking on their bins, the rest poured in. The clip runs three curves in turn, an exponential, a Rayleigh and a standard normal, and each pile takes that law’s shape, which is then outlined as a dotted curve. Where the curve is steep, the falling dots crowd together; where it is flat, they spread out. Readouts give the shape, the dots landed and, after the clip, the value xx. After the clip a slider, “Level u”, sets uu from 0.01 to 0.99 on the standard normal curve, and a marker runs from uu across to the curve and down to the value x=F−1(u)x=F^{-1}(u); at 0.975 it reads 1.9600.

Even dots in, any shape out

Computers make random numbers that are spread evenly between 0 and 1: a uniform variable U∼U(0,1)U\sim\mathrm{U}(0,1), from Continuous random variables. A simulation usually needs something else: waiting times, noise, heights. The picture shows how to get any shape from even dots.

Each dot starts at a height uu and moves across to the curve FXF_X, then drops to the xx where FX(x)=uF_X(x)=u. That xx is written FX−1(u)F_X^{-1}(u), the inverse of the distribution function: it answers “at which value has the curve climbed to uu?”. So the method is

X=FX−1(U).X=F_X^{-1}(U).

Why does the pile take the shape of FXF_X? Because FXF_X climbs, FX−1(U)≤xF_X^{-1}(U)\le x happens exactly when U≤FX(x)U\le F_X(x). And a uniform UU lands below any level with chance equal to that level:

Pr⁡{FX−1(U)≤x}=Pr⁡{U≤FX(x)}=FX(x).\Pr\{F_X^{-1}(U)\le x\}=\Pr\{U\le F_X(x)\}=F_X(x).

The new variable has distribution function FXF_X, which is what I wanted. This is the inverse distribution function method, often called the inverse CDF method.

Exponential waits. For Exp(λ)\mathrm{Exp}(\lambda), FX(x)=1−e−λxF_X(x)=1-e^{-\lambda x}. Solve 1−e−λx=u1-e^{-\lambda x}=u for xx:

x=−ln⁡(1−u)λ.x=-\frac{\ln(1-u)}{\lambda}.

With λ=1\lambda=1, the level u=0.3u=0.3 gives −ln⁡0.7=0.3567-\ln0.7=0.3567.

Steep and flat

Look at where the dots land. A band of starting heights from uu to u+Δuu+\Delta u catches the same share of dots, Δu\Delta u, wherever it is. Where the curve is steep, that band meets the curve over a narrow stretch of xx, so its dots land close together and the pile is tall. Where the curve is flat, the same share of dots is spread over a wide stretch of xx, and the pile is low.

That is the density again: the slope of FXF_X is fXf_X. Steep means a high density, flat means a low one.

The density of a function

The same idea answers a general question. If XX has a known density and Y=g(X)Y=g(X) for some function gg, what is the density of YY?

Take gg increasing, so that it keeps order. Then Y≤yY\le y exactly when X≤g−1(y)X\le g^{-1}(y), and

FY(y)=FX(g−1(y)).F_Y(y)=F_X\big(g^{-1}(y)\big).

Take the slope of both sides, by the chain rule. For gg decreasing the same steps give a minus sign, and the absolute value covers both:

fY(y)=fX(g−1(y)) ∣d g−1(y)dy∣.f_Y(y)=f_X\big(g^{-1}(y)\big)\,\left\lvert\frac{d\,g^{-1}(y)}{dy}\right\rvert.

The last factor is the stretch factor. Where gg stretches a stretch of xx into a wider stretch of yy, the same probability is spread thinner, and the density drops by that factor.

A small case: X∼U(0,1)X\sim\mathrm{U}(0,1) and Y=2X+1Y=2X+1. Then g−1(y)=(y−1)/2g^{-1}(y)=(y-1)/2, its slope is 12\tfrac12, and YY has density 1×12=121\times\tfrac12=\tfrac12 on (1,3)(1,3): the same flat shape, twice as wide and half as tall.

Watch out

Leaving out the stretch factor is the common mistake. Without it, Y=2X+1Y=2X+1 would get height 1 on a width of 2, an area of 2. A density must have area 1, and the stretch factor is what keeps it there.

Three shapes made by transforming

Rayleigh. Take a point whose two coordinates are independent and each N(0,σ2)\mathcal{N}(0,\sigma^2), and let RR be its distance from the origin. RR follows the Rayleigh law, with FR(r)=1−e−r2/(2σ2)F_R(r)=1-e^{-r^2/(2\sigma^2)} for r≥0r\ge0. Its inverse gives the recipe

r=σ−2ln⁡(1−u),r=\sigma\sqrt{-2\ln(1-u)},

and its mean is σπ/2=1.2533 σ\sigma\sqrt{\pi/2}=1.2533\,\sigma.

Log-normal. If ZZ is standard normal, Y=eZY=e^Z is log-normal: always positive, with a long tail to the right. An increasing map keeps the middle value in the middle, so its median is e0=1e^0=1. Its mean is larger, e1/2=1.6487e^{1/2}=1.6487, pulled up by the tail.

Chi-square with one degree of freedom. Y=Z2Y=Z^2 is the square of a standard normal. Its mean is E{Z2}=1\mathbb{E}\{Z^2\}=1, the variance of ZZ. The square is not increasing over all of zz, since zz and −z-z give the same yy; its density adds the two halves, one from each side.

Worked example

1. Squaring a uniform. Let X∼U(0,1)X\sim\mathrm{U}(0,1) and Y=X2Y=X^2, which also lies between 0 and 1. Squaring is increasing there, so

FY(y)=Pr⁡{X2≤y}=Pr⁡{X≤y}=y,fY(y)=12y.F_Y(y)=\Pr\{X^2\le y\}=\Pr\{X\le\sqrt y\}=\sqrt y,\qquad f_Y(y)=\frac{1}{2\sqrt y}.

The density is tall near 0 and low near 1: squaring pushes small values closer to 0. Half of the values of YY lie below 0.25, since FY(0.25)=0.25=0.5F_Y(0.25)=\sqrt{0.25}=0.5.

Its mean is E{X2}=∫01x2 dx=13\mathbb{E}\{X^2\}=\int_0^1x^2\,dx=\tfrac13, not (12)2=14\left(\tfrac12\right)^2=\tfrac14, as Expected value warned.

2. Reading levels off three curves. Here is what the slider gives at a few levels uu, for the exponential with rate λ=1\lambda=1, the Rayleigh with σ=1\sigma=1 and the standard normal:

LevelExponentialRayleighStandard normal
0.50.69311.17740
0.92.30262.14601.2816
0.9753.68892.71621.9600
0.994.60523.03492.3263

The standard normal at 0.975 gives 1.96, the edge of the 95 % band of The normal distribution: 2.5 % lies above it and 2.5 % below −1.96-1.96.

Try it yourself. An exponential draw with rate 1 from the level u=0.3u=0.3: −ln⁡0.7=0.3567-\ln0.7=0.3567.

Where you’ll meet this

Simulation. Simulators of queues, networks and markets need random draws of every shape. Most start from uniform numbers and use this page’s method, or a cousin of it, for each law.

The envelope of noise. The strength of narrow-band radio noise, its envelope, is the distance from the origin of two independent bell-shaped parts: a Rayleigh variable. Fading radio channels are described the same way.

Decibels and growth. Quantities that grow by multiplying, such as incomes or signal strengths through many layers of fading, are often log-normal: their logarithm, in decibels, is a bell.

Squared errors. The square of a standard bell-shaped value is a chi-square variable; a bell-shaped error with mean 0, squared and divided by its variance, is one, and so is the power of a bell-shaped noise sample scaled the same way. Chi-square tests are built on sums of them.

In signal processing · the envelope of noise

In the signal processing course, The Hilbert transform and the analytic signal reads the envelope of narrow-band Gaussian noise, and finds it follows the Rayleigh law above: the distance from the origin of two independent bell-shaped parts of equal variance.

Reference card

QuantityFormulaNotes
Inverse CDF methodX=FX−1(U)X=F_X^{-1}(U), U∼U(0,1)U\sim\mathrm{U}(0,1)even dots into any shape
Exponential drawsx=−ln⁡(1−u)/λx=-\ln(1-u)/\lambda
Rayleigh drawsr=σ−2ln⁡(1−u)r=\sigma\sqrt{-2\ln(1-u)}mean 1.2533 σ1.2533\,\sigma
Function of XX, gg one-to-onefY(y)=fX(g−1(y)) ∣dg−1/dy∣f_Y(y)=f_X(g^{-1}(y))\,\lvert dg^{-1}/dy\rvertthe stretch factor keeps area 1
Steep FXF_Xdots crowd: high densityflat FXF_X: dots spread
Log-normal eZe^Zmedian 1, mean e1/2=1.6487e^{1/2}=1.6487
Chi-square, one degreeZ2Z^2, mean 1

Keep reading with a free account.

By creating an account you accept the terms of use.

Sign in

Already have an account? Use the service you signed up with.

Or keep reading: get new lessons by email

Are you 13 or over?

Accounts and email updates are for people aged 13 or over. We keep only your answer, never your date of birth.

End of the lesson

Where to go next.

Phasorium

Account

Sign inKeep your place on any device

Subjects

Site

Look