TExcelTAD
Home › Discrete Mathematics › Events and Probability Spaces

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 SS is the set of all possible outcomes of an experiment. An event is a subset E⊆SE \subseteq S. A probability function assigns to each outcome ω\omega a number Pr⁡[ω]≥0\Pr[\omega] \ge 0 with ∑ω∈SPr⁡[ω]=1\sum_{\omega \in S} \Pr[\omega] = 1, and then

Pr⁡[E]=∑ω∈EPr⁡[ω]\Pr[E] = \sum_{\omega \in E} \Pr[\omega]

When all ∣S∣\lvert S \rvert outcomes are equally likely (a uniform space), Pr⁡[E]=∣E∣∣S∣\Pr[E] = \frac{\lvert E \rvert}{\lvert S \rvert}: probability becomes counting.

Rules that follow from the definition: Pr⁡[E‾]=1−Pr⁡[E]\Pr[\overline{E}] = 1 - \Pr[E]; Pr⁡[A∪B]=Pr⁡[A]+Pr⁡[B]−Pr⁡[A∩B]\Pr[A \cup B] = \Pr[A] + \Pr[B] - \Pr[A \cap B]; the union bound Pr⁡[A1∪⋯∪An]≤∑Pr⁡[Ai]\Pr[A_1 \cup \cdots \cup A_n] \le \sum \Pr[A_i].

The four-step method

Intuition about probability is notoriously unreliable. A safe procedure:

  1. Find the sample space: list the outcomes, often as the leaves of a tree diagram.
  2. Define the event of interest as a set of outcomes.
  3. Assign outcome probabilities: multiply the probabilities along each path of the tree.
  4. 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, 13\frac13 each), your pick (3 ways, 13\frac13 each), the host's door (forced, or 2 choices of 12\frac12 when you picked the prize). Switching wins exactly when your first pick was wrong, which has probability 23\frac{2}{3}.

python · try it yourself
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 0+1+2=30 + 1 + 2 = 3.

Infinite sample spaces

Flip a fair coin until the first head. The outcomes H,TH,TTH,…H, TH, TTH, \ldots have probabilities 12,14,18,…\frac12, \frac14, \frac18, \ldots, which sum to 1. The probability that the first head comes on an even flip is 14+116+⋯=1/41−1/4=13\frac14 + \frac1{16} + \cdots = \frac{1/4}{1 - 1/4} = \frac13.

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 TT steps" or "two keys collide". The union bound is the workhorse: if each of nn bad events has probability at most 1n2\frac{1}{n^2}, the chance that any of them happens is at most 1n\frac{1}{n}.

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

536\frac{5}{36}; 1136\frac{11}{36}.

Solution

36 equally likely outcomes. Sum 8: (2,6),(3,5),(4,4),(5,3),(6,2)(2,6), (3,5), (4,4), (5,3), (6,2). At least one 6: complement of "no 6", 1−2536=11361 - \frac{25}{36} = \frac{11}{36}.

Exercise 2: Union bound

A program makes 1000 independent network calls, each failing with probability 10−510^{-5}. Bound the probability that at least one fails.

Answer

At most 10−210^{-2} (exactly 1−(1−10−5)1000≈0.009951 - (1 - 10^{-5})^{1000} \approx 0.00995).

Solution

Union bound: 1000⋅10−5=0.011000 \cdot 10^{-5} = 0.01. The exact value uses independence: 1−0.999991000≈0.009951 - 0.99999^{1000} \approx 0.00995, 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

25\frac{2}{5}.

Solution

Same colour: 35⋅24+25⋅14=6+220=25\frac{3}{5} \cdot \frac{2}{4} + \frac{2}{5} \cdot \frac{1}{4} = \frac{6 + 2}{20} = \frac{2}{5}.

python · try it yourself
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, Pr⁡[E]\Pr[E] equals…

  1. ∣E∣\lvert E \rvert
  2. ∣E∣/∣S∣\lvert E \rvert / \lvert S \rvert
  3. 1/∣E∣1 / \lvert E \rvert
  4. ∣S∣/∣E∣\lvert S \rvert / \lvert E \rvert

In the Monty Hall problem, the probability of winning by switching is…

  1. 13\frac13
  2. 12\frac12
  3. 23\frac23
  4. 1

The union bound states that Pr⁡[A1∪⋯∪An]\Pr[A_1 \cup \cdots \cup A_n] is…

  1. equal to ∑Pr⁡[Ai]\sum \Pr[A_i]
  2. at most ∑Pr⁡[Ai]\sum \Pr[A_i]
  3. at least ∑Pr⁡[Ai]\sum \Pr[A_i]
  4. the product of Pr⁡[Ai]\Pr[A_i]