Skip to content
BIASED

Functions of random variables

The distribution of $Y = g(X)$: sums over preimages, the CDF method, the one-dimensional change of variables, standardization, the log-normal distribution and inverse transform sampling.

New variables from old ones

The topic Continuous distributions gave us densities and CDFs for a few named families: uniform, exponential, normal. Real pipelines rarely stop at a named family. We standardize features, take the log of a latency, square an error, push a score through a sigmoid. Each time, a new random variable Y=g(X)Y = g(X) is built from an old one. This topic answers one question: if we know the distribution of XX, what is the distribution of YY?

That topic already gave a hint. Switching from centimetres to metres, Y=X/100Y = X/100, made the density 100 times taller while every probability stayed put. Here we turn the hint into a general rule.

Start with a discrete XX, where the answer is bookkeeping. Each value xx carries its probability to g(x)g(x). When several values land on the same yy, their probabilities add up:

P(Y=y)=∑x: g(x)=yP(X=x).P(Y = y) = \sum_{x:\ g(x) = y} P(X = x).

The set of all xx with g(x)=yg(x) = y is the preimage of yy.

Take a fair die XX and Y=(X−3)2Y = (X - 3)^2. The value Y=1Y = 1 has two preimages, X=2X = 2 and X=4X = 4, so P(Y=1)=2/6P(Y = 1) = 2/6. The value Y=9Y = 9 has one, X=6X = 6, so P(Y=9)=1/6P(Y = 9) = 1/6. Nothing is lost and nothing is created: the probabilities of YY still add up to 1. They are just regrouped.

The CDF method

For a continuous XX every single point has probability zero, so adding up preimages point by point gives nothing. Work with the CDF instead. It is the probability of an event, and an event can be carried through gg:

FY(y)=P(Y≤y)=P(g(X)≤y).F_Y(y) = P(Y \le y) = P\big(g(X) \le y\big).

Rewrite the event {g(X)≤y}\{g(X) \le y\} as a condition on XX, compute its probability with FXF_X, and differentiate in yy to get the density pYp_Y. This is the CDF method. It works for any gg, monotone or not.

Take X∼U(−1,1)X \sim U(-1, 1) and Y=X2Y = X^2. For 0≤y≤10 \le y \le 1 the event {X2≤y}\{X^2 \le y\} is {−y≤X≤y}\{-\sqrt y \le X \le \sqrt y\}. It is an interval of length 2y2\sqrt y inside a range of length 2. Its probability and the derivative of that probability are

FY(y)=y,pY(y)=12y,0<y≤1.F_Y(y) = \sqrt y, \qquad p_Y(y) = \frac{1}{2\sqrt y}, \quad 0 < y \le 1 .

(The derivative of y\sqrt y is 1/(2y)1/(2\sqrt y). For a refresher on derivatives, see the topic Derivatives and Taylor series in the course Mathematics for ML.)

Squaring is unfair to the range of YY. We get P(Y≤1/4)=1/2P(Y \le 1/4) = 1/2, although [0,1/4][0, 1/4] is only a quarter of [0,1][0, 1]. Every xx near 0 lands on a yy near 0, and the density there grows without bound. That is allowed. As in Continuous distributions, a density is not a probability, and the total area is still ∫01dy2y=1\int_0^1 \frac{dy}{2\sqrt y} = 1.

Linear maps and standardization

The simplest new variable is a linear one, Y=aX+bY = aX + b with a>0a > 0. The CDF method takes one line:

FY(y)=P(aX+b≤y)=P(X≤y−ba)=FX(y−ba).F_Y(y) = P(aX + b \le y) = P\Big(X \le \frac{y - b}{a}\Big) = F_X\Big(\frac{y - b}{a}\Big).

Differentiating with the chain rule brings out a factor 1/a1/a. For a<0a < 0 the inequality flips, and the factor becomes 1/∣a∣1/\lvert a\rvert. In both cases

pY(y)=1∣a∣ pX(y−ba).p_Y(y) = \frac{1}{\lvert a\rvert}\, p_X\Big(\frac{y - b}{a}\Big).

The graph of pXp_X is shifted, stretched along the axis by ∣a∣\lvert a\rvert and pressed down by the same factor, so the area stays 1.

Now plug in the normal density. If X∼N(μ,σ2)X \sim \mathcal N(\mu, \sigma^2), then aX+b∼N(aμ+b, a2σ2)aX + b \sim \mathcal N(a\mu + b,\ a^2\sigma^2): a linear map of a normal is normal. With a=1/σa = 1/\sigma and b=−μ/σb = -\mu/\sigma we get the standardization

Z=X−μσ∼N(0,1).Z = \frac{X - \mu}{\sigma} \sim \mathcal N(0, 1).

This proves the rule P(X≤x)=Φ((x−μ)/σ)P(X \le x) = \Phi\big((x - \mu)/\sigma\big) from Continuous distributions: one function Φ\Phi serves every normal. ML pipelines standardize features for the same reason: shift by the centre, divide by the scale, and every feature lives on a common axis. A temperature of N(20,32)\mathcal N(20, 3^2) in degrees Celsius becomes N(68,5.42)\mathcal N(68, 5.4^2) in degrees Fahrenheit, since tF=1.8 tC+32t_F = 1.8\, t_C + 32.

The change of variables

For a strictly increasing differentiable gg the CDF method has a shortcut. Take a short interval of length dxdx around xx. It carries probability pX(x) dxp_X(x)\,dx. Its image under gg is an interval around y=g(x)y = g(x) of length dy=∣g′(x)∣ dxdy = \lvert g'(x)\rvert\,dx, and it must carry the same probability:

pY(y) dy=pX(x) dx.p_Y(y)\,dy = p_X(x)\,dx .

Dividing by dydy gives the change of variables formula:

pY(y)=pX(x)∣g′(x)∣,x=g−1(y).p_Y(y) = \frac{p_X(x)}{\lvert g'(x)\rvert}, \qquad x = g^{-1}(y).

The derivative g′(x)g'(x) is the local stretch factor of gg near xx. The absolute value makes the same formula work for a decreasing gg. The linear case is g′(x)=ag'(x) = a.

Probability is conserved; density is not. Where gg squeezes, ∣g′∣<1\lvert g'\rvert < 1, the same mass fits into a shorter interval and the density rises. Where gg stretches, ∣g′∣>1\lvert g'\rvert > 1, the density falls.

If gg is not monotone, each preimage contributes its own term:

pY(y)=∑x: g(x)=ypX(x)∣g′(x)∣.p_Y(y) = \sum_{x:\ g(x) = y} \frac{p_X(x)}{\lvert g'(x)\rvert}.

Check it on Y=X2Y = X^2 with X∼U(−1,1)X \sim U(-1, 1). The preimages ±y\pm\sqrt y each have pX=1/2p_X = 1/2 and ∣g′∣=2y\lvert g'\rvert = 2\sqrt y. Each gives 14y\frac{1}{4\sqrt y}, and together they give 12y\frac{1}{2\sqrt y}, as the CDF method found.

The log-normal distribution

Here is the formula at work. Let X∼N(0,1)X \sim \mathcal N(0, 1), write φ\varphi for its density, and put Y=eXY = e^X. The preimage is x=ln⁡yx = \ln y and the slope is g′(x)=ex=yg'(x) = e^x = y, so

pY(y)=φ(ln⁡y)y,y>0.p_Y(y) = \frac{\varphi(\ln y)}{y}, \qquad y > 0 .

This is the log-normal distribution: YY is log-normal when ln⁡Y\ln Y is normal.

Watch how the exponential moves mass around. The band −1.2≤x≤−0.7-1.2 \le x \le -0.7 holds probability 0.1270.127 and maps onto an interval of length 0.1950.195, an average density of about 0.650.65. The band 0.8≤x≤1.30.8 \le x \le 1.3 holds almost as much, 0.1150.115, but maps onto an interval of length 1.4441.444, an average density of about 0.080.08. Squeezed on the left, stretched on the right: the log-normal has a peak near zero and a long right tail.

An increasing gg keeps the order of values, so it carries "below" events over intact: P(Y≤g(c))=P(X≤c)P(Y \le g(c)) = P(X \le c). Quantiles move with gg. In particular, the median, the 0.50.5-quantile, goes to g(median of X)g(\text{median of } X).

Log-normal shapes are common in data: incomes, file sizes, server response times. Such a quantity is often a product of many positive factors. Its logarithm is then a sum, and sums tend towards the normal (that is the topic The central limit theorem). In practice we often go the other way: take the log of a skewed positive target, and it looks much more like a bell.

Inverse transform sampling

Read the same idea backwards, and it becomes a recipe for sampling. A computer gives us U∼U(0,1)U \sim U(0, 1). How do we get a sample with a given CDF FF? If FF is continuous and strictly increasing, put

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

Then XX has CDF FF, because for every xx

P(F−1(U)≤x)=P(U≤F(x))=F(x).P\big(F^{-1}(U) \le x\big) = P\big(U \le F(x)\big) = F(x).

The first step applies the increasing FF to both sides. The second is the CDF of U(0,1)U(0, 1). This is inverse transform sampling.

For Exp⁡(1)\operatorname{Exp}(1) the CDF is F(x)=1−e−xF(x) = 1 - e^{-x}, and its inverse is F−1(u)=−ln⁡(1−u)F^{-1}(u) = -\ln(1 - u). The evenly spaced u=0.1,0.3,0.5,0.7,0.9u = 0.1, 0.3, 0.5, 0.7, 0.9 go to x=0.105,0.357,0.693,1.204,2.303x = 0.105, 0.357, 0.693, 1.204, 2.303. The gaps widen in the tail, where FF rises slowly.

The idea works for a discrete distribution too. Cut [0,1][0, 1] into consecutive pieces of lengths p1,…,pKp_1, \dots, p_K and return the piece where UU lands. In practice, compare UU with the cumulative sums p1, p1+p2,…p_1,\ p_1 + p_2, \dots. This is how a language model draws its next token from a categorical distribution (see Discrete distributions), after temperature or top-p has reshaped it. For any CDF with jumps or flat parts, the quantile function Q(u)=min⁡{x:F(x)≥u}Q(u) = \min\{x : F(x) \ge u\}, the leftmost uu-quantile from Continuous distributions, plays the role of F−1F^{-1}.

The recipe needs F−1F^{-1} in a usable form, and the normal CDF has no elementary inverse. Libraries use numerical inversion or special tricks such as Box–Muller (more in Monte Carlo methods). The converse also holds: if FF is continuous, then F(X)∼U(0,1)F(X) \sim U(0, 1). This probability integral transform is how calibrated forecasts are checked: the values F(x)F(x) at the observed outcomes should look uniform.

Flows and the limits of the method

The change of variables also drives a whole family of generative models. A normalizing flow takes a simple Z∼N(0,1)Z \sim \mathcal N(0, 1) and pushes it through a chain of invertible maps, Y=gK(⋯g1(Z))Y = g_K(\cdots g_1(Z)). Apply the formula once per map and take logs:

log⁡pY(y)=log⁡φ(z0)−∑k=1Klog⁡∣gk′(zk−1)∣.\log p_Y(y) = \log\varphi(z_0) - \sum_{k=1}^{K} \log\lvert g_k'(z_{k-1})\rvert .

Here z0z_0 is yy mapped back through all the inverses, and zk=gk(zk−1)z_k = g_k(z_{k-1}). The flow gives the exact log-density of every data point, so its parameters can be fitted by making that log-density large on the data (the topic Maximum likelihood makes this precise).

In more than one dimension, ∣g′∣\lvert g'\rvert becomes the absolute value of the determinant of the Jacobian matrix. The calculus is in the course Mathematics for ML (topic Multiple integrals and change of variables), and the flows themselves are in the course Generative Models (topic The change of variables in the module Normalizing flows). In this course we stay in one dimension.

Two warnings. First, the formula needs a differentiable, locally invertible gg. Rounding and clipping have flat pieces, and a flat piece collapses a whole interval into one value with positive probability. Rounding makes YY discrete. Clipping, Y=min⁡(X,c)Y = \min(X, c), gives a density plus a point mass at cc: a mixed distribution, like the revenue per visitor in Continuous distributions. Second, a function of several variables, such as a sum X1+X2X_1 + X_2, needs their joint distribution. That comes in Joint distributions and independence.

We can now find the whole distribution of g(X)g(X). Often we want much less: where the distribution sits and how widely it spreads, in one or two numbers. That is the next topic, Expectation and variance. It also brings a shortcut: the average of g(X)g(X) can be computed without finding the distribution of g(X)g(X) at all.

Check your understanding

The same questions as in Pulse. Answers are shared between modes.

Full notes

Answers are saved on the server and tied to this browser until you sign in.