Skip to main content

1  Probability spaces

Suppose a cloud service records whether each of four batch jobs completes before its deadline. Before the jobs run, we may want the probability that exactly three finish on time. The arithmetic will depend on more than the number four: we must say what the possible outcomes are, which outcomes count as success, and how probability is assigned to them. This chapter builds that language.

1.1 Experiments, outcomes and events

A random experiment is a specified procedure with an outcome that is not known in advance. The sample space \(\Omega\) is the set of possible outcomes. An event \(A\) is a subset of \(\Omega\) for which we can assign a probability.

For two time-stamped tasks, let \(S\) and \(F\) mean completion on time and failure to complete on time. One useful space is

\[ \Omega = \{SS,SF,FS,FF\}. \]

The event “at least one job finishes on time” is \(A=\{SS,SF,FS\}\). The distinction between \(SF\) and \(FS\) retains the identity of the task. If we instead model only the number of successes, the space becomes \(\{0,1,2\}\). A model on the second space cannot answer a question about the first task separately.

Sets let us combine statements:

Operation Meaning
\(A\cup B\) At least one event occurs
\(A\cap B\) Both events occur
\(A^c=\Omega\setminus A\) Event \(A\) does not occur
\(A\setminus B\) \(A\) occurs but \(B\) does not
\(A\subseteq B\) Every occurrence of \(A\) is an occurrence of \(B\)

Events are mutually exclusive (disjoint) if \(A\cap B=\varnothing\). They are exhaustive if their union is \(\Omega\). A collection of disjoint and exhaustive sets is a partition. Partitions will be central when we condition on different circumstances in the next chapter.

Rectangle representing the sample space with overlapping circles A and B, showing the intersection, distinct regions and complement.

Two overlapping events in a sample space. Each shaded region has a separate probability.

Disjoint is not synonymous with independent. If two events have positive probabilities and cannot happen together, knowing one happened changes the probability of the other to zero.

1.1.1 The event collection

For a finite or countably infinite sample space, it is usually sufficient to take the event collection \(\mathcal F\) to be all subsets, denoted \(2^\Omega\). For continuous experiments, such as the time until a component stops working, we use a collection closed under the operations we need. A sigma-algebra \(\mathcal F\) is a collection of subsets of \(\Omega\) such that:

  1. \(\Omega\in\mathcal F\).
  2. If \(A\in\mathcal F\), then \(A^c\in\mathcal F\).
  3. If \(A_1,A_2,\ldots\in\mathcal F\), then \(\bigcup_{i=1}^{\infty}A_i\in\mathcal F\).

This implies closure under countable intersections through De Morgan’s law, and inclusion of the empty set. In calculus-based continuous probability we work with the Borel sigma-algebra: the smallest sigma-algebra containing all open intervals (or open sets in \(\mathbb R^d\)). Every interval event used in this book is measurable.

The sigma-algebra requirement avoids pathological subsets of the real line that cannot be assigned probabilities consistently in the usual continuous models. You do not need to construct those subsets to use the theory.

1.2 The axioms of probability

A probability space is a triple \((\Omega,\mathcal F,\Pr)\) satisfying:

\[ \begin{aligned} \Pr(A)&\ge 0 &&\text{for all } A\in\mathcal F,\\ \Pr(\Omega)&=1,\\ \Pr\left(\bigcup_{i=1}^{\infty}A_i\right) &=\sum_{i=1}^{\infty}\Pr(A_i) &&\text{for pairwise disjoint } A_i\in\mathcal F. \end{aligned} \]

The third axiom is countable additivity. Finite additivity follows by including empty sets. To see that \(\Pr(\varnothing)=0\), apply the third axiom to \(A_1=\Omega\) and all remaining sets empty, or apply additivity to countably many copies of \(\varnothing\): a strictly positive mass would make the sum infinite, whereas probabilities are finite.

1.2.1 Complements and monotonicity

Since \(A\) and \(A^c\) form a disjoint partition of the space,

\[ 1=\Pr(A\cup A^c)=\Pr(A)+\Pr(A^c). \]

Thus \(\Pr(A^c)=1-\Pr(A)\). Nonnegativity then gives \(0\le\Pr(A)\le1\).

If \(A\subseteq B\), the sets \(A\) and \(B\setminus A\) partition \(B\). Consequently \(\Pr(B)=\Pr(A)+\Pr(B\setminus A)\ge\Pr(A)\). This is monotonicity; a more restrictive statement cannot be more probable.

1.2.2 Inclusion-exclusion and the union bound

An ordinary sum \(\Pr(A)+\Pr(B)\) double-counts the intersection. Decompose \(A\cup B\) into \(A\) and \(B\setminus A\), and use \(\Pr(B\setminus A)=\Pr(B)-\Pr(A\cap B)\). We obtain

\[ \Pr(A\cup B)=\Pr(A)+\Pr(B)-\Pr(A\cap B). \]

For three events repeat this identity:

\[ \begin{aligned} \Pr(A\cup B\cup C) ={}&\Pr(A)+\Pr(B)+\Pr(C)\\ &-\Pr(A\cap B)-\Pr(A\cap C)-\Pr(B\cap C)\\ &+\Pr(A\cap B\cap C). \end{aligned} \]

The last term restores the mass of outcomes present in all three events. For any finite or countable collection,

\[ \Pr\left(\bigcup_i A_i\right)\le \sum_i\Pr(A_i). \]

Proof. Define \(B_1=A_1\) and \(B_i=A_i\setminus\bigcup_{j<i} A_j\). The \(B_i\) are disjoint and have the same union as the \(A_i\). Because \(B_i\subseteq A_i\), additivity and monotonicity give the result. This inequality is useful even when events are dependent and exact intersections are difficult.

1.2.3 Worked example: two alert types

A service dashboard raises a latency alert \(L\) on 8% of days and a memory alert \(M\) on 5% of days. Both appear on 2% of days. The probability of at least one alert is \(0.08+0.05-0.02=0.11\). The probability of no alert is \(0.89\), and the probability of exactly one is \((0.08-0.02)+(0.05-0.02)=0.09\).

Notice that multiplying \(0.08\) by \(0.05\) would imply an intersection probability of \(0.004\), inconsistent with the observed model. The probability axioms constrain allowable combinations: for example, \(\Pr(L\cap M)\) cannot exceed \(0.05\).

1.3 Counting equally likely outcomes

If a finite sample space has \(N\) equally likely elementary outcomes, additivity forces each to have probability \(1/N\). Hence

\[ \Pr(A)=\frac{|A|}{|\Omega|}. \]

This is a derived rule, not a definition that can be applied to every experiment. An unfair spinner may have equally many sectors but different probability masses.

1.3.1 The multiplication principle

If stage one has \(n_1\) choices, and each of them permits exactly \(n_2\) choices at stage two, there are \(n_1 n_2\) ordered pairs. Applying the argument repeatedly gives \(\prod_{j=1}^k n_j\) complete sequences.

For \(k\) selections from \(n\) distinct items without replacement, ordering matters and the available counts decrease:

\[ P(n,k)=n(n-1)\cdots(n-k+1)=\frac{n!}{(n-k)!}. \]

Here \(n!=1\cdot2\cdots n\) and \(0!=1\). Each unordered set of \(k\) distinct items corresponds to exactly \(k!\) orderings. Dividing gives the binomial coefficient

\[ \binom nk=\frac{n!}{k!(n-k)!}. \]

The identity \(\binom nk=\binom n{n-k}\) follows by selecting either the included items or the excluded items. Summing over \(k\) counts every subset once, so \(\sum_{k=0}^{n}\binom nk=2^n\).

Experiment Count
Ordered, repetition allowed \(n^k\)
Ordered, no repetition \(n!/(n-k)!\)
Unordered, no repetition \(\binom nk\)
Unordered, repetition allowed \(\binom{n+k-1}{k}\)

The last expression counts nonnegative integer solutions \(x_1+\cdots+x_n=k\). Place \(k\) identical objects in a row with \(n-1\) separators; choosing the separator locations among \(n+k-1\) positions gives a one-to-one correspondence.

1.3.2 Worked example: selecting a review panel

A workshop has 7 software engineers and 5 statisticians. Four people are sampled uniformly without replacement for a review panel. There are \(\binom{12}{4}=495\) equally likely panels. To obtain exactly two people from each group, choose them separately:

\[ \Pr(\text{two from each}) =\frac{\binom72\binom52}{\binom{12}4} =\frac{210}{495}=\frac{14}{33}. \]

The formula counts unordered panels. It would also work to count ordered sequences, but the numerator and denominator must then use the same ordering convention.

1.3.3 Worked example: assigning unique identifiers

Suppose three labels are sampled independently and uniformly from an alphabet of nine labels. We are interested in at least one collision. The total is \(9^3\). Exactly \(9\cdot8\cdot7\) sequences are collision-free, so

\[ \Pr(\text{collision})=1-\frac{9\cdot8\cdot7}{9^3} =1-\frac{56}{81}=\frac{25}{81}. \]

The complement turns a complicated union of pairwise collision events into a manageable product.

1.4 Probability models and repeated experiments

A probability model may be defined by weights. For example, a small classifier returns one of three labels with probabilities \(0.15,0.35,0.50\). These masses are nonnegative and sum to one. The probability of a collection of labels is the sum of their masses.

The empirical frequency of an event in \(N\) repetitions is

\[ \widehat p_N = \frac{1}{N}\sum_{i=1}^{N} \mathbf{1}\{A_i\}, \]

where \(\mathbf{1}\) equals one if its statement holds and zero otherwise. If the experiments are independent and generated by the same probability model, the law of large numbers (Chapter 9) explains why this fraction tends to the true probability. A finite simulation does not prove an identity; it checks whether a numerical experiment is consistent with the mathematics.

1.4.1 Python experiment: exact calculation and simulation

The example below uses the review-panel model. The first sample method draws an entire panel uniformly; it does not draw four people with replacement. Repeating it lets us compare exact and approximate answers.

import math
import numpy as np

rng = np.random.default_rng(731)
people = np.array([1] * 7 + [0] * 5)
exact = math.comb(7, 2) * math.comb(5, 2) / math.comb(12, 4)
N = 30_000
hits = np.array([
    rng.choice(people, size=4, replace=False).sum() == 2
    for _ in range(N)
])
estimate = hits.mean()
se = np.sqrt(estimate * (1 - estimate) / N)
print(f"Exact: {exact:.6f}")
print(f"Simulation: {estimate:.6f} (MC standard error {se:.4f})")
assert abs(estimate - exact) < 5 * se

The estimated standard error measures Monte Carlo variability, not an error in the formula. With more repetitions, the discrepancy typically decreases proportionally to \(N^{-1/2}\).

1.5 Practice problems

Try each problem before revealing its solution. Work in your browser with Google Colab, or download the notebooks for Jupyter.

Practice notebook

Open in Colab Download .ipynb

Worked solutions

Open in Colab Download .ipynb

1.5.1 Exercise 1: Two workshop sessions

At an event, 65% attend a session on optimization, 48% attend a session on statistics, and 30% attend both. Find the probability of attending at least one, exactly one, and neither.

Let \(O\) and \(S\) be the events. Inclusion-exclusion gives \(\Pr(O\cup S)=0.65+0.48-0.30=0.83\). Exactly one has probability \(0.65+0.48-2(0.30)=0.53\). Neither has probability \(1-0.83=0.17\). The three categories both, exactly one, neither sum to \(0.30+0.53+0.17=1\).

1.5.2 Exercise 2: Bounds without intersections

For events \(A,B\) with \(\Pr(A)=0.7\) and \(\Pr(B)=0.55\), prove that \(0.25\le \Pr(A\cap B)\le0.55\). Show that both extremes are attainable with a uniform experiment on \([0,1]\).

The upper bound is monotonicity. The lower bound follows from \(1\ge\Pr(A\cup B)=1.25-\Pr(A\cap B)\). Take \(A=[0,0.7]\) and \(B=[0.45,1]\) for the minimum: their overlap has length \(0.25\). For the maximum take \(B=[0,0.55]\subset A\). Endpoints have zero probability in the uniform model.

1.5.3 Exercise 3: Ordering files

Seven different files must be placed in a queue. Three specified files must appear consecutively, in any internal order. How many valid queues are there, and what is the probability when all \(7!\) orders are equally likely?

Treat the three consecutive files as one block. The block and the four remaining files form five distinct objects with \(5!\) possible orders. The block has \(3!\) internal permutations. The count is \(5!3!=720\) and the probability is \(720/7!=1/7\).

1.5.4 Exercise 4: Python — validate a batch acceptance rule

A delivery contains 10 satisfactory and 3 damaged items. An inspector samples 5 without replacement; a batch is accepted if the sample contains at most one damaged item. Derive the exact probability, implement the formula with combinations, simulate 40,000 inspections, and print the Monte Carlo standard error. Explain why sampling with replacement changes the model.

The number of equally likely samples is \(\binom{13}{5}\). Zero damaged items gives \(\binom{10}{5}\) samples and one gives \(\binom31\binom{10}{4}\). Thus

\[ p=\frac{\binom{10}{5}+3\binom{10}{4}}{\binom{13}{5}}. \]

from math import comb
import numpy as np

rng = np.random.default_rng(738)
exact = (comb(10, 5) + comb(3, 1) * comb(10, 4)) / comb(13, 5)
items = np.r_[np.zeros(10, dtype=int), np.ones(3, dtype=int)]
N = 40_000
accepted = np.array([
    rng.choice(items, 5, replace=False).sum() <= 1 for _ in range(N)
])
p_hat = accepted.mean()
se = (p_hat * (1 - p_hat) / N) ** 0.5
print(f"Exact {exact:.5f}; simulation {p_hat:.5f}; SE {se:.5f}")
assert abs(p_hat - exact) < 5 * se

With replacement, a damaged item can be chosen more than once; the remaining proportions do not change after a draw. The count is then binomial rather than hypergeometric.

1.5.5 Exercise 5: Python — collision probability as the system grows

There are \(m=30\) possible labels. For \(k=1,\ldots,20\), calculate the exact probability that at least two of \(k\) independently sampled labels match. Simulate 15,000 sequences at each \(k\), then plot exact and simulated probabilities. Find the smallest \(k\) with collision probability at least one half.

The first label is unrestricted. Given no previous collision, label \(j+1\) has \((m-j)/m\) chance of differing from all previous labels. Therefore \(p_k=1-\prod_{j=0}^{k-1}(1-j/m)\).

import numpy as np
import matplotlib.pyplot as plt

rng = np.random.default_rng(59)
m, N = 30, 15_000
ks = np.arange(1, 21)
exact = np.array([1 - np.prod(1 - np.arange(k) / m) for k in ks])
sim = []
for k in ks:
    draws = np.sort(rng.integers(m, size=(N, k)), axis=1)
    sim.append(np.any(np.diff(draws, axis=1) == 0, axis=1).mean())
sim = np.array(sim)
threshold = ks[exact >= .5][0]
print("First crossing:", threshold)
assert threshold == 7
plt.plot(ks, exact, label="Exact")
plt.scatter(ks, sim, s=18, label="Simulation")
plt.axhline(.5, color="gray", linestyle="--")
plt.xlabel("Number of labels")
plt.ylabel("Collision probability")
plt.legend()
plt.show()

The crossing is \(k=7\). The number of possible matching pairs grows quadratically with \(k\), so a collision becomes likely well before all 30 labels have been used.