Events and Probability Spaces
Probability in computer science is mostly discrete: coin flips, dice, random hash values, random pivots, packet losses. This part of the course gives the discrete foundations and the tools algorithm designers use most. The Probability Theory course goes much further (continuous distributions, limit theorems, Markov chains).
Sample spaces and events
A sample space is the set of all possible outcomes of an experiment. An event is a subset . A probability function assigns to each outcome a number with , and then
When all outcomes are equally likely (a uniform space), : probability becomes counting.
Rules that follow from the definition: ; ; the union bound .
The four-step method
Intuition about probability is notoriously unreliable. A safe procedure:
- Find the sample space: list the outcomes, often as the leaves of a tree diagram.
- Define the event of interest as a set of outcomes.
- Assign outcome probabilities: multiply the probabilities along each path of the tree.
- Compute the event probability by adding the outcomes in the event.
The Monty Hall problem
A prize is behind one of three doors. You pick a door; the host, who knows where the prize is, opens another door with no prize and offers you a switch. Should you switch?
Tree: the prize location (3 ways, each), your pick (3 ways, each), the host's door (forced, or 2 choices of when you picked the prize). Switching wins exactly when your first pick was wrong, which has probability .
from fractions import Fraction
from itertools import product
# exact: enumerate the tree
p_switch_wins = Fraction(0)
for prize, pick in product(range(3), range(3)):
p = Fraction(1, 9)
hosts = [d for d in range(3) if d != pick and d != prize]
for host in hosts:
final = next(d for d in range(3) if d not in (pick, host))
if final == prize:
p_switch_wins += p / len(hosts)
print("P(win by switching) =", p_switch_wins)
# simulation
import random
rng = random.Random(0)
trials, wins = 100000, 0
for _ in range(trials):
prize, pick = rng.randrange(3), rng.randrange(3)
host = rng.choice([d for d in range(3) if d not in (pick, prize)])
wins += (3 - pick - host) == prize
print("simulated:", wins / trials)P(win by switching) = 2/3
simulated: 0.66549
3 - pick - host is the remaining door, since the three doors .
Infinite sample spaces
Flip a fair coin until the first head. The outcomes have probabilities , which sum to 1. The probability that the first head comes on an even flip is .
In software
Randomized algorithms are analysed in exactly this framework: the sample space is the set of all random choices (pivot positions, hash functions), and an event is "the algorithm takes more than steps" or "two keys collide". The union bound is the workhorse: if each of bad events has probability at most , the chance that any of them happens is at most .
Exercises
Exercise 1: Two dice
Two fair dice are rolled. What is the probability that the sum is 8? That at least one die shows 6?
Answer
; .
Solution
36 equally likely outcomes. Sum 8: . At least one 6: complement of "no 6", .
Exercise 2: Union bound
A program makes 1000 independent network calls, each failing with probability . Bound the probability that at least one fails.
Answer
At most (exactly ).
Solution
Union bound: . The exact value uses independence: , very close to the bound because failures are rare.
Exercise 3: Four-step method (code)
A bag has 3 red and 2 blue balls. Draw two without replacement. Compute the probability that they have the same colour exactly by enumerating ordered draws.
Answer
.
Solution
Same colour: .
from fractions import Fraction
from itertools import permutations
balls = ["R", "R", "R", "B", "B"]
draws = list(permutations(range(5), 2))
same = sum(1 for i, j in draws if balls[i] == balls[j])
print(Fraction(same, len(draws)))2/5
Test yourself
In a uniform sample space, equals…
In the Monty Hall problem, the probability of winning by switching is…
- 1
The union bound states that is…
- equal to
- at most
- at least
- the product of