In the previous chapter we used Bayesβs Theorem to solve a cookie problem; then we solved it again using a Bayes table. In this chapter, at the risk of testing your patience, we will solve it one more time using a Pmf object, which represents a βprobability mass functionβ. Iβll explain what that means, and why it is useful for Bayesian statistics.
Weβll use Pmf objects to solve some more challenging problems and take one more step toward Bayesian statistics. But weβll start with distributions.
In statistics a distribution is a set of possible outcomes and their corresponding probabilities. For example, if you toss a coin, there are two possible outcomes with approximately equal probability. If you roll a six-sided die, the set of possible outcomes is the numbers 1 to 6, and the probability associated with each outcome is 1/6.
To represent distributions, weβll use a library called empiricaldist. An βempiricalβ distribution is based on data, as opposed to a theoretical distribution. Weβll use this library throughout the book. Iβll introduce the basic features in this chapter and weβll see additional features later.
If the outcomes in a distribution are discrete, we can describe the distribution with a probability mass function, or PMF, which is a function that maps from each possible outcome to its probability.
Pmf() creates an empty Pmf with no outcomes. Then we can add new outcomes using the bracket operator. In this example, the two outcomes are represented with strings, and they have the same probability, 0.5.
In this example, all outcomes in the sequence appear once, so they all have the same probability, \(1/6\text{.}\) More generally, outcomes can appear more than once, as in the following example:
The letter M appears once out of 11 characters, so its probability is \(1/11\text{.}\) The letter i appears 4 times, so its probability is \(4/11\text{.}\)
The Pmf class inherits from a Pandas Series, so anything you can do with a Series, you can also do with a Pmf. For example, you can use the bracket operator to look up a quantity and get the corresponding probability.
The quantities in a Pmf can be strings, numbers, or any other type that can be stored in the index of a Pandas Series. If you are familiar with Pandas, that will help you work with Pmf objects. But I will explain what you need to know as we go along.
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?
To update the distribution based on new data (the vanilla cookie), we multiply the priors by the likelihoods. The likelihood of drawing a vanilla cookie from Bowl 1 is \(3/4\text{.}\) The likelihood for Bowl 2 is \(1/2\text{.}\)
The result is the unnormalized posteriors; that is, they donβt add up to 1. To make them add up to 1, we can use normalize, which is a method provided by Pmf
The return value from normalize is the total probability of the data, which is \(5/8\text{.}\)posterior, which contains the posterior probability for each hypothesis, is called (wait now) the posterior distribution.
One benefit of using Pmf objects is that it is easy to do successive updates with more data. For example, suppose you put the first cookie back (so the contents of the bowls donβt change) and draw again from the same bowl. If the second cookie is also vanilla, we can do a second update like this:
As in the previous version, there are only two kinds of cookies, vanilla and chocolate. So Bowl 0 is all chocolate cookies, Bowl 1 is 99% chocolate, and so on. Suppose we choose a bowl at random, choose a cookie at random, and it turns out to be vanilla. What is the probability that the cookie came from Bowl \(x\text{,}\) for each value of \(x\text{?}\)
As this example shows, we can initialize a Pmf with two parameters. The first parameter is the prior probability; the second parameter is a sequence of quantities.
In this example, the probabilities are all the same, so we only have to provide one of them; it gets βbroadcastβ across the hypotheses. Since all hypotheses have the same prior probability, this distribution is uniform.
The posterior probability of Bowl 0 is 0 because it contains no vanilla cookies. The posterior probability of Bowl 100 is the highest because it contains the most vanilla cookies. In between, the shape of the posterior distribution is a line because the likelihoods are proportional to the bowl numbers.
After two vanilla cookies, the high-numbered bowls have the highest posterior probabilities because they contain the most vanilla cookies; the low-numbered bowls have the lowest probabilities.
Now Bowl 100 has been eliminated because it contains no chocolate cookies. But the high-numbered bowls are still more likely than the low-numbered bowls, because we have seen more vanilla cookies than chocolate.
In fact, the peak of the posterior distribution is at Bowl 67, which corresponds to the fraction of vanilla cookies in the data weβve observed, \(2/3\text{.}\)
The quantity with the highest posterior probability is called the MAP, which stands for βmaximum a posteriori probabilityβ, where βa posterioriβ is unnecessary Latin for βposteriorβ.
As you might suspect, this example isnβt really about bowls; itβs about estimating proportions. Imagine that you have one bowl of cookies. You donβt know what fraction of cookies are vanilla, but you think it is equally likely to be any fraction from 0 to 1. If you draw three cookies and two are vanilla, what proportion of cookies in the bowl do you think are vanilla? The posterior distribution we just computed is the answer to that question.
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?
The likelihood for the 6-sided die is 0 because it is not possible to get a 7 on a 6-sided die. The other two likelihoods are the same as in the previous update.
The first line selects quantities from the Pmf which represent the hypotheses. Since the hypotheses are integers, we can use them to compute the likelihoods. In general, if there are n sides on the die, the probability of any possible outcome is 1/n.
However, we have to check for impossible outcomes! If the outcome exceeds the hypothetical number of sides on the die, the probability of that outcome is 0.
impossible is a Boolean Series that is True for each impossible outcome. I use it as an index into likelihood to set the corresponding probabilities to 0.
empiricaldist is based on Pandas; the Pmf class inherits from the Pandas Series class and provides additional features specific to probability mass functions. Weβll use Pmf and other classes from empiricaldist throughout the book because they simplify the code and make it more readable. But we could do the same things directly with Pandas.
We use a Pmf to solve the cookie problem and the dice problem, which we saw in the previous chapter. With a Pmf it is easy to perform sequential updates with multiple pieces of data.
We also solved a more general version of the cookie problem, with 101 bowls rather than two. Then we computed the MAP, which is the quantity with the highest posterior probability.
In the next chapter, Iβll introduce the Euro problem, and we will use the binomial distribution. And, at last, we will make the leap from using Bayesβs Theorem to doing Bayesian statistics.
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 four times, and get 1, 3, 5, and 7. What is the probability that I chose the 8-sided die?
In the previous version of the dice problem, the prior probabilities are the same because the box contains one of each die. But suppose the box contains 1 die that is 4-sided, 2 dice that are 6-sided, 3 dice that are 8-sided, 4 dice that are 12-sided, and 5 dice that are 20-sided. I choose a die, roll it, and get a 7. What is the probability that I chose an 8-sided die?
# Notice that I don't bother to normalize the prior.
# The `Pmf` gets normalized during the update, so we
# don't have to normalize it before.
ps = [1,2,3,4,5]
qs = [4,6,8,12,20]
pmf = Pmf(ps, qs)
update_dice(pmf, 7)
pmf
Suppose I have two sock drawers. One contains equal numbers of black and white socks. The other contains equal numbers of red, green, and blue socks. Suppose I choose a drawer at random, choose two socks at random, and I tell you that I got a matching pair. What is the probability that the socks are white?
# In the BlackWhite drawer, the probability of getting a match is 1/2
# In the RedGreenBlue drawer, the probability of a match is 1/3
hypos = ['BlackWhite', 'RedGreenBlue']
prior = Pmf(1/2, hypos)
likelihood = 1/2, 1/3
posterior = prior * likelihood
posterior.normalize()
posterior
probs
BlackWhite 0.6
RedGreenBlue 0.4
# If I drew from the BlackWhite drawer, the probability the
# socks are white is 1/2
posterior['BlackWhite'] / 2
# The trick to this question is to notice that Elvis's twin was a brother.
# If they were identical twins, it is certain they would be the same sex.
# If they were fraternal twins, the likelihood is only 50%.
# Here's a solution using a Bayes table
import pandas as pd
table = pd.DataFrame(index=['identical', 'fraternal'])
table['prior'] = 1/3, 2/3
table['likelihood'] = 1, 1/2
table['unnorm'] = table['prior'] * table['likelihood']
prob_data = table['unnorm'].sum()
table['posterior'] = table['unnorm'] / prob_data
table