Skip to content
VibeFormer
Beginner20 min

Random Variables

Mapping outcomes to numbers, discrete vs continuous, and the support of a random variable.

Random Variables

Intuition first

Events are sets — "the roll is even", "the email is spam". Sets are awkward to do arithmetic with. You cannot average a set, or compute its variance, or feed it to a model.

A random variable fixes that by attaching a number to every outcome. Roll two dice: the outcome is a pair like (3,5)(3, 5), but the random variable "sum" turns it into 88. Now you can ask for the average sum, the spread of sums, the chance the sum exceeds 9 — all numerical questions.

The key thing to get straight, and it trips up nearly everyone at first: a random variable is a function, not a number. It has no value until an outcome occurs. The notation X=8X = 8 does not mean "X equals 8"; it is shorthand for the event consisting of all outcomes that the function XX maps to 8.

Definition

A random variable is a function from the sample space to the real numbers:

X:ΩRX : \Omega \to \mathbb{R}
Notation used in this lesson
SymbolMeaning
X, Y, ZRandom variables — capital letters
x, y, zParticular values a random variable can take
{X = x}The event that X takes the value x
P(X = x)Probability of that event
SₓSupport — the set of values X can actually take

A worked mapping

Toss a fair coin three times. The sample space has eight equally likely outcomes:

Ω={HHH,HHT,HTH,HTT,THH,THT,TTH,TTT}\Omega = \{HHH,\, HHT,\, HTH,\, HTT,\, THH,\, THT,\, TTH,\, TTT\}

Define XX = number of heads. The function maps:

OutcomeXX
HHHHHH3
HHTHHT, HTHHTH, THHTHH2
HTTHTT, THTTHT, TTHTTH1
TTTTTT0

The support is SX={0,1,2,3}S_X = \{0, 1, 2, 3\}. Now collect probabilities by counting outcomes in each pre-image:

P(X=0)=18,P(X=1)=38,P(X=2)=38,P(X=3)=18\Prob(X = 0) = \tfrac{1}{8}, \quad \Prob(X = 1) = \tfrac{3}{8}, \quad \Prob(X = 2) = \tfrac{3}{8}, \quad \Prob(X = 3) = \tfrac{1}{8}

These sum to 1, as they must, because XX assigns every outcome to exactly one value.

ΩHHHHHTHTHHTTTHHTHTTTHTTT3210X = number of heads
A random variable is a function from outcomes to numbers. Several outcomes can map to the same value — three sequences give X = 2 — and the probability of that value is the total probability of its pre-image.

Discrete versus continuous

Discrete — the support is finite or countably infinite. Counts, categories, number of arrivals. Individual values carry positive probability.

Continuous — the support is an interval. Heights, times, temperatures. Here something unintuitive happens: P(X=x)=0\Prob(X = x) = 0 for every single value.

Why continuous variables assign zero probability to every pointAdvanced

Suppose XX is uniform on [0,1][0, 1] and every single value had the same probability p>0p > 0. The interval contains uncountably many points, so any positive pp would give total probability exceeding 1 — indeed exceeding any bound. So pp must be 0.

Yet some value occurs. Both statements are consistent because probability for continuous variables lives in intervals, not points:

P(aXb)=abf(x)dx\Prob(a \leq X \leq b) = \int_a^b f(x)\,dx

and the integral over a single point is zero. A density f(x)f(x) is therefore not a probability — it is probability per unit length, and can exceed 1 without contradiction. A uniform distribution on [0,0.5][0, 0.5] has density 2 everywhere on that interval.

A practical consequence: for continuous variables the inequalities do not matter,

P(Xa)=P(X<a)\Prob(X \leq a) = \Prob(X < a)

because the single point aa contributes nothing. For discrete variables they differ, and conflating them is a standard source of off-by-one errors.

Functions of random variables

If XX is a random variable and gg is a function, then g(X)g(X) is also a random variable — a composition of functions is a function. This is why expressions like X2X^2, eXe^X and X1+X2X_1 + X_2 make sense, and it is what makes random variables useful for building models.

Solved problem 1 · Two random variables on the same sample space

Two fair dice are rolled. Define SS = sum of the dice and MM = maximum of the two dice. Find the distribution of each, and P(S=7)\Prob(S = 7) and P(M=5)\Prob(M = 5).

Step 1 — the sample space

Ordered pairs, so Ω=36\lvert \Omega \rvert = 36, each with probability 1/361/36.

Step 2 — outcomes giving S = 7

(1,6),(2,5),(3,4),(4,3),(5,2),(6,1)(1,6),\, (2,5),\, (3,4),\, (4,3),\, (5,2),\, (6,1)

Six outcomes, so

P(S=7)=636=16\Prob(S = 7) = \frac{6}{36} = \frac{1}{6}

Step 3 — outcomes giving M = 5

The maximum is 5 when at least one die shows 5 and neither shows 6. Count directly: pairs where the larger value is exactly 5.

(1,5),(2,5),(3,5),(4,5),(5,5),(5,4),(5,3),(5,2),(5,1)(1,5),\, (2,5),\, (3,5),\, (4,5),\, (5,5),\, (5,4),\, (5,3),\, (5,2),\, (5,1)

Nine outcomes:

P(M=5)=936=14\Prob(M = 5) = \frac{9}{36} = \frac{1}{4}

Step 4 — a general formula for M, as a check

The maximum is at most mm exactly when both dice are at most mm, which happens in m2m^2 of the 36 outcomes. So

P(Mm)=m236P(M=m)=m2(m1)236=2m136\Prob(M \leq m) = \frac{m^2}{36} \quad\Longrightarrow\quad \Prob(M = m) = \frac{m^2 - (m-1)^2}{36} = \frac{2m - 1}{36}

For m=5m = 5: (2×51)/36=9/36  (2 \times 5 - 1)/36 = 9/36 \;\checkmark

Step 5 — full distributions

P(S=s)=6s736,s=2,,12\Prob(S = s) = \frac{6 - \lvert s - 7 \rvert}{36}, \qquad s = 2, \dots, 12P(M=m)=2m136,m=1,,6\Prob(M = m) = \frac{2m - 1}{36}, \qquad m = 1, \dots, 6

Check that each sums to 1. For MM: 1+3+5+7+9+11=361 + 3 + 5 + 7 + 9 + 11 = 36, so 36/36=1  36/36 = 1 \;\checkmark

Answer

P(S=7)=1/6\Prob(S = 7) = 1/6 and P(M=5)=1/4\Prob(M = 5) = 1/4. Two different random variables on the same sample space, each summarising the outcome differently — and note that SS and MM are not independent, since knowing the maximum constrains the possible sums.

Indicator random variables

The simplest and most useful special case. For an event AA:

1A(ω)={1if ωA0otherwise\mathbb{1}_A(\omega) = \begin{cases} 1 & \text{if } \omega \in A \\ 0 & \text{otherwise}\end{cases}

Its expectation is the probability of the event, E[1A]=P(A)\E[\mathbb{1}_A] = \Prob(A), which turns counting problems into expectation problems. Writing a count as a sum of indicators and using linearity of expectation is one of the most powerful techniques in probability, and it appears repeatedly from here on.

python
import numpy as np

rng = np.random.default_rng(0)
rolls = rng.integers(1, 7, size=(200_000, 2))   # two dice, many trials

S = rolls.sum(axis=1)     # the random variable "sum"
M = rolls.max(axis=1)     # the random variable "maximum"

print(f"P(S=7) empirical {np.mean(S == 7):.4f}  theory {1/6:.4f}")
print(f"P(M=5) empirical {np.mean(M == 5):.4f}  theory {9/36:.4f}")

# Full distribution of M against the formula (2m-1)/36.
for m in range(1, 7):
    print(f"  P(M={m}) empirical {np.mean(M == m):.4f}  theory {(2*m - 1)/36:.4f}")

Exercise 1

A coin is tossed until the first head, at most 4 times. Let XX be the number of tosses. Give the distribution of XX and verify it sums to 1.

Show solution

The process stops at the first head, or after 4 tosses regardless.

P(X=1)=P(H)=12\Prob(X = 1) = \Prob(H) = \tfrac12P(X=2)=P(TH)=14\Prob(X = 2) = \Prob(TH) = \tfrac14P(X=3)=P(TTH)=18\Prob(X = 3) = \Prob(TTH) = \tfrac18

For X=4X = 4 the toss happens whether or not it is a head, since we stop after four either way. So X=4X = 4 covers both TTTHTTTH and TTTTTTTT:

P(X=4)=P(TTT)=18\Prob(X = 4) = \Prob(TTT) = \tfrac18

Verify:

12+14+18+18=4+2+1+18=1  \tfrac12 + \tfrac14 + \tfrac18 + \tfrac18 = \tfrac{4 + 2 + 1 + 1}{8} = 1 \;\checkmark

The subtlety is the final value. A truncated geometric distribution puts the leftover probability mass on the last value, so P(X=4)=1/8\Prob(X = 4) = 1/8 rather than 1/161/16.

Exercise 2

Explain why P(X=3)\Prob(X = 3) can be non-zero for a discrete random variable but must be zero for a continuous one, and what replaces it.

Show solution

For a discrete variable, {X=3}\{X = 3\} is a genuine event — a set of outcomes with positive total probability. The three-coin example has P(X=3)=1/8\Prob(X = 3) = 1/8, the single outcome HHHHHH.

For a continuous variable the support is uncountable. If every point had positive probability pp, then summing over uncountably many points would exceed 1, so p=0p = 0 necessarily. There is no contradiction with "some value occurs", because probability is assigned to intervals via an integral, and the integral over a degenerate interval [3,3][3,3] is zero.

What replaces it is the density f(x)f(x), which gives probability per unit length:

P(3ϵ2X3+ϵ2)f(3)ϵ\Prob(3 - \tfrac{\epsilon}{2} \leq X \leq 3 + \tfrac{\epsilon}{2}) \approx f(3)\,\epsilon

So f(3)f(3) tells you how probable values near 3 are relative to elsewhere, and multiplying by a width gives an actual probability. Densities can exceed 1; probabilities cannot.


Next: PMF, PDF and CDF, the three standard ways to describe a random variable's distribution.