TExcelTAD
Home › Math for Machine Learning › Probability Spaces and Random Variables

Probability Spaces and Random Variables

ML is about uncertainty: noisy labels, unknown parameters, predictions with confidence. Probability theory gives the vocabulary. This lesson sets up the three ingredients of a probability space and the idea that makes them usable in code, the random variable. For a full course see Probability Theory.

The probability space

A probability space has three parts:

Part Symbol Example: two dice
sample space, all outcomes Ω\Omega the 36 pairs (i,j)(i, j)
event space, the sets of outcomes we can ask about A\mathcal{A} "the sum is 7", "the first die is even", ...
probability measure PP P(A)=∣A∣/36P(A) = \lvert A \rvert / 36 for fair dice

PP must satisfy P(A)≥0P(A) \ge 0, P(Ω)=1P(\Omega) = 1, and additivity: for disjoint events, P(A∪B)=P(A)+P(B)P(A \cup B) = P(A) + P(B). Everything else follows, for example P(Ac)=1−P(A)P(A^c) = 1 - P(A) and P(A∪B)=P(A)+P(B)−P(A∩B)P(A \cup B) = P(A) + P(B) - P(A \cap B).

Random variables

We rarely care about the raw outcome; we care about a number computed from it. A random variable is a function X:Ω→TX: \Omega \to \mathcal{T} from outcomes to a target space (usually R\mathbb{R} or RD\mathbb{R}^D). For two dice, X(i,j)=i+jX(i, j) = i + j. The probability of a value is the probability of all outcomes that map to it:

PX(S)=P(X∈S)=P({ω∈Ω:X(ω)∈S})P_X(S) = P(X \in S) = P(\{\omega \in \Omega : X(\omega) \in S\})

PXP_X is the distribution of XX. In ML we usually work with distributions directly and forget the underlying Ω\Omega.

python · try it yourself
import numpy as np
from fractions import Fraction
from collections import Counter

omega = [(i, j) for i in range(1, 7) for j in range(1, 7)]
X = Counter(i + j for i, j in omega)               # push the uniform measure through X
dist = {s: Fraction(c, 36) for s, c in sorted(X.items())}
print("P(X = s):", {s: str(p) for s, p in dist.items()})
print("total:", sum(dist.values()))
# simulation agrees with the exact distribution
rng = np.random.default_rng(0)
rolls = rng.integers(1, 7, size=(200000, 2)).sum(axis=1)
print("P(X = 7) exact:", float(dist[7]), " simulated:", round(float(np.mean(rolls == 7)), 4))
P(X = s): {2: '1/36', 3: '1/18', 4: '1/12', 5: '1/9', 6: '5/36', 7: '1/6', 8: '5/36', 9: '1/9', 10: '1/12', 11: '1/18', 12: '1/36'}
total: 1
P(X = 7) exact: 0.16666666666666666  simulated: 0.1678

Two interpretations

Frequentist: P(A)P(A) is the long-run fraction of trials in which AA happens (as in the simulation). Bayesian: P(A)P(A) is a degree of belief, which also makes sense for one-off questions like "what is the probability that this model's weight is positive?" ML uses both: frequentist ideas for evaluation (test error), Bayesian ideas for uncertainty in parameters (Bayesian Linear Regression).

Discrete and continuous

If XX takes countably many values, its distribution is a probability mass function (pmf). If it takes values in a continuum, single values have probability 0 and we use a density: next lesson, Discrete and Continuous Distributions.

In software

  • A classifier's softmax output is a pmf over the classes; predict_proba in scikit-learn returns it.
  • np.random.default_rng simulates random variables; Monte Carlo estimates of probabilities are averages of indicators, as above.
  • scipy.stats objects bundle a distribution's pmf or pdf, cdf, mean and sampler.

Exercises

Exercise 1: Complement

Two fair dice are rolled. What is the probability that at least one shows a 6?

Answer

1136\frac{11}{36}.

Solution

The complement "no 6" has 5⋅5=255 \cdot 5 = 25 outcomes, so P=1−2536=1136P = 1 - \frac{25}{36} = \frac{11}{36}.

Exercise 2: Inclusion-exclusion

P(A)=0.5P(A) = 0.5, P(B)=0.4P(B) = 0.4, P(A∩B)=0.2P(A \cap B) = 0.2. Find P(A∪B)P(A \cup B).

Answer

0.7.

Solution

P(A∪B)=0.5+0.4−0.2=0.7P(A \cup B) = 0.5 + 0.4 - 0.2 = 0.7. Without subtracting the intersection, the outcomes in both events would be counted twice.

Exercise 3: Distribution of a maximum (code)

Let MM be the larger of two fair dice. Compute P(M=m)P(M = m) exactly for m=1,…,6m = 1, \ldots, 6.

Answer

P(M=m)=2m−136P(M = m) = \frac{2m - 1}{36}: 136,336,…,1136\frac{1}{36}, \frac{3}{36}, \ldots, \frac{11}{36}.

Solution

P(M≤m)=(m/6)2P(M \le m) = (m/6)^2, so P(M=m)=m2−(m−1)236=2m−136P(M = m) = \frac{m^2 - (m - 1)^2}{36} = \frac{2m - 1}{36}.

python · try it yourself
from collections import Counter
c = Counter(max(i, j) for i in range(1, 7) for j in range(1, 7))
print([f"{c[m]}/36" for m in range(1, 7)])
['1/36', '3/36', '5/36', '7/36', '9/36', '11/36']

Test yourself

A random variable is…

  1. a variable whose value is chosen by the programmer
  2. a function from outcomes to numbers (or vectors)
  3. a probability
  4. a type of event

Which is NOT an axiom of probability?

  1. P(A)≥0P(A) \ge 0
  2. P(Ω)=1P(\Omega) = 1
  3. P(A∪B)=P(A)+P(B)P(A \cup B) = P(A) + P(B) for disjoint A,BA, B
  4. P(A∩B)=P(A)P(B)P(A \cap B) = P(A)P(B) for all A,BA, B

The output of a softmax classifier for one input is best described as…

  1. a density
  2. a probability mass function over the classes
  3. a sample space
  4. a random seed