Chapter 3: Discrete random variables

Previous: PS 02 Conditional Probability and Bayes Rule | PS 00 Course Map | Next: PS 04 Expectation and Variance

The big idea

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.

In this chapter you will learn to
  1. Define a random variable and describe it by its PMF and CDF.
  2. Recognize Bernoulli, binomial, geometric, Poisson and categorical situations.
  3. Derive the binomial formula from counting.
  4. Compute probabilities with each distribution.
  5. Choose the right model for a counting problem, and spot when the assumptions fail.
Before you start

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.

Definition: random variable

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
2110

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

Definition: PMF

The probability mass function of a discrete random variable is

It satisfies for all , and .

For the two coins:

012

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

Definition: CDF

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: .


3.3 Bernoulli: one yes/no trial

Bernoulli()

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.

Binomial()

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?

010.0003
150.0064
2100.0512
3100.2048
450.4096
510.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

Check all four before using the binomial
  1. A fixed number of trials .
  2. Each trial has two outcomes.
  3. The success probability is the same for every trial.
  4. 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.


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).

Geometric()

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

The geometric distribution has no memory

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.

Two conventions

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.

Poisson()

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:

Poisson approximation

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.


3.7 Categorical: one of several labels

Categorical()

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

SituationDistribution
one trial, two outcomesBernoulli()
number of successes in a fixed number of independent trialsBinomial()
number of trials until the first successGeometric()
number of events in a fixed interval at a constant ratePoisson()
one of labelsCategorical
draws without replacement from a finite groupHypergeometric (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

DistributionPMFMeanVariance
Bernoulli()
Binomial()
Geometric()
Poisson()

Three facts to remember:

  1. A random variable is a rule that turns outcomes into numbers.
  2. Binomial = count successes; geometric = wait for one; Poisson = count rare events in a window.
  3. 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 .

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?

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 .

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 .


Where this is used

Next: PS 04 Expectation and Variance: summarizing a distribution with two numbers.