Unit 10, lesson 71

Prior, Likelihood and Posterior

How Bayesian updating turns a prior distribution over competing hypotheses into a posterior distribution, what the normalizing constant does, and how prior odds, posterior odds and the Bayes factor fit together.

  • Video 71
  • Bayesian Inference

A packing machine runs at a low or a high setting, and its display has failed. At the low setting a packet comes out marked with probability 0.20.2, and at the high setting with probability 0.60.6. One packet is inspected and it is marked. Either setting could have produced it, so the packet does not prove which setting is in use, yet it makes the high setting more plausible than before. This lesson measures that change by multiplying starting probabilities by likelihoods and rescaling, and then writes the same update in terms of odds. The machine and its rates are a constructed teaching model.

Key terms

Prior distribution

Definition Suppose a model allows a finite list of hypotheses H1,…,HkH_1, \dots, H_k, exactly one of which is true within the model, and each hypothesis fixes a probability model for the data, for example by fixing the value of an unknown parameter. A prior distribution assigns each hypothesis a probability P(Hi)≥0P(H_i) \ge 0 before the data at hand are used, with

P(H1)+⋯+P(Hk)=1.P(H_1) + \dots + P(H_k) = 1.

The prior is a stated input of the analysis. It may come from earlier evidence, from knowledge of the process or from a deliberate convention such as equal weights, and its basis should be reported with the result.

In plain words The prior records how plausible each candidate is before the new observation is taken into account. It only spreads probability over the hypotheses the model lists. A hypothesis that is left off the list, or given prior probability 00, gets posterior probability 00 whatever data arrive, so an exclusion of that kind should be made on purpose.

Example In the machine model the unknown quantity is the marking rate pp, and the two hypotheses are p=0.2p = 0.2 for the low setting and p=0.6p = 0.6 for the high setting. Equal weights give the prior P(p=0.2)=P(p=0.6)=0.5P(p = 0.2) = P(p = 0.6) = 0.5. If maintenance records showed that the machine usually runs at the low setting, a prior of 0.80.8 for p=0.2p = 0.2 and 0.20.2 for p=0.6p = 0.6 might be better supported. Either way it is an assumption made before the packet is inspected.

Posterior distribution

Definition Let DD be the observed data. The likelihood of a hypothesis HH in the list is P(D∣H)P(D \mid H), the probability that HH gives to the data actually observed, read as a function of the hypothesis while the data stay fixed. Provided the normalizing constant P(D)P(D) of the next card is positive, the posterior distribution assigns each hypothesis HH in the list the probability

P(H∣D)=P(D∣H)P(H)P(D).P(H \mid D) = \frac{P(D \mid H)P(H)}{P(D)}.

This is Bayes’ theorem applied to each hypothesis. For continuous data the probabilities of the data are replaced by densities. The posterior probabilities are nonnegative and add to 11, and they are conditional on the model, the prior and the data together.

In plain words The posterior reweights the prior by how well each hypothesis predicted what was actually seen. A hypothesis that made the data more probable gains weight relative to one that made them less probable. The likelihood P(D∣H)P(D \mid H) and the posterior P(H∣D)P(H \mid D) condition in opposite directions, so a likelihood of 0.60.6 does not mean a posterior probability of 0.60.6. The posterior is also not the observed frequency. One marked packet out of one is a frequency of 11, but no hypothesis in this model has p=1p = 1, and the posterior can only spread probability over 0.20.2 and 0.60.6.

Example Write MM for the event that the inspected packet is marked. With equal priors the products of prior and likelihood are 0.5×0.2=0.10.5 \times 0.2 = 0.1 for p=0.2p = 0.2 and 0.5×0.6=0.30.5 \times 0.6 = 0.3 for p=0.6p = 0.6. Dividing each product by their sum gives the posterior probability of the high setting,

0.30.1+0.3=0.75,\frac{0.3}{0.1 + 0.3} = 0.75,

and the posterior probability 0.1/0.4=0.250.1/0.4 = 0.25 of the low setting. Given the two-setting model, the equal prior and one marked packet, the high setting has posterior probability 0.750.75.

Normalizing constant

Definition The normalizing constant of the update is the total probability of the observed data under the model,

P(D)=∑HP(D∣H) P(H),P(D) = \sum_{H} P(D \mid H)\,P(H),

with the sum running over the kk hypotheses in the list, by the law of total probability. It is the probability the model, prior included, gave to the data before they were observed, and it is also called the marginal likelihood or the evidence. It is the same number for every hypothesis, so the posterior is often written P(H∣D)∝P(D∣H) P(H)P(H \mid D) \propto P(D \mid H)\,P(H), where ∝\propto means equal up to a positive factor that is the same for every hypothesis. The update needs P(D)>0P(D) > 0. If every hypothesis with positive prior probability gives the observed data probability 00, the update is not defined, and the model itself needs another look.

In plain words The products of prior and likelihood already stand in the right ratios to one another. They just do not add to 11. Dividing every product by their total fixes the scale without changing any ratio.

Example With equal priors the constant for a marked packet is P(M)=0.1+0.3=0.4P(M) = 0.1 + 0.3 = 0.4. Before inspection the model said a packet would be marked with probability 0.40.4. The likelihoods 0.20.2 and 0.60.6 add to 0.80.8 rather than 11, and nothing is wrong with that, because they are probabilities of one observation under two different hypotheses, not probabilities of different outcomes under one hypothesis. With the prior 0.80.8 and 0.20.2 the products become 0.160.16 and 0.120.12, and the constant changes to 0.280.28.

Bayesian updating

Definition Bayesian updating is the passage from a prior distribution to a posterior distribution by conditioning on observed data within a stated model. With finitely many hypotheses it has three steps. Multiply each prior probability by the likelihood of the data under that hypothesis, add the products, and divide each product by the sum. When data arrive in two parts D1D_1 and then D2D_2, the posterior after D1D_1 serves as the prior for D2D_2, and the likelihood for the second step is P(D2∣Hi,D1)P(D_2 \mid H_i, D_1). If the two parts are conditionally independent given each hypothesis, that likelihood is simply P(D2∣Hi)P(D_2 \mid H_i). Updating in two steps then gives the same posterior as one update with the likelihood P(D1∣Hi) P(D2∣Hi)P(D_1 \mid H_i)\,P(D_2 \mid H_i) of both parts together, provided each observation is used exactly once.

In plain words The posterior from today becomes the prior for tomorrow. Each new observation shifts probability toward the hypotheses that predicted it better than the prior average P(D)P(D) and away from those that predicted it worse, which can mean away from the current favorite. Updating means following the evidence in whichever direction it points, not growing more certain of whatever led after the first observation.

Example Packets are assumed independent given the setting. After the marked packet the probabilities are 0.250.25 and 0.750.75. A second packet is unmarked, which has probability 0.80.8 when p=0.2p = 0.2 and 0.40.4 when p=0.6p = 0.6. The products are 0.25×0.8=0.20.25 \times 0.8 = 0.2 and 0.75×0.4=0.30.75 \times 0.4 = 0.3, with sum 0.50.5, so the posterior is 0.40.4 for p=0.2p = 0.2 and 0.60.6 for p=0.6p = 0.6. The unmarked packet lowered the probability of the high setting, though it is still ahead. Updating once on both packets from the equal prior gives the likelihoods 0.2×0.8=0.160.2 \times 0.8 = 0.16 and 0.6×0.4=0.240.6 \times 0.4 = 0.24, products 0.080.08 and 0.120.12, sum 0.20.2, and again 0.40.4 and 0.60.6.

Prior odds and posterior odds

Definition For two hypotheses H1H_1 and H0H_0 with P(H0)>0P(H_0) > 0, the prior odds of H1H_1 against H0H_0 are P(H1)/P(H0)P(H_1)/P(H_0). After data DD with P(H0∣D)>0P(H_0 \mid D) > 0, the posterior odds of H1H_1 against H0H_0 are P(H1∣D)/P(H0∣D)P(H_1 \mid D)/P(H_0 \mid D). Odds can be any number from 00 upward. The odds of H0H_0 against H1H_1 are the reciprocal, when P(H1)>0P(H_1) > 0. If H1H_1 and H0H_0 are the only hypotheses in the model, odds OO correspond to the probability

P(H1)=O1+O,P(H_1) = \frac{O}{1 + O},

and the same conversion turns posterior odds into a posterior probability. With more hypotheses in the model, the number O/(1+O)O/(1 + O) is the probability of H1H_1 given that one of the two is true.

In plain words Odds compare two hypotheses directly and leave the others aside. They are a ratio, not a probability, so odds of 33 are perfectly possible while a probability of 33 is not. Keep the direction fixed, saying which hypothesis is on top, throughout a calculation.

Example With equal priors the prior odds of p=0.6p = 0.6 against p=0.2p = 0.2 are 0.5/0.5=10.5/0.5 = 1. After the marked packet the posterior odds are 0.75/0.25=30.75/0.25 = 3, and since the model has only these two hypotheses, the posterior probability of p=0.6p = 0.6 is 3/(1+3)=3/43/(1 + 3) = 3/4. The posterior odds of p=0.2p = 0.2 against p=0.6p = 0.6 are 1/31/3.

Bayes factor

Definition For hypotheses H1H_1 and H0H_0 and data DD with P(D∣H0)>0P(D \mid H_0) > 0, the Bayes factor in favor of H1H_1 against H0H_0 is

B10=P(D∣H1)P(D∣H0).B_{10} = \frac{P(D \mid H_1)}{P(D \mid H_0)}.

If P(H0)>0P(H_0) > 0, Bayes’ theorem gives

P(H1∣D)P(H0∣D)=P(H1)P(H0)⋅B10,\frac{P(H_1 \mid D)}{P(H_0 \mid D)} = \frac{P(H_1)}{P(H_0)} \cdot B_{10},

because the normalizing constant cancels from the ratio. In words, posterior odds equal prior odds times the Bayes factor. The Bayes factor of H0H_0 against H1H_1 is 1/B101/B_{10} when B10>0B_{10} > 0. For a simple hypothesis, one that fixes every parameter, P(D∣H)P(D \mid H) is an ordinary likelihood value. For a composite hypothesis that allows a range of parameter values, P(D∣H)P(D \mid H) is the likelihood averaged over a prior for the parameter within that hypothesis, so the Bayes factor then depends on that prior too.

In plain words The Bayes factor is the part of the update that comes from the data. It says how many times as probable the observed data are under one hypothesis as under the other, and it does not depend on the prior odds of the two hypotheses. For two simple hypotheses no prior enters it at all. It is not the odds that one hypothesis is true. Those also need the prior odds, and a strong prior can outweigh a moderate Bayes factor.

Example For a marked packet the Bayes factor of p=0.6p = 0.6 against p=0.2p = 0.2 is 0.6/0.2=30.6/0.2 = 3, and for an unmarked packet it is 0.4/0.8=1/20.4/0.8 = 1/2. Now take the prior 0.80.8 for p=0.2p = 0.2 and 0.20.2 for p=0.6p = 0.6, so the prior odds of p=0.6p = 0.6 against p=0.2p = 0.2 are 1/41/4. After one marked packet the posterior odds are 14⋅3=34\tfrac14 \cdot 3 = \tfrac34, and

P(p=0.6∣M)=3/41+3/4=37≈0.429.\begin{aligned} P(p = 0.6 \mid M) &= \frac{3/4}{1 + 3/4} \\[0.4em] &= \frac{3}{7} \approx 0.429. \end{aligned}

The direct calculation agrees, since 0.12/0.28=3/70.12/0.28 = 3/7. The data favor the high setting by a factor of 33, yet the low setting remains more probable, because it started four times as probable.

Check yourself

  1. In the machine model, suppose the prior is 0.70.7 for p=0.2p = 0.2 and 0.30.3 for p=0.6p = 0.6, and one inspected packet is unmarked. Find the posterior probability of p=0.6p = 0.6, then check it with the Bayes factor and odds.
Show the answer

Write UU for the event that the packet is unmarked. It has probability 0.80.8 when p=0.2p = 0.2 and 0.40.4 when p=0.6p = 0.6. The products are 0.7×0.8=0.560.7 \times 0.8 = 0.56 and 0.3×0.4=0.120.3 \times 0.4 = 0.12, and the normalizing constant is 0.680.68. So

P(p=0.6∣U)=0.120.68=317≈0.176.\begin{aligned} P(p = 0.6 \mid U) &= \frac{0.12}{0.68} \\[0.4em] &= \frac{3}{17} \approx 0.176. \end{aligned}

With odds, the Bayes factor of p=0.6p = 0.6 against p=0.2p = 0.2 is 0.4/0.8=1/20.4/0.8 = 1/2 and the prior odds are 0.3/0.7=3/70.3/0.7 = 3/7, so the posterior odds are 3/143/14. Converting gives 3/(3+14)=3/173/(3 + 14) = 3/17 again. The unmarked packet lowered the probability of the high setting from 0.30.3, as it should, because that observation is more probable at the low setting.

  1. Start from equal priors. A marked packet and then an unmarked packet are inspected. A student takes the posterior after the first packet, 0.250.25 and 0.750.75, as the new prior, but then multiplies by the likelihood of both packets, 0.160.16 and 0.240.24. What does the student get, what is the correct posterior, and what went wrong?
Show the answer

The student’s products are 0.25×0.16=0.040.25 \times 0.16 = 0.04 and 0.75×0.24=0.180.75 \times 0.24 = 0.18, so the student reports a posterior probability for p=0.6p = 0.6 of

0.180.04+0.18=911≈0.818.\frac{0.18}{0.04 + 0.18} = \frac{9}{11} \approx 0.818.

The correct answer is 0.60.6. Either multiply the prior 0.250.25 and 0.750.75 by the likelihood of the second packet only, which gives 0.20.2 and 0.30.3 and then 0.40.4 and 0.60.6, or multiply the original equal prior by the likelihood of both packets. The student used the marked packet twice, once inside the new prior and once inside the joint likelihood, which is the same as pretending that two marked packets and one unmarked packet were seen.

  1. Extend the model to three settings, with p=0.2p = 0.2, p=0.4p = 0.4 and p=0.6p = 0.6 and prior probabilities 0.250.25, 0.50.5 and 0.250.25. One packet is inspected and it is marked. Find the posterior distribution. A colleague notes that the Bayes factor of p=0.6p = 0.6 against p=0.2p = 0.2 is 33 and the prior odds are 11, and concludes that P(p=0.6∣M)=3/4P(p = 0.6 \mid M) = 3/4. Is that right?
Show the answer

The products of prior and likelihood are 0.25×0.2=0.050.25 \times 0.2 = 0.05, 0.5×0.4=0.20.5 \times 0.4 = 0.2 and 0.25×0.6=0.150.25 \times 0.6 = 0.15, and their sum is 0.40.4. The posterior probabilities are therefore 0.1250.125, 0.50.5 and 0.3750.375, which add to 11. The posterior odds of p=0.6p = 0.6 against p=0.2p = 0.2 are indeed 0.375/0.125=30.375/0.125 = 3, the prior odds times the Bayes factor. The conclusion is wrong, though, because turning odds OO into the probability O/(1+O)O/(1 + O) assumes the two hypotheses are the only ones. Here 3/43/4 is the probability of p=0.6p = 0.6 given that pp is 0.20.2 or 0.60.6. The posterior probability of p=0.6p = 0.6 is 3/83/8, and the most probable setting after the marked packet is p=0.4p = 0.4.

How this lesson connects