Unit 11, lesson 68

Transformations and Inverse-CDF Sampling

How the CDF method finds the distribution of a function of a random variable for monotone and many-to-one maps, why a density can be unbounded, and how inverse-CDF sampling uses the generalized quantile.

  • Video 68
  • Changing Variables and Sampling

A measurement error XX is equally likely to fall anywhere between −1-1 and 11, and a quality score records the squared error Y=X2Y = X^2. How likely is a squared error of at most 14\tfrac14? Squaring sends −0.4-0.4 and 0.40.4 to the same output, so the answer has to collect probability from errors of both signs. This lesson finds the distribution of a function of a random variable by following events through the function, and then runs the same idea backward to turn uniform random numbers into draws from a chosen distribution, discrete or continuous.

Key terms

CDF method

Definition Let XX be a random variable with a known distribution, let gg be a function defined at every value XX can take, and let Y=g(X)Y = g(X). The CDF method finds the CDF of YY at each real yy from

FY(y)=P(g(X)≤y)=P(X∈Ay),\begin{aligned} F_Y(y) &= P(g(X) \le y) \\[0.4em] &= P(X \in A_y), \end{aligned}

where Ay={x:g(x)≤y}A_y = \{x : g(x) \le y\} is the set of all inputs whose output is at most yy, the preimage of (−∞,y](-\infty, y]. First find the set of values YY can take. Below all of them FY(y)=0F_Y(y) = 0, and at or above all of them FY(y)=1F_Y(y) = 1. For the remaining yy, solve the inequality g(x)≤yg(x) \le y for xx and compute P(X∈Ay)P(X \in A_y) from the distribution of XX. If the resulting FYF_Y is continuous everywhere and differentiable at all but finitely many points, then fY=FY′f_Y = F_Y' is a density of YY, with any values at the exceptional points.

In plain words Work with events rather than with density formulas. The output event ”YY is at most yy” is the same event as ”XX lands among the inputs that produce such outputs”, so the two have the same probability. Once that input set is written down correctly, what remains is a probability calculation for XX.

Example A wait XX measured in minutes has X∼Exp⁡(2)X \sim \operatorname{Exp}(2), so its mean is half a minute. The same wait in seconds is Y=60XY = 60X. Every value of YY is nonnegative, so FY(y)=0F_Y(y) = 0 for y<0y < 0. For y≥0y \ge 0,

FY(y)=P(60X≤y)=P(X≤y/60)=1−e−y/30.\begin{aligned} F_Y(y) &= P(60X \le y) \\[0.4em] &= P(X \le y/60) \\[0.4em] &= 1 - e^{-y/30}. \end{aligned}

So Y∼Exp⁡(1/30)Y \sim \operatorname{Exp}(1/30), with rate 130\tfrac{1}{30} per second and mean 3030 seconds, the same half minute. Changing units divides the rate by the conversion factor.

Monotone transformation

Definition Let XX take its values in an interval II, let gg be continuous and strictly monotone on II, and let Y=g(X)Y = g(X). If gg is strictly increasing, then for every x0x_0 in II the events {g(X)≤g(x0)}\{g(X) \le g(x_0)\} and {X≤x0}\{X \le x_0\} are equal, so

FY(g(x0))=FX(x0).F_Y(g(x_0)) = F_X(x_0).

If gg is strictly decreasing, the inequality reverses. The event {g(X)≤g(x0)}\{g(X) \le g(x_0)\} equals {X≥x0}\{X \ge x_0\}, so

FY(g(x0))=P(X≥x0),F_Y(g(x_0)) = P(X \ge x_0),

which equals 1−FX(x0)1 - F_X(x_0) when XX is continuous. Each yy in the image g(I)g(I) is g(x0)g(x_0) for exactly one x0x_0 in II. For yy outside g(I)g(I), FY(y)F_Y(y) is 00 when yy lies below g(I)g(I) and 11 when yy lies above it.

In plain words An increasing map keeps the order of values, so “output at most yy” means “input at most the matching point”. A decreasing map turns the order around, so the same output event becomes an upper tail event for the input. Forgetting to flip the inequality is the usual mistake.

Example Let U∼Uniform⁡(0,1)U \sim \operatorname{Uniform}(0, 1). The map V=3+2UV = 3 + 2U is increasing, and for 3≤v≤53 \le v \le 5

FV(v)=P(U≤v−32)=v−32.\begin{aligned} F_V(v) &= P\Bigl(U \le \frac{v - 3}{2}\Bigr) \\[0.4em] &= \frac{v - 3}{2}. \end{aligned}

The map W=5−2UW = 5 - 2U is decreasing, and for 3≤w≤53 \le w \le 5

FW(w)=P(U≥5−w2)=1−5−w2=w−32.\begin{aligned} F_W(w) &= P\Bigl(U \ge \frac{5 - w}{2}\Bigr) \\[0.4em] &= 1 - \frac{5 - w}{2} \\[0.4em] &= \frac{w - 3}{2}. \end{aligned}

Both VV and WW are Uniform⁡(3,5)\operatorname{Uniform}(3, 5). They are still different random variables, since V+W=8V + W = 8. The draw U=0.1U = 0.1 gives V=3.2V = 3.2 and W=4.8W = 4.8. Two different maps can produce the same distribution.

Many-to-one transformation

Definition A function gg is many-to-one on the values of XX when some output is produced by two or more different inputs that XX can take. Then an output event can come from inputs on several branches of the inverse, and FY(y)F_Y(y) must include the probability of every input xx with g(x)≤yg(x) \le y, on every branch. For Y=X2Y = X^2, FY(y)=0F_Y(y) = 0 when y<0y < 0, and for y≥0y \ge 0

{X2≤y}={−y≤X≤y}.\begin{aligned} &\{X^2 \le y\} \\[0.4em] &\quad = \{-\sqrt{y} \le X \le \sqrt{y}\}. \end{aligned}

So for a continuous XX and y≥0y \ge 0,

FY(y)=FX(y)−FX(−y).\begin{aligned} F_Y(y) &= F_X(\sqrt{y}) \\[0.4em] &\quad - F_X(-\sqrt{y}). \end{aligned}

In plain words Before solving for xx, ask how many inputs give each output. Solving x2=yx^2 = y as x=yx = \sqrt{y} alone throws away the negative inputs and the probability they carry.

Example Let X∼Uniform⁡(−1,1)X \sim \operatorname{Uniform}(-1, 1) and Y=X2Y = X^2. The values of YY lie in [0,1][0, 1]. For 0≤y≤10 \le y \le 1 the input interval from −y-\sqrt{y} to y\sqrt{y} has length 2y2\sqrt{y} out of a total length 22, so

FY(y)={0,y<0,y,0≤y≤1,1,y>1.F_Y(y) = \begin{cases} 0, & y < 0, \\ \sqrt{y}, & 0 \le y \le 1, \\ 1, & y > 1. \end{cases}

In particular P(Y≤14)=12P(Y \le \tfrac14) = \tfrac12, which agrees with P(−12≤X≤12)=12P(-\tfrac12 \le X \le \tfrac12) = \tfrac12. Keeping only the branch 0≤X≤y0 \le X \le \sqrt{y} would give y/2\sqrt{y}/2, which is only 12\tfrac12 at y=1y = 1, although the event Y≤1Y \le 1 is certain.

Unbounded density

Definition A density ff is unbounded when it takes arbitrarily large values, that is, for every number MM there is a point yy with f(y)>Mf(y) > M. This is allowed. A density only has to be nonnegative with total integral 11, so its graph may rise without bound near a point as long as the area under it stays finite. A random variable with an unbounded density is still continuous, so every single value has probability 00.

In plain words A tall density near a point means that short intervals near that point carry a lot of probability for their length. It does not put probability on the point itself. A density value is not a probability, so it may exceed 11.

Example For Y=X2Y = X^2 with X∼Uniform⁡(−1,1)X \sim \operatorname{Uniform}(-1, 1), differentiating FY(y)=yF_Y(y) = \sqrt{y} on (0,1)(0, 1) gives

fY(y)=12y,f_Y(y) = \frac{1}{2\sqrt{y}},

and fY(y)=0f_Y(y) = 0 outside [0,1][0, 1]. Near 00 it grows without bound, for example fY(0.0001)=50f_Y(0.0001) = 50, yet

∫01dy2y=[y ]01=1.\int_0^1 \frac{dy}{2\sqrt{y}} = \Bigl[\sqrt{y}\,\Bigr]_0^1 = 1.

The short interval [0,0.01][0, 0.01] and the much longer interval (0.81,1](0.81, 1] each have probability 0.10.1, since 0.01=0.1\sqrt{0.01} = 0.1 and 1−0.81=0.11 - \sqrt{0.81} = 0.1. The single value 00 has P(Y=0)=P(X=0)=0P(Y = 0) = P(X = 0) = 0.

Inverse-CDF sampling

Definition Let U∼Uniform⁡(0,1)U \sim \operatorname{Uniform}(0, 1), so P(U≤u)=uP(U \le u) = u for 0≤u≤10 \le u \le 1. Let FF be a CDF that is continuous and strictly increasing on an interval (a,b)(a, b), where aa may be −∞-\infty and bb may be ∞\infty, with F(x)→0F(x) \to 0 as x→ax \to a and F(x)→1F(x) \to 1 as x→bx \to b. Then FF maps (a,b)(a, b) one to one onto (0,1)(0, 1), and since UU lies in (0,1)(0, 1) with probability 11, the random variable X=F−1(U)X = F^{-1}(U) is defined with probability 11. It has CDF FF. For xx in (a,b)(a, b) the events {F−1(U)≤x}\{F^{-1}(U) \le x\} and {U≤F(x)}\{U \le F(x)\} agree whenever UU lies in (0,1)(0, 1), so

P(X≤x)=P(U≤F(x))=F(x),\begin{aligned} P(X \le x) &= P(U \le F(x)) \\[0.4em] &= F(x), \end{aligned}

and for x≤ax \le a or x≥bx \ge b both sides are 00 or both are 11. Generating XX this way is inverse-CDF sampling. The next card removes the conditions of continuity and strict increase. The reverse statement, that F(X)F(X) is Uniform⁡(0,1)\operatorname{Uniform}(0, 1) when XX has CDF FF, holds whenever FF is continuous, but it fails for discrete distributions.

In plain words Read a uniform number uu as a probability level, and take as the sample the point where the CDF reaches that level. Where FF rises steeply, many levels land in a short stretch of xx values, so values there come up often.

Example For Exp⁡(λ)\operatorname{Exp}(\lambda) with rate λ>0\lambda > 0, the CDF is F(x)=1−e−λxF(x) = 1 - e^{-\lambda x} for x≥0x \ge 0, so a=0a = 0 and b=∞b = \infty. Solving u=1−e−λxu = 1 - e^{-\lambda x} for 0<u<10 < u < 1 gives

F−1(u)=−ln⁡(1−u)λ,F^{-1}(u) = -\frac{\ln(1 - u)}{\lambda},

so X=−ln⁡(1−U)/λX = -\ln(1 - U)/\lambda has the distribution Exp⁡(λ)\operatorname{Exp}(\lambda). With λ=2\lambda = 2 and u=12u = \tfrac12 the draw is 12ln⁡2≈0.3466\tfrac12 \ln 2 \approx 0.3466, the median of Exp⁡(2)\operatorname{Exp}(2). The formula needs u<1u < 1. Many software generators return numbers in [0,1)[0, 1), which keeps ln⁡(1−u)\ln(1 - u) finite. The variant −ln⁡(U)/λ-\ln(U)/\lambda is also Exp⁡(λ)\operatorname{Exp}(\lambda) in theory, because 1−U1 - U is uniform too, but it fails if the generator returns exactly 00.

Generalized quantile

Definition For any CDF FF and any uu with 0<u<10 < u < 1, the generalized quantile is

Q(u)=inf⁡{x:F(x)≥u}.Q(u) = \inf\{x : F(x) \ge u\}.

The set is nonempty and bounded below, because FF tends to 11 on the right and to 00 on the left, and right-continuity of FF makes the infimum attained, so F(Q(u))≥uF(Q(u)) \ge u. For every real xx and every uu in (0,1)(0, 1),

Q(u)≤x  ⟺  u≤F(x).Q(u) \le x \iff u \le F(x).

Hence X=Q(U)X = Q(U) has CDF FF when U∼Uniform⁡(0,1)U \sim \operatorname{Uniform}(0, 1), with no continuity or strict increase required. When FF is continuous and strictly increasing where 0<F<10 < F < 1, the generalized quantile is the ordinary inverse F−1F^{-1} of the previous card.

In plain words The generalized quantile at level uu is the first point where the CDF reaches uu. At a jump of the CDF a whole range of levels lands on the same value, and the length of that range is the probability of the value. Points inside a flat stretch of the CDF are never produced, which matches the zero probability there.

Example Let XX take the values 00, 11 and 22 with probabilities 0.250.25, 0.50.5 and 0.250.25. Its CDF is 0.250.25 on [0,1)[0, 1), 0.750.75 on [1,2)[1, 2) and 11 from 22 on, and the generalized quantile is constant on three ranges of levels.

Level uu(0,0.25](0, 0.25](0.25,0.75](0.25, 0.75](0.75,1)(0.75, 1)
Q(u)Q(u)001122

The levels 0.20.2, 0.60.6 and 0.90.9 give the draws 00, 11 and 22. The three ranges of levels have lengths 0.250.25, 0.50.5 and 0.250.25, exactly the masses. The level 0.250.25 itself gives 00, since F(0)=0.25≥0.25F(0) = 0.25 \ge 0.25, and such single levels have probability 00 anyway. The reverse statement fails for this XX, because F(X)F(X) takes only the values 0.250.25, 0.750.75 and 11, so it is not uniform.

Check yourself

  1. Let U∼Uniform⁡(0,1)U \sim \operatorname{Uniform}(0, 1) and V=U2V = U^2. Find the CDF of VV and its generalized quantile, and check that the quantile at level 0.750.75 is 0.56250.5625.
Show the answer

The values of VV lie in [0,1][0, 1], so FV(v)=0F_V(v) = 0 for v<0v < 0 and FV(v)=1F_V(v) = 1 for v>1v > 1. Since U≥0U \ge 0, squaring is strictly increasing on the values of UU, and for 0≤v≤10 \le v \le 1

FV(v)=P(U≤v)=v.F_V(v) = P(U \le \sqrt{v}) = \sqrt{v}.

On (0,1)(0, 1) this CDF is continuous and strictly increasing, so the quantile solves r=vr = \sqrt{v}, which gives Q(r)=r2Q(r) = r^2 for 0<r<10 < r < 1. Then Q(0.75)=0.752=0.5625Q(0.75) = 0.75^2 = 0.5625, and the check FV(0.5625)=0.5625=0.75F_V(0.5625) = \sqrt{0.5625} = 0.75 returns the level. This is the same CDF as that of X2X^2 for X∼Uniform⁡(−1,1)X \sim \operatorname{Uniform}(-1, 1). A monotone map on [0,1][0, 1] and a two-branch map on [−1,1][-1, 1] give one distribution.

  1. Let X∼Uniform⁡(−1,2)X \sim \operatorname{Uniform}(-1, 2) and Y=X2Y = X^2. Find FY(y)F_Y(y) for every real yy and the probability P(Y≤94)P(Y \le \tfrac94). What goes wrong if you keep only the branch X=YX = \sqrt{Y}?
Show the answer

The values of YY lie in [0,4][0, 4], and XX has density 13\tfrac13 on (−1,2)(-1, 2). For 0≤y≤10 \le y \le 1 both −y-\sqrt{y} and y\sqrt{y} lie in [−1,2][-1, 2], so FY(y)=P(−y≤X≤y)=2y/3F_Y(y) = P(-\sqrt{y} \le X \le \sqrt{y}) = 2\sqrt{y}/3. For 1<y≤41 < y \le 4 the negative branch is cut off at −1-1, so FY(y)=P(−1≤X≤y)=(1+y)/3F_Y(y) = P(-1 \le X \le \sqrt{y}) = (1 + \sqrt{y})/3. Altogether FY(y)=0F_Y(y) = 0 for y<0y < 0, FY(y)=1F_Y(y) = 1 for y>4y > 4, and in between

FY(y)=2y3  on [0,1],FY(y)=1+y3  on (1,4].\begin{gathered} F_Y(y) = \frac{2\sqrt{y}}{3} \ \text{ on } [0, 1], \\[0.4em] F_Y(y) = \frac{1 + \sqrt{y}}{3} \ \text{ on } (1, 4]. \end{gathered}

Both formulas give 23\tfrac23 at y=1y = 1. Then P(Y≤94)=(1+32)/3=56P(Y \le \tfrac94) = (1 + \tfrac32)/3 = \tfrac56. Keeping only the positive branch gives P(0≤X≤y)=y/3P(0 \le X \le \sqrt{y}) = \sqrt{y}/3, which reaches only 23\tfrac23 at y=4y = 4. The missing 13\tfrac13 is P(−1<X<0)P(-1 < X < 0), the probability of the negative inputs.

  1. Let XX take the values 11, 22 and 33 with probabilities 0.20.2, 0.30.3 and 0.50.5. Write its generalized quantile and the draws it gives at the levels 0.10.1, 0.50.5 and 0.70.7. A classmate claims that F(X)F(X) is Uniform⁡(0,1)\operatorname{Uniform}(0, 1) for every random variable XX with CDF FF. Test the claim on this XX.
Show the answer

The CDF is 0.20.2 on [1,2)[1, 2), 0.50.5 on [2,3)[2, 3) and 11 from 33 on, so the generalized quantile is as follows.

Level uu(0,0.2](0, 0.2](0.2,0.5](0.2, 0.5](0.5,1)(0.5, 1)
Q(u)Q(u)112233

The level 0.10.1 gives 11, the level 0.50.5 gives 22 because F(2)=0.5≥0.5F(2) = 0.5 \ge 0.5, and the level 0.70.7 gives 33. The claim fails. The variable F(X)F(X) takes the values 0.20.2, 0.50.5 and 11 with probabilities 0.20.2, 0.30.3 and 0.50.5, so P(F(X)≤0.4)=0.2P(F(X) \le 0.4) = 0.2, while a uniform variable would give 0.40.4. The claim is true when FF is continuous. Sampling with Q(U)Q(U) still works for this discrete XX, because it runs from UU to XX and rests only on the equivalence Q(u)≤x  ⟺  u≤F(x)Q(u) \le x \iff u \le F(x).

How this lesson connects