Lesson 4 of 16 · Chance
Independence
Independent events and the multiplication rule, why independent isn't mutually exclusive, why the gambler's fallacy is wrong, and what i.i.d. data means in ML.
- 8 min read
- Intermediate
- Reviewed
- Explain what it means for two events to be independent
- Use the multiplication rule P(A and B) = P(A) × P(B) for independent events
- Test events for independence by counting in Python
- Tell independent events apart from mutually exclusive ones
- Recognise the gambler's fallacy and the i.i.d. assumption in machine learning
What independence means
Flip a coin and roll a die. The coin can't influence the die, and the die can't influence the coin. Events like these are independent.
That formula is the multiplication rule for independent events. The chance of heads and a six is 1/2 × 1/6 = 1/12. It extends to any number of independent events: the chance of three heads in a row is 1/2 × 1/2 × 1/2 = 1/8.
Testing for independence
Independence isn't always obvious, but it can be checked: count P(A and B) and compare it with P(A) × P(B). Here are three pairs of two-dice events:
from itertools import product
from math import isclose
space = set(product(range(1, 7), repeat=2))
P = lambda event: len(event) / len(space)
def check(name, A, B):
both, product_of_ps = P(A & B), P(A) * P(B)
verdict = "independent" if isclose(both, product_of_ps) else "NOT independent"
print(f"{name}: P(A and B) = {both:.4f} P(A) x P(B) = {product_of_ps:.4f} {verdict}")
first_even = {o for o in space if o[0] % 2 == 0}
second_six = {o for o in space if o[1] == 6}
total_10_plus = {o for o in space if sum(o) >= 10}
doubles = {o for o in space if o[0] == o[1]}
total_7 = {o for o in space if sum(o) == 7}
first_six = {o for o in space if o[0] == 6}
check("first even, second is 6", first_even, second_six)
check("total 10+, doubles ", total_10_plus, doubles)
check("total is 7, first is 6 ", total_7, first_six)- First die even, second die 6: independent, as expected, because the two dice don't affect each other.
- Total 10 or more, doubles: not independent. Knowing you rolled doubles makes a high total more likely, since (5,5) and (6,6) are 2 of the 6 doubles.
- Total is 7, first die is 6: independent! Whatever the first die shows, exactly one value of the second die makes a total of 7, so the total being 7 has probability 1/6 no matter what the first die is.
The last one is a good reminder: even when events involve the same dice, only the numbers can tell you whether they're independent.
Independent is not mutually exclusive
These two ideas are easy to mix up, but they are almost opposites:
| Mutually exclusive | Independent | |
|---|---|---|
| Meaning | A and B can't happen together | A doesn't affect the chance of B |
| P(A and B) | 0 | P(A) × P(B) |
| Two-dice example | total is 2, total is 12 | first die even, second die 6 |
If two possible events are mutually exclusive, they are dependent: knowing that A happened tells you for certain that B didn't.
The gambler's fallacy
A fair coin lands heads five times in a row. Is tails now “due”? Let's flip a coin 200,000 times and look at every flip that came straight after five heads in a row:
import random
random.seed(2)
flips = [random.choice("HT") for _ in range(200_000)]
# Find every flip that comes straight after five heads in a row
after_streak = [flips[i] for i in range(5, len(flips)) if flips[i - 5:i] == ["H"] * 5]
print(f"times we saw five heads in a row: {len(after_streak)}")
print(f"share of heads on the very next flip: {after_streak.count('H') / len(after_streak):.3f}")Heads still comes up half the time. The coin has no memory: each flip is independent of the ones before. Believing that past results change the odds of an independent event is called the gambler's fallacy.
i.i.d.: the most common assumption in machine learning
Most machine learning methods assume the training examples are i.i.d.: each example is a fresh, independent draw from the same population that the model will later see. That assumption is what makes a randomly chosen test set a fair measure of performance.
Common mistakes
Use the multiplication rule to test whether drawing a red card and drawing a face card (J, Q or K) are independent events.
from itertools import product
from math import isclose
ranks = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]
suits = ["hearts", "diamonds", "clubs", "spades"]
deck = list(product(ranks, suits)) # 52 equally likely cards
def P(event):
return sum(1 for card in deck if event(card)) / len(deck)
red = lambda card: card[1] in ("hearts", "diamonds")
face = lambda card: card[0] in ("J", "Q", "K")
# Is "red" independent of "face card"?
# Print P(red and face) and P(red) * P(face), each rounded to 4 decimals,
# then on a new line print whether the two are equal (use isclose).
A fair coin has landed tails eight times in a row. What is the probability that the next flip is heads?
Summary
- Events are independent when one doesn't change the probability of the other.
- Multiplication rule: for independent events, P(A and B) = P(A) × P(B).
- Test independence by comparing P(A and B) with P(A) × P(B).
- Mutually exclusive possible events are always dependent.
- The gambler's fallacy wrongly expects independent events to balance out.
- Machine learning usually assumes i.i.d. data: independent draws from one distribution.
Next: conditional probability.
Finished reading? Tick it off to track your progress.