4.2. Examples#
This section is a workout in finding probabilities of events determined by two jointly distributed random variables. We will apply the computational methods of the previous section and also the mathematical framework for finding probabilities that was developed there.
In some of the examples you may find yourself wondering why we are bothering to write out math notation for numerical answers that we have already obtained using Python. It is because the Python visualizations help us understand the math. That understanding then helps us answer questions in generality, not just in particular numerical settings, as you will see in the final example.
4.2.1. Fair and Loaded Dice#
I have two dice, one of which is fair. The other die is shaped so that it is biased towards larger numbers of spots. The distribution of the number of spots on one roll of this biased shape is given by
value |
1 |
2 |
3 |
4 |
5 |
6 |
---|---|---|---|---|---|---|
probability |
1/16 |
1/16 |
3/16 |
3/16 |
4/16 |
4/16 |
Suppose I roll each die once.
4.2.2. Equality#
Question: What is the chance that I get the same number on both dice?
Answer 1, by symmetry: No matter what number appears on the biased shape, the chance that the fair die shows that number is 1/6. So the answer is 1/6.
Not convinced? Then let’s calculate.
Answer 2: Let
We want
4.2.3. Difference#
Question: What is the chance that the number on the biased shape exceeds the number on the fair die by more than 2?
Answer: We can visualize the event
We know that the joint distribution of
We can display this in a joint distribution table.
spots = np.arange(1, 7) # possible values of F; same set for S
fair = (1/6) * np.ones(6)
biased = make_array(1/16, 1/16, 3/16, 3/16, 4/16, 4/16)
def joint_probability(i, j): # returns P(F = i, S = j)
return fair.item(i-1) * biased.item(j-1)
# joint distribution table of F and S
two_dice = Table().values('F', spots, 'S', spots).probability_function(joint_probability)
two_dice
F=1 | F=2 | F=3 | F=4 | F=5 | F=6 | |
---|---|---|---|---|---|---|
S=6 | 0.041667 | 0.041667 | 0.041667 | 0.041667 | 0.041667 | 0.041667 |
S=5 | 0.041667 | 0.041667 | 0.041667 | 0.041667 | 0.041667 | 0.041667 |
S=4 | 0.031250 | 0.031250 | 0.031250 | 0.031250 | 0.031250 | 0.031250 |
S=3 | 0.031250 | 0.031250 | 0.031250 | 0.031250 | 0.031250 | 0.031250 |
S=2 | 0.010417 | 0.010417 | 0.010417 | 0.010417 | 0.010417 | 0.010417 |
S=1 | 0.010417 | 0.010417 | 0.010417 | 0.010417 | 0.010417 | 0.010417 |
Define the indicator function of the event event
method.
def indicator(i, j):
return j > i + 2
two_dice.event(indicator, 'F', 'S')
P(Event) = 0.2395833333333333
F=1 | F=2 | F=3 | F=4 | F=5 | F=6 | |
---|---|---|---|---|---|---|
S=6 | 0.041667 | 0.041667 | 0.041667 | |||
S=5 | 0.041667 | 0.041667 | ||||
S=4 | 0.03125 | |||||
S=3 | ||||||
S=2 | ||||||
S=1 |
The answer
Expressed in math notation, the calculation is
For each fixed value of
4.2.4. Absolute Difference#
Question: What is the chance that the numbers on the two dice differ by no more than 1?
Answer: The goal is to find two_dice
, the joint distribution of
def indicator_absdiff_atmost_1(i, j):
return abs(i - j) <= 1
two_dice.event(indicator_absdiff_atmost_1, 'F', 'S')
P(Event) = 0.44791666666666674
F=1 | F=2 | F=3 | F=4 | F=5 | F=6 | |
---|---|---|---|---|---|---|
S=6 | 0.041667 | 0.041667 | ||||
S=5 | 0.041667 | 0.041667 | 0.041667 | |||
S=4 | 0.03125 | 0.03125 | 0.03125 | |||
S=3 | 0.03125 | 0.03125 | 0.03125 | |||
S=2 | 0.010417 | 0.010417 | 0.010417 | |||
S=1 | 0.010417 | 0.010417 |
The calculation shows that
The event is a diagonal band of cells, bounded by the lines
Notice the edge cases
Check carefully that you agree that in math notation the calculation is
4.2.5. Sum#
Question: What is the chance that the sum of the numbers on the two dice is 7?
Answer: This time we’ll write out the math first.
The event
because
Notice that the argument doesn’t depend on the nature of the bias in
We can check the answer by computation.
def indicator_sum_7(i, j):
return i + j == 7
two_dice.event(indicator_sum_7, 'F', 'S')
P(Event) = 0.16666666666666663
F=1 | F=2 | F=3 | F=4 | F=5 | F=6 | |
---|---|---|---|---|---|---|
S=6 | 0.041667 | |||||
S=5 | 0.041667 | |||||
S=4 | 0.03125 | |||||
S=3 | 0.03125 | |||||
S=2 | 0.010417 | |||||
S=1 | 0.010417 |
Question: Now suppose I have two
Answer: We can’t use our computational methods for this one because the model isn’t numerical. But we know how to solve the problem mathematically.
Let