Bayes' Rule

Bayes' rule turns a conditional probability around. We know how likely an observation is in each of several cases, and the rule gives how likely each case is, given the observation.

Our example is a factory that makes one kind of part on three machines.

A part is found to be defective. We want the probability that each machine made it.

Count the parts

Suppose the factory makes 1000 parts. Machine 1 makes 600 of them, machine 2 makes 300, and machine 3 makes 100. Of the 600 parts from machine 1, 1 in 15 is defective, which is 40 parts. In the same way, 30 of the parts from machine 2 are defective, and 20 of the parts from machine 3.

Defective Good Total
Machine 1 40 560 600
Machine 2 30 270 300
Machine 3 20 80 100
Total 90 910 1000

We know the part is defective, so we look only at the 90 defective parts, which are at the front of the figure. Machine 1 made 40 of them, so the probability that machine 1 made the defective part is 40/90=4/940/90 = 4/9. For machine 2 it is 30/90=1/330/90 = 1/3, and for machine 3 it is 20/90=2/920/90 = 2/9.

Machine 3 makes a defective part 3 times as often as machine 1 (1 part in 5 against 1 in 15). However, the defective part is half as likely to have come from machine 3 as from machine 1 (2/92/9 against 4/94/9). The reason is that machine 1 makes 6 times as many parts, so it makes more defective parts even though a smaller share of its parts is defective.

Bayes' rule for the three machines

The count used 1000 parts, but the answers do not depend on that number. We now find them from the probabilities alone.

Choose one part at random. Let M1M_1, M2M_2, and M3M_3 be the events that machine 1, machine 2, and machine 3 made it, and let DD be the event that it is defective. We want P(M3∣D)P(M_3 \mid D), and the same for the other two machines.

On paper Draw the tree for one part: first which machine makes it, with three branches, then whether it is defective. Write a probability on every branch and the product at the end of every path. Circle the three paths that end in a defective part.

The definition of conditional probability gives

P(M3∣D)=P(M3∩D)P(D).P(M_3 \mid D) = \frac{P(M_3 \cap D)}{P(D)}.

The top is the path of the tree through machine 3 that ends in a defective part. Its probability is the product along the path, by the multiplication rule:

P(M3∩D)=P(M3) P(D∣M3)=110⋅15=2100.P(M_3 \cap D) = P(M_3)\,P(D \mid M_3) = \frac{1}{10} \cdot \frac{1}{5} = \frac{2}{100}.

The bottom is the probability that a part is defective. Three paths end in a defective part, one for each machine, and no part follows two paths, so we add the three:

P(D)=P(M1) P(D∣M1)+P(M2) P(D∣M2)+P(M3) P(D∣M3)=35⋅115+310⋅110+110⋅15=4100+3100+2100=9100.\begin{aligned} P(D) &= P(M_1)\,P(D \mid M_1) + P(M_2)\,P(D \mid M_2) + P(M_3)\,P(D \mid M_3) \\[6pt] &= \frac{3}{5} \cdot \frac{1}{15} + \frac{3}{10} \cdot \frac{1}{10} + \frac{1}{10} \cdot \frac{1}{5} \\[6pt] &= \frac{4}{100} + \frac{3}{100} + \frac{2}{100} = \frac{9}{100}. \end{aligned}

Of all the parts, 9 in 100 are defective, which is the 90 of 1000 in the count. With the top and the bottom written out, we have

P(M3∣D)=P(M3) P(D∣M3)P(M1) P(D∣M1)+P(M2) P(D∣M2)+P(M3) P(D∣M3)=2/1009/100=29.\begin{aligned} P(M_3 \mid D) &= \frac{P(M_3)\,P(D \mid M_3)}{P(M_1)\,P(D \mid M_1) + P(M_2)\,P(D \mid M_2) + P(M_3)\,P(D \mid M_3)} \\[6pt] &= \frac{2/100}{9/100} = \frac{2}{9}. \end{aligned}

This is Bayes' rule for the three machines. The bottom has one term for each machine, and the top is the term of machine 3. For the other two machines only the top changes, so P(M1∣D)=4/9P(M_1 \mid D) = 4/9 and P(M2∣D)=3/9=1/3P(M_2 \mid D) = 3/9 = 1/3. These are the answers from the count.

In the figure, the thickness of a band is a probability.

The rule in general

The same steps work for any number of cases, as long as exactly one of the cases happens. Events A1,A2,…,AnA_1, A_2, \dots, A_n form a partition when every outcome is in exactly one of them (every part is made by exactly one machine). Let BB be any event (the part is defective).

The sum on the bottom is the law of total probability:

P(B)=P(A1) P(B∣A1)+P(A2) P(B∣A2)+⋯+P(An) P(B∣An).(1)P(B) = P(A_1)\,P(B \mid A_1) + P(A_2)\,P(B \mid A_2) + \dots + P(A_n)\,P(B \mid A_n). \tag{1}

It has one term for each case: the probability of the case, times the probability of BB given that case.

Bayes' rule divides one of these terms by the sum:

P(Ai∣B)=P(Ai) P(B∣Ai)P(A1) P(B∣A1)+⋯+P(An) P(B∣An),(2)P(A_i \mid B) = \frac{P(A_i)\,P(B \mid A_i)}{P(A_1)\,P(B \mid A_1) + \dots + P(A_n)\,P(B \mid A_n)}, \tag{2}

where AiA_i is any one of the cases. Both rules hold when every P(Ai)>0P(A_i) > 0, and (2) also needs P(B)>0P(B) > 0.

Prior and posterior

Bayes' rule gave one probability for each machine. To see what the defective part changed, we compare each of them with the probability of that machine before we knew the part was defective.

The probability of a case before the observation is its prior probability, and the probability of the case given the observation is its posterior probability. For the machines:

Prior Posterior
Machine 1 3/53/5 4/94/9
Machine 2 3/103/10 1/31/3
Machine 3 1/101/10 2/92/9

In the figure, the width of a strip is a machine's prior probability, and the bar at the front shows the posterior probabilities.

A machine becomes more likely when its parts are defective more often than parts are defective overall. For machine 3 this is 1/51/5 against 9/1009/100, so its probability rose from 1/101/10 to 2/92/9. For machine 1 it is 1/151/15, which is less than 9/1009/100, so its probability fell from 3/53/5 to 4/94/9.

Machine 3 rose, but it started from 1/101/10, so it is still the least likely of the three. The posterior probability depends on the prior probability as well as on how often each machine makes a defective part.

Practice

Work each problem on paper before opening its solution.

Problem 1

An ice cream stand

On a summer day at a beach, the weather is sunny with probability 1/21/2, cloudy with probability 3/103/10, and rainy with probability 1/51/5. An ice cream stand sells out with probability 3/53/5 on a sunny day, 1/51/5 on a cloudy day, and 1/101/10 on a rainy day.

Draw the tree for one day: first the weather, with three branches, then whether the stand sells out. Write a probability on every branch and the product at the end of every path.

Find the probability that the stand sells out, as an exact fraction.

Solution

Step 1: Name the cases

Let SS, CC, and RR be the events that the day is sunny, cloudy, and rainy, and let OO be the event that the stand sells out. Every day has exactly one kind of weather, so SS, CC, and RR form a partition.

Step 2: Multiply along the paths that end in a sold-out stand

Three paths end in a sold-out stand, one for each kind of weather. Their probabilities are

12⋅35=1550,310⋅15=350,and15⋅110=150.\frac{1}{2} \cdot \frac{3}{5} = \frac{15}{50}, \qquad \frac{3}{10} \cdot \frac{1}{5} = \frac{3}{50}, \qquad\text{and}\qquad \frac{1}{5} \cdot \frac{1}{10} = \frac{1}{50}.

Step 3: Add the three paths

No day follows two paths, so the law of total probability, (1) from the lesson, gives

P(O)=P(S) P(O∣S)+P(C) P(O∣C)+P(R) P(O∣R)=1550+350+150=1950.\begin{aligned} P(O) &= P(S)\,P(O \mid S) + P(C)\,P(O \mid C) + P(R)\,P(O \mid R) \\[6pt] &= \frac{15}{50} + \frac{3}{50} + \frac{1}{50} \\[6pt] &= \frac{19}{50}. \end{aligned}

Step 4: Why the plain average is wrong

The average of the three probabilities of selling out is 13(35+15+110)=1550\frac{1}{3}\left(\frac{3}{5} + \frac{1}{5} + \frac{1}{10}\right) = \frac{15}{50}. This would be the answer only if the three kinds of weather were equally likely. Half of the days are sunny, so 3/53/5 counts for more than the other two, and the answer, 19/5019/50, is greater than 15/5015/50.

Answer:

19/5019/50

Problem 2

Three bakers

A bakery has three bakers.

  • Baker 1 bakes 1/21/2 of the loaves and burns 1 in 20 of them.
  • Baker 2 bakes 1/31/3 of the loaves and burns 1 in 10 of them.
  • Baker 3 bakes 1/61/6 of the loaves and burns 1 in 4 of them.

One loaf is chosen at random from all the loaves, and it is burnt.

Make a table with a row for each baker and four columns: the prior probability, the probability of a burnt loaf for that baker, their product, and the posterior probability. Fill it in, and write the total under the first, third, and fourth columns.

Find the probability that each baker baked the burnt loaf, as exact fractions.

Solution

Step 1: Multiply across each row

The product in a row is the probability that the baker baked the loaf and burnt it. Written over one denominator, the three products are 3/1203/120, 4/1204/120, and 5/1205/120.

Step 2: Add the products

The products add to 12/120=1/1012/120 = 1/10. By the law of total probability, this is the probability that a loaf is burnt.

Step 3: Divide each product by the total

Bayes' rule, (2) from the lesson, divides each product by their total.

Prior Burnt Product Posterior
Baker 1 1/21/2 1/201/20 3/1203/120 3/12=1/43/12 = 1/4
Baker 2 1/31/3 1/101/10 4/1204/120 4/12=1/34/12 = 1/3
Baker 3 1/61/6 1/41/4 5/1205/120 5/125/12
Total 11 12/12012/120 11

The same table can be counted. Of 120 loaves, the bakers bake 60, 40, and 20, and they burn 3, 4, and 5. Of the 12 burnt loaves, 3, 4, and 5 came from each baker.

Step 4: Compare before and after

The burnt loaf made baker 3 more likely (1/61/6 to 5/125/12) and baker 1 less likely (1/21/2 to 1/41/4). It left baker 2 at 1/31/3.

To see why, write Bayes' rule for one baker as a product:

P(baker∣burnt)=P(baker)×P(burnt∣baker)P(burnt).P(\text{baker} \mid \text{burnt}) = P(\text{baker}) \times \frac{P(\text{burnt} \mid \text{baker})}{P(\text{burnt})}.

The posterior probability is the prior probability times a factor. Baker 2 burns 1 loaf in 10, and 1 loaf in 10 is burnt overall, so the factor is 1. A burnt loaf is no more likely from baker 2 than from the bakery as a whole, so it says nothing about baker 2. For baker 3 the factor is 1/41/10=5/2\frac{1/4}{1/10} = 5/2, and for baker 1 it is 1/201/10=1/2\frac{1/20}{1/10} = 1/2.

The division in the last column changes every row by the same factor. The products are in the ratio 3 to 4 to 5, and so are the posterior probabilities.

Answer:

1/41/4, 1/31/3, and 5/125/12

Problem 3

The same filter at another inbox

The filter in the problem "A flagged email" flags a spam email with probability 3/43/4 and an email that is not spam with probability 1/61/6. At the inbox of that problem, 1 email in 10 was spam, and the answer was that a flagged email is spam with probability 1/31/3.

The same filter is put on a second inbox, where 1 email in 4 is spam.

Draw the tree for one email at the second inbox: first whether it is spam, then whether it is flagged. Write a probability on every branch and the product at the end of every path.

An email at the second inbox is flagged. Find the probability that it is spam, as an exact fraction.

Solution

Step 1: Separate what changed from what did not

Let SS be the event that the email is spam and FF the event that it is flagged. The filter is the same, so P(F∣S)=3/4P(F \mid S) = 3/4 and P(F∣Sc)=1/6P(F \mid S^c) = 1/6 as before. Only the prior probability changed: P(S)=1/4P(S) = 1/4 and P(Sc)=3/4P(S^c) = 3/4.

Step 2: Add the paths that end in a flag

By the law of total probability,

P(F)=14⋅34+34⋅16=316+216=516.P(F) = \frac{1}{4} \cdot \frac{3}{4} + \frac{3}{4} \cdot \frac{1}{6} = \frac{3}{16} + \frac{2}{16} = \frac{5}{16}.

Step 3: Divide

Bayes' rule gives

P(S∣F)=P(S) P(F∣S)P(F)=3/165/16=35.P(S \mid F) = \frac{P(S)\,P(F \mid S)}{P(F)} = \frac{3/16}{5/16} = \frac{3}{5}.

Step 4: Compare the two inboxes

The same filter gives 1/31/3 at the first inbox and 3/53/5 at the second. The probability that a flagged email is spam is therefore not a number that belongs to the filter. It also depends on how much spam arrives.

Counting 240 emails at each inbox shows the difference. At the first inbox, 24 are spam and 18 of them are flagged, and 216 are not spam and 36 of them are flagged. So 18 of 54 flagged emails are spam. At the second inbox, 60 are spam and 45 of them are flagged, and 180 are not spam and 30 of them are flagged. So 45 of 75 flagged emails are spam.

The figure shows the 240 emails of the second inbox.

The share given "flagged", 45/75=3/545/75 = 3/5, is the answer. The share given "spam", 45/60=3/445/60 = 3/4, is the number that belongs to the filter.

Answer:

3/53/5

Problem 4

A search that finds nothing

A set of keys is lost. The keys are in a coat with probability 1/21/2, in a bag with probability 3/103/10, and in a car with probability 1/51/5.

We search the coat. If the keys are in the coat, a search of the coat finds them with probability 4/54/5. If they are somewhere else, it finds nothing. The search finds nothing.

Make a table with a row for each place and four columns: the prior probability, the probability that the search of the coat finds nothing if the keys are in that place, their product, and the posterior probability. Write the total under the first, third, and fourth columns.

Find the probability that the keys are in each place, given that the search found nothing, as exact fractions.

Solution

Step 1: Find the probability of the observation in each case

The observation is that the search of the coat found nothing. If the keys are in the coat, the search misses them with probability 1−4/5=1/51 - 4/5 = 1/5. If the keys are in the bag or in the car, the search of the coat is certain to find nothing, so the probability is 1.

Step 2: Multiply and add

Prior Finds nothing Product Posterior
Coat 1/21/2 1/51/5 1/101/10 1/61/6
Bag 3/103/10 11 3/103/10 3/6=1/23/6 = 1/2
Car 1/51/5 11 2/102/10 2/6=1/32/6 = 1/3
Total 11 6/106/10 11

The products add to 6/106/10. By the law of total probability, this is the probability that the search finds nothing.

Step 3: Divide each product by the total

Bayes' rule divides each product by 6/106/10, which gives 1/61/6 for the coat, 1/21/2 for the bag, and 1/31/3 for the car.

Step 4: What the search changed

The coat fell from 1/21/2 to 1/61/6. It did not fall to 0, because a search misses the keys 1 time in 5.

The bag and the car both rose, from 3/103/10 to 1/21/2 and from 1/51/5 to 1/31/3, although nobody looked in either. The three probabilities add to 1, so what the coat lost went to the other two. Their ratio is 3 to 2 before and after, because the search of the coat is equally likely to find nothing whether the keys are in the bag or in the car.

Answer:

1/61/6, 1/21/2, and 1/31/3

Problem 5

A second positive test

In the lesson on conditional probability, a condition affects 1 person in 100. Of the people who have it, 9 in 10 test positive, and of the people who do not, 1 in 10 test positive. A person tests positive, and so has the condition with probability 1/121/12.

This person takes the test again. The result of each test depends only on whether the person has the condition, and not on the results of the earlier tests.

Use the probability after one test as the prior probability for the next test. Find each probability as an exact fraction.

  1. The second test is also positive. Find the probability that the person has the condition, given two positive tests.
  2. A third test is also positive. Find the probability that the person has the condition, given three positive tests.
Solution

Step 1: Start from the answer after one test

Let DD be the event that the person has the condition. After one positive test, the probability of DD is 1/121/12 and the probability of DcD^c is 11/1211/12. These are the prior probabilities for the second test.

Step 2: Apply Bayes' rule for the second test

A second positive test has probability 9/109/10 if the person has the condition and 1/101/10 if not, whatever the first test showed. The two products are

112⋅910=9120and1112⋅110=11120.\frac{1}{12} \cdot \frac{9}{10} = \frac{9}{120} \qquad\text{and}\qquad \frac{11}{12} \cdot \frac{1}{10} = \frac{11}{120}.

They add to 20/12020/120, so the probability that the person has the condition, given two positive tests, is

9/12020/120=920.\frac{9/120}{20/120} = \frac{9}{20}.

Step 3: Do the same for the third test

The prior probabilities are now 9/209/20 and 11/2011/20. The two products are 920⋅910=81200\frac{9}{20} \cdot \frac{9}{10} = \frac{81}{200} and 1120⋅110=11200\frac{11}{20} \cdot \frac{1}{10} = \frac{11}{200}, which add to 92/20092/200. So the probability, given three positive tests, is

81/20092/200=8192.\frac{81/200}{92/200} = \frac{81}{92}.

Step 4: Check by counting

Take 100,000 people. The table follows the people who are positive on every test so far.

Start One positive Two positive Three positive
Have the condition 1,000 900 810 729
Do not 99,000 9,900 990 99

After two tests, 810 of 810+990=1800810 + 990 = 1800 people have the condition, which is 9/209/20. After three tests, 729 of 729+99=828729 + 99 = 828 have it, which is 81/9281/92.

Step 5: What each test did

The probability went from 1/1001/100 to 1/121/12, then 9/209/20, then 81/9281/92: about 1%, 8%, 45%, and 88%. Each positive test keeps 9 in 10 of the people who have the condition and 1 in 10 of the people who do not. One positive test is weak evidence here because so few people have the condition at the start. Three positive tests are strong evidence.

Answer:

9/209/20 and 81/9281/92

Problem 6

Three doors

A car is behind one of three doors, and each door is equally likely. There is a goat behind each of the other two. We choose door 1. The host then opens door 2 or door 3.

For each host below, imagine 18 games, with the car behind each door in 6 of them. When a host opens a door with probability 1/21/2, count half of the games. Make a table with a row for each place the car can be.

  1. The host knows where the car is and always opens a door with a goat behind it. If doors 2 and 3 both have goats, the host opens each with probability 1/21/2. Count the games in which this host opens door 3. Find the probability that the car is behind door 2, given that the host opens door 3.
  2. A different host does not know where the car is, and opens door 2 or door 3 with probability 1/21/2 each. Count the games in which this host opens door 3 and there is a goat behind it. Find the probability that the car is behind door 2, given that the host opens door 3 and there is a goat behind it.
Solution

Step 1: Count the games for the host who knows

Car behind Games Host opens door 3
Door 1 6 3
Door 2 6 6
Door 3 6 0
Total 18 9

If the car is behind door 1, the host may open either door, and opens door 3 in half of the 6 games. If the car is behind door 2, the host must open door 3. If the car is behind door 3, the host never opens it.

The host opens door 3 in 9 games, and the car is behind door 2 in 6 of them. The probability is 6/9=2/36/9 = 2/3.

Step 2: Write the same count with Bayes' rule

The three cases are where the car is, each with prior probability 1/31/3. The observation is that the host opens door 3, which has probability 1/21/2, 11, and 00 in the three cases. The products are 1/61/6, 2/62/6, and 00, and they add to 3/63/6. So the posterior probabilities are 1/31/3, 2/32/3, and 00.

Step 3: Count the games for the host who does not know

Car behind Games Host opens door 3 and shows a goat
Door 1 6 3
Door 2 6 3
Door 3 6 0
Total 18 6

This host opens door 3 in half of the games, wherever the car is. If the car is behind door 3, opening it shows the car and not a goat, so those games do not count.

The host opens door 3 and shows a goat in 6 games, and the car is behind door 2 in 3 of them. The probability is 3/6=1/23/6 = 1/2.

Step 4: Compare the two hosts

We saw the same thing both times: door 3 open, with a goat behind it. The prior probabilities are also the same. The answers differ because the probability of what we saw, given that the car is behind door 2, differs. The first host had to open door 3 in that case, so the probability is 1. The second host opened it by chance, so the probability is 1/21/2.

Bayes' rule needs the probability of the observation in every case. What was observed is not enough to find it. We also have to know how the observation came about.

Answer:

2/32/3 and 1/21/2