Settings

Theme
Lite Mode (saves mobile data)

In Lite Mode, animations and videos don't play until you tap them, and their size is shown first. Interactive widgets still work.

Text size

Back up or move your progress →

Part 1: Math FoundationsProbability and Statistics

Random variables

P1.D240 min●●○ IntermediateReviewed Dec 1, 2026

Why this matters for machine learning

Models in machine learning do not just output numbers: they output uncertainty, and a random variable with its PMF or density is how that uncertainty is written down and handled.

  • A classifier predicts a PMF over the class labels (its softmax output), and the label is a discrete random variable. (P3.A8: Softmax regression, coming soon)
  • A language model outputs a PMF over the vocabulary: the next token ID is a discrete random variable, and generating text means drawing from that PMF. (P10.A1: What is a Large Language Model?, coming soon)
  • A regression target is modelled as a prediction plus noise, and the noise is a continuous random variable with a density, usually the Gaussian. (P3.A2: Linear regression, coming soon)
  • The likelihood of the training data is a product of PMF or density values, and maximum likelihood chooses the parameters that make it largest. (P1.D10: Maximum Likelihood Estimation)
  • Sampling from a model draws a value from its distribution; for a continuous variable one standard method pushes uniform random numbers through the inverse CDF. (P1.D9: Sampling and Monte Carlo)
  • Medians and percentiles are read off the CDF, and expectation and variance are sums or integrals against the PMF or density. (P1.D4: Expectation and variance)

Before this: Probability basics, Integrals as accumulation, Sets, notation and reading math

After this lesson you can

  • Explain a random variable XX as a rule that gives every outcome a number, and read the notation xx, {X=x}\{X=x\}, {X≤x}\{X\le x\} and P(X=x)P(X=x)
  • Tell discrete from continuous variables, write a valid PMF (nonnegative, adding to 1) and use it, for example for one die and for the sum of two dice
  • Build the CDF F(x)=P(X≤x)F(x)=P(X\le x) of a discrete variable (a right-continuous staircase whose jumps are the PMF) and of a continuous one, and use P(a<X≤b)=F(b)−F(a)P(a\lt X\le b)=F(b)-F(a)
  • Explain why a density is not a probability (it can exceed 1, and P(X=x)=0P(X=x)=0), find probabilities as areas, and use F′(x)=p(x)F'(x)=p(x) to move between density and CDF
  • Find the distribution of a function Y=g(X)Y=g(X) in simple cases, and read a median or a percentile from the CDF

Roll two dice. You rarely care which die showed which face; you care about the total, say 7. A classifier looks at a photo and produces a label. A language model produces the ID of the next token. In each case the outcome of a random process is turned into a number, and then we ask: how likely is each number, and how likely is it to be at most 5?

That turning of outcomes into numbers is a random variable, and the tools for describing how likely each number is are the PMF, the density and the CDF. They are the language in which every later probability lesson, and every model that outputs uncertainty, is written.

A random variable is a function

An experiment has a set of possible outcomes, the sample space Ω\Omega (P1.D1), and an event is a set of outcomes. Take two fair dice. The outcomes are the 36 pairs (a,b)(a,b), such as (3,4)(3,4). A random variable is a rule that gives every outcome a number. The rule “add the two faces” is one, written for the outcome ω=(a,b)\omega=(a,b) and then for (3,4)(3,4):

X(ω)=a+bfor ω=(a,b),X((3,4))=7.\begin{aligned} X(\omega)&=a+b\quad\text{for }\omega=(a,b),\\ X\bigl((3,4)\bigr)&=7 . \end{aligned}

The name is misleading in two ways. A random variable is not random: the rule is fixed, and the same outcome always gets the same number. It is also not a variable in the sense of algebra: it is a function, from outcomes to numbers (P1.A4). The randomness lives in which outcome happens. Once the dice have landed, XX has a definite value.

Four pieces of notation, all of them standard:

  • A capital letter, XX, is the rule. A lowercase letter, xx, is one particular number that the rule can produce. Writing X=xX=x for “XX takes the value xx” is the usual shorthand, and you will see it in every paper.
  • {X=x}\{X=x\} is the event made of all outcomes that XX turns into xx. In set-builder notation (P1.A4) it is {ω:X(ω)=x}\{\omega : X(\omega)=x\}. For the sum of two dice, {X=7}\{X=7\} is the set of the six outcomes (1,6)(1,6), (2,5)(2,5), (3,4)(3,4), (4,3)(4,3), (5,2)(5,2) and (6,1)(6,1), out of 36.
  • P(X=x)P(X=x) is short for the probability of that event: P(X=7)=636=16P(X=7)=\frac{6}{36}=\frac16.
  • {X≤x}\{X\le x\} is the event of all outcomes with X(ω)≤xX(\omega)\le x, and P(X≤x)P(X\le x) is its probability. This one gets its own name below, the CDF.

The probabilities themselves come from the rules of P1.D1: they are at least 0, and the whole sample space has probability 1. A random variable adds nothing new to those rules. It only gives us numbers to ask questions about.

A random variable does not have to be a sum. The number on one die is a random variable (X(ω)=ωX(\omega)=\omega, the identity). A coin turned into 1 for heads and 0 for tails is one. So is the class label of a randomly drawn image, the height of a randomly chosen person, or the output of a model whose input or internal sampling is random: a function of a random variable is again a random variable, as the section on functions below shows.

Discrete or continuous

A random variable is discrete if its possible values can be listed one after another, even if the list never ends: the faces 1 to 6, the class IDs 0,1,…,90,1,\dots,9, the token IDs of a vocabulary, the number of emails you get in an hour (0,1,2,…0,1,2,\dots). It is continuous if it can take any value in an interval of the real line, so there is no list: a waiting time, a temperature, the noise added to a measurement.

Real data is always recorded with finite precision, so the choice is a modelling decision. A pixel stored as a whole number from 0 to 255 is discrete, but when the numbers are fine-grained it is often convenient to model it as continuous.

Some variables are mixed: part of the probability sits on single values, and the rest is spread out. Take a standard Gaussian variable ZZ and apply ReLU (P4.4), Y=max⁡(0,Z)Y=\max(0,Z). By symmetry P(Z≤0)=12P(Z\le0)=\frac12, so P(Y=0)=0.5P(Y=0)=0.5, a lump of probability on the single value 0, while the positive values of YY are spread over a continuous range. Neither a pure PMF nor a pure density describes YY, but the CDF does: FY(y)=0F_Y(y)=0 for y<0y\lt0 and FY(y)=P(Z≤y)F_Y(y)=P(Z\le y) for y≥0y\ge0, which is the Gaussian CDF value (0.5 at y=0y=0). So FYF_Y jumps from 0 to 0.50.5 at 0 and then rises smoothly. Mixed variables are the exception; the rest of the lesson treats the two pure cases.

The PMF: probability on each value

For a discrete XX, the probability mass function (PMF) gives the probability of each possible value:

pX(x)=P(X=x).p_X(x)=P(X=x).

The subscript says which variable the PMF belongs to, and is dropped when it is clear. Two rules make a function a valid PMF: it is never negative, and it adds up to 1 over all possible values,

p(x)≥0,∑xp(x)=1.\begin{aligned} p(x)&\ge0,\\ \sum_{x}p(x)&=1 . \end{aligned}

Probability sits on the points like lumps of clay, with a total mass of 1; hence “mass”. The analogy is a fair picture of a PMF, and it stops working for continuous variables, where no single point carries any mass.

One fair die. p(x)=16p(x)=\frac16 for x=1,2,…,6x=1,2,\dots,6. The six probabilities add up to 1.

The sum of two dice. Count the outcomes of the 36 that give each total. The counts for x=2,3,…,12x=2,3,\dots,12 are 1,2,3,4,5,6,5,4,3,2,11,2,3,4,5,6,5,4,3,2,1, which is 6−∣x−7∣6-|x-7| outcomes out of 36. So p(x)=6−∣x−7∣36p(x)=\frac{6-|x-7|}{36}: the single most likely total, 7, has probability 636=0.1667\frac6{36}=0.1667, and 2 and 12 have 136\frac1{36} each. The counts add up to 36.

The probability of an event is the sum of the PMF over its values. For the two dice: P(X≥10)=3+2+136=16P(X\ge10)=\frac{3+2+1}{36}=\frac16, and the even totals 2,4,…,122,4,\dots,12 have 1+3+5+5+3+136=1836=12\frac{1+3+5+5+3+1}{36}=\frac{18}{36}=\frac12.

Models output PMFs. A classifier with three classes turns three scores (logits) into probabilities with the softmax function (P3.A8). For the scores (2,1,0)(2,1,0) the probabilities are (0.665, 0.245, 0.090)(0.665,\,0.245,\,0.090): nonnegative, adding up to 1. This is the model’s predicted PMF over the label. A language model does the same over a vocabulary of VV token IDs, with VV numbers adding to 1 (P10.A1).

A PMF value is a probability, so it can never be above 1.

The CDF of a discrete variable: a staircase

Asking “what is P(X≤x)P(X\le x)?” for every xx gives a function with its own name, the cumulative distribution function (CDF):

FX(x)=P(X≤x).F_X(x)=P(X\le x).

It is defined for every real number xx, not only for possible values, and for every kind of random variable. It is the one description that works for discrete, continuous and mixed variables alike.

For one die: F(0.5)=0F(0.5)=0 (no face is at most 0.5), F(1)=16F(1)=\frac16, F(2.5)=26F(2.5)=\frac26 (the faces 1 and 2), F(3)=36=0.5F(3)=\frac36=0.5, F(6)=1F(6)=1 and F(10)=1F(10)=1. Plot it and you see a staircase: flat between possible values, with a jump of height 16\frac16 at each face. In general:

  • The jump at a possible value xx equals p(x)p(x), because P(X=x)=F(x)−F(x−)P(X=x)=F(x)-F(x^-), where F(x−)F(x^-) is the value just to the left of xx.
  • At a jump the CDF takes the upper value. F(3)=36F(3)=\frac36 already counts the face 3, since the event is “at most 3”. A function that behaves like this is called right-continuous: approaching xx from the right, the values settle on F(x)F(x). Plots draw a closed dot on the top of the step and an open dot at the bottom.
  • Between possible values the CDF is flat: no new probability is added.

For every random variable, discrete or not, the CDF has four properties:

  1. 0≤F(x)≤10\le F(x)\le1, since it is a probability.
  2. It never decreases: if x<yx\lt y then {X≤x}\{X\le x\} is inside {X≤y}\{X\le y\}, so F(x)≤F(y)F(x)\le F(y) (more outcomes, at least as much probability).
  3. F(x)→0F(x)\to0 as x→−∞x\to-\infty and F(x)→1F(x)\to1 as x→+∞x\to+\infty.
  4. It is right-continuous.

Intervals. The event {X≤b}\{X\le b\} splits into the two pieces {X≤a}\{X\le a\} and {a<X≤b}\{a\lt X\le b\} that do not overlap (for a<ba\lt b). Their probabilities add, so

P(a<X≤b)=F(b)−F(a),P(X>x)=1−F(x).\begin{aligned} P(a\lt X\le b)&=F(b)-F(a),\\ P(X\gt x)&=1-F(x). \end{aligned}

For a die, P(2<X≤5)=F(5)−F(2)=56−26=12P(2\lt X\le5)=F(5)-F(2)=\frac56-\frac26=\frac12 (the faces 3, 4 and 5). The ends of the interval matter for a discrete variable: P(2≤X≤5)P(2\le X\le5) also counts the face 2, so it is F(5)−F(1)=56−16=23F(5)-F(1)=\frac56-\frac16=\frac23, not 12\frac12. For the sum of two dice, F(7)=2136=712=0.5833F(7)=\frac{21}{36}=\frac7{12}=0.5833 and F(4)=636=16F(4)=\frac6{36}=\frac16, so P(4<X≤7)=1536=512=0.4167P(4\lt X\le7)=\frac{15}{36}=\frac5{12}=0.4167.

Continuous variables: probability 0 at every point

Now let XX be continuous. Say it is equally likely to be anywhere in [0,2][0,2]. There are infinitely many possible values, all equally likely, and the probabilities must add up to 1. If each value had the same positive probability, the total would be infinite, so each value gets probability 0. (A pile of probability on single values is possible for other variables, such as token IDs, counts, or the point mass of the ReLU example above; it is the spreading over a whole interval that forces 0 at each point.) The consequence is surprising but correct:

P(X=x)=0for every x.P(X=x)=0\quad\text{for every }x .

This does not mean that xx is impossible: XX certainly lands somewhere. Think of a dart thrown at a line: any exact point has probability 0 of being hit, yet the dart lands on some point. Probability zero for one point is different from impossible. What can be positive is the probability of an interval.

P1.C9 solved this with a probability density function (PDF) p(x)p(x). The probability of an interval is the area under the density over it:

P(a≤X≤b)=∫abp(x) dx,p(x)≥0,∫−∞∞p(x) dx=1.\begin{aligned} P(a\le X\le b)&=\int_a^b p(x)\,dx,\\ p(x)&\ge0,\\ \int_{-\infty}^{\infty}p(x)\,dx&=1 . \end{aligned}

Two examples from P1.C9:

  • Uniform on [0,2][0,2]: p(x)=12p(x)=\frac12 on [0,2][0,2] and 0 elsewhere. The total area is 12×2=1\frac12\times2=1, and P(0.5≤X≤1.5)=12×1=12P(0.5\le X\le1.5)=\frac12\times1=\frac12.
  • Exponential: p(x)=e−xp(x)=\mathrm{e}^{-x} for x≥0x\ge0, a model of a waiting time. The areas are P(X≤1)=0.632P(X\le1)=0.632, P(1≤X≤2)=0.2325P(1\le X\le2)=0.2325 and P(X≥2)=0.1353P(X\ge2)=0.1353, which add up to 1.

Because single points carry no probability, the ends of an interval do not matter for a continuous variable: P(a<X<b)P(a\lt X\lt b), P(a≤X≤b)P(a\le X\le b) and the two mixed versions are all equal. For a discrete variable they are not, as the die showed.

A density is not a probability. Its height is probability per unit of xx, and this has three consequences.

  • It can exceed 1. The uniform density on [0,0.5][0,0.5] has height 22: the area is 2×0.5=12\times0.5=1, which is all that is required. Then P(0.1≤X≤0.2)=2×0.1=0.2P(0.1\le X\le0.2)=2\times0.1=0.2.
  • It has units. If xx is a time in seconds, p(x)p(x) is per second. The exponential density at 1 second is 0.3680.368 per second. Measure the same waiting time in milliseconds and the same point has density 0.0003680.000368 per millisecond, because a millisecond is a thousand times shorter. The probabilities do not change: an area is (per second) ×\times seconds, with the units cancelling.
  • Over a tiny interval, probability is about height times width: P(x≤X≤x+Δ)≈p(x) ΔP(x\le X\le x+\Delta)\approx p(x)\,\Delta for small Δ\Delta. For the exponential density, P(0.9≤X≤1.1)=0.0737P(0.9\le X\le1.1)=0.0737, and the estimate p(1)×0.2=0.0736p(1)\times0.2=0.0736 is almost the same.

A density value is therefore never the probability of a point. To get a probability from a density, integrate over an interval.

The CDF is defined the same way, F(x)=P(X≤x)F(x)=P(X\le x), which for a density is the area up to xx:

F(x)=∫−∞xp(t) dt.F(x)=\int_{-\infty}^{x}p(t)\,dt .

This is exactly the accumulation function of P1.C9, and its first half of the fundamental theorem gives

F′(x)=p(x)  where p is continuous.F'(x)=p(x)\;\text{where }p\text{ is continuous.}

The density is the slope of the CDF, and the CDF is the running area under the density. For the exponential: F(x)=1−e−xF(x)=1-\mathrm{e}^{-x} for x≥0x\ge0, and F′(x)=e−x=p(x)F'(x)=\mathrm{e}^{-x}=p(x). At x=1x=1 the slope is 0.36790.3679, the same as the height of the density. For the uniform density on [0,2][0,2]: F(x)=x2F(x)=\frac x2 on [0,2][0,2], a straight ramp of slope 12\frac12 from 0 to 1.

A continuous CDF has no jumps, because every single point has probability 0. The same four properties hold: it goes from 0 up to 1 and never decreases. The correspondence between the two pictures is worth memorising:

  • Where the CDF is steep, the density is tall: a lot of probability per unit of xx.
  • Where the CDF is flat, the density is 0: no probability is added.
  • For a discrete variable, the CDF jumps where the PMF has a bar, and the jump is as tall as the bar.

The standard Gaussian density is p(x)=12πe−x2/2p(x)=\frac1{\sqrt{2\pi}}\mathrm{e}^{-x^2/2} (P1.D3). Its CDF has no formula built from the usual elementary functions, as P1.C9 noted for the area under e−x2/2\mathrm{e}^{-x^2/2}, so it is computed numerically through the error function erf⁡\operatorname{erf}: F(x)=12(1+erf⁡(x/2))F(x)=\frac12\bigl(1+\operatorname{erf}(x/\sqrt2)\bigr). The values to remember are F(0)=0.5F(0)=0.5, F(1)=0.8413F(1)=0.8413 and P(−1<X≤1)=0.6827P(-1\lt X\le1)=0.6827. The sigmoid of P1.A5 is also a CDF, that of the standard logistic distribution (P1.C9): σ(0)=0.5\sigma(0)=0.5 and P(X≤2)=σ(2)=0.881P(X\le2)=\sigma(2)=0.881.

The two plots, linked

In the widget, the top plot shows the PMF (bars) or the density (curve), and the bottom plot shows the CDF. They describe the same variable. Drag the point on either plot, or use the keyboard or the sliders: the shaded probability at the top equals the height of the dot at the bottom.

PDF and CDF, linked

X is the number on a fair six-sided die: p(x) = 1/6 for x = 1, 2, …, 6.

0123456700.050.10.150.2x

Top: the PMF, one bar per possible value (height = P(X = x)). Blue bars are the ones added up.

0123456700.51xx

Bottom: the CDF F(x) = P(X ≤ x). The dot sits at height F(x); the thick blue segment on the left edge is the probability, read on the vertical axis. Closed dots belong to the curve; open dots are the value just before the jump.

x = 3
P(X = x) = 1/6 = 0.1667
P(X ≤ x) = F(x) = 3/6 = 0.5
P(X > x) = 1 − F(x) = 0.5
values counted: 1, 2, 3

x = 3 is a possible value, so the CDF jumps here: F(3) = 0.5 includes the bar at 3 (P(X = 3) = 0.1667). Just to the left of 3 the CDF is 0.3333. The CDF is right-continuous: at a jump it takes the upper value.

Drag on either plot, or focus a dot and use the arrow keys (one step is 0.25; Page Up and Page Down move 1; Home and End jump to the ends).

The PMF or density (top) and the CDF (bottom) of one random variable. Choose a distribution. In “Everything up to x”, the shaded bars or area are P(X ≤ x), and the dot on the CDF sits at the same value F(x), also shown as a thick blue segment on the left edge. In “Interval a to b” a second pink handle appears: the shaded part is P(a < X ≤ b), which is F(b) − F(a), the length of the segment between the two heights. Drag on either plot, move the dots with the arrow keys, or use the sliders. The readouts and the status line say what the current position means, including whether x is a possible value of a discrete variable.

Things to try, with the numbers you should see:

  1. Fair die, one threshold. Set x=3x=3: three bars turn blue and F(3)=36=0.5F(3)=\frac36=0.5. The status line says that 3 is a possible value, so the CDF has jumped here and includes the bar at 3, with 0.33330.3333 just to the left. Move xx to 2.752.75: the readout is 26\frac26 and P(X=x)=0P(X=x)=0, since 2.75 is not a face; the CDF is flat between 2 and 3.
  2. Sum of two dice. At x=7x=7 the readout is F(7)=2136=0.5833F(7)=\frac{21}{36}=0.5833, which includes a jump of 636\frac6{36} from 0.41670.4167. Switch to “Interval a to b” with a=4a=4 and b=7b=7: P(4<X≤7)=F(7)−F(4)=0.5833−0.1667=0.4167P(4\lt X\le7)=F(7)-F(4)=0.5833-0.1667=0.4167. The counted values are 5,6,75,6,7 (not 4).
  3. Uniform on [0,0.5][0,0.5]. The top plot is a tall rectangle with height 2, above the 1 mark on the axis, and the status line says the height is allowed to be above 1. At x=0.25x=0.25 the area is 0.50.5, and so is F(0.25)F(0.25). The CDF is a ramp that climbs from 0 to 1 over a width of only 0.5.
  4. Exponential. The density is tallest at 0, and the CDF is steepest there. Take a=1a=1 and b=2b=2: P(1<X≤2)=F(2)−F(1)≈0.8647−0.6321P(1\lt X\le2)=F(2)-F(1)\approx0.8647-0.6321, which is 0.23250.2325 when computed from the unrounded values, the number from P1.C9.
  5. Gaussian. With a=−1a=-1 and b=1b=1 the shaded area is 0.68270.6827. With a=−2a=-2 and b=2b=2 it is 0.95450.9545. Move xx to the far tail: the CDF gets close to 1 but never reaches it.

Using the CDF: intervals, tails and quantiles

Because it works for every kind of variable, the CDF is the handiest object for questions about “at most” and “between”:

P(X≤b)=F(b),P(X>a)=1−F(a),P(a<X≤b)=F(b)−F(a).\begin{aligned} P(X\le b)&=F(b),\\ P(X\gt a)&=1-F(a),\\ P(a\lt X\le b)&=F(b)-F(a). \end{aligned}

A quantile turns the question around. Given a probability qq, find the value xx with F(x)=qF(x)=q. The 0.50.5-quantile is the median: half of the probability lies below it. The 0.90.9-quantile is the 90th percentile. For a continuous variable whose CDF is strictly increasing, the quantile is the inverse function, x=F−1(q)x=F^{-1}(q): look at height qq on the CDF and read the value across.

  • Exponential: solving 1−e−x=q1-\mathrm{e}^{-x}=q gives x=−ln⁡(1−q)x=-\ln(1-q). The median is ln⁡2=0.6931\ln2=0.6931 and the 90th percentile is −ln⁡0.1=2.3026-\ln0.1=2.3026.
  • Standard Gaussian: the median is 00 (the density is symmetric), and F(1.96)=0.975F(1.96)=0.975, so 1.96 is the 97.5th percentile.

For a discrete variable the staircase has no exact inverse (it jumps over most heights), so a common convention takes the smallest xx with F(x)≥qF(x)\ge q. For a die and q=0.9q=0.9: F(5)=0.833F(5)=0.833 is too small and F(6)=1F(6)=1 is enough, so the 0.9-quantile is 6. Conventions differ between books and libraries, so check which one a tool uses. Percentiles and the median as summaries of data come back in P1.D12.

Functions of a random variable

If XX is a random variable and gg is a function, then Y=g(X)Y=g(X) is a random variable too: the sum of two dice is a function of the pair of dice, and a model’s output is a function of a random input. Its distribution is not found by applying gg to the PMF or density; it has to be worked out.

Discrete case: add up the probabilities that land on the same value. Let XX be one die and Y=∣X−3∣Y=|X-3|. The faces 1,2,3,4,5,61,2,3,4,5,6 give Y=2,1,0,1,2,3Y=2,1,0,1,2,3. So P(Y=0)=16P(Y=0)=\frac16, P(Y=1)=26P(Y=1)=\frac26 (the faces 2 and 4), P(Y=2)=26P(Y=2)=\frac26 (the faces 1 and 5) and P(Y=3)=16P(Y=3)=\frac16. The four values add up to 1. When gg sends two different xx to the same yy, their probabilities are added.

Continuous case: go through the CDF. Let XX be uniform on [0,1][0,1] and Y=X2Y=X^2. For 0≤y≤10\le y\le1,

FY(y)=P(X2≤y)=P(X≤y)=y,\begin{aligned} F_Y(y)&=P(X^2\le y)\\ &=P(X\le\sqrt y)\\ &=\sqrt y , \end{aligned}

because XX is uniform, so the CDF of XX at y\sqrt y is y\sqrt y. Differentiating gives the density pY(y)=12yp_Y(y)=\frac1{2\sqrt y} on (0,1)(0,1). This is not a uniform density: P(Y≤0.25)=0.25=0.5P(Y\le0.25)=\sqrt{0.25}=0.5, so half of the probability sits in the lowest quarter of the range, and the density is tall near 0 (it grows without bound there, yet its total area is still 1). Squaring squeezes values toward 0.

Stretching also changes the height. If XX is uniform on [0,1][0,1] and Y=2X+1Y=2X+1, then YY is uniform on [1,3][1,3] with density 12\frac12: the same probability is spread over twice the width, so the height halves. A density is probability per unit, and a change of variable changes the unit. The Go-deeper box below gives the rule, which is the one-dimensional case of the Jacobian factor of P1.C7.

Go deeper How a density changes when the variable is transformed

Let Z=f(X)Z=f(X) where ff is differentiable with f′(x)>0f'(x)\gt0 everywhere on the values of XX, so ff is strictly increasing and has an inverse. (Strictly increasing alone is not enough: x3x^3 is strictly increasing but has f′(0)=0f'(0)=0.) Then FZ(z)=P(f(X)≤z)=P(X≤f−1(z))=FX(f−1(z))F_Z(z)=P(f(X)\le z)=P(X\le f^{-1}(z))=F_X(f^{-1}(z)). Differentiating with the chain rule (P1.C3), with x=f−1(z)x=f^{-1}(z), gives pZ(z)=pX(x) dxdz=pX(x)f′(x)p_Z(z)=p_X(x)\,\frac{dx}{dz}=\frac{p_X(x)}{f'(x)}. Rearranged:

pX(x)=pZ(f(x)) ∣f′(x)∣.p_X(x)=p_Z\bigl(f(x)\bigr)\,\bigl|f'(x)\bigr| .

This is the formula of P1.C7, pX(x)=pZ(f(x)) ∣det⁡Jf(x)∣p_X(\mathbf{x})=p_Z(\mathbf{f}(\mathbf{x}))\,|\det\mathbf{J}_{\mathbf{f}}(\mathbf{x})|, with a 1×11\times1 Jacobian whose determinant is f′(x)f'(x). The absolute value makes it work for a strictly decreasing ff as well. The factor ∣f′(x)∣|f'(x)| says how much ff stretches a small interval around xx, and density per unit falls by that factor when the interval is stretched.

Check it for Z=2X+1Z=2X+1 with XX uniform on [0,1][0,1]: pX(x)=1p_X(x)=1, pZ=12p_Z=\frac12 and f′=2f'=2, so pZ(f(x)) ∣f′(x)∣=12⋅2=1=pX(x)p_Z(f(x))\,|f'(x)|=\frac12\cdot2=1=p_X(x). Check it for Z=X2Z=X^2 on x>0x\gt0 (increasing there), with z=x2z=x^2: pZ(z)=pX(z)2z=12zp_Z(z)=\frac{p_X(\sqrt z)}{2\sqrt z}=\frac1{2\sqrt z}, the density found above from the CDF. If ff is not one-to-one (for instance x2x^2 on negative and positive xx), add the contributions of all xx with f(x)=zf(x)=z at which f′(x)≠0f'(x)\ne0: pZ(z)=∑x: f(x)=zpX(x)∣f′(x)∣p_Z(z)=\sum_{x:\,f(x)=z}\frac{p_X(x)}{|f'(x)|}.

Go deeper Inverse-CDF sampling: random numbers with any CDF

Computers produce numbers UU uniform on [0,1][0,1], for which P(U≤u)=uP(U\le u)=u. Suppose FF is a continuous CDF that is strictly increasing, and let X=F−1(U)X=F^{-1}(U). Then P(X≤x)=P(U≤F(x))=F(x)P(X\le x)=P\bigl(U\le F(x)\bigr)=F(x), so XX has exactly the CDF FF. To sample from a distribution, push uniform numbers through the inverse of its CDF.

For the exponential, F−1(u)=−ln⁡(1−u)F^{-1}(u)=-\ln(1-u). For a discrete variable the same idea works with the staircase: for uu in the range (F(x−), F(x)]\bigl(F(x^-),\,F(x)\bigr] return xx. For a die and u=0.4u=0.4: F(2)=13<0.4≤F(3)=12F(2)=\frac13\lt0.4\le F(3)=\frac12, so the sample is 3. Each face is returned for a stretch of uu values of length 16\frac16, which is its probability. The second code cell below tries it on the exponential. P1.D9 develops sampling and when other methods are needed (when F−1F^{-1} has no formula, as for the Gaussian).

Go deeper What the formal definition requires

Formally, a random variable is a function X:Ω→RX:\Omega\to\mathbb{R} with one technical requirement: for every real xx, the set {X≤x}\{X\le x\} must be an event, one that has a probability. This is what makes F(x)=P(X≤x)F(x)=P(X\le x) defined for every xx. In practice every function you will meet satisfies it.

Right-continuity of FF follows from the countable version of the third rule of P1.D1 (countable additivity): the events {X≤x+1/n}\{X\le x+1/n\} shrink toward {X≤x}\{X\le x\} as nn grows, and probabilities of shrinking events converge to the probability of the limit event. Left limits behave differently: F(x−)=P(X<x)F(x^-)=P(X\lt x) can be smaller than F(x)F(x), and the gap is P(X=x)P(X=x), which is the jump. A standard theorem says the four properties (never decreasing, limits 0 and 1, right-continuous) are all that a function needs to be the CDF of some random variable.

A density is one way to have a continuous CDF, and the usual way in machine learning. A continuous CDF does not always have a density (the Cantor function is the textbook counterexample), but such cases are rare in practice and are not needed in this course.

Several random variables

Everything so far is about one random variable. Two dice give two variables, X1X_1 and X2X_2, and their sum X1+X2X_1+X_2 is a third. A data point with dd features is dd random variables at once, a random vector. How they relate, including how knowing one changes what you expect of the other, is the subject of P1.D5 (joint, marginal and conditional probability).

Models as random variables

The same ideas explain how learning systems use probability.

  • Classification. The label is a discrete random variable. The model’s softmax output is its predicted PMF for the label (P3.A8). Training pushes the probability of the true label up.
  • Language models. The next token ID is a discrete random variable whose PMF has one entry per vocabulary item (P10.A1). Generating text means drawing from that PMF (P1.D9, P10.A5).
  • Regression. A common model says y=f(x)+εy=f(\mathbf{x})+\varepsilon, where the noise ε\varepsilon is a continuous random variable with a Gaussian density (P3.A2, P1.D3). The target yy is then random too, because it is a function of a random variable.
  • Likelihood. Given data, the likelihood multiplies the PMF or density at each observed value. If a die shows 3,3,53,3,5, a fair die gives (16)3=0.0046\left(\frac16\right)^3=0.0046. If waiting times 0.50.5 and 11 are modelled by the exponential density, the product is e−0.5⋅e−1=0.2231\mathrm{e}^{-0.5}\cdot\mathrm{e}^{-1}=0.2231. A product of densities is not a probability (a density value is a height), but it can be compared between models for the same data, and that is how maximum likelihood chooses (P1.D10).

Intuition

A random variable is a measuring device pointed at a random experiment: it reads each outcome as a number. The PMF says how much probability sits at each reading. The CDF is a running total: walk along the number line from the far left and keep adding probability, and the CDF is the total so far. A discrete variable adds it in lumps (a staircase); a continuous one adds it smoothly (a ramp or an S-curve). The density is how fast the running total grows. The analogy of probability as clay or water breaks in one place: the total is always exactly 1, and a point of a continuous variable holds no clay at all, only a rate of piling up.

Math

X:Ω→RX:\Omega\to\mathbb{R}; {X=x}={ω:X(ω)=x}\{X=x\}=\{\omega:X(\omega)=x\}.

Discrete: p(x)=P(X=x)≥0p(x)=P(X=x)\ge0, ∑xp(x)=1\sum_xp(x)=1, P(X∈A)=∑x∈Ap(x)P(X\in A)=\sum_{x\in A}p(x).

CDF: F(x)=P(X≤x)F(x)=P(X\le x); nondecreasing, right-continuous, F(−∞)=0F(-\infty)=0, F(∞)=1F(\infty)=1.

  • P(a<X≤b)=F(b)−F(a)P(a\lt X\le b)=F(b)-F(a)
  • P(X>x)=1−F(x)P(X\gt x)=1-F(x)
  • P(X=x)=F(x)−F(x−)P(X=x)=F(x)-F(x^-)

Continuous with density: F(x)=∫−∞xp(t) dtF(x)=\int_{-\infty}^xp(t)\,dt, F′(x)=p(x)F'(x)=p(x) where pp is continuous, P(X=x)=0P(X=x)=0, ∫p=1\int p=1, p≥0p\ge0 (no upper bound on pp).

Quantile: F−1(q)F^{-1}(q) for a strictly increasing continuous FF; in general inf⁡{x:F(x)≥q}\inf\{x:F(x)\ge q\}.

Function of a variable Y=g(X)Y=g(X):

  • Discrete: pY(y)=∑x: g(x)=ypX(x)p_Y(y)=\sum_{x:\,g(x)=y}p_X(x).
  • Continuous: FY(y)=P(g(X)≤y)F_Y(y)=P(g(X)\le y).
  • Strictly monotone gg with X=g−1(Y)X=g^{-1}(Y): pY(y)=pX(x) ∣dxdy∣p_Y(y)=p_X(x)\,\bigl|\tfrac{dx}{dy}\bigr|.

Code

import numpy as np

# two dice: x = 2..12
counts = [1, 2, 3, 4, 5, 6,
          5, 4, 3, 2, 1]
pmf = np.array(counts) / 36
cdf = np.cumsum(pmf)

print(cdf[5])    # F(7) = 0.5833...

# discrete quantile for q = 0.9:
# the smallest x with F(x) >= q
print(2 + np.searchsorted(cdf, 0.9))
# prints 10

np.cumsum turns a PMF into a CDF. For a continuous variable, the CDF of a Gaussian is 0.5 * (1 + math.erf(x / math.sqrt(2))) with the standard library’s math.erf.

Try it: PMFs, CDFs and densities in code

The first cell builds the PMF of the sum of two dice by counting, turns it into a CDF, checks the interval rule, finds the PMF of a function of XX, and compares with a simulation. Predict F(7)F(7) before you run it. The second cell checks that the slope of the exponential CDF is the density, computes Gaussian probabilities with math.erf, finds quantiles, and samples by the inverse CDF.

Try it: PMF, CDF and a function of X for two dice

Runs real Python in your browser. First run downloads about 6 MB (once; your browser keeps it).

Challenge: change the code to three dice (add a third loop variable and use 216 outcomes). What is P(X=10)P(X=10)? Compare with the number of ways to roll 10 with three dice, which you can count by hand.

Try it: density as the slope of the CDF, quantiles and sampling

Runs real Python in your browser. First run downloads about 6 MB (once; your browser keeps it).

Challenge: use the inverse-CDF idea for the uniform density on [0,2][0,2] (F(x)=x/2F(x)=x/2) and check that the share of samples at most 0.50.5 is about 0.250.25.

Common mistakes

Check your understanding

  1. A fair die is rolled. What is P(2≤X≤5)P(2\le X\le5)?

  2. A random variable has the density p(x)=4p(x)=4 for 0≤x≤0.250\le x\le0.25 and p(x)=0p(x)=0 elsewhere. Which statement is correct?

  3. Which of these functions cannot be the CDF of any random variable?

  4. For a standard Gaussian variable XX the density at 1 is p(1)=0.242p(1)=0.242. What is P(X=1)P(X=1)?

  5. Let XX be uniform on [0,1][0,1] and Y=X2Y=X^2. What is P(Y≤0.25)P(Y\le0.25)?

Exercises

  1. Two fair dice are summed. Find P(X=9)P(X=9) and P(X≥9)P(X\ge9).
    Show answerThere are 4 ways to get 9, so P(X=9)=436=19P(X=9)=\frac4{36}=\frac19. The ways to get 9, 10, 11 and 12 are 4+3+2+1=104+3+2+1=10, so P(X≥9)=1036=518≈0.278P(X\ge9)=\frac{10}{36}=\frac5{18}\approx0.278.
  2. For one fair die, find F(0)F(0), F(2.5)F(2.5) and F(6)F(6).
    Show answerF(0)=0F(0)=0, F(2.5)=26=13F(2.5)=\frac26=\frac13 (the faces 1 and 2), F(6)=1F(6)=1.
  3. For one fair die, find P(X<5)P(X\lt5) and P(X≤5)P(X\le5), and say which of them equals F(4)F(4).
    Show answerP(X<5)=46=23P(X\lt5)=\frac46=\frac23 and P(X≤5)=56P(X\le5)=\frac56. The first one equals F(4)F(4), because the possible values below 5 are the ones up to 4.
  4. Is p(1)=0.5p(1)=0.5, p(2)=0.3p(2)=0.3, p(3)=0.3p(3)=0.3 a valid PMF? If not, fix it by changing one number.
    Show answerNo: the numbers add up to 1.11.1, not 1. Changing p(3)p(3) to 0.20.2 gives a valid PMF.
  5. XX is uniform on [0,4][0,4]. Give its density, its CDF on [0,4][0,4], P(1<X<3)P(1\lt X\lt3) and its median.
    Show answerThe density is 14\frac14 on [0,4][0,4] (area 14⋅4=1\frac14\cdot4=1), and F(x)=x4F(x)=\frac x4. P(1<X<3)=F(3)−F(1)=34−14=12P(1\lt X\lt3)=F(3)-F(1)=\frac34-\frac14=\frac12. The median solves x4=0.5\frac x4=0.5, so it is 2.
  6. For the exponential density e−x\mathrm{e}^{-x} (x≥0x\ge0), find P(X>3)P(X\gt3).
    Show answerP(X>3)=1−F(3)=e−3≈0.0498P(X\gt3)=1-F(3)=\mathrm{e}^{-3}\approx0.0498.
  7. Show that p(x)=3x2p(x)=3x^2 on [0,1][0,1] (and 0 elsewhere) is a valid density. Find its CDF on [0,1][0,1] and P(0.5<X≤1)P(0.5\lt X\le1). Where is the density above 1?
    Show answerIt is nonnegative and ∫013x2 dx=[x3]01=1\int_0^13x^2\,dx=\bigl[x^3\bigr]_0^1=1. The CDF is F(x)=x3F(x)=x^3 on [0,1][0,1], and F′(x)=3x2F'(x)=3x^2 confirms it. P(0.5<X≤1)=1−0.53=0.875P(0.5\lt X\le1)=1-0.5^3=0.875. The density is above 1 for x>1/3≈0.577x\gt1/\sqrt3\approx0.577, up to 3 at x=1x=1.
  8. For the standard Gaussian, F(1)=0.8413F(1)=0.8413. Find P(X>1)P(X\gt1), P(X≤−1)P(X\le-1) and P(−1<X≤1)P(-1\lt X\le1).
    Show answerP(X>1)=1−0.8413=0.1587P(X\gt1)=1-0.8413=0.1587. By symmetry P(X≤−1)=0.1587P(X\le-1)=0.1587 as well. So P(−1<X≤1)=1−2(0.1587)=0.6827P(-1\lt X\le1)=1-2(0.1587)=0.6827.
  9. XX is uniform on [0,1][0,1] and Y=2X+1Y=2X+1. Find the CDF and the density of YY.
    Show answerFY(y)=P(2X+1≤y)=P(X≤y−12)=y−12F_Y(y)=P(2X+1\le y)=P\bigl(X\le\frac{y-1}2\bigr)=\frac{y-1}2 for 1≤y≤31\le y\le3. The density is the derivative, 12\frac12 on [1,3][1,3]: YY is uniform on [1,3][1,3].
  10. Find the 75th percentile of the uniform density on [0,2][0,2] and of the exponential density.
    Show answerUniform: x2=0.75\frac x2=0.75 gives 1.51.5. Exponential: −ln⁡(1−0.75)=ln⁡4≈1.386-\ln(1-0.75)=\ln4\approx1.386.
  11. Code: in the first cell, find the PMF of the largest of two dice, max⁡(a,b)\max(a,b). What is P(max⁡=6)P(\max=6)?
    Show answerCount pairs whose maximum is mm: 2m−12m-1 of the 36, so P(max⁡=m)=2m−136P(\max=m)=\frac{2m-1}{36} and P(max⁡=6)=1136≈0.306P(\max=6)=\frac{11}{36}\approx0.306.

Next: P1.D3 gathers the common named distributions, with their PMFs or densities and CDFs. P1.D4 summarises a distribution with its expected value and variance, which are sums over the PMF or integrals against the density. P1.D5 puts several random variables together, and P1.D9 shows how to draw samples from a distribution.

Key takeaways

  • A random variable XX is a rule that gives each outcome of an experiment a number. It is neither random (the rule is fixed) nor a variable (it is a function); the randomness is in which outcome happens. {X=x}\{X=x\} is the event “the outcomes that XX turns into xx”, and P(X=x)P(X=x) is its probability.
  • Discrete variables take values you can list (die faces, class IDs, token IDs). Their PMF p(x)=P(X=x)p(x)=P(X=x) is at least 0 and adds up to 1, so it never exceeds 1. Continuous variables take any value in an interval, and they are described by a density instead.
  • The CDF F(x)=P(X≤x)F(x)=P(X\le x) exists for every random variable. It rises from 0 to 1 and never decreases. For a discrete variable it is a staircase that is right-continuous, with a jump of size p(x)p(x) at each possible value; for a continuous variable it is smooth. P(a<X≤b)=F(b)−F(a)P(a\lt X\le b)=F(b)-F(a) and P(X>x)=1−F(x)P(X\gt x)=1-F(x).
  • A density p(x)≥0p(x)\ge0 has total area 1, and P(a≤X≤b)P(a\le X\le b) is the area under it. Its height is probability per unit of xx, so it can exceed 1 (a density of 2 on [0,0.5][0,0.5] is valid). For a continuous variable P(X=x)=0P(X=x)=0 for every xx, which does not make xx impossible, and <\lt and ≤\le give the same probability.
  • The CDF is the accumulated area of the density, so F′(x)=p(x)F'(x)=p(x) wherever pp is continuous (the fundamental theorem of P1.C9). Steep parts of FF are tall parts of pp, and flat parts of FF are zero parts of pp.
  • A function of a random variable, Y=g(X)Y=g(X), is a new random variable. For a discrete XX, add the probabilities of all xx that give the same yy. For a continuous XX, work with the CDF: P(Y≤y)=P(g(X)≤y)P(Y\le y)=P(g(X)\le y). A quantile inverts the CDF: the median solves F(x)=0.5F(x)=0.5.
Found a mistake? Tell us