Sample Spaces and Distribution Functions
To reason exactly instead of by simulation, we need a model. For experiments with finitely or countably many outcomes, the model has two parts: the set of outcomes and a weight for each one.
The model
- The sample space is the set of all possible outcomes of the experiment.
- A distribution function assigns each outcome a probability, with .
- An event is a subset , with probability .
- A random variable is a function that assigns a number to each outcome, such as "the sum of the dice".
When all outcomes have weight the distribution is uniform, and .
Example: the sum of two dice
The sample space is the 36 ordered pairs , each with weight . The random variable takes values 2 to 12, but not uniformly: there are more ways to make 7 than 2. Its distribution is found by grouping outcomes.
from fractions import Fraction
from itertools import product
from collections import Counter
omega = list(product(range(1, 7), repeat=2))
m = {w: Fraction(1, 36) for w in omega}
X = Counter()
for (a, b), p in m.items():
X[a + b] += p
for s in range(2, 13):
print(f"P(X={s:2d}) = {str(X[s]):5s} " + "#" * int(X[s] * 36))
print("total:", sum(X.values()))P(X= 2) = 1/36 #
P(X= 3) = 1/18 ##
P(X= 4) = 1/12 ###
P(X= 5) = 1/9 ####
P(X= 6) = 5/36 #####
P(X= 7) = 1/6 ######
P(X= 8) = 5/36 #####
P(X= 9) = 1/9 ####
P(X=10) = 1/12 ###
P(X=11) = 1/18 ##
P(X=12) = 1/36 #
total: 1
A common mistake is to take the sample space to be the 11 possible sums with equal weights. The model must reflect how the experiment actually works: the dice are distinguishable (even when they look alike), so the 36 ordered pairs are the equally likely outcomes.
Choosing the sample space
The same experiment can be described at different levels of detail. For "toss a coin 3 times, count heads", you can use the 8 sequences (uniform) or the 4 counts with weights . Both are correct; the first is easier to justify, the second is more compact. A good rule: start with outcomes that are equally likely by symmetry, then derive the distribution of what you care about.
Infinite discrete sample spaces
Toss a coin until the first head. The outcome is the number of tosses, , with . The weights sum to 1 as a geometric series:
import random
from collections import Counter
rng = random.Random(2)
def tosses_until_head():
k = 1
while rng.random() >= 0.5:
k += 1
return k
trials = 100_000
counts = Counter(tosses_until_head() for _ in range(trials))
for k in range(1, 7):
print(k, round(counts[k] / trials, 4), 0.5 ** k)
print("P(even number of tosses) ~", round(sum(c for k, c in counts.items() if k % 2 == 0) / trials, 4))1 0.5008 0.5
2 0.2485 0.25
3 0.1258 0.125
4 0.0634 0.0625
5 0.0305 0.03125
6 0.0152 0.015625
P(even number of tosses) ~ 0.3325
The exact probability of an even number of tosses is .
In software
Every probabilistic program has an implicit sample space: the seeds of its random generator. Probabilistic programming languages (PyMC, Stan, Pyro, NumPyro) let you write the model as code and compute distributions of random variables automatically, by exact enumeration for small discrete models and by sampling otherwise.
Exercises
Exercise 1: Three coins
Toss 3 fair coins. What is the probability of exactly 2 heads? Of at least 2?
Answer
and .
Solution
Of the 8 sequences, HHT, HTH, THH have exactly 2 heads; adding HHH gives 4 with at least 2, so .
Exercise 2: A loaded die
A die is loaded so that the probability of face is proportional to . Find and the probability of an even face.
Answer
; .
Solution
with , so . Even faces: .
Exercise 3: Max of two dice (code)
Find the distribution of for two dice and check that .
Answer
for .
Solution
, so .
from fractions import Fraction
from itertools import product
from collections import Counter
Y = Counter(max(a, b) for a, b in product(range(1, 7), repeat=2))
print([Fraction(Y[k], 36) for k in range(1, 7)])
print(all(Y[k] == 2 * k - 1 for k in range(1, 7)))[Fraction(1, 36), Fraction(1, 12), Fraction(5, 36), Fraction(7, 36), Fraction(1, 4), Fraction(11, 36)]
True
Test yourself
Which condition must a distribution function on a discrete sample space satisfy?
- and the weights sum to 1
- is increasing
What is for two fair dice?
- 1/11
- 1/36
- 1/18
- 1/6