Lesson 3 of 16 · Chance
Rules of Probability
The basic rules of probability: values from 0 to 1, the complement rule and the addition rule, each checked in Python, and the famous birthday problem solved.
- 7 min read
- Intermediate
- Reviewed
- State the three basic rules every probability follows
- Use the complement rule to find P(not A)
- Use the addition rule for P(A or B), including overlapping events
- Solve “at least one” questions, such as the birthday problem, with the complement trick
Three rules everything is built on
Every probability you will ever calculate obeys three simple rules. You met them briefly in lesson 1; here they are in plain words:
- Every probability is between 0 and 1. 0 means impossible, 1 means certain. A probability of 1.2 or −0.1 is always a mistake.
- Something in the sample space happens. P(S) = 1, so the probabilities of all the outcomes add up to exactly 1.
- Mutually exclusive events add. If A and B can't both happen, P(A or B) = P(A) + P(B).
Two very useful rules follow from these three.
The complement rule
An event either happens or it doesn't, and those two cases are mutually exclusive and together cover everything. So P(A) + P(not A) = 1, which gives:
If there's a 0.3 chance of rain, there's a 0.7 chance of no rain. That sounds obvious, but it is one of the most powerful tricks in probability: when an event is hard to count, its complement is often easy.
The addition rule
Lesson 2 showed that adding P(A) and P(B) double counts the outcomes in both events. Subtracting the overlap once fixes it:
Let's check both rules against plain counting with two dice. A is “the total is 10 or more” and B is “doubles”:
from itertools import product
space = set(product(range(1, 7), repeat=2))
P = lambda event: len(event) / len(space)
A = {o for o in space if sum(o) >= 10} # total of 10 or more
B = {o for o in space if o[0] == o[1]} # doubles
# Addition rule: P(A or B) = P(A) + P(B) - P(A and B)
print(f"by counting: P(A or B) = {P(A | B):.4f}")
print(f"by addition rule: P(A or B) = {P(A) + P(B) - P(A & B):.4f}")
# Complement rule: P(not A) = 1 - P(A)
print(f"P(not A) = {P(space - A):.4f} = 1 - {P(A):.4f}")Counting and the rule agree. The overlap, A and B, is the outcomes (5,5) and (6,6): doubles that also total 10 or more.
“At least one”: the complement trick
Questions with “at least one” are usually hard directly and easy through the complement, because:
P(at least one) = 1 − P(none)“None” is a single, simple case, while “at least one” splits into many cases (exactly one, exactly two, …).
A famous example is the birthday problem: in a room of 23 people, how likely is it that at least two share a birthday? Most people guess around 5%. Let's calculate it, ignoring leap years and assuming every birthday is equally likely:
import random
# The birthday problem: in a group of n people, how likely is it that two share a birthday?
def p_shared(n):
p_all_different = 1.0
for i in range(n):
p_all_different *= (365 - i) / 365 # each new person avoids the birthdays taken so far
return 1 - p_all_different # complement rule
for n in (10, 23, 50):
print(f"{n} people: P(shared birthday) = {p_shared(n):.3f}")
# Check n = 23 by simulation
random.seed(1)
trials = 20_000
hits = sum(len(set(random.randint(1, 365) for _ in range(23))) < 23 for _ in range(trials))
print(f"simulated, 23 people: {hits / trials:.3f}")With just 23 people the chance is over 50%, and with 50 people it is 97%. The simulation agrees with the formula.
How does p_shared work? Instead of counting all the ways birthdays could match, it calculates the complement, “everyone's birthday is different”, one person at a time. The first person can have any birthday (365/365), the second must avoid one taken day (364/365), the third must avoid two (363/365), and so on. Multiplying the chances step by step gives P(all different), and the complement rule turns that into P(at least one shared).
Multiplying probabilities like this is the multiplication rule. The next two lessons explain exactly when it works: for independent events (lesson 4), P(A and B) = P(A) × P(B).
Common mistakes
Five independent servers each fail tonight with probability 0.03. Use the complement rule to find the chance that at least one of them fails.
p_fail = 0.03 # each server fails tonight with probability 0.03, independently servers = 5 # Use the complement rule: P(at least one fails) = 1 - P(none fail). # Print P(at least one fails), rounded to 4 decimals.
P(A) = 0.5, P(B) = 0.4 and P(A and B) = 0.2. What is P(A or B)?
Summary
- Every probability is between 0 and 1, and the probabilities of all outcomes add up to 1.
- Complement rule: P(not A) = 1 − P(A).
- Addition rule: P(A or B) = P(A) + P(B) − P(A and B).
- For “at least one” questions, calculate 1 − P(none).
- In the birthday problem, 23 people are enough for a better than even chance of a shared birthday.
Next: independence.
Finished reading? Tick it off to track your progress.