SigmaField
Mathematical Foundations for Quant Finance
0 of 256 lessons
      1. 5.6.1 Functions of random variablesConcept · 35 min
      2. 5.6.2 Moment generating and characteristic functionsConcept · 30 min
      3. 5.6.3 Sums of independent random variablesConcept · 25 min
      4. 5.6.4 Practice set: transformationsPractice set · 20 min
      5. 5.6.5 Case study: the lognormal from the normalCase study · 30 min

Level 5, Probability and statistics 5.6 Transformations and generating functions Lesson 1 of 5

ConceptLesson 5.6.1 in Level 5, Probability and statistics. About 35 minutes.

Functions of random variables

A function of a random variable

Lesson 5.2.1 defined a random variable as a number attached to each outcome. Apply a function gg to that number and you attach a new number, y=g(x)y = g(x), to the same outcome. So Y=g(X)Y = g(X) is a random variable too, and its probabilities come from those of XX.

Take a fair die XX and its squared distance from 3, Y=(X−3)2Y = (X - 3)^2.

face xx123456
y=(x−3)2y = (x - 3)^2410149

Two faces give y=1y = 1 and two give y=4y = 4, so those values collect two sixths each. The pmf of YY is

yy0149
pY(y)p_Y(y)16\frac{1}{6}13\frac{1}{3}13\frac{1}{3}16\frac{1}{6}

Rule: Pmf of a function

pY(y)=∑x : g(x)=ypX(x)p_Y(y) = \sum_{x \,:\, g(x) = y} p_X(x)

Add up the probabilities of every value xx that gg sends to yy. Values of yy that no xx reaches have probability 0.

For the mean alone, LOTUS (lesson 5.3.1) skips pYp_Y: E[(X−3)2]=4+1+0+1+4+96=196≈3.1667\mathbb{E}[(X - 3)^2] = \frac{4 + 1 + 0 + 1 + 4 + 9}{6} = \frac{19}{6} \approx 3.1667. The pmf of YY gives the same, 0⋅16+1⋅13+4⋅13+9⋅16=1960 \cdot \frac{1}{6} + 1 \cdot \frac{1}{3} + 4 \cdot \frac{1}{3} + 9 \cdot \frac{1}{6} = \frac{19}{6}. It also answers questions LOTUS does not, such as P(Y≥4)=12P(Y \ge 4) = \frac{1}{2}.

Try it

The loop builds pYp_Y by adding 16\frac{1}{6} 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 16\frac{1}{6}, 13\frac{1}{3}, 13\frac{1}{3} and 16\frac{1}{6}, and the mean near 3.1667. They come from one seeded run, rounded.

⌘/Ctrl + Enter runs the cell

Continuous XX: 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)F_Y(y) = P(Y \le y) is the probability that g(X)≤yg(X) \le y, and that is an event about XX.

Take UU uniform on [0,1][0, 1], with density 1 there, and Y=U2Y = U^2. For yy between 0 and 1, U2≤yU^2 \le y means U≤yU \le \sqrt{y}, because UU is never negative. So FY(y)=P(U≤y)=yF_Y(y) = P(U \le \sqrt{y}) = \sqrt{y}.

yy0.040.250.81
FY(y)=yF_Y(y) = \sqrt{y}0.20.50.9
simulated share of U2≤yU^2 \le y0.20060.49940.9004

Half the values of U2U^2 lie below 0.25: squaring a number between 0 and 1 pushes it towards 0. Differentiating gives the density, fY(y)=FY′(y)=12yf_Y(y) = F_Y'(y) = \frac{1}{2\sqrt{y}} for 0<y≤10 < y \le 1, and 0 elsewhere.

Rule: Distribution-function method

FY(y)=P(g(X)≤y)fY(y)=FY′(y)\begin{gathered} F_Y(y) = P\big(g(X) \le y\big) \\[4pt] f_Y(y) = F_Y'(y) \end{gathered}

Turn the event g(X)≤yg(X) \le y into an event about XX, find its probability with FXF_X, then differentiate. It works for any gg.

The density 12y\frac{1}{2\sqrt{y}} 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\sqrt{y} for the table. The cell then squares 100,000 seeded uniform draws and counts the share below each yy.

quad (lesson 2.6.3) finds the area under 12y\frac{1}{2\sqrt{y}}, which blows up at 0; the area is 1.

⌘/Ctrl + Enter runs the cell

A monotone gg: the density rule

When gg is increasing, the method collapses into one formula. Write x=g−1(y)x = g^{-1}(y) for the value that gg sends to yy. Then g(X)≤yg(X) \le y exactly when X≤xX \le x, so FY(y)=FX(x)F_Y(y) = F_X(x), and the chain rule (lesson 2.3.3) gives fY(y)=fX(x)dxdyf_Y(y) = f_X(x)\frac{dx}{dy}.

The picture behind it is a thin strip. The strip of width dxdx around xx holds probability about fX(x) dxf_X(x)\,dx, and gg carries it onto a strip of width dydy around yy. The probability is the same, so fY(y) dy=fX(x) dxf_Y(y)\,dy = f_X(x)\,dx: where gg stretches the strip, the density thins.

Take Y=eUY = e^U, with UU uniform on [0,1][0, 1] in the part of XX. It rises from 1 to ee, and its inverse is u=ln⁡yu = \ln y, so dudy=1y\frac{du}{dy} = \frac{1}{y}. The density is fY(y)=1⋅1yf_Y(y) = 1 \cdot \frac{1}{y} for 1≤y≤e1 \le y \le e.

Equal strips of uu land on yy-intervals of different widths. The same probability 0.1 spread over a wider interval makes a lower density, so fY(y)=1yf_Y(y) = \frac{1}{y} falls from 1 to 1e\frac{1}{e}.

The strips in numbers:

strip of uu[0,0.1][0, 0.1][0.9,1][0.9, 1]
lands on yy in[1,1.105171][1, 1.105171][2.459603,2.718282][2.459603, 2.718282]
width in yy0.1051710.258679
average density, 0.1÷0.1 \div width0.95080.3866

Rule: Density of a monotone function

fY(y)=fX(x)∣dxdy∣x=g−1(y)\begin{gathered} f_Y(y) = f_X(x)\left\lvert \frac{dx}{dy} \right\rvert \\[4pt] x = g^{-1}(y) \end{gathered}

For yy in the range of gg, and 0 elsewhere. The factor ∣dxdy∣=1∣g′(x)∣\left\lvert \frac{dx}{dy} \right\rvert = \frac{1}{\lvert g'(x) \rvert} divides by the stretch. It is the change of variables of lesson 4.7.2 with a 1×11 \times 1 Jacobian, which case study 4.7.5 used in two dimensions.

Probabilities follow by integrating the density. P(Y≤1.5)=∫11.5dyy=ln⁡1.5≈0.405465P(Y \le 1.5) = \int_1^{1.5} \frac{dy}{y} = \ln 1.5 \approx 0.405465, and P(Y≥2)=1−ln⁡2≈0.306853P(Y \ge 2) = 1 - \ln 2 \approx 0.306853. One seeded run of 100,000 draws gives 0.4014 and 0.3088.

The mean is ∫1ey⋅1y dy=e−1≈1.718282\int_1^e y \cdot \frac{1}{y}\,dy = e - 1 \approx 1.718282. LOTUS gives the same, ∫01eu du=e−1\int_0^1 e^u\,du = e - 1. It is not eE[U]=e0.5≈1.648721e^{\mathbb{E}[U]} = e^{0.5} \approx 1.648721 (lesson 5.3.1).

Quantiles carry over

An increasing gg keeps values in order, so it carries quantiles across. If half of UU lies below 12\frac{1}{2}, half of eUe^U lies below e0.5e^{0.5}: the median of YY is e0.5≈1.648721e^{0.5} \approx 1.648721, and the simulation gives 1.6569.

The mean does not carry over in this way: it is e−1e - 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 eue^u and prints their widths and average densities.

Then 100,000 seeded draws of eUe^U: 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+bY = aX + b with a≠0a \neq 0, the inverse is x=y−bax = \frac{y - b}{a} and ∣dxdy∣=1∣a∣\left\lvert \frac{dx}{dy} \right\rvert = \frac{1}{\lvert a \rvert}. So

fY(y)=1∣a∣ fX ⁣(y−ba)f_Y(y) = \frac{1}{\lvert a \rvert}\, f_X\!\left(\frac{y - b}{a}\right)

The shape is the same, moved by bb and stretched by ∣a∣\lvert a \rvert, and its height is divided by ∣a∣\lvert a \rvert so the area stays 1. A negative aa also flips it: 1−U1 - U has density fU(1−y)=1f_U(1 - y) = 1 on [0,1][0, 1], uniform again.

The normal. Take ZZ standard normal, with density φ\varphi, and X=μ+σZX = \mu + \sigma Z with σ>0\sigma > 0. The rule gives fX(x)=1σφ ⁣(x−μσ)f_X(x) = \frac{1}{\sigma}\varphi\!\left(\frac{x - \mu}{\sigma}\right), the N(μ,σ2)\mathcal{N}(\mu, \sigma^2) density of lesson 5.4.3: standardising read backwards. With μ=2\mu = 2 and σ=3\sigma = 3, fX(2)=φ(0)3≈0.132981f_X(2) = \frac{\varphi(0)}{3} \approx 0.132981 and fX(5)=φ(1)3≈0.080657f_X(5) = \frac{\varphi(1)}{3} \approx 0.080657.

The lognormal. Exponentiate that normal XX: as for eUe^U, the inverse is x=ln⁡yx = \ln y with dxdy=1y\frac{dx}{dy} = \frac{1}{y}, so eXe^X has density 1σyφ ⁣(ln⁡y−μσ)\frac{1}{\sigma y}\varphi\!\left(\frac{\ln y - \mu}{\sigma}\right) for y>0y > 0. That is the lognormal density of lesson 5.4.4; its 1y\frac{1}{y} is the stretch of exe^x. With μ=0\mu = 0 and σ=0.5\sigma = 0.5 it is 0.38287 at y=1.5y = 1.5.

Units. A density is probability per unit of the variable. If XX is uniform on [0,2][0, 2] dollars, its density is 0.5 per dollar. In cents, 100X100X is uniform on [0,200][0, 200] with density 0.005 per cent: a hundredth as tall.

Try it

The first line builds the N(2,9)\mathcal{N}(2, 9) density from φ\varphi 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 100X100X in cents.

⌘/Ctrl + Enter runs the cell

A decreasing gg: from uniform to exponential

Y=−ln⁡UY = -\ln U falls as UU rises: uu near 1 gives yy near 0, and uu near 0 gives large yy. Its inverse is u=e−yu = e^{-y}, with dudy=−e−y\frac{du}{dy} = -e^{-y}, which is negative. The absolute value gives fY(y)=1⋅e−yf_Y(y) = 1 \cdot e^{-y} for y≥0y \ge 0: the exponential distribution with λ=1\lambda = 1 (lesson 5.4.2).

Without the absolute value, the "density" −e−y-e^{-y} would be negative. For any decreasing gg, FY(y)=1−FX(x)F_Y(y) = 1 - F_X(x), and the minus sign of that 1−F1 - F cancels the minus sign of dxdy\frac{dx}{dy}.

Dividing by λ\lambda gives −1λln⁡U-\frac{1}{\lambda}\ln U, exponential with rate λ\lambda and mean 1λ\frac{1}{\lambda}. A computer can turn uniform draws into exponential ones this way.

Why the inverse transform works

Lesson 5.4.1 drew XX as xUx_U: the quantile function of XX applied to a uniform UU, the inverse transform method. The distribution-function method shows why the draws have the distribution FF. For an increasing, continuous FF, xU≤xx_U \le x exactly when U≤F(x)U \le F(x), so P(xU≤x)=P(U≤F(x))=F(x)P(x_U \le x) = P\big(U \le F(x)\big) = F(x).

For the exponential, F(x)=1−e−λxF(x) = 1 - e^{-\lambda x} gives the quantile xu=−1λln⁡(1−u)x_u = -\frac{1}{\lambda}\ln(1 - u). Since 1−U1 - U is uniform too, −1λln⁡U-\frac{1}{\lambda}\ln U 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.135335e^{-2} \approx 0.135335, which expon.sf(2) also gives.

With λ=4\lambda = 4 the mean is near 14\frac{1}{4}.

⌘/Ctrl + Enter runs the cell

When gg folds: add the branches

If several values of xx give the same yy, each sends probability to yy, and the contributions add. Take Y=Z2Y = Z^2 with ZZ standard normal. Both z=−1z = -1 and z=1z = 1 give y=1y = 1.

By the distribution-function method, Z2≤yZ^2 \le y means −y≤Z≤y-\sqrt{y} \le Z \le \sqrt{y}, so FY(y)=N(y)−N(−y)F_Y(y) = N(\sqrt{y}) - N(-\sqrt{y}) for y>0y > 0. Differentiate with N′=φN' = \varphi and the chain rule:

fY(y)=φ(y) 12y+φ(−y) 12y=φ(y)y=e−y/22πy\begin{aligned} f_Y(y) &= \varphi(\sqrt{y})\,\frac{1}{2\sqrt{y}} + \varphi(-\sqrt{y})\,\frac{1}{2\sqrt{y}} \\ &= \frac{\varphi(\sqrt{y})}{\sqrt{y}} = \frac{e^{-y/2}}{\sqrt{2\pi y}} \end{aligned}

The two terms are the monotone rule on each branch, z=yz = \sqrt{y} and z=−yz = -\sqrt{y}, each with ∣dzdy∣=12y\left\lvert \frac{dz}{dy} \right\rvert = \frac{1}{2\sqrt{y}}. The normal is symmetric, so they are equal.

Rule: Density when g folds

fY(y)=∑kfX(xk)∣dxkdy∣f_Y(y) = \sum_k f_X(x_k)\left\lvert \frac{dx_k}{dy} \right\rvert

Add one term for every xkx_k with g(xk)=yg(x_k) = y. Cut the line of xx into pieces on which gg is monotone; each piece gives one term.

Strips near z=1z = 1 and z=−1z = -1 both land near y=1y = 1, so the density of Z2Z^2 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=1y = 1 both give 0.241971. Its probabilities come from the normal: P(Z2≤1)=P(∣Z∣≤1)≈0.682689P(Z^2 \le 1) = P(\lvert Z \rvert \le 1) \approx 0.682689, the 68% of lesson 5.4.3. Since 1.962=3.84161.96^2 = 3.8416, P(Z2≤3.8416)≈0.950004P(Z^2 \le 3.8416) \approx 0.950004.

Try it

The loop compares e−y/22πy\frac{e^{-y/2}}{\sqrt{2\pi y}} 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. fYf_Y is 0 outside the values gg can take: eUe^U lives on [1,e][1, e], while −ln⁡U-\ln U and Z2Z^2 live on [0,∞)[0, \infty).
  • Flat points. Where g′(x)=0g'(x) = 0, as for U2U^2 and Z2Z^2 at 0, the factor ∣dxdy∣\left\lvert \frac{dx}{dy} \right\rvert is infinite and the density blows up. Its area stays finite, so this does no harm.
  • Flat stretches. If gg is constant on an interval, YY piles probability onto one value, and there is no density there.

Y=max⁡(Z,0)Y = \max(Z, 0) is such a function: it is 0 whenever Z≤0Z \le 0, so P(Y=0)=12P(Y = 0) = \frac{1}{2}. Above 0 it has density φ(y)\varphi(y), whose area is the other half. An option's payoff, max⁡(ST−K,0)\max(S_T - K, 0), has this shape (case study 2.6.5).

Try it

100,000 seeded draws of max⁡(Z,0)\max(Z, 0): the share exactly equal to 0 comes out near 12\frac{1}{2}.

⌘/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

  1. Keep the factor ∣dxdy∣\left\lvert \frac{dx}{dy} \right\rvert. For eUe^U, fU(ln⁡y)=1f_U(\ln y) = 1 alone on [1,e][1, e] has area e−1≈1.718282e - 1 \approx 1.718282, not 1.
  2. Divide by the stretch, do not multiply. The factor is dxdy=1g′(x)\frac{dx}{dy} = \frac{1}{g'(x)}, not dydx\frac{dy}{dx}.
  3. Take the absolute value. For a decreasing gg such as −ln⁡U-\ln U, dxdy\frac{dx}{dy} is negative, and without ∣  ∣\lvert\;\rvert the density would be too.
  4. Add every branch. One branch of Z2Z^2 gives half the density, with area 12\frac{1}{2}.
  5. Answer in yy, with its range. Put x=g−1(y)x = g^{-1}(y) into fXf_X, and say where fYf_Y is 0.
  6. The mean does not pass through gg. The median of eUe^U is e0.5e^{0.5}, but its mean is e−1e - 1.

Functions of random variables

0 of 13 correct

  1. 1.
  2. 2.
  3. 3.
  4. 4.
  5. 5.
  6. 6.
  7. 7.
  8. 8.
  9. 9.
  10. 10.
  11. 11.
  12. 12.
  13. 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)Y = g(X) is a random variable. Discrete: pY(y)=∑g(x)=ypX(x)p_Y(y) = \sum_{g(x) = y} p_X(x).
  • Distribution-function method, for any gg: FY(y)=P(g(X)≤y)F_Y(y) = P(g(X) \le y), turned into an event about XX, then fY=FY′f_Y = F_Y'.
  • Monotone gg: fY(y)=fX(x)∣dxdy∣f_Y(y) = f_X(x)\left\lvert \frac{dx}{dy} \right\rvert with x=g−1(y)x = g^{-1}(y), the one-dimensional change of variables (lesson 4.7.2).
  • aX+baX + b: fY(y)=1∣a∣fX(y−ba)f_Y(y) = \frac{1}{\lvert a \rvert}f_X\left(\frac{y - b}{a}\right); μ+σZ\mu + \sigma Z is N(μ,σ2)\mathcal{N}(\mu, \sigma^2); −1λln⁡U-\frac{1}{\lambda}\ln U is exponential with rate λ\lambda.
  • When gg folds, add a term for each branch: Z2Z^2 has density φ(y)y\frac{\varphi(\sqrt{y})}{\sqrt{y}}, the chi-squared with 1 degree of freedom.
  • An increasing gg carries quantiles across, not the mean. The quantile function of a uniform UU has distribution FF: the inverse transform.
  • A flat stretch of gg puts a lump of probability on one value.

Free further reading

Optional. This lesson covers what you need; these add more examples and practice. All are free to read.