Velamuvi AIAcademy
Probability

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
In this lesson you will learn
  • 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:

Try it yourselfPython
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:

00.050.10.150.2P(X = 2) = 0.02782P(X = 3) = 0.05563P(X = 4) = 0.08334P(X = 5) = 0.11115P(X = 6) = 0.13896P(X = 7) = 0.16677P(X = 8) = 0.13898P(X = 9) = 0.11119P(X = 10) = 0.083310P(X = 11) = 0.055611P(X = 12) = 0.027812
mean = 7standard deviation = 2.415
Totals of two fair dice. 7 is the most likely total (6 of 36 outcomes); 2 and 12 are the least likely (1 of 36 each).

Combining events

Events are sets, so set operations build new events from old ones:

OperationMeaningPythonNotation
UnionA or B (or both)A | BA ∪ B
IntersectionA and BA & BA ∩ B
Complementnot AS - AAᶜ or A′
Try it yourselfPython
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 else

Look 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

Practice exercise

Build an event from a sample space and find its probability by counting equally likely outcomes.

Your solutionPython
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.
Knowledge check

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.product builds 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.