Probability in several steps
Be able to draw tree diagrams and compute the probability of events in sequence.
Prerequisites
Intuition
When something happens in several steps you draw a tree diagram.
A bag with 3 red and 2 blue marbles. You draw two, without putting them back.
┌─ red 2/4 ──→ RR: 3/5 · 2/4 = 6/20
red 3/5 ─┤
└─ blue 2/4 ─→ RB: 3/5 · 2/4 = 6/20
start ──┤
┌─ red 3/4 ──→ BR: 2/5 · 3/4 = 6/20
blue 2/5 ─┤
└─ blue 1/4 ─→ BB: 2/5 · 1/4 = 2/20
Two rules, and they are all you need:
| Rule | When |
|---|---|
| Multiply along a branch | the events happen one after the other («and») |
| Add between branches | several different routes give the same thing («or») |
A check: all the final results should add up to 1. Here: 6/20 + 6/20 + 6/20 + 2/20 = 20/20 = 1. ✓
That check takes five seconds and catches nearly every arithmetic slip.
Formal
With or without replacement is the decisive difference:
| With replacement | Without replacement | |
|---|---|---|
| The denominator | unchanged every time | decreases by 1 |
| The events are | independent | dependent |
| Example | dice throws, coin throws | drawing marbles, dealing cards |
In the example above the denominator changed from 5 to 4 — that is what «without replacement» means in practice.
A conditional probability is written and means «the probability of B, given that A has already happened». The multiplication rule then becomes:
If the events are independent then , and the rule simplifies to an ordinary product.
The complement rule saves a lot of work:
An example: you throw three dice. The probability of at least one six?
Computing it directly requires summing «exactly one», «exactly two» and «exactly three». Computing the opposite requires one step:
The rule to remember: when the question contains «at least one» — compute «none» and subtract from 1.
The most common source of error is forgetting that the denominator changes without replacement. The second most common is adding when you should multiply. The tree diagram protects against both, since you see the steps in front of you.
Code
from fractions import Fraction as F
import random
# Without replacement: the denominator decreases
red, blue = 3, 2
routes = {
"RR": F(red, 5) * F(red - 1, 4),
"RB": F(red, 5) * F(blue, 4),
"BR": F(blue, 5) * F(red, 4),
"BB": F(blue, 5) * F(blue - 1, 4),
}
for name, p in routes.items():
print(f" {name}: {p} = {float(p):.3f}")
print("the sum:", sum(routes.values())) # 1 ← the check
print("at least one red:", 1 - routes["BB"], "=", float(1 - routes["BB"])) # 9/10 = 0.9
print("exactly one red:", routes["RB"] + routes["BR"]) # 3/5
# With replacement: the denominator is unchanged
with_repl = {"RR": F(3, 5) ** 2, "RB": F(3, 5) * F(2, 5),
"BR": F(2, 5) * F(3, 5), "BB": F(2, 5) ** 2}
print("with replacement, both red:", with_repl["RR"], "against without:", routes["RR"])
# 9/25 = 0.36 against 3/10 = 0.30
# The complement rule: at least one six on three dice
print("at least one six:", 1 - F(5, 6) ** 3, "=", float(1 - F(5, 6) ** 3)) # 91/216 = 0.421
# Simulate and check
def simulate(n=200_000, seed=0):
rng = random.Random(seed)
hits = 0
for _ in range(n):
bag = ["R"] * 3 + ["B"] * 2
rng.shuffle(bag)
if bag[0] == "R" and bag[1] == "R":
hits += 1
return hits / n
print(f"simulated: {simulate():.4f} theoretical: {float(routes['RR']):.4f}")
# simulated: 0.3001 theoretical: 0.3000
The simulation at the bottom is worth running: when a tree diagram feels uncertain you can always check it by letting the computer draw marbles two hundred thousand times.
Mastery means
- Draws a tree diagram
- Multiplies along branches and adds between them
- Tells sampling with and without replacement apart
Sign in to do the exercises and build your mastery up.
Sources
- Matteboken (Mattecentrum) — free to read, non-profit association
- Khan Academy — matematik — CC BY-NC-SA 3.0
- Statistics Sweden — myndighetsmaterial