Random Variables
Mapping outcomes to numbers, discrete vs continuous, and the support of a random variable.
Assumes you know
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 , but the random variable "sum" turns it into . 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 does not mean "X equals 8"; it is shorthand for the event consisting of all outcomes that the function maps to 8.
Definition
A random variable is a function from the sample space to the real numbers:
| Symbol | Meaning | Read aloud |
|---|---|---|
| X, Y, Z | Random variables — capital letters | big X |
| x, y, z | Particular values a random variable can take | little x |
| {X = x} | The event that X takes the value x | the event X equals x |
| P(X = x) | Probability of that event | P of X equals x |
| Sₓ | Support — the set of values X can actually take | support of X |
A worked mapping
Toss a fair coin three times. The sample space has eight equally likely outcomes:
Define = number of heads. The function maps:
| Outcome | |
|---|---|
| 3 | |
| , , | 2 |
| , , | 1 |
| 0 |
The support is . Now collect probabilities by counting outcomes in each pre-image:
These sum to 1, as they must, because assigns every outcome to exactly one value.
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: for every single value.
Why continuous variables assign zero probability to every pointAdvanced
Suppose is uniform on and every single value had the same probability . The interval contains uncountably many points, so any positive would give total probability exceeding 1 — indeed exceeding any bound. So must be 0.
Yet some value occurs. Both statements are consistent because probability for continuous variables lives in intervals, not points:
and the integral over a single point is zero. A density is therefore not a probability — it is probability per unit length, and can exceed 1 without contradiction. A uniform distribution on has density 2 everywhere on that interval.
A practical consequence: for continuous variables the inequalities do not matter,
because the single point contributes nothing. For discrete variables they differ, and conflating them is a standard source of off-by-one errors.
Functions of random variables
If is a random variable and is a function, then is also a random variable — a composition of functions is a function. This is why expressions like , and 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 = sum of the dice and = maximum of the two dice. Find the distribution of each, and and .
Step 1 — the sample space
Ordered pairs, so , each with probability .
Step 2 — outcomes giving S = 7
Six outcomes, so
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.
Nine outcomes:
Step 4 — a general formula for M, as a check
The maximum is at most exactly when both dice are at most , which happens in of the 36 outcomes. So
For :
Step 5 — full distributions
Check that each sums to 1. For : , so
Answer
and . Two different random variables on the same sample space, each summarising the outcome differently — and note that and are not independent, since knowing the maximum constrains the possible sums.
Indicator random variables
The simplest and most useful special case. For an event :
Its expectation is the probability of the event, , 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.
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 be the number of tosses. Give the distribution of and verify it sums to 1.
Show solutionHide solution
The process stops at the first head, or after 4 tosses regardless.
For the toss happens whether or not it is a head, since we stop after four either way. So covers both and :
Verify:
The subtlety is the final value. A truncated geometric distribution puts the leftover probability mass on the last value, so rather than .
Exercise 2
Explain why can be non-zero for a discrete random variable but must be zero for a continuous one, and what replaces it.
Show solutionHide solution
For a discrete variable, is a genuine event — a set of outcomes with positive total probability. The three-coin example has , the single outcome .
For a continuous variable the support is uncountable. If every point had positive probability , then summing over uncountably many points would exceed 1, so 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 is zero.
What replaces it is the density , which gives probability per unit length:
So 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.