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.
Machine 1 makes 3 parts in 5, and 1 in 15 of its parts is defective.
Machine 2 makes 3 parts in 10, and 1 in 10 of its parts is defective.
Machine 3 makes 1 part in 10, and 1 in 5 of its parts is defective.
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/9. For
machine 2 it is 30/90=1/3, and for machine 3 it is 20/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/9 against 4/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 M1, M2, and M3 be the events that
machine 1, machine 2, and machine 3 made it, and let D be the event that
it is defective. We want P(M3∣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(D)P(M3∩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)=101⋅51=1002.
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:
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/9 and
P(M2∣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,…,An form a partition when every
outcome is in exactly one of them (every part is made by exactly one
machine). Let B be any event (the part is defective).
The sum on the bottom is the law of total probability:
where Ai is any one of the cases. Both rules hold when every
P(Ai)>0, and (2) also needs P(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/5
4/9
Machine 2
3/10
1/3
Machine 3
1/10
2/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/5 against 9/100, so
its probability rose from 1/10 to 2/9. For machine 1 it is 1/15, which
is less than 9/100, so its probability fell from 3/5 to 4/9.
Machine 3 rose, but it started from 1/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/2,
cloudy with probability 3/10, and rainy with probability 1/5. An ice
cream stand sells out with probability 3/5 on a sunny day, 1/5 on a
cloudy day, and 1/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 S, C, and R be the events that the day is sunny, cloudy, and
rainy, and let O be the event that the stand sells out. Every day has
exactly one kind of weather, so S, C, and R 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
21⋅53=5015,103⋅51=503,and51⋅101=501.
Step 3: Add the three paths
No day follows two paths, so the law of total probability, (1) from the
lesson, gives
The average of the three probabilities of selling out is
31(53+51+101)=5015.
This would be the answer only if the three kinds of weather were equally
likely. Half of the days are sunny, so 3/5 counts for more than the other
two, and the answer, 19/50, is greater than 15/50.
Answer:
19/50
Problem 2
Three bakers
A bakery has three bakers.
Baker 1 bakes 1/2 of the loaves and burns 1 in 20 of them.
Baker 2 bakes 1/3 of the loaves and burns 1 in 10 of them.
Baker 3 bakes 1/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/120,
4/120, and 5/120.
Step 2: Add the products
The products add to 12/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/2
1/20
3/120
3/12=1/4
Baker 2
1/3
1/10
4/120
4/12=1/3
Baker 3
1/6
1/4
5/120
5/12
Total
1
12/120
1
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/6 to 5/12) and baker 1 less
likely (1/2 to 1/4). It left baker 2 at 1/3.
To see why, write Bayes' rule for one baker as a product:
P(baker∣burnt)=P(baker)×P(burnt)P(burnt∣baker).
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/101/4=5/2, and for baker 1 it is 1/101/20=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/4, 1/3, and 5/12
Problem 3
The same filter at another inbox
The filter in the problem "A flagged email" flags a spam email with
probability 3/4 and an email that is not spam with probability 1/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/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 S be the event that the email is spam and F the event that it is
flagged. The filter is the same, so P(F∣S)=3/4 and
P(F∣Sc)=1/6 as before. Only the prior probability changed:
P(S)=1/4 and P(Sc)=3/4.
Step 2: Add the paths that end in a flag
By the law of total probability,
P(F)=41⋅43+43⋅61=163+162=165.
Step 3: Divide
Bayes' rule gives
P(S∣F)=P(F)P(S)P(F∣S)=5/163/16=53.
Step 4: Compare the two inboxes
The same filter gives 1/3 at the first inbox and 3/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/5, is the answer. The share given
"spam", 45/60=3/4, is the number that belongs to the filter.
Answer:
3/5
Problem 4
A search that finds nothing
A set of keys is lost. The keys are in a coat with probability 1/2, in a
bag with probability 3/10, and in a car with probability 1/5.
We search the coat. If the keys are in the coat, a search of the coat finds
them with probability 4/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/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/2
1/5
1/10
1/6
Bag
3/10
1
3/10
3/6=1/2
Car
1/5
1
2/10
2/6=1/3
Total
1
6/10
1
The products add to 6/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/10, which gives 1/6 for the coat,
1/2 for the bag, and 1/3 for the car.
Step 4: What the search changed
The coat fell from 1/2 to 1/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/10 to 1/2 and from 1/5 to 1/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/6, 1/2, and 1/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/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.
The second test is also positive. Find the probability that the person
has the condition, given two positive tests.
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 D be the event that the person has the condition. After one positive
test, the probability of D is 1/12 and the probability of Dc is
11/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/10 if the person has the
condition and 1/10 if not, whatever the first test showed. The two
products are
121⋅109=1209and1211⋅101=12011.
They add to 20/120, so the probability that the person has the condition,
given two positive tests, is
20/1209/120=209.
Step 3: Do the same for the third test
The prior probabilities are now 9/20 and 11/20. The two products are
209⋅109=20081 and
2011⋅101=20011, which add to
92/200. So the probability, given three positive tests, is
92/20081/200=9281.
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=1800 people have the condition, which
is 9/20. After three tests, 729 of 729+99=828 have it, which is
81/92.
Step 5: What each test did
The probability went from 1/100 to 1/12, then 9/20, then 81/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/20 and 81/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/2, count half of the
games. Make a table with a row for each place the car can be.
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/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.
A different host does not know where the car is, and opens door 2 or
door 3 with probability 1/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/3.
Step 2: Write the same count with Bayes' rule
The three cases are where the car is, each with prior probability 1/3.
The observation is that the host opens door 3, which has probability 1/2,
1, and 0 in the three cases. The products are 1/6, 2/6, and 0, and
they add to 3/6. So the posterior probabilities are 1/3, 2/3, and 0.
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/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/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.