Lesson 5.2.1 defined a random variable as a number attached to each outcome. Apply a function g to that number and you attach a new number, y=g(x), to the same outcome. So Y=g(X) is a random variable too, and its probabilities come from those of X.
Take a fair die X and its squared distance from 3, Y=(X−3)2.
face x
1
2
3
4
5
6
y=(x−3)2
4
1
0
1
4
9
Two faces give y=1 and two give y=4, so those values collect two sixths each. The pmf of Y is
y
0
1
4
9
pY(y)
61
31
31
61
Rule: Pmf of a function
pY(y)=x:g(x)=y∑pX(x)
Add up the probabilities of every value x that g sends to y. Values of y that no x reaches have probability 0.
For the mean alone, LOTUS (lesson 5.3.1) skips pY: E[(X−3)2]=64+1+0+1+4+9=619≈3.1667. The pmf of Y gives the same, 0⋅61+1⋅31+4⋅31+9⋅61=619. It also answers questions LOTUS does not, such as P(Y≥4)=21.
Try it
The loop builds pY by adding 61 for each face, with exact fractions. Then the mean both ways.
The last lines roll 60,000 seeded dice. The shares of 0, 1, 4 and 9 come out near 61, 31, 31 and 61, and the mean near 3.1667. They come from one seeded run, rounded.
⌘/Ctrl + Enter runs the cell
Continuous X: the distribution-function method
A continuous random variable gives each single value probability 0 (lesson 5.4.1), so adding up probabilities of values fails. Work with the distribution function instead. FY(y)=P(Y≤y) is the probability that g(X)≤y, and that is an event about X.
Take U uniform on [0,1], with density 1 there, and Y=U2. For y between 0 and 1, U2≤y means U≤y, because U is never negative. So FY(y)=P(U≤y)=y.
y
0.04
0.25
0.81
FY(y)=y
0.2
0.5
0.9
simulated share of U2≤y
0.2006
0.4994
0.9004
Half the values of U2 lie below 0.25: squaring a number between 0 and 1 pushes it towards 0. Differentiating gives the density, fY(y)=FY′(y)=2y1 for 0<y≤1, and 0 elsewhere.
Rule: Distribution-function method
FY(y)=P(g(X)≤y)fY(y)=FY′(y)
Turn the event g(X)≤y into an event about X, find its probability with FX, then differentiate. It works for any g.
The density 2y1 is 5 at 0.01, 1 at 0.25 and 0.5 at 1. It blows up at 0, yet its area is still 1: an improper integral of the kind in lesson 2.6.3.
Try it
The first line prints y for the table. The cell then squares 100,000 seeded uniform draws and counts the share below each y.
quad (lesson 2.6.3) finds the area under 2y1, which blows up at 0; the area is 1.
⌘/Ctrl + Enter runs the cell
A monotone g: the density rule
When g is increasing, the method collapses into one formula. Write x=g−1(y) for the value that g sends to y. Then g(X)≤y exactly when X≤x, so FY(y)=FX(x), and the chain rule (lesson 2.3.3) gives fY(y)=fX(x)dydx.
The picture behind it is a thin strip. The strip of width dx around x holds probability about fX(x)dx, and g carries it onto a strip of width dy around y. The probability is the same, so fY(y)dy=fX(x)dx: where g stretches the strip, the density thins.
Take Y=eU, with U uniform on [0,1] in the part of X. It rises from 1 to e, and its inverse is u=lny, so dydu=y1. The density is fY(y)=1⋅y1 for 1≤y≤e.
Equal strips of u land on y-intervals of different widths. The same probability 0.1 spread over a wider interval makes a lower density, so fY(y)=y1 falls from 1 to e1.
The strips in numbers:
strip of u
[0,0.1]
[0.9,1]
lands on y in
[1,1.105171]
[2.459603,2.718282]
width in y
0.105171
0.258679
average density, 0.1÷ width
0.9508
0.3866
Rule: Density of a monotone function
fY(y)=fX(x)dydxx=g−1(y)
For y in the range of g, and 0 elsewhere. The factor dydx=∣g′(x)∣1 divides by the stretch. It is the change of variables of lesson 4.7.2 with a 1×1 Jacobian, which case study 4.7.5 used in two dimensions.
Probabilities follow by integrating the density. P(Y≤1.5)=∫11.5ydy=ln1.5≈0.405465, and P(Y≥2)=1−ln2≈0.306853. One seeded run of 100,000 draws gives 0.4014 and 0.3088.
The mean is ∫1ey⋅y1dy=e−1≈1.718282. LOTUS gives the same, ∫01eudu=e−1. It is not eE[U]=e0.5≈1.648721 (lesson 5.3.1).
Quantiles carry over
An increasing g keeps values in order, so it carries quantiles across. If half of U lies below 21, half of eU lies below e0.5: the median of Y is e0.5≈1.648721, and the simulation gives 1.6569.
The mean does not carry over in this way: it is e−1, larger than the median. Case study 5.6.5 meets the same gap in stock prices.
Try it
The loop maps both strips through eu and prints their widths and average densities.
Then 100,000 seeded draws of eU: the two probabilities, the mean (by quad and simulated) and the median, against the exact values.
⌘/Ctrl + Enter runs the cell
Linear functions: moving and stretching
For Y=aX+b with a=0, the inverse is x=ay−b and dydx=∣a∣1. So
fY(y)=∣a∣1fX(ay−b)
The shape is the same, moved by b and stretched by ∣a∣, and its height is divided by ∣a∣ so the area stays 1. A negative a also flips it: 1−U has density fU(1−y)=1 on [0,1], uniform again.
The normal. Take Z standard normal, with density φ, and X=μ+σZ with σ>0. The rule gives fX(x)=σ1φ(σx−μ), the N(μ,σ2) density of lesson 5.4.3: standardising read backwards. With μ=2 and σ=3, fX(2)=3φ(0)≈0.132981 and fX(5)=3φ(1)≈0.080657.
The lognormal. Exponentiate that normal X: as for eU, the inverse is x=lny with dydx=y1, so eX has density σy1φ(σlny−μ) for y>0. That is the lognormal density of lesson 5.4.4; its y1 is the stretch of ex. With μ=0 and σ=0.5 it is 0.38287 at y=1.5.
Units. A density is probability per unit of the variable. If X is uniform on [0,2] dollars, its density is 0.5 per dollar. In cents, 100X is uniform on [0,200] with density 0.005 per cent: a hundredth as tall.
Try it
The first line builds the N(2,9) density from φ at 2 and 5. SciPy's norm(2, 3) takes the standard deviation 3, not the variance, and agrees.
The third line builds the lognormal density at 1.5 and compares it with SciPy's lognorm (lesson 5.4.4). The last line is the density of 100X in cents.
⌘/Ctrl + Enter runs the cell
A decreasing g: from uniform to exponential
Y=−lnU falls as U rises: u near 1 gives y near 0, and u near 0 gives large y. Its inverse is u=e−y, with dydu=−e−y, which is negative. The absolute value gives fY(y)=1⋅e−y for y≥0: the exponential distribution with λ=1 (lesson 5.4.2).
Without the absolute value, the "density" −e−y would be negative. For any decreasing g, FY(y)=1−FX(x), and the minus sign of that 1−F cancels the minus sign of dydx.
Dividing by λ gives −λ1lnU, exponential with rate λ and mean λ1. A computer can turn uniform draws into exponential ones this way.
Why the inverse transform works
Lesson 5.4.1 drew X as xU: the quantile function of X applied to a uniform U, the inverse transform method. The distribution-function method shows why the draws have the distribution F. For an increasing, continuous F, xU≤x exactly when U≤F(x), so P(xU≤x)=P(U≤F(x))=F(x).
For the exponential, F(x)=1−e−λx gives the quantile xu=−λ1ln(1−u). Since 1−U is uniform too, −λ1lnU does the same job.
Try it
100,000 seeded uniform draws become exponential ones. The mean comes out near 1, and the share above 2 near e−2≈0.135335, which expon.sf(2) also gives.
With λ=4 the mean is near 41.
⌘/Ctrl + Enter runs the cell
When g folds: add the branches
If several values of x give the same y, each sends probability to y, and the contributions add. Take Y=Z2 with Z standard normal. Both z=−1 and z=1 give y=1.
By the distribution-function method, Z2≤y means −y≤Z≤y, so FY(y)=N(y)−N(−y) for y>0. Differentiate with N′=φ and the chain rule:
The two terms are the monotone rule on each branch, z=y and z=−y, each with dydz=2y1. The normal is symmetric, so they are equal.
Rule: Density when g folds
fY(y)=k∑fX(xk)dydxk
Add one term for every xk with g(xk)=y. Cut the line of x into pieces on which g is monotone; each piece gives one term.
Strips near z=1 and z=−1 both land near y=1, so the density of Z2 adds two branches. The simulated draws follow the two-branch density; one branch alone is half as tall.
This is the chi-squared distribution with 1 degree of freedom of lesson 5.4.4, chi2(1) in SciPy; at y=1 both give 0.241971. Its probabilities come from the normal: P(Z2≤1)=P(∣Z∣≤1)≈0.682689, the 68% of lesson 5.4.3. Since 1.962=3.8416, P(Z2≤3.8416)≈0.950004.
Try it
The loop compares 2πye−y/2 with SciPy's chi2(1).pdf. Then the two probabilities, and the area of the one-branch density: 0.5.
The last line squares 100,000 seeded normal draws: the shares come out near 0.6827 and 0.95.
⌘/Ctrl + Enter runs the cell
Where the density rule needs care
The range.fY is 0 outside the values g can take: eU lives on [1,e], while −lnU and Z2 live on [0,∞).
Flat points. Where g′(x)=0, as for U2 and Z2 at 0, the factor dydx is infinite and the density blows up. Its area stays finite, so this does no harm.
Flat stretches. If g is constant on an interval, Y piles probability onto one value, and there is no density there.
Y=max(Z,0) is such a function: it is 0 whenever Z≤0, so P(Y=0)=21. Above 0 it has density φ(y), whose area is the other half. An option's payoff, max(ST−K,0), has this shape (case study 2.6.5).
Try it
100,000 seeded draws of max(Z,0): the share exactly equal to 0 comes out near 21.
⌘/Ctrl + Enter runs the cell
Lesson 5.6.2 describes a distribution by a different function, its moment generating function. Case study 5.6.5 uses this lesson's rule to find the density of a future stock price.
Common mistakes
Keep the factor dydx. For eU, fU(lny)=1 alone on [1,e] has area e−1≈1.718282, not 1.
Divide by the stretch, do not multiply. The factor is dydx=g′(x)1, not dxdy.
Take the absolute value. For a decreasing g such as −lnU, dydx is negative, and without ∣∣ the density would be too.
Add every branch. One branch of Z2 gives half the density, with area 21.
Answer in y, with its range. Put x=g−1(y) into fX, and say where fY is 0.
The mean does not pass through g. The median of eU is e0.5, but its mean is e−1.
Functions of random variables
0 of 13 correct
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
How to type answers
Powers with ^: x^7, 2^(1/3), a^-2.
Multiply with * or by writing side by side: 3x^2, 2(x+1).
ln(x) is the natural log, log(x, 2) is log base 2, e is Euler's number, sqrt(x) is a square root.
sin(x), cos(x) and tan(x) take radians; write pi for π and sin(x)^2 for sin²x.
Your first answer to each question is saved: it counts towards your score and brings the question back for review.
Exact answers like ln(20)/ln(3) are best. Decimals need about 4 significant figures.
Summary card
Y=g(X) is a random variable. Discrete: pY(y)=∑g(x)=ypX(x).
Distribution-function method, for any g: FY(y)=P(g(X)≤y), turned into an event about X, then fY=FY′.
Monotone g: fY(y)=fX(x)dydx with x=g−1(y), the one-dimensional change of variables (lesson 4.7.2).
aX+b: fY(y)=∣a∣1fX(ay−b); μ+σZ is N(μ,σ2); −λ1lnU is exponential with rate λ.
When g folds, add a term for each branch: Z2 has density yφ(y), the chi-squared with 1 degree of freedom.
An increasing g carries quantiles across, not the mean. The quantile function of a uniform U has distribution F: the inverse transform.
A flat stretch of g puts a lump of probability on one value.