Skip to content
LeetQuant
F4 Random Variables and Expectation

F4 · Random Variables and Expectation · Lesson 6 of 8

Transformations of Random Variables

Find the distribution of a function of random variables: the CDF method, the change-of-variables formula, convolution for sums, minima and maxima, and the lognormal.

18 min

The question

You know the distribution of XX (or of XX and YY) and want the distribution of Y=g(X)Y = g(X), of X+YX + Y, or of max⁡(X,Y)\max(X, Y). Interviewers ask this directly ("what is the density of U2U^2?") and indirectly, whenever a payoff, a price or a waiting time is a function of something simpler.

The intuition: probability mass is moved, not created. A transformation carries each small piece of probability to a new location. Where the map stretches an interval, the same mass is spread thinner and the density falls; where it compresses, the density rises.

The CDF method always works

Go through the CDF, never straight to the density:

FY(y)=P(g(X)≤y)=P(X∈{x:g(x)≤y}),F_Y(y) = P(g(X) \le y) = P\big(X \in \{x : g(x) \le y\}\big),

then differentiate. The only real work is describing the set {x:g(x)≤y}\{x : g(x) \le y\} correctly.

Example. U∼Uniform(0,1)U \sim \text{Uniform}(0, 1) and Y=U2Y = U^2. For 0≤y≤10 \le y \le 1, P(U2≤y)=P(U≤y)=yP(U^2 \le y) = P(U \le \sqrt y) = \sqrt y, so fY(y)=12yf_Y(y) = \dfrac{1}{2\sqrt y}. The density blows up near 0 because squaring crushes [0,0.1][0, 0.1] into [0,0.01][0, 0.01].

Non-monotone example. Z∼N(0,1)Z \sim N(0, 1) and Y=Z2Y = Z^2. For y>0y > 0, P(Z2≤y)=P(−y≤Z≤y)=2Φ(y)−1P(Z^2 \le y) = P(-\sqrt y \le Z \le \sqrt y) = 2\Phi(\sqrt y) - 1. Differentiating,

fY(y)=2φ(y)2y=12πy e−y/2,y>0,f_Y(y) = \frac{2\varphi(\sqrt y)}{2\sqrt y} = \frac{1}{\sqrt{2\pi y}}\,e^{-y/2}, \qquad y > 0,

the chi-squared distribution with one degree of freedom. Both branches ±y\pm\sqrt y contribute.

The change-of-variables formula

If gg is strictly monotone and differentiable with inverse h=g−1h = g^{-1}, differentiating the CDF gives

fY(y)=fX(h(y)) ∣h′(y)∣.f_Y(y) = f_X\big(h(y)\big)\,\left|h'(y)\right| .

The factor ∣h′(y)∣|h'(y)| is the stretching correction: it converts "mass per unit of xx" into "mass per unit of yy". For a map that is monotone on pieces, sum this expression over every xx with g(x)=yg(x) = y, as in the Z2Z^2 example.

Two consequences worth knowing:

  • Quantiles commute with increasing maps; means do not. The median of g(X)g(X) is g(median of X)g(\text{median of } X), but E[g(X)]≠g(E[X])\mathbb{E}[g(X)] \ne g(\mathbb{E}[X]) unless gg is linear. Jensen's inequality says which way: E[g(X)]≥g(E[X])\mathbb{E}[g(X)] \ge g(\mathbb{E}[X]) for convex gg.
  • Probability integral transform. If XX is continuous with CDF FF, then F(X)∼Uniform(0,1)F(X) \sim \text{Uniform}(0, 1), because P(F(X)≤u)=P(X≤F−1(u))=uP(F(X) \le u) = P(X \le F^{-1}(u)) = u. Run backwards, F−1(U)F^{-1}(U) has CDF FF: this is inverse-transform sampling.

The lognormal, from the normal

If X∼N(μ,σ2)X \sim N(\mu, \sigma^2) and Y=eXY = e^X, then h(y)=ln⁡yh(y) = \ln y and h′(y)=1/yh'(y) = 1/y, so

fY(y)=1y σ2πexp⁡ ⁣(−(ln⁡y−μ)22σ2),y>0.f_Y(y) = \frac{1}{y\,\sigma\sqrt{2\pi}} \exp\!\left(-\frac{(\ln y - \mu)^2}{2\sigma^2}\right), \qquad y > 0 .

Its moments come from the normal moment generating function E[etX]=eμt+σ2t2/2\mathbb{E}[e^{tX}] = e^{\mu t + \sigma^2 t^2/2} with t=1t = 1 and t=2t = 2:

QuantityValue
Meaneμ+σ2/2e^{\mu + \sigma^2/2}
Variance(eσ2−1) e2μ+σ2(e^{\sigma^2} - 1)\,e^{2\mu + \sigma^2}
Medianeμe^{\mu} (quantiles commute with exp⁡\exp)
Modeeμ−σ2e^{\mu - \sigma^2}

So for any σ>0\sigma > 0, mode < median < mean: the right tail drags the mean up.

Worked example

the square root of an exponential

X∼Exp(1)X \sim \text{Exp}(1). Find the density, the mean and the median of Y=XY = \sqrt X.

Show solution

CDF. For y≥0y \ge 0, P(Y≤y)=P(X≤y2)=1−e−y2P(Y \le y) = P(X \le y^2) = 1 - e^{-y^2}.

Density. Differentiate: fY(y)=2y e−y2f_Y(y) = 2y\,e^{-y^2}. (Same result from the formula: h(y)=y2h(y) = y^2, h′(y)=2yh'(y) = 2y, fX(y2)=e−y2f_X(y^2) = e^{-y^2}.) This is a Rayleigh distribution.

Mean. Substitute x=y2x = y^2:

E[Y]=E[X1/2]=∫0∞x1/2e−x dx=Γ ⁣(32)=π2≈0.886.\mathbb{E}[Y] = \mathbb{E}[X^{1/2}] = \int_0^\infty x^{1/2} e^{-x}\,dx = \Gamma\!\left(\tfrac32\right) = \frac{\sqrt\pi}{2} \approx 0.886 .

Median. Solve 1−e−y2=121 - e^{-y^2} = \tfrac12: y=ln⁡2≈0.833y = \sqrt{\ln 2} \approx 0.833, which is exactly the square root of the median of XX (ln⁡2\ln 2). The mean, 0.8860.886, is below E[X]=1\sqrt{\mathbb{E}[X]} = 1, as Jensen predicts for the concave square root.

Sums: convolution

For independent XX and YY, condition on XX:

fX+Y(z)=∫−∞∞fX(x) fY(z−x) dx,P(X+Y=z)=∑xP(X=x) P(Y=z−x).f_{X+Y}(z) = \int_{-\infty}^{\infty} f_X(x)\,f_Y(z - x)\,dx, \qquad P(X + Y = z) = \sum_x P(X = x)\,P(Y = z - x) .

The hard part is the limits of integration: the integrand is non-zero only where both xx and z−xz - x lie in their supports.

  • Two dice. P(S=s)=6−∣s−7∣36P(S = s) = \dfrac{6 - |s - 7|}{36} for s=2,…,12s = 2, \dots, 12: count the pairs on each anti-diagonal.
  • Two uniforms on [0,1][0, 1]. The integrand is 1 when 0≤x≤10 \le x \le 1 and 0≤z−x≤10 \le z - x \le 1. The overlap has length zz for z≤1z \le 1 and 2−z2 - z for 1≤z≤21 \le z \le 2: a triangle peaking at z=1z = 1.
  • Normals. The convolution of N(μ1,σ12)N(\mu_1, \sigma_1^2) and N(μ2,σ22)N(\mu_2, \sigma_2^2) is N(μ1+μ2,σ12+σ22)N(\mu_1 + \mu_2, \sigma_1^2 + \sigma_2^2). Gamma with a common rate and Poisson are also closed under independent sums.

For sums, moment generating functions are often faster than convolving: MX+Y(t)=MX(t) MY(t)M_{X+Y}(t) = M_X(t)\,M_Y(t) for independent X,YX, Y, and you recognise the product.

Worked example

the wait for the second trade

Trades arrive as a Poisson process at rate 1 per minute, so the gaps X1,X2X_1, X_2 between trades are independent Exp(1)\text{Exp}(1). Find the density of Z=X1+X2Z = X_1 + X_2, the time of the second trade, and P(Z>2)P(Z > 2).

Show solution

Both densities vanish below 0, so the integrand is non-zero only for 0≤x≤z0 \le x \le z:

fZ(z)=∫0ze−xe−(z−x) dx=e−z∫0zdx=z e−z,z≥0.f_Z(z) = \int_0^z e^{-x} e^{-(z - x)}\,dx = e^{-z}\int_0^z dx = z\,e^{-z}, \qquad z \ge 0 .

This is Gamma(2, 1). With rate λ\lambda the same steps give λ2z e−λz\lambda^2 z\,e^{-\lambda z}. Integrating by parts,

P(Z>2)=∫2∞z e−z dz=[−ze−z−e−z]2∞=3e−2≈0.406.P(Z > 2) = \int_2^\infty z\,e^{-z}\,dz = \big[-z e^{-z} - e^{-z}\big]_2^\infty = 3e^{-2} \approx 0.406 .

Cross-check out loud. The second trade comes after minute 2 exactly when at most one trade happens in the first two minutes. The count is Poisson(2), so the probability is e−2(1+2)=3e−2e^{-2}(1 + 2) = 3e^{-2}. The two descriptions agree.

Minima and maxima of independent variables

The max is at most tt exactly when every variable is at most tt; the min exceeds tt exactly when every variable exceeds tt. With independence the probabilities multiply:

P(max⁡iXi≤t)=∏iFi(t),P(min⁡iXi>t)=∏i(1−Fi(t)).P(\max_i X_i \le t) = \prod_i F_i(t), \qquad P(\min_i X_i > t) = \prod_i \big(1 - F_i(t)\big).

For nn i.i.d. variables with CDF FF and density ff, the max has density nF(t)n−1f(t)n F(t)^{n-1} f(t). For uniforms, P(max⁡≤t)=tnP(\max \le t) = t^n; for exponentials, P(min⁡>t)=e−(λ1+⋯+λn)tP(\min > t) = e^{-(\lambda_1 + \dots + \lambda_n)t}, so the minimum is exponential with the summed rate.

A useful trick for two variables: max⁡(X,Y)=X+Y2+∣X−Y∣2\max(X, Y) = \dfrac{X + Y}{2} + \dfrac{|X - Y|}{2} and min⁡(X,Y)=X+Y2−∣X−Y∣2\min(X, Y) = \dfrac{X + Y}{2} - \dfrac{|X - Y|}{2}. Expected maxima then reduce to the expected absolute difference.

Worked example

is a lognormal stock more likely to end up or down?

Under zero rates, a stock follows ST=S0exp⁡ ⁣(−12σ2T+σT Z)S_T = S_0 \exp\!\big(-\tfrac12\sigma^2 T + \sigma\sqrt T\,Z\big) with Z∼N(0,1)Z \sim N(0, 1). Find E[ST]\mathbb{E}[S_T], the median of STS_T, and P(ST>S0)P(S_T > S_0) when σ=20%\sigma = 20\% and T=1T = 1.

Show solution

Here ln⁡(ST/S0)∼N(μ,s2)\ln(S_T/S_0) \sim N(\mu, s^2) with μ=−12σ2T\mu = -\tfrac12\sigma^2 T and s2=σ2Ts^2 = \sigma^2 T.

Mean. E[ST]=S0 eμ+s2/2=S0 e0=S0\mathbb{E}[S_T] = S_0\,e^{\mu + s^2/2} = S_0\,e^{0} = S_0. The −12σ2T-\tfrac12\sigma^2 T term is there precisely to make the price a martingale.

Median. S0 eμ=S0 e−0.02≈0.980 S0S_0\,e^{\mu} = S_0\,e^{-0.02} \approx 0.980\,S_0.

Probability of ending up.

P(ST>S0)=P(σT Z>12σ2T)=P(Z>12σT)=P(Z>0.1)≈0.460.P(S_T > S_0) = P\big(\sigma\sqrt T\,Z > \tfrac12\sigma^2 T\big) = P\big(Z > \tfrac12\sigma\sqrt T\big) = P(Z > 0.1) \approx 0.460 .

The stock is fair on average yet ends below its start more often than not: a few large up-moves pay for many small losses. The effect grows with total volatility; at σT=1\sigma\sqrt T = 1 the probability of ending up is P(Z>0.5)≈0.309P(Z > 0.5) \approx 0.309.

Knowledge check

U∼Uniform(0,1)U \sim \text{Uniform}(0, 1). What is the density of Y=U2Y = U^2 at y=0.25y = 0.25?

Knowledge check

XX and YY are independent and uniform on [0,1][0, 1]. What is the density of X+YX + Y at z=1.5z = 1.5?

Knowledge check

XX and YY are independent standard normals. What is E[max⁡(X,Y)]\mathbb{E}[\max(X, Y)]?

Knowledge check

ln⁡Y∼N(0,1)\ln Y \sim N(0, 1). Which ordering of the mode, median and mean of YY is correct?

Key takeaways

  • Go through the CDF: write P(g(X)≤y)P(g(X) \le y) as a probability about XX, then differentiate. It handles non-monotone maps and edge cases that the formula hides.
  • For monotone gg: fY(y)=fX(g−1(y)) ∣(g−1)′(y)∣f_Y(y) = f_X(g^{-1}(y))\,|(g^{-1})'(y)|. Sum over branches when gg is not monotone.
  • Increasing maps carry quantiles over exactly; they do not carry means (Jensen).
  • Sums of independent variables convolve; get the integration limits from where both supports overlap, or multiply MGFs.
  • P(max⁡≤t)=∏Fi(t)P(\max \le t) = \prod F_i(t) and P(min⁡>t)=∏(1−Fi(t))P(\min > t) = \prod (1 - F_i(t)) for independent variables.
  • Lognormal: median eμe^\mu, mean eμ+σ2/2e^{\mu + \sigma^2/2}, mode eμ−σ2e^{\mu - \sigma^2}. A martingale lognormal price ends below its start with probability above one half.

Practice problems

Finished reading?

Answer the 4 knowledge checks to complete this lesson (0 done).