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 is built from an old one. This topic answers one question: if we know the distribution of , what is the distribution of ?
That topic already gave a hint. Switching from centimetres to metres, , made the density 100 times taller while every probability stayed put. Here we turn the hint into a general rule.
Start with a discrete , where the answer is bookkeeping. Each value carries its probability to . When several values land on the same , their probabilities add up:
The set of all with is the preimage of .
Take a fair die and . The value has two preimages, and , so . The value has one, , so . Nothing is lost and nothing is created: the probabilities of still add up to 1. They are just regrouped.
The CDF method
For a continuous 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 :
Rewrite the event as a condition on , compute its probability with , and differentiate in to get the density . This is the CDF method. It works for any , monotone or not.
Take and . For the event is . It is an interval of length inside a range of length 2. Its probability and the derivative of that probability are
(The derivative of is . 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 . We get , although is only a quarter of . Every near 0 lands on a 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 .
Linear maps and standardization
The simplest new variable is a linear one, with . The CDF method takes one line:
Differentiating with the chain rule brings out a factor . For the inequality flips, and the factor becomes . In both cases
The graph of is shifted, stretched along the axis by and pressed down by the same factor, so the area stays 1.
Now plug in the normal density. If , then : a linear map of a normal is normal. With and we get the standardization
This proves the rule from Continuous distributions: one function 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 in degrees Celsius becomes in degrees Fahrenheit, since .
The change of variables
For a strictly increasing differentiable the CDF method has a shortcut. Take a short interval of length around . It carries probability . Its image under is an interval around of length , and it must carry the same probability:
Dividing by gives the change of variables formula:
The derivative is the local stretch factor of near . The absolute value makes the same formula work for a decreasing . The linear case is .
Probability is conserved; density is not. Where squeezes, , the same mass fits into a shorter interval and the density rises. Where stretches, , the density falls.
If is not monotone, each preimage contributes its own term:
Check it on with . The preimages each have and . Each gives , and together they give , as the CDF method found.
The log-normal distribution
Here is the formula at work. Let , write for its density, and put . The preimage is and the slope is , so
This is the log-normal distribution: is log-normal when is normal.
Watch how the exponential moves mass around. The band holds probability and maps onto an interval of length , an average density of about . The band holds almost as much, , but maps onto an interval of length , an average density of about . Squeezed on the left, stretched on the right: the log-normal has a peak near zero and a long right tail.
An increasing keeps the order of values, so it carries "below" events over intact: . Quantiles move with . In particular, the median, the -quantile, goes to .
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 . How do we get a sample with a given CDF ? If is continuous and strictly increasing, put
Then has CDF , because for every
The first step applies the increasing to both sides. The second is the CDF of . This is inverse transform sampling.
For the CDF is , and its inverse is . The evenly spaced go to . The gaps widen in the tail, where rises slowly.
The idea works for a discrete distribution too. Cut into consecutive pieces of lengths and return the piece where lands. In practice, compare with the cumulative sums . 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 , the leftmost -quantile from Continuous distributions, plays the role of .
The recipe needs 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 is continuous, then . This probability integral transform is how calibrated forecasts are checked: the values 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 and pushes it through a chain of invertible maps, . Apply the formula once per map and take logs:
Here is mapped back through all the inverses, and . 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, 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 . Rounding and clipping have flat pieces, and a flat piece collapses a whole interval into one value with positive probability. Rounding makes discrete. Clipping, , gives a density plus a point mass at : a mixed distribution, like the revenue per visitor in Continuous distributions. Second, a function of several variables, such as a sum , needs their joint distribution. That comes in Joint distributions and independence.
We can now find the whole distribution of . 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 can be computed without finding the distribution of at all.
Check your understanding
The same questions as in Pulse. Answers are shared between modes.
Answers are saved on the server and tied to this browser until you sign in.