Bayes' rule
A check that is almost always right raises an alarm. How likely is it that the alarm is real, and why can the answer be far smaller than the check’s own success rate?
Where you are 3 in Conditional probability
When you reach for it
Information often arrives the wrong way round. A factory knows how its inspection camera behaves on a faulty circuit board and on a good one: it knows P(\text{flagged} \mid \text{faulty}) and P(\text{flagged} \mid \text{good}). The person holding a flagged board wants the other direction, P(\text{faulty} \mid \text{flagged}): given what the camera said, what is the board really like?
These two conditional probabilities are different numbers, and they can be wildly different. Bayes’ rule is the move that turns one into the other. Reach for it whenever
- the situation splits into cases (faulty or good, which supplier, which coin), and you know the chance of each case,
- you know how likely some piece of evidence is within each case, and
- the evidence has now been seen, and you want the chance of a case given the evidence.
The question “given the result, what was the cause?” is the signal. It covers rare faults and the false alarms they bring with them, reading back from an effect to the supplier that produced it, and updating a judgment when a second piece of evidence arrives.
The procedure
The method rests on three things already known: the formula for a conditional probability, the general multiplication rule and the law of total probability. Nothing new is assumed; Bayes’ rule is these three used in the right order.
Draw the tree in the direction the information comes. The first fan of the tree is the partition into cases, labeled with their chances P(B_i). The second fan, from each case, is the evidence, labeled with the chances given that case, P(A \mid B_i). This is the order the numbers are known in, so every branch gets a number that was actually given. The branch you want, a case given the evidence, appears nowhere on this tree, which is exactly why a calculation is needed.
Multiply along each path. The general multiplication rule says P(B_i \text{ and } A) = P(B_i)\,P(A \mid B_i), so the product along a path is the probability of that whole path. The paths do not overlap and together they cover everything, so their products add to 1.
Add the paths that match the evidence. Once the evidence A is seen, only the paths ending in A are still possible. Their products add to P(A), by the law of total probability. This total is the size of the world that is left.
Divide the wanted path by that total. The formula for a conditional probability says \displaystyle P(B_k \mid A) = \frac{P(B_k \text{ and } A)}{P(A)}. The top is the one path through B_k that ends in A; the bottom is the total just found. So the answer is the wanted path’s share of all the paths that match the evidence.
With two cases, B and “not B”, this is
where
With a partition B_1, B_2, \ldots, B_n into any number of cases,
That is Bayes’ rule. On the tree it reads: the wanted path, divided by the sum of all the paths ending in the evidence.
The same move with counts. Probabilities can be traded for counts by imagining a round number of items, say 10{,}000, and splitting them along the tree. Each branch takes its share of the items that reach it, so the counts at the leaves are the path products times 10{,}000. The answer is then “how many of the items showing the evidence are in the wanted case”, a plain fraction of two whole numbers. Counts and probabilities give the same answer, because the 10{,}000 cancels top and bottom.
Worked examples
A rare fault and a sensitive camera
A factory makes circuit boards, and 1\% of them are faulty. An inspection camera flags 95\% of the faulty boards. It also flags 8\% of the good boards, because a speck of dust can look like a bad joint. A board is chosen at random and the camera flags it. Find the probability that it is faulty. Then find the probability that a board the camera does not flag is faulty.
SolutionThe cases are faulty and good, with P(\text{faulty}) = 0.01 and P(\text{good}) = 0.99. The camera’s rates are shares of one kind of board, so they are conditional probabilities: P(\text{flagged} \mid \text{faulty}) = 0.95 and P(\text{flagged} \mid \text{good}) = 0.08. The complements fill in the other two branches, 0.05 and 0.92. Figure 1 shows the tree, with each path’s product at its end.
Two paths end in “flagged”:
They add to P(\text{flagged}) = 0.0887, by the law of total probability. The wanted path is the faulty one, so
A flagged board is faulty only about 11\% of the time, even though the camera catches 95\% of faulty boards. The reason is on the tree: the good boards are so many that 8\% of them, 0.0792, is more than eight times the faulty boards’ whole contribution, 0.0095. Most flags are false alarms.
For a board that is not flagged, the two matching paths are 0.01 \times 0.05 = 0.0005 and 0.99 \times 0.92 = 0.9108, adding to 0.9113. So
A board the camera passes is very likely good. The camera is far better at clearing boards than at condemning them.
The same camera with ten thousand boards
Redo Example 1 with counts. Picture 10{,}000 boards, fill in a two-way table of faulty or good against flagged or not, and read off both answers.
SolutionOf 10{,}000 boards, 1\% are faulty: 100 boards, leaving 9900 good. The camera flags 95\% of the faulty ones, 95 boards, and misses 5. It flags 8\% of the good ones, 0.08 \times 9900 = 792 boards, and passes the other 9108.
| Flagged | Not flagged | Total | |
|---|---|---|---|
| Faulty | 95 | 5 | 100 |
| Good | 792 | 9108 | 9900 |
| Total | 887 | 9113 | 10{,}000 |
Every flagged board is in the Flagged column, which holds 887 boards. Of these, 95 are faulty, so
the same as before, as it must be: \displaystyle \frac{95}{887} is \displaystyle \frac{0.0095}{0.0887} with top and bottom multiplied by 10{,}000. The Not flagged column holds 9113 boards, of which 5 are faulty, so \displaystyle P(\text{faulty} \mid \text{not flagged}) = \frac{5}{9113} \approx 0.00055.
The table makes the false alarms easy to see. Reading along the Faulty row gives \displaystyle \frac{95}{100}, the camera’s success on faulty boards. Reading down the Flagged column gives \displaystyle \frac{95}{887}. The two fractions share a top, 95, but the column also holds 792 good boards, so its bottom is far bigger. Reversing the condition changes which total you divide by. This is the conditional relative frequency of a two-way table, read down a column instead of along a row.
Which bakery sent the stale loaf
In the law of total probability, a canteen takes 50\% of its loaves from the Hill bakery, 30\% from Mill and 20\% from Dock, and 2\%, 3\% and 5\% of their loaves are stale. That gave P(\text{stale}) = 0.029. A loaf turns out to be stale. Find the probability that each bakery sent it.
SolutionThe stale paths are 0.5 \times 0.02 = 0.010 through Hill, 0.3 \times 0.03 = 0.009 through Mill and 0.2 \times 0.05 = 0.010 through Dock, adding to 0.029. Each bakery’s chance, given stale, is its path’s share of that total:
The three add to \displaystyle \frac{29}{29} = 1, which is a check: a stale loaf came from exactly one bakery. Learning that the loaf is stale has moved the chances. Dock supplies only 20\% of the bread but sends about 34.5\% of the stale loaves, because its stale rate is the highest; Hill falls from 50\% to about 34.5\%.
A second inspection after a first flag
Every board the camera in Example 1 flags is sent to an X-ray check. The X-ray flags 90\% of faulty boards and 5\% of good ones, and it works independently of the camera: for a board of either kind, whether the X-ray flags it does not depend on what the camera did. A board is flagged by both. Find the probability that it is faulty. What if the X-ray clears it instead?
SolutionAfter the camera’s flag, the board is no longer a random board. Example 1 found its chances: faulty with probability \displaystyle \frac{95}{887} and good with probability \displaystyle \frac{792}{887}. These become the first fan of a new tree, and the X-ray’s rates are its second fan. The two paths ending in an X-ray flag are
The 887 cancels in the division, so
The second flag has lifted the chance from about 0.107 to about 0.683. One test leaves a flagged board probably good; two agreeing tests make it probably faulty.
Check in one step. The same answer comes from a single tree with three fans: faulty or good, then the camera, then the X-ray. The path “faulty, camera flags, X-ray flags” has probability 0.01 \times 0.95 \times 0.9 = 0.00855, and “good, camera flags, X-ray flags” has 0.99 \times 0.08 \times 0.05 = 0.00396. These add to 0.01251, so the answer is \displaystyle \frac{0.00855}{0.01251}, which is again \displaystyle \frac{95}{139}. Updating twice in a row is the same as updating once on both pieces of evidence.
If the X-ray clears the board, the matching paths use 0.1 and 0.95:
Here the 887 has been canceled already, and “X-ray clears” means a board the camera flagged and the X-ray then cleared.
The camera’s flag raised the chance from 0.01 to about 0.107, and the X-ray’s clearance brings it back down to about 0.012.
How rare is rare
Keep the camera of Example 1, flagging 95\% of faulty boards and 8\% of good ones, but let the share of faulty boards be p. Find P(\text{faulty} \mid \text{flagged}) as a formula in p, work it out for p = 0.001, 0.2 and 0.5, and find the value of p that makes a flagged board equally likely to be faulty or good.
SolutionThe faulty path is 0.95p and the good path through a flag is 0.08(1 - p), so by Bayes’ rule
Putting in the values, together with p = 0.01 from Example 1:
| Share faulty p | P(\text{faulty} \mid \text{flagged}) |
|---|---|
| 0.001 | 0.012 |
| 0.01 | 0.107 |
| 0.2 | 0.748 |
| 0.5 | 0.922 |
For p = 0.2, for example, the fraction is \displaystyle \frac{0.19}{0.19 + 0.064} = \frac{0.19}{0.254}, about 0.748.
The camera is the same in every row; only the share of faulty boards changes, and the meaning of a flag changes with it. For a flagged board to be faulty with probability \displaystyle \frac{1}{2}, the two paths must be equal:
so 0.95p + 0.08p = 0.08, that is 1.03p = 0.08, and \displaystyle p = \frac{0.08}{1.03} = \frac{8}{103}, about 0.078. Below about 7.8\% faulty, a flagged board is more likely good than faulty. The chance of the case before any evidence, often called the base rate, is as much a part of the answer as the test’s own rates.
A coin that may have two heads
A drawer holds three coins that look the same. Two are fair; the third has heads on both sides. A coin is taken at random and tossed twice, and it shows heads both times. Find the probability that it is the two-headed coin. It is tossed a third time and shows heads again; find the probability now.
SolutionThe cases are fair, with probability \displaystyle \frac{2}{3}, and two-headed, with probability \displaystyle \frac{1}{3}. A fair coin shows two heads with probability \displaystyle \frac{1}{2} \times \frac{1}{2} = \frac{1}{4}, by the multiplication rule for its independent tosses; the two-headed coin always does. The paths ending in two heads are \displaystyle \frac{1}{3} \times 1 = \frac{1}{3} and \displaystyle \frac{2}{3} \times \frac{1}{4} = \frac{1}{6}, so
For three heads in a row, a fair coin’s chance is \displaystyle \frac{1}{8}, so the fair path is \displaystyle \frac{2}{3} \times \frac{1}{8} = \frac{1}{12} and
Each head makes the two-headed coin more likely, but never certain: a fair coin can show any number of heads in a row. A single tail, on the other hand, settles it at once. The two-headed path has probability 0 for a tail, so P(\text{two-headed} \mid \text{a tail}) has a top of 0 and is 0.
Boundaries
The evidence must be possible. The division is by P(A), so the rule needs P(A) > 0. If no path ends in the evidence, there is nothing to condition on.
The cases must form a partition. As in the law of total probability, the cases must not overlap and must cover every outcome, or the bottom of the fraction counts some paths twice or leaves some out. The checks are that the case chances add to 1 and that the answers P(B_i \mid A) add to 1 as well.
The base rate is needed. The rates P(A \mid B_i) alone cannot give P(B_i \mid A). The camera’s 95\% and 8\% gave answers from 0.012 to 0.922 in Example 5, depending on the share of faulty boards. Without P(B_i), the reverse probability is not determined.
The base rate must fit the item. The 1\% in Example 1 is the share of faulty boards among boards chosen at random. A board picked out because it smells burnt is not a random board, and its chance of being faulty before the camera looks is no longer 0.01, so the answer 0.107 does not apply to it.
Updating twice needs independence given the case. Example 4 multiplied the X-ray’s rates onto the camera’s paths, which is right only if, for a board of each kind, the X-ray’s result does not depend on the camera’s. Two runs of the same camera that misread the same speck of dust are not independent in this way, and treating them as two separate tests overstates how much the second flag tells you.
Where this goes next
Bayes’ rule is the turning point of conditional probability: it is how a chance known one way round is read the other way. Its denominator is the law of total probability, and its division is the formula for a conditional probability, so the three belong together.
The habit it teaches, starting from a base rate and updating it as evidence arrives, as in Example 4, is the foundation of a whole way of doing statistics, called Bayesian statistics, in which every unknown carries a probability that each new observation adjusts. The same reversal sits behind judging any detector or screening check: how often it is right about the items that have the condition is one question, and how often an alarm is real is another, and only the base rate links them. When a test result turns out to change nothing, as in Exercise 18, the evidence and the case are independent events.
Exercises
Reversing on a tree
- P(B) = 0.3, P(A \mid B) = 0.6 and P(A \mid \text{not } B) = 0.2. Find P(B \mid A).
- With the numbers of Exercise 1, find P(B \mid \text{not } A).
- A second-hand bookshop’s stock is 70\% paperbacks and 30\% hardbacks. Of the paperbacks, 10\% have a torn page; of the hardbacks, 4\%. A book picked at random has a torn page. Find the probability that it is a hardback.
- On 20\% of nights a fox visits a garden. A motion-sensitive camera in the garden is set off on 80\% of the nights the fox visits and, by wind and cats, on 15\% of the nights it does not. Find the probability that the fox visited on a night the camera was set off, and on a night it was not.
Three or more cases
- A phone repair shop buys 50\% of its replacement screens from supplier A, 30\% from B and 20\% from C. Of the screens from A, 1\% are faulty; from B, 2\%; from C, 3\%. A screen is faulty. Find the probability that it came from each supplier.
- Bag 1 holds 2 red and 3 blue counters, bag 2 holds 4 red and 1 blue, and bag 3 holds 5 red. A fair die is rolled: on 1, 2 or 3 a counter is drawn from bag 1, on 4 or 5 from bag 2, and on 6 from bag 3. The counter is red. Find the probability that it came from bag 2.
- P(A) = 0.25, P(B) = 0.4 and P(A \mid B) = 0.5. Find P(B \mid A) and P(A \mid \text{not } B).
Rare events and false alarms
- Of the payments made with a bank’s cards, 0.2\% are fraudulent. The bank’s fraud check flags 99\% of fraudulent payments and 2\% of genuine ones. Find the probability that a flagged payment is fraudulent.
- A kit tests well water for a contaminant. Of the wells in a region, 5\% are contaminated. The kit gives a positive result for 90\% of contaminated wells and for 10\% of clean ones. Picture 1000 wells: make a two-way table of contaminated or clean against positive or negative, and use it to find P(\text{contaminated} \mid \text{positive}) and P(\text{clean} \mid \text{negative}).
- A well from Exercise 9 tests positive and is tested again with a second kit of the same kind, whose result, for a well of each kind, is independent of the first. The second result is also positive. Find the probability that the well is contaminated.
- For the kit of Exercise 9, what share of wells would have to be contaminated for a positive result to mean a \displaystyle \frac{1}{2} chance of contamination?
Fractions and repeated evidence
- A drawer holds four coins: three fair and one with heads on both sides. A coin is taken at random and tossed three times, showing heads each time. Find the probability that it is the two-headed coin.
- Box A holds 6 bulbs, 2 of which do not work. Box B holds 4 bulbs, 1 of which does not work. A fair coin chooses a box and a bulb is taken from it at random. The bulb does not work. Find the probability that it came from box A.
- On a test where every question has 4 options, a student knows the answer to 60\% of the questions and picks an option at random on the rest. A question was answered correctly. Find the probability that the student knew the answer.
Reasoning
- Use the table of Example 2 to explain, in terms of the flagged boards, why P(\text{faulty} \mid \text{flagged}) is so much smaller than P(\text{flagged} \mid \text{faulty}).
- Show that P(A)\,P(B \mid A) = P(B)\,P(A \mid B) whenever P(A) > 0 and P(B) > 0, and use it to derive the two-case form of Bayes’ rule.
- A check never flags an item without the condition: P(A \mid \text{not } B) = 0, while P(A \mid B) > 0 and P(B) > 0. Show that P(B \mid A) = 1, whatever the base rate, and say what this looks like on the tree.
- Show that if P(A \mid B) = P(A \mid \text{not } B), with 0 < P(B) < 1, then P(B \mid A) = P(B). Explain what this says about a check that flags both kinds of item at the same rate.
- In Example 4, explain why the answer would be too high if the “second inspection” were just the same camera photographing the board again, so that a speck of dust that fooled it the first time fools it every time.
Answers
- 0.5625. The path through B to A is 0.3 \times 0.6 = 0.18, the other is 0.7 \times 0.2 = 0.14, so P(A) = 0.32 and \displaystyle P(B \mid A) = \frac{0.18}{0.32} = 0.5625.
- \displaystyle \frac{3}{17} \approx 0.176. The paths ending in “not A” are 0.3 \times 0.4 = 0.12 and 0.7 \times 0.8 = 0.56, adding to 0.68, so \displaystyle P(B \mid \text{not } A) = \frac{0.12}{0.68} = \frac{3}{17}.
- \displaystyle \frac{6}{41} \approx 0.146. The torn paths are 0.7 \times 0.1 = 0.07 and 0.3 \times 0.04 = 0.012, adding to 0.082, so \displaystyle P(\text{hardback} \mid \text{torn}) = \frac{0.012}{0.082} = \frac{6}{41}.
- Set off: \displaystyle \frac{4}{7} \approx 0.571; not set off: \displaystyle \frac{1}{18} \approx 0.056. The set-off paths are 0.2 \times 0.8 = 0.16 and 0.8 \times 0.15 = 0.12, so \displaystyle \frac{0.16}{0.28} = \frac{4}{7}. The other paths are 0.2 \times 0.2 = 0.04 and 0.8 \times 0.85 = 0.68, so \displaystyle \frac{0.04}{0.72} = \frac{1}{18}.
- A: \displaystyle \frac{5}{17} \approx 0.294; B: \displaystyle \frac{6}{17} \approx 0.353; C: \displaystyle \frac{6}{17} \approx 0.353. The faulty paths are 0.005, 0.006 and 0.006, adding to 0.017; each supplier’s answer is its path over 0.017, and \displaystyle \frac{5 + 6 + 6}{17} = 1.
- \displaystyle \frac{8}{19}. The red paths are \displaystyle \frac{1}{2} \times \frac{2}{5} = \frac{6}{30}, \displaystyle \frac{1}{3} \times \frac{4}{5} = \frac{8}{30} and \displaystyle \frac{1}{6} \times 1 = \frac{5}{30}, adding to \displaystyle \frac{19}{30}, so the answer is \displaystyle \frac{8}{30} \div \frac{19}{30} = \frac{8}{19}.
- P(B \mid A) = 0.8 and \displaystyle P(A \mid \text{not } B) = \frac{1}{12} \approx 0.083. Here P(A \text{ and } B) = 0.4 \times 0.5 = 0.2, so \displaystyle P(B \mid A) = \frac{0.2}{0.25} = 0.8. Then P(A \text{ and not } B) = 0.25 - 0.2 = 0.05 and P(\text{not } B) = 0.6, so \displaystyle P(A \mid \text{not } B) = \frac{0.05}{0.6} = \frac{1}{12}.
- \displaystyle \frac{99}{1097} \approx 0.090. The flagged paths are 0.002 \times 0.99 = 0.00198 and 0.998 \times 0.02 = 0.01996, adding to 0.02194, so the answer is \displaystyle \frac{0.00198}{0.02194} = \frac{99}{1097}. About 9\% of flagged payments are fraudulent.
- Contaminated given positive: \displaystyle \frac{9}{28} \approx 0.321. Clean given negative: \displaystyle \frac{171}{172} \approx 0.994. Of 1000 wells, 50 are contaminated (45 positive, 5 negative) and 950 clean (95 positive, 855 negative). The Positive column holds 140 wells and the Negative column 860, so the answers are \displaystyle \frac{45}{140} = \frac{9}{28} and \displaystyle \frac{855}{860} = \frac{171}{172}. As a table, the rows Contaminated and Clean read 45, 5, 50 and 95, 855, 950 across Positive, Negative and Total, and the Total row reads 140, 860, 1000.
- 0.81. After the first positive, the well is contaminated with probability \displaystyle \frac{45}{140} and clean with probability \displaystyle \frac{95}{140}. The second positive gives paths 45 \times 0.9 = 40.5 and 95 \times 0.1 = 9.5 over 140, so the answer is \displaystyle \frac{40.5}{40.5 + 9.5} = \frac{40.5}{50} = 0.81.
- 10\%. The two positive paths must be equal: 0.9p = 0.1(1 - p), so p = 0.1.
- \displaystyle \frac{8}{11} \approx 0.727. The paths ending in three heads are \displaystyle \frac{1}{4} \times 1 = \frac{8}{32} and \displaystyle \frac{3}{4} \times \frac{1}{8} = \frac{3}{32}, so the answer is \displaystyle \frac{8}{32} \div \frac{11}{32} = \frac{8}{11}.
- \displaystyle \frac{4}{7}. The paths are \displaystyle \frac{1}{2} \times \frac{2}{6} = \frac{1}{6} through box A and \displaystyle \frac{1}{2} \times \frac{1}{4} = \frac{1}{8} through box B. In twenty-fourths these are \displaystyle \frac{4}{24} and \displaystyle \frac{3}{24}, so the answer is \displaystyle \frac{4}{4 + 3} = \frac{4}{7}.
- \displaystyle \frac{6}{7} \approx 0.857. Knowing and correct has probability 0.6 \times 1 = 0.6; guessing and correct has 0.4 \times 0.25 = 0.1. So \displaystyle P(\text{knew} \mid \text{correct}) = \frac{0.6}{0.7} = \frac{6}{7}.
- P(\text{flagged} \mid \text{faulty}) looks only at the 100 faulty boards and asks how many are flagged: 95. P(\text{faulty} \mid \text{flagged}) looks at all 887 flagged boards and asks how many are faulty: still 95. The tops are the same, but the flagged boards also include 792 good ones, because 8\% of a group of 9900 is far more than 95\% of a group of 100. The false alarms swamp the true ones, so the second fraction is much smaller.
- By the formula for a conditional probability, P(A)\,P(B \mid A) = P(A \text{ and } B) and P(B)\,P(A \mid B) = P(B \text{ and } A), and these are the same event, so the two products are equal. Dividing by P(A) gives \displaystyle P(B \mid A) = \frac{P(B)\,P(A \mid B)}{P(A)}. The law of total probability with the partition B, “not B” gives that P(A) is P(B)\,P(A \mid B) plus P(\text{not } B)\,P(A \mid \text{not } B); putting this in the bottom gives the two-case form.
- The path “not B, then A” has probability P(\text{not } B) \times 0 = 0, so the only path to A with any probability is the one through B. Then P(A) = P(B)\,P(A \mid B) + 0, which is exactly the top of Bayes’ rule, so P(B \mid A) = 1. On the tree, every path that ends in a flag runs through B, so a flag can only have come from B, however rare B is.
- Write r for the common rate. Then P(A) = P(B)\,r + P(\text{not } B)\,r = r, so \displaystyle P(B \mid A) = \frac{P(B)\,r}{r} = P(B) (here r > 0, as P(A) must be positive). A check that flags both kinds of item at the same rate tells you nothing: after a flag, the chance of the condition is exactly what it was before.
- Example 4 multiplied by 0.05 for the chance that a good board, already flagged by the camera, is flagged again. That is right only if the second check knows nothing of what fooled the first. If the same camera looks again at the same speck of dust, a good board it flagged once will very likely be flagged again, so the good path through two flags is much larger than \displaystyle \frac{792}{887} \times 0.05. A larger good path means a larger bottom and a smaller answer, so 0.683 would overstate the chance that the board is faulty.