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 | the 36 pairs | |
| event space, the sets of outcomes we can ask about | "the sum is 7", "the first die is even", ... | |
| probability measure | for fair dice |
must satisfy , , and additivity: for disjoint events, . Everything else follows, for example and .
Random variables
We rarely care about the raw outcome; we care about a number computed from it. A random variable is a function from outcomes to a target space (usually or ). For two dice, . The probability of a value is the probability of all outcomes that map to it:
is the distribution of . In ML we usually work with distributions directly and forget the underlying .
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: is the long-run fraction of trials in which happens (as in the simulation). Bayesian: 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 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_probain scikit-learn returns it. np.random.default_rngsimulates random variables; Monte Carlo estimates of probabilities are averages of indicators, as above.scipy.statsobjects 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
.
Solution
The complement "no 6" has outcomes, so .
Exercise 2: Inclusion-exclusion
, , . Find .
Answer
0.7.
Solution
. Without subtracting the intersection, the outcomes in both events would be counted twice.
Exercise 3: Distribution of a maximum (code)
Let be the larger of two fair dice. Compute exactly for .
Answer
: .
Solution
, so .
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…
- a variable whose value is chosen by the programmer
- a function from outcomes to numbers (or vectors)
- a probability
- a type of event
Which is NOT an axiom of probability?
- for disjoint
- for all
The output of a softmax classifier for one input is best described as…
- a density
- a probability mass function over the classes
- a sample space
- a random seed