Lesson 9 of 16 · Random Variables
Discrete Random Variables
Random variables explained: the probability mass function (PMF) and cumulative distribution function (CDF), built and checked in Python with interactive charts.
- 6 min read
- Intermediate
- Reviewed
- Explain what a random variable is
- Tell discrete random variables apart from continuous ones
- Build a probability mass function (PMF) in Python
- Use a cumulative distribution function (CDF) to answer “at most” questions
- Compare a PMF with simulated data
From outcomes to numbers
So far, outcomes have been things like (3, 4) or HHT. Usually what we care about is a number that comes from the outcome: the total of the dice, the number of heads, the number of customers who click.
The probability mass function
A discrete random variable is described completely by listing each value with its probability.
Let X be the number of heads in three coin flips. Counting the 8 equally likely outcomes gives its PMF. Python's Fraction keeps the answers exact:
from collections import Counter
from fractions import Fraction
from itertools import product
# X = the number of heads in three coin flips
outcomes = list(product("HT", repeat=3)) # 8 equally likely outcomes
counts = Counter(outcome.count("H") for outcome in outcomes)
pmf = {x: Fraction(counts[x], len(outcomes)) for x in sorted(counts)}
for x, p in pmf.items():
print(f"P(X = {x}) = {p} ({float(p):.3f})")
print("total:", sum(pmf.values()))A bar chart is the natural picture of a PMF. Here's the simplest one, a fair die, where every value has the same probability. Drag the slider to change the number of sides:
The mean under the chart is the long-run average value. It's called the expected value, and it's the subject of lesson 11.
The cumulative distribution function
Many questions ask for “at most” or “at least”: at most 3 defects, at least 2 heads. For these, add up the PMF.
Here's the CDF of the total of two dice, built from its PMF with itertools.accumulate, next to the results of 10,000 simulated rolls:
import random
from itertools import accumulate, product
# X = the total of two dice. Its PMF comes from the 36 equally likely pairs
totals = [a + b for a, b in product(range(1, 7), repeat=2)]
values = list(range(2, 13))
pmf = [totals.count(x) / 36 for x in values]
cdf = list(accumulate(pmf)) # running total: P(X <= x)
# Roll two dice 10,000 times and compare
random.seed(5)
rolls = [random.randint(1, 6) + random.randint(1, 6) for _ in range(10_000)]
print(" x P(X = x) P(X <= x) share in 10,000 rolls")
for x, p, c in zip(values, pmf, cdf):
print(f"{x:2} {p:.3f} {c:.3f} {rolls.count(x) / len(rolls):.3f}")The simulated shares land close to the PMF, as the law of large numbers promises. Drag the slider to see the CDF as an area of the chart: the highlighted bars add up to P(X ≤ x).
Common mistakes
Use the PMF to find the probability of at most 2 tickets in an hour, and of at least 2. Why don't the two answers add up to 1?
# X = the number of support tickets that arrive in an hour
pmf = {0: 0.1, 1: 0.3, 2: 0.3, 3: 0.2, 4: 0.1}
# Calculate P(X <= 2) and P(X >= 2) from the PMF.
# Print both on one line, each rounded to 2 decimals.
X is the total of two dice, and P(X ≤ 6) = 0.417. What is P(X ≥ 7)?
Summary
- A random variable is a number that depends on a random outcome.
- Discrete random variables take values you can list, usually counts.
- The PMF gives P(X = x) for every value; its values add up to 1.
- The CDF gives P(X ≤ x), the running total of the PMF.
- Use the complement rule for “at least”: P(X ≥ x) = 1 − P(X ≤ x − 1) for whole-number values.
Next: continuous random variables.
Finished reading? Tick it off to track your progress.