Step 1 of 3 33%
Step 1 of 3 · Bayes' Rule

From the table to Bayes' Rule

Recap of where we are

You are an analyst for a city water department. A predictive model flags homes likely to be connected by a lead service line, and this morning it flagged 1204 Ferndale Street. Before the crew drives out, you want to know how likely it is that this home actually has a lead pipe.

Three numbers were established earlier in the exercise:

  • Base rate. 40% of homes in the survey area have a lead line. P(Lead) = 0.40
  • Sensitivity. Of the homes that have lead, 90% get flagged. P(+ | Lead) = 0.90
  • False positive rate. Of the homes with no lead, 20% still get flagged. P(+ | No Lead) = 0.20

Applied to an imagined 1,000 homes, those three numbers fill in a table. Knowing 1204 Ferndale was flagged puts it in the blue row and throws away the rest of the table: the 520 cleared homes are no longer relevant.

Has lead line No lead line Row total
Model flags it (+) 360 120 480
Model clears it (−) 40 480 520
Column total 400 600 1,000
P(Lead | flagged), read off the table
75%
360 homes with lead out of the 480 homes that get flagged

The same answer, from the formula

The table works, but only because we invented a convenient population of 1,000 homes. Bayes' Rule does the identical arithmetic directly on the probabilities, with no table required.

Bayes' Rule, in general
P(A | B) =
P(B | A) × P(A)
P(B | A) × P(A)  +  P(B | Not A) × P(Not A)

Written for our problem, with A = the home has lead and B = the model flags it:

P(Lead | +) =
P(+ | Lead) × P(Lead)
P(+ | Lead) × P(Lead)  +  P(+ | No Lead) × P(No Lead)

Now fill in the four inputs

Enter each value as a decimal, so 90% is entered as 0.9. Three of the four were given at the top of this page, repeated here so you do not have to scroll back:

Base rate P(Lead) 0.40
Sensitivity P(+ | Lead) 0.90
False positive rate P(+ | No Lead) 0.20
P(Lead | +) =
P(+ | Lead) × P(Lead)
P(+ | Lead) × P(Lead)  +  P(+ | No Lead) × P(No Lead)
%
Worth memorizing

The denominator of Bayes' Rule is always just every way the evidence could have happened: the true positives plus the false positives. So the whole formula can be read as

P(Lead | +) = True Positives / (True Positives + False Positives)

Step 2 of 3 · The formula, drawn to scale

Seeing the 1,000 homes

On the previous page you filled in Bayes' Rule and got 75%. The table of 1,000 homes gave 75% as well. They agree because they are the same calculation. This page shows you why, by drawing it.

The formula you just used
P(Lead | +) =
P(+ | Lead) × P(Lead)
P(+ | Lead) × P(Lead) + P(+ | No Lead) × P(No Lead)
Two coloured terms, and that is the whole formula. Everything below is those two terms, drawn as areas.
What to watch for

Bayes' Rule does three things: it multiplies, it adds, and it divides. Each one is a move you can see in the picture. The rest of this page is those three moves, in order.

1
Multiply: every area is a probability
Width times height is exactly what the formula multiplies

The rectangle below is all 1,000 homes. Its total area is 1. The width of each column is set by the base rate. The height of each block is set by the model's accuracy for that column: the sensitivity and the false positive rate, each sitting above its own complement. So the area of any block is width times height, which is a probability times a conditional probability. That is the multiplication in Bayes' Rule, done geometrically.

All 1,000 homes. Total area = 1
width = P(Lead) = base rate = 0.4
width = P(No Lead) = 1 − base rate = 0.6
Missed lead  ·  P(− | Lead) = miss rate = 0.1
0.1 × 0.4 = 0.04 (40 homes)
Flagged, has lead
height = P(+ | Lead) = sensitivity = 0.9
area = 0.9 × 0.4 = 0.36
360 homes
Correctly cleared
height = P(− | No Lead) = specificity = 0.8
area = 0.8 × 0.6 = 0.48
480 homes
Flagged, no lead
height = P(+ | No Lead) = false positive rate = 0.2
area = 0.2 × 0.6 = 0.12  (120 homes)
Widths are the base rate and its complement. Heights are the four accuracy rates: sensitivity and miss rate on the left, specificity and false positive rate on the right.
Each column is filled top to bottom by a complementary pair, which is why sensitivity + miss rate = 1 on the left and specificity + false positive rate = 1 on the right.
0.36 + 0.12 + 0.04 + 0.48 = 1.00
The connection

Look back at the formula. The blue block is P(+ | Lead) × P(Lead), which is 0.9 × 0.4 = 0.36. The teal block is P(+ | No Lead) × P(No Lead), which is 0.2 × 0.6 = 0.12. Click either term, or either block, to see them light up together.

2
Add: the flag narrows the world
Blue plus teal is the denominator

1204 Ferndale was flagged. Learning that shrinks the world we are reasoning about. The two upper blocks are gone, because this home is neither a missed pipe nor a cleared home. The world is now the band along the bottom, made of the blue block and the teal block together.

The new world: flagged homes only
P(Lead) = 0.4
P(No Lead) = 0.6
outside the world
Flagged, has lead
0.36
360 homes
outside the world
Flagged, no lead
0.12  (120 homes)
Total shaded area = 0.36 + 0.12 = 0.48, which is 480 of the 1,000 homes. This is the denominator of Bayes' Rule.
Why the denominator is a sum

The flag narrows the world to the shaded band. Two kinds of home live in that world, flagged with lead and flagged without, so the denominator adds both.

3
Divide: zooming in is the division
The answer is what share of the world is blue

The band is the whole world now, so stretch it to fill the full width. That stretching is the division: rescaling the band so 0.48 becomes 1.00.

Only the flagged homes, stretched to full width
The band above, rescaled so that 0.48 becomes 1.00
Has lead: 360 homes
0.36 / 0.48 = 0.75
No lead: 120
0.12 / 0.48 = 0.25
75%
25%
What the picture shows

The blue share of the world

75%
What the formula says
0.36
0.36 + 0.12
75%
The same picture, written as the formula
P(Lead | +) =
P(+ | Lead) × P(Lead)
P(+ | Lead) × P(Lead) + P(+ | No Lead) × P(No Lead)
=
0.36
0.36 + 0.12
= 0.36 / 0.48 = 0.75
Numerator: the blue area, 0.36. Denominator: the whole world, 0.36 + 0.12 = 0.48. The fraction is the blue share of that world.

The idea: Bayes' Rule is a proportion


Check yourself

P(Lead) = 0.4
P(No Lead) = 0.6
Missed lead
Flagged, has lead
Correctly cleared
Flagged, no lead
The same rectangle as above: widths are the base rate, heights are the accuracy rates.
Hint: use the interactive rectangle below to see this proportionally instead of numerically. Drag the false positive rate from 20% to 40% and watch which block grows, what happens to the total flagged area, and which way the answer moves. Then answer in your own words.
P(Lead): the base rate
How common lead lines are before the model says anything.
40%
P(+ | Lead): sensitivity
How often the model flags a home that really has lead.
90%
P(+ | No Lead): false positive rate
Start here: move this from 20% to 40%.
20%
P(Lead | flagged)
75%
360 with lead out of 480 flagged
The flag moved the probability from 40% to 75%.
All 1,000 homes
Population rectangle.
Flagged, lead Flagged, no lead Missed lead Correctly cleared
Zooming into only the flagged homes
Zoomed strip.
Have lead: 360 P(Lead|+) = 75% 120 do not
The word the city would use

Water utilities call this number the hit rate: the share of excavations that actually turn up a lead pipe. It is the same quantity you just calculated, P(Lead | +), and it is how the program's performance gets reported to the state and the press.

Step 3 of 3 · Bayesian updating

What you just did has a name

Starting with a prior probability, collecting evidence, and arriving at a revised probability is called Bayesian updating.

The updating chain

Prior: P(Lead) = 40%, the base rate before the model says anything.

New information: the model flags 1204 Ferndale.

Posterior: P(Lead | +) = 75%, the revised probability after accounting for the flag.

The key point is that updating does not have to stop. Today's posterior becomes tomorrow's prior as soon as new evidence arrives.

We will explore this more next class!