Chapter 3: Discrete random variables
Previous: PS 02 Conditional Probability and Bayes Rule | PS 00 Course Map | Next: PS 04 Expectation and Variance
A random variable attaches a number to each outcome of an experiment, so we can do arithmetic with uncertainty. A handful of named distributions (Bernoulli, binomial, geometric, Poisson, categorical) describe most situations where you count things. Recognizing which one applies is half the work.
- Define a random variable and describe it by its PMF and CDF.
- Recognize Bernoulli, binomial, geometric, Poisson and categorical situations.
- Derive the binomial formula from counting.
- Compute probabilities with each distribution.
- Choose the right model for a counting problem, and spot when the assumptions fail.
You need the axioms and independence (PS 01 Sample Spaces Events and Axioms, PS 02 Conditional Probability and Bayes Rule) and binomial coefficients (DM 07 Counting): is the number of ways to choose items from .
3.1 What is a random variable?
Numbers from outcomes
Outcomes are often not numbers (, “card is the queen of hearts”). To compute averages, spreads and so on, we attach a number to each outcome.
A random variable is a function that assigns a real number to each outcome in the sample space.
Example. Flip two coins and let be the number of heads:
| Outcome | ||||
|---|---|---|---|---|
| 2 | 1 | 1 | 0 |
Despite the name, a random variable is not “random” in itself, nor a “variable” in the algebra sense. It is a fixed rule. The randomness comes from which outcome occurs.
Notation
- Upper case : the random quantity (the rule).
- Lower case : a particular value it might take.
- "" is an event: the set of outcomes where equals 1. Here it is .
A random variable is discrete if its possible values can be listed: finitely many, or a countable sequence like (Continuous random variables are chapter 5.)
3.2 PMF and CDF
Probability mass function
The probability mass function of a discrete random variable is
It satisfies for all , and .
For the two coins:
| 0 | 1 | 2 | |
|---|---|---|---|
A PMF is often drawn as a bar chart: one bar per value, with height equal to its probability.
To find the probability of a set of values, add the PMF over that set: .
Cumulative distribution function
The cumulative distribution function is
For the two coins: , , . Between those values it stays flat (a staircase). Every CDF starts at 0 on the far left, ends at 1 on the far right, and never decreases.
Useful identity for integers: .
A random variable has PMF , , . Find , and .
Answer. . . .
3.3 Bernoulli: one yes/no trial
takes the value 1 (“success”) with probability and 0 (“failure”) with probability .
Examples: one coin flip, one grasp attempt, whether one prediction is correct, whether one email is spam.
Its mean is and its variance is (computed in chapter 4). The variance is largest at , where the outcome is hardest to predict, and zero at or , where there is no uncertainty.
Bernoulli variables are the building blocks of everything else in this chapter.
3.4 Binomial: successes in trials
The setting
Repeat a Bernoulli() trial times, independently, with the same each time. Let be the number of successes.
Where the formula comes from
Take and . One particular sequence with 2 successes is . By independence its probability is
Every other sequence with 2 successes (, , , , ) has the same probability, just multiplied in a different order. How many such sequences are there? Choose which 2 of the 4 positions are successes: . These sequences are disjoint events, so add:
In general: arrangements, each with probability .
Worked example 3.1: a policy evaluation
A robot policy succeeds with probability on each trial. You run trials. What is the distribution of the number of successes?
| 0 | 1 | 0.0003 | |
| 1 | 5 | 0.0064 | |
| 2 | 10 | 0.0512 | |
| 3 | 10 | 0.2048 | |
| 4 | 5 | 0.4096 | |
| 5 | 1 | 0.3277 |
Check: the probabilities sum to 1.
The lesson. Even with a true success rate of 80%, a 5-trial evaluation shows 3 or fewer successes with probability . More than a quarter of the time, a good policy looks mediocre. Small evaluations are noisy. Chapter 10 quantifies how many trials you need.
Mean and variance
(Derived in chapter 4 by writing as a sum of Bernoulli variables.) For example 3.1: mean 4, variance 0.8.
The binomial assumptions
- A fixed number of trials .
- Each trial has two outcomes.
- The success probability is the same for every trial.
- Trials are independent.
If a robot’s success rate drops as its battery drains (assumption 3 fails), or trials share the same tricky object placement (assumption 4 fails), the binomial is only an approximation.
- A fair coin is flipped 4 times. Write the full PMF of the number of heads.
- A 90% policy is run 10 times. What is ?
Answers. 1. . 2. : a perfect run happens only about a third of the time.
3.5 Geometric: waiting for the first success
The setting
Repeat independent Bernoulli() trials until the first success. Let be the number of trials needed (including the successful one).
Why: the first trials must fail (probability ), then the -th must succeed (probability ).
In words: if each attempt works 1 time in 10, expect 10 attempts on average.
Worked example 3.2
Rolling a die until a six appears:
- .
- .
- Expected number of rolls: 6.
Memorylessness
Having already failed times does not bring success any closer.
Proof: .
This is the precise statement of why the gambler’s fallacy is a fallacy: after 20 tails, a fair coin is not “due” a head.
Some books count the number of failures before the first success (), which shifts the mean to . Check which version a source uses.
3.6 Poisson: counting rare events
The setting
Count events that happen independently at a constant average rate over a fixed interval of time or space: emails per hour, faults per day, typos per page.
is the average count in the interval. Mean and variance are equal, a quick check for whether count data is “Poisson-like”.
Where it comes from
Split an hour into tiny slots. In each slot an event happens with small probability , independently. The count is Binomial(, ). As , this binomial tends to the Poisson formula. So:
If is large and is small, Binomial() is close to Poisson().
Worked example 3.3
A robot cell averages 2 faults per 8-hour shift.
- .
- .
- .
Worked example 3.4: scaling the interval
The same cell, over half a shift: the rate scales to . .
Worked example 3.5: approximation
A batch of 1,000 parts, each defective with probability 0.002. ?
- Exact binomial: .
- Poisson with : .
Almost identical, and the Poisson is much easier to compute for larger counts.
Requests arrive at 3 per second on average. Find and .
Answer. . .
3.7 Categorical: one of several labels
takes one of values (categories) with probabilities , where .
A loaded die is categorical. So is the output of a classifier’s softmax layer, and the distribution a discrete-action robot policy samples its next action from.
Repeating a categorical trial times and counting how many land in each category gives the multinomial distribution, the many-category version of the binomial.
3.8 Choosing a distribution
| Situation | Distribution |
|---|---|
| one trial, two outcomes | Bernoulli() |
| number of successes in a fixed number of independent trials | Binomial() |
| number of trials until the first success | Geometric() |
| number of events in a fixed interval at a constant rate | Poisson() |
| one of labels | Categorical |
| draws without replacement from a finite group | Hypergeometric (not binomial) |
Hypergeometric, briefly. Drawing 5 cards and counting hearts is not binomial, because the probability changes as hearts are removed. The exact formula is . When the population is much larger than the sample, the binomial is a good approximation.
Summary
| Distribution | PMF | Mean | Variance |
|---|---|---|---|
| Bernoulli() | |||
| Binomial() | |||
| Geometric() | |||
| Poisson() |
Three facts to remember:
- A random variable is a rule that turns outcomes into numbers.
- Binomial = count successes; geometric = wait for one; Poisson = count rare events in a window.
- Check independence and constant probability before trusting any of them.
Exercises
Part A: warm-up
A1. A random variable has , , , . Find , and .
A2. A fair coin is flipped 6 times. Find and .
A3. A die is rolled until a 1 appears. Find and the expected number of rolls.
A4. Calls arrive at a help desk at 4 per hour. Find and .
A1. . . .
A2. . .
A3. . Expected: 6.
A4. . For 15 minutes : .
Part B: core
B1. A grasping policy succeeds with probability 0.7. In 10 independent trials, find , and the expected number of successes.
B2. A sensor gives a false reading with probability 0.01 per measurement. In 500 measurements, use the Poisson approximation to find .
B3. You keep running trials until the first failure, with success probability 0.95 per trial. What is the expected number of trials until a failure? What is ?
B4. Two policies are each run 5 times. A succeeds 5 times, B succeeds 3 times. If both truly have success rate 0.8, how likely is a result at least as lopsided as “5 versus 3 or fewer”? (Compute .) What does that suggest about comparing policies with 5 trials?
B5. From a deck, 5 cards are dealt. Compute exactly (hypergeometric) and with the binomial approximation (). How far apart are they?
B1. . . Mean .
B2. . .
B3. Failure is the “success” of a geometric with : expected trials. .
B4. and (from example 3.1). Product . So almost 9% of the time, two identical policies produce a result that looks like a clear win for one of them. Five trials cannot reliably separate policies.
B5. Exact: . Binomial: . Close, because 5 is small compared with 52.
Part C: explain and prove
C1. Show that the binomial probabilities sum to 1, using the binomial theorem .
C2. Show that the geometric probabilities sum to 1, using the geometric series for .
C3. Explain in your own words why the number of heads in 10 flips of a coin whose bias was chosen at random (say, is 0.2 or 0.8 with equal chance) is not binomial.
C4. Prove that the Poisson probabilities sum to 1, using .
C1. .
C2. .
C3. Given the bias, flips are independent; but without knowing it, early flips tell you about the bias, which changes your prediction for later flips. The flips are dependent overall (conditionally independent given , as in chapter 2), so assumption 4 fails.
C4. .
Part D: practise with code
D1. Use scipy.stats.binom.pmf(k, n, p) to reproduce the table in example 3.1, and plot it as a bar chart.
D2. Simulate 10,000 evaluations of a 0.8-success policy with 5 trials each (rng.binomial(5, 0.8, 10000)). What fraction show 3 or fewer successes? Repeat with 50 trials each. How does the spread of the observed success rate change?
D3. Compare Binomial(1000, 0.003) with Poisson(3) for on one plot.
D4. Simulate the geometric distribution by repeatedly flipping a coin until success. Check that the average number of flips is close to 5, and that .
D2. With 5 trials, about 26% of runs show 3 or fewer. With 50 trials, the observed rate clusters much more tightly around 0.8 (spread shrinks like , chapter 8).
Where this is used
- Evaluation tables: success counts are binomial, which is what makes confidence intervals for them possible (PS 10 Confidence Intervals and Hypothesis Tests, 16 Evaluation and Uncertainty).
- Discrete action policies and classifiers output categorical distributions (05 Training Neural Policies).
- Reliability engineering: faults per hour (Poisson) and attempts until success (geometric).
Next: PS 04 Expectation and Variance: summarizing a distribution with two numbers.