ML Interview Q Series: Defining the Sample Space and All Events for a Two-Coin Toss
Browse all the Probability Interview Questions here.
Describe the sample space and all 16 events for a trial in which two coins are thrown and each shows either a head or a tail.
Short Compact solution
The sample space can be written as S = {hh, ht, th, tt}. Since there are 4 outcomes in S, the collection of all possible events is the power set of S, which contains 2^4 = 16 subsets. Specifically, the 16 events (subsets) are: ∅, {hh}, {ht}, {th}, {tt}, {hh, ht}, {hh, th}, {hh, tt}, {ht, th}, {ht, tt}, {th, tt}, {hh, ht, th}, {hh, ht, tt}, {hh, th, tt}, {ht, th, tt}, and {hh, ht, th, tt}.
Comprehensive Explanation
An outcome in this experiment is determined by the combined results of tossing two coins, where each coin shows either head (h) or tail (t). Because each coin has 2 possible outcomes, the total number of possible outcomes (the size of the sample space) is 2 * 2 = 4. Denoting heads by h and tails by t, we list the sample space as:
hh (first coin heads, second coin heads)
ht (first coin heads, second coin tails)
th (first coin tails, second coin heads)
tt (first coin tails, second coin tails)
Below is a common way to represent the sample space with a set notation:
Here, |Ω| = 4. Each event is simply any subset of Ω. Because there are 4 elements in Ω, the power set (i.e., the set of all subsets) has size 2^4 = 16:
These 16 subsets are all the possible events, ranging from the empty set ∅ (which means the event “nothing happens,” or “no outcome occurs,” used often in probability theory as an impossible event) to the entire set Ω itself (the “certain event” that must always happen).
Events can represent any combination of outcomes. For instance, the event “getting at least one head” corresponds to {hh, ht, th}, and so on. In probability terms (if we assume each coin is fair and the tosses are independent), each of the 4 outcomes in Ω has an equal probability of 1/4, and any event’s probability is the sum of the probabilities of all outcomes it contains.
Potential Follow-Up Questions
If we add a third coin, how large does the sample space become?
When you have three coins, each can land in two ways (head or tail). So the total number of outcomes in the sample space for three coins is 2 * 2 * 2 = 8. More generally, if you have n coins, there are 2^n possible outcomes in the sample space.
If the coins are biased, does that change the sample space?
The sample space itself (i.e., the set of outcomes) does not change if the coins are biased. You still list all possible outcomes in exactly the same way. What changes is the probability measure over these outcomes. Instead of each outcome being equally likely with probability 1/4 in the two-coin case, you would assign probabilities according to the bias of each coin (e.g., p for heads, 1 - p for tails for each coin), and then combine them accordingly.
Are the outcomes always equally likely in the real world?
In many theoretical probability problems, we assume each coin is a fair coin. That makes all outcomes equally likely. However, in real scenarios, coins may be slightly biased, or the flipping process might introduce other biases. The sample space remains the same, but the probabilities associated with each outcome might differ.
Suppose we define a random variable that counts the number of heads. How do we find its distribution?
Let X be the random variable denoting the number of heads in the outcome. For two coins:
X can be 0 (if outcome is tt).
X can be 1 (if outcome is ht or th).
X can be 2 (if outcome is hh).
If the coins are fair, the probability distribution is:
P(X=0) = 1/4,
P(X=1) = 2/4,
P(X=2) = 1/4.
In a biased scenario, if the probability of heads is p (and tails is 1 - p), then:
P(X=0) = (1 - p)^2,
P(X=1) = 2 * p * (1 - p),
P(X=2) = p^2.
How might this be implemented in Python?
Below is a simple Python snippet demonstrating how to enumerate the sample space of two coins and then list out all events (subsets). While in practice you might not need to generate all subsets for a small problem, this demonstration can be useful for larger or more complex scenarios:
import itertools
# Sample space for two coins
coins = ['h', 't']
sample_space = list(itertools.product(coins, repeat=2))
print("Sample Space (all possible outcomes):")
print(sample_space) # [('h','h'), ('h','t'), ('t','h'), ('t','t')]
# Generate all events (subsets of the sample space)
all_events = []
for r in range(len(sample_space) + 1):
# Use itertools.combinations to find subsets of size r
for subset in itertools.combinations(sample_space, r):
all_events.append(set(subset))
print("\nAll Events (each event is a subset of the sample space):")
for event in all_events:
print(event)
In more advanced probability or machine learning topics, these foundational concepts still apply when reasoning about state spaces, random variables, or training data distributions. Understanding sample spaces and event sets is crucial when formulating probability distributions, whether you are dealing with coin tosses or large-scale machine learning datasets.