Lesson 2 of 16 · Chance
Events and Sample Spaces
The language of probability: experiments, outcomes, sample spaces and events. Count equally likely outcomes, chart two dice and combine events with Python sets.
- 6 min read
- Intermediate
- Reviewed
- Define experiments, outcomes, sample spaces and events
- Calculate probabilities by counting equally likely outcomes
- Build sample spaces in Python with itertools.product
- Combine events with union, intersection and complement
The words of probability
Before calculating, we need precise words for what we're calculating about.
When every outcome is equally likely, probability is just counting:
P(event) = (number of outcomes in the event) / (number of outcomes in the sample space)For one die, P(even) = 3/6 = 0.5.
Two dice: a bigger sample space
Roll two dice. Each die has 6 faces, so there are 6 × 6 = 36 equally likely (first, second) pairs. Python's itertools.product builds them all:
from itertools import product
# Sample space for rolling two dice: every (first, second) pair, all equally likely
space = list(product(range(1, 7), repeat=2))
print("Outcomes in the sample space:", len(space))
# Events are subsets of the sample space
total_is_7 = [o for o in space if sum(o) == 7]
doubles = [o for o in space if o[0] == o[1]]
print("Total is 7:", total_is_7)
print(f"P(total is 7) = {len(total_is_7)}/36 = {len(total_is_7) / 36:.3f}")
print(f"P(doubles) = {len(doubles)}/36 = {len(doubles) / 36:.3f}")A total of 7 can happen six ways, so it's the most common total. A total of 2 can only happen one way, (1, 1). Here are all the totals at once. Hover over a bar to see its exact probability:
Combining events
Events are sets, so set operations build new events from old ones:
| Operation | Meaning | Python | Notation |
|---|---|---|---|
| Union | A or B (or both) | A | B | A ∪ B |
| Intersection | A and B | A & B | A ∩ B |
| Complement | not A | S - A | Aᶜ or A′ |
from itertools import product
space = set(product(range(1, 7), repeat=2))
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
def P(event):
return len(event) / len(space) # equally likely outcomes
print(f"P(A) = {P(A):.3f}")
print(f"P(B) = {P(B):.3f}")
print(f"P(A or B) = {P(A | B):.3f}") # union: in A, in B, or in both
print(f"P(A and B) = {P(A & B):.3f}") # intersection: in both
print(f"P(not A) = {P(space - A):.3f}") # complement: everything elseLook at the numbers: P(A or B) = 0.278 is not P(A) + P(B) = 0.333. The outcomes in both A and B, here (5,5) and (6,6), were counted twice. The next lesson turns that into a rule.
Common mistakes
Build an event from a sample space and find its probability by counting equally likely outcomes.
from itertools import product
space = list(product(["H", "T"], repeat=3)) # three coin flips, e.g. ('H', 'T', 'H')
# Count the outcomes with exactly two heads, then print the probability as a fraction
# of the sample space, rounded to 3 decimals.
You roll two dice. Why is a total of 7 more likely than a total of 12?
Summary
- An experiment has a sample space of possible outcomes; an event is a set of outcomes.
- With equally likely outcomes, P(event) = outcomes in the event ÷ outcomes in the sample space.
itertools.productbuilds sample spaces such as all pairs of two dice.- Combine events with union (or), intersection (and) and complement (not).
- Mutually exclusive events can't happen together.
Next: the rules of probability.
Finished reading? Tick it off to track your progress.