As an example, we used data from the General Social Survey and Bayesβs Theorem to compute conditional probabilities. But since we had the complete dataset, we didnβt really need Bayesβs Theorem. It was easy enough to compute the left side of the equation directly, and no easier to compute the right side.
But often we donβt have a complete dataset, and in that case Bayesβs Theorem is more useful. In this chapter, weβll use it to solve several more challenging problems related to conditional probability.
Now suppose you choose one of the bowls at random and, without looking, choose a cookie at random. If the cookie is vanilla, what is the probability that it came from Bowl 1?
\(P(B_1)\text{,}\) the probability that we chose Bowl 1, unconditioned by what kind of cookie we got. Since the problem says we chose a bowl at random, we assume \(P(B_1) = 1/2\text{.}\)
This example demonstrates one use of Bayesβs theorem: it provides a way to get from \(P(B|A)\) to \(P(A|B)\text{.}\) This strategy is useful in cases like this where it is easier to compute the terms on the right side than the term on the left.
There is another way to think of Bayesβs theorem: it gives us a way to update the probability of a hypothesis, \(H\text{,}\) given some body of data, \(D\text{.}\)
This interpretation is diachronic, which means βrelated to change over timeβ; in this case, the probability of the hypotheses changes as we see new data.
Sometimes we can compute the prior based on background information. For example, the cookie problem specifies that we choose a bowl at random with equal probability.
In other cases the prior is subjective; that is, reasonable people might disagree, either because they use different background information or because they interpret the same information differently.
The likelihood is usually the easiest part to compute. In the cookie problem, we are given the number of cookies in each bowl, so we can compute the probability of the data under each hypothesis.
Computing the total probability of the data can be tricky. It is supposed to be the probability of seeing the data under any hypothesis at all, but it can be hard to nail down what that means.
A convenient tool for doing a Bayesian update is a Bayes table. You can write a Bayes table on paper or use a spreadsheet, but in this section Iβll use a Pandas DataFrame.
You might notice that the likelihoods donβt add up to 1. Thatβs OK; each of them is a probability conditioned on a different hypothesis. Thereβs no reason they should add up to 1 and no problem if they donβt.
The posterior probability for Bowl 1 is 0.6, which is what we got using Bayesβs Theorem explicitly. As a bonus, we also get the posterior probability of Bowl 2, which is 0.4.
When we add up the unnormalized posteriors and divide through, we force the posteriors to add up to 1. This process is called normalization, which is why the total probability of the data is also called the normalizing constant.
Suppose I have a box with a 6-sided die, an 8-sided die, and a 12-sided die. I choose one of the dice at random, roll it, and report that the outcome is a 1. What is the probability that I chose the 6-sided die?
If I chose the 6-sided die, the probability of the data is 1/6. If I chose the 8-sided die, the probability is 1/8, and if I chose the 12-sided die, itβs 1/12.
The posterior probability of the 6-sided die is 4/9, which is a little more than the probabilities for the other dice, 3/9 and 2/9. Intuitively, the 6-sided die is the most likely because it had the highest likelihood of producing the outcome we saw.
Suppose you pick Door 1. Before opening the door you chose, Monty opens Door 3 and reveals a goat. Then Monty offers you the option to stick with your original choice or switch to the remaining unopened door.
If you have not encountered this problem before, you might find that answer surprising. You would not be alone; many people have the strong intuition that it doesnβt matter if you stick or switch. There are two doors left, they reason, so the chance that the car is behind Door A is 50%. But that is wrong.
To see why, it can help to use a Bayes table. We start with three hypotheses: the car might be behind Door 1, 2, or 3. According to the statement of the problem, the prior probability for each door is 1/3.
After Monty opens Door 3, the posterior probability of Door 1 is \(1/3\text{;}\) the posterior probability of Door 2 is \(2/3\text{.}\) So you are better off switching from Door 1 to Door 2.
In this chapter we solved the Cookie Problem using Bayesβs theorem explicitly and using a Bayes table. Thereβs no real difference between these methods, but the Bayes table can make it easier to compute the total probability of the data, especially for problems with more than two hypotheses.
If the Monty Hall problem makes your head hurt, you are not alone. But I think it demonstrates the power of Bayesβs Theorem as a divide-and-conquer strategy for solving tricky problems. And I hope it provides some insight into why the answer is what it is.
When Monty opens a door, he provides information we can use to update our belief about the location of the car. Part of the information is obvious. If he opens Door 3, we know the car is not behind Door 3. But part of the information is more subtle. Opening Door 3 is more likely if the car is behind Door 2, and less likely if it is behind Door 1. So the data is evidence in favor of Door 2. We will come back to this notion of evidence in future chapters.
Suppose you have two coins in a box. One is a normal coin with heads on one side and tails on the other, and one is a trick coin with heads on both sides. You choose a coin at random and see that one of the sides is heads. What is the probability that you chose the trick coin?
Suppose you meet someone and learn that they have two children. You ask if either child is a girl and they say yes. What is the probability that both children are girls?
There are many variations of the Monty Hall problem. For example, suppose Monty always chooses Door 2 if he can, and only chooses Door 3 if he has to (because the car is behind Door 2).
# If the car is behind Door 1, Monty would always open Door 2
# If the car was behind Door 2, Monty would have opened Door 3
# If the car is behind Door 3, Monty would always open Door 2
table6 = pd.DataFrame(index=['Door 1', 'Door 2', 'Door 3'])
table6['prior'] = 1/3
table6['likelihood'] = 1, 0, 1
update(table6)
table6
# If the car is behind Door 1, Monty would have opened Door 2
# If the car is behind Door 2, Monty would always open Door 3
# If the car is behind Door 3, Monty would have opened Door 2
table7 = pd.DataFrame(index=['Door 1', 'Door 2', 'Door 3'])
table7['prior'] = 1/3
table7['likelihood'] = 0, 1, 0
update(table7)
table7
M&Mβs are small candy-coated chocolates that come in a variety of colors. Mars, Inc., which makes M&Mβs, changes the mixture of colors from time to time. In 1995, they introduced blue M&Mβs.
Suppose a friend of mine has two bags of M&Mβs, and he tells me that one is from 1994 and one from 1996. He wonβt tell me which is which, but he gives me one M&M from each bag. One is yellow and one is green. What is the probability that the yellow one came from the 1994 bag?