Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

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.1Fair 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

value123456
probability1/161/163/163/164/164/16

Suppose I roll each die once.

4.2.2Equality

Question: What is the chance that I get the same number on both dice?

4.2.3Difference

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 {S>F+2}\{ S > F + 2 \} using the methods of the previous section.

We know that the joint distribution of FF and SS is given by

P(F=i,S=j) = {16116,   1i6, j=1,216316,   1i6, j=3,416416,   1i6, j=5,6P(F = i, S = j) ~ = ~ \begin{cases} \frac{1}{6} \cdot \frac{1}{16}, ~~~ 1 \le i \le 6, ~ j = 1, 2 \\ \frac{1}{6} \cdot \frac{3}{16}, ~~~ 1 \le i \le 6, ~ j = 3, 4 \\ \frac{1}{6} \cdot \frac{4}{16}, ~~~ 1 \le i \le 6, ~ j = 5, 6 \end{cases}

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
Loading...

Define the indicator function of the event {S>F+2}\{S > F + 2 \} and then use the event method.

def indicator(i, j):
    return j > i + 2

two_dice.event(indicator, 'F', 'S')
P(Event) = 0.2395833333333333
Loading...

The answer P(S>F+2)0.24P(S > F + 2) \approx 0.24 has been obtained by adding the chances of all the cells (i,j)(i, j) for which j>i+2j > i+2 or equivalently i<j2i < j-2. Since jj can be at most 6, this implies 1i31 \le i \le 3, as is visible in the display.

Expressed in math notation, the calculation is

P(S>F+2) = j>i+2P(F=i,S=j) = i=13j=i+36P(F=i,S=j)P(S > F + 2) ~ = ~ \mathop{\sum \sum}_{j > i+2} P(F = i, S= j) ~ = ~ \sum_{i=1}^3\sum_{j=i+3}^6 P(F = i, S= j)

For each fixed value of ii, the inner sum is the sum of the terms visible in the column labeled F=iF = i. The outer sum adds up the three column sums.

4.2.4Absolute 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 P(FS1)P(\vert F - S \vert \le 1). We defined two_dice, the joint distribution of FF and SS, in the previous problem. So now our work is simple.

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
Loading...

The calculation shows that P(FS1)0.448P(\vert F - S \vert \le 1) \approx 0.448.

The event is a diagonal band of cells, bounded by the lines j=i1j = i-1 and j=i+1j = i+1. That is because the condition ij1\vert i - j \vert \le 1 is the same as i1ji+1i-1 \le j \le i+1.

Notice the edge cases i=1i=1 and i=6i=6. When i=1i=1, the condition ij1\vert i - j \vert \le 1 is only satisfied by j=1j=1 and j=2j=2, because j=1j = -1 is not a possible outcome of the dice. Nor is j=7j = 7 when i=6i = 6. So there are two terms to add in each of the columns labeled F=1F=1 and F=6F=6, and three in each of the other columns.

Check carefully that you agree that in math notation the calculation is

P(FS1) = j=12P(F=1,S=j) + i=25j=i1i+1P(F=i,S=j) + j=56P(F=6,S=j)P(\vert F - S \vert \le 1) ~ = ~ \sum_{j=1}^2 P(F = 1, S = j) ~ + ~ \sum_{i=2}^5\sum_{j=i-1}^{i+1} P(F = i, S = j) ~ + ~ \sum_{j=5}^6 P(F = 6, S = j)

4.2.5Sum

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 {F+S=7}\{ F + S = 7 \} consists of all possible pairs (i,j)(i, j) such that i+j=7i + j = 7. For each fixed ii, there is exactly one jj that satisfies i+j=7i+j = 7, and that’s j=7ij = 7-i. So

P(F+S=7) = i=16P(F=i,S=7i) = i=1616P(S=7i) = 16i=16P(S=7i) = 16P(F+S = 7) ~ = ~ \sum_{i=1}^6 P(F = i, S = 7-i) ~ = ~ \sum_{i=1}^6 \frac{1}{6}P(S = 7-i) ~ = ~ \frac{1}{6} \sum_{i=1}^6 P(S = 7-i) ~ = ~ \frac{1}{6}

because i=16P(S=7i)=P(S=6)+P(S=5)++P(S=1)=1\sum_{i=1}^6 P(S = 7-i) = P(S=6) + P(S=5) + \cdots + P(S=1) = 1.

Notice that the argument doesn’t depend on the nature of the bias in SS. The chance that the sum of the numbers on two dice equals 7 is 1/6 as long as one of the dice is fair.

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
Loading...

Question: Now suppose I have two nn-sided dice, at least one of which is fair. I roll each of them once. What is the chance that the sum of the two numbers is n+1n+1?

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 FnF_n be the number on a fair die and DnD_n the number on the other die.

P(Fn+Dn=n+1) = i=1nP(Fn=i,Dn=n+1i)= i=1n1nP(Dn=n+1i)= 1ni=1nP(Dn=n+1i)= 1n\begin{align*} P(F_n + D_n = n+1) ~ &= ~ \sum_{i=1}^n P(F_n = i, D_n = n+1-i) \\ &= ~ \sum_{i=1}^n \frac{1}{n}P(D_n = n+1-i) \\ &= ~ \frac{1}{n} \sum_{i=1}^n P(D_n = n+1-i) \\ &= ~ \frac{1}{n} \end{align*}