Probability · Reasoning Under Uncertainty

Bayes' Rule
Updating Belief with Evidence

A test is 80% accurate. It comes back positive. So you have the disease 80% of the time — right? No. The honest answer is a coin flip: 50%. Bayes' rule is the one equation that tells you what a piece of evidence is actually worth.

Prerequisites: multiplying and dividing fractions + the idea that a probability is a number between 0 and 1. That's it.
6
Chapters
5+
Simulations
0
Assumed Knowledge

Chapter 0: The Trap — A Positive Test That Means Almost Nothing

Imagine you walk into a clinic and take a screening test for a particular disease. The nurse tells you the test is 80% accurate — when someone has the disease it flags positive 80% of the time, and when someone is healthy it correctly says negative 80% of the time. Your result comes back. It's positive.

Take a moment with the obvious question: given a positive result, what is the chance you actually have the disease? Almost everyone — including most doctors, in studies that have been run on exactly this question — blurts out "80%." The test is 80% accurate, it said positive, so 80% chance. It feels airtight.

It is wrong. For the numbers we'll use in this lesson, the true answer is 50%. A literal coin flip. The positive result is worth far, far less than your gut insists. And if the disease were rarer — say 1 in 1000 instead of 1 in 5 — that same positive test would mean you have the disease less than half of one percent of the time. The test could scream "POSITIVE" and you'd still almost certainly be fine.

Why does intuition fail so badly here? Because there is a number you forgot to ask about: how common the disease is in the first place — the base rate, also called the prevalence. The test's accuracy describes how the test behaves per person tested. But whether a positive result should scare you depends on how many sick people versus healthy people are feeding into the test. If healthy people vastly outnumber sick people, then even a small slice of the healthy crowd getting a false positive can drown out the genuinely sick.

This is the base-rate trap: ignoring how common something is and judging the evidence in a vacuum. It is not a niche statistics puzzle. It is why mass screening for rare cancers produces floods of false alarms, why a "99% accurate" terrorist-detector flags mostly innocent people, why a spam filter that's right most of the time can still bury real mail. The fix is a single equation, three centuries old, that tells you precisely how to fold a base rate together with a piece of evidence. That equation is Bayes' rule, and by the end of this lesson you will compute it in your sleep.

Before any math, let's make the trap visceral. Below is a crowd of 100 people. A few are genuinely sick (warm dots); most are healthy (teal dots). Run the test on everyone and watch who lights up as "positive." The question is not how many positives there are — it's what fraction of the positives are truly sick.

100 people, one screening test — who's actually sick among the positives?

Drag the prevalence slider and press Run Test. Dots with a ring tested positive. Notice how many ringed dots are teal (healthy) — those are false alarms.

Prevalence (truly sick) 20%
The mistake everyone makes. "The test is 80% accurate" tells you P(positive given sick). Your gut silently flips it into P(sick given positive) and treats them as the same number. They are not the same, and the gap between them is the entire subject of this lesson. Flipping a conditional probability without accounting for the base rate is the single most common reasoning error in all of applied statistics.

Here's the worked count behind the picture, for prevalence 20% (1 in 5). Out of 100 people, 20 are sick and 80 are healthy. The test catches 80% of the sick — that's 0.8 × 20 = 16 true positives. But it also wrongly flags 20% of the healthy — that's 0.2 × 80 = 16 false positives. So 32 people test positive, and only 16 of them are sick. 16 out of 32 is exactly one half. The positive result moved you from a 1-in-5 prior to a 1-in-2 posterior — informative, but nowhere near 80%.

Where we're headed. Everything in this lesson is built to make that "16 out of 32 = 1/2" calculation feel inevitable, generalize to any numbers, and turn into a formula you can apply to spam, fraud, search-and-rescue, and machine-learning classifiers. The crowd above is Bayes' rule — we just haven't written the equation yet.
A test is 80% accurate and comes back positive for a disease that affects 1 in 5 people. Why is the chance you're actually sick far below 80%?

Chapter 1: Conditional Probability — Restricting the World to a Smaller Room

To fix the trap we need a tool, and the tool is older and simpler than Bayes' rule: conditional probability. It answers questions of the form "given that I already know X is true, how likely is Y?" Written P(Y | X) and read aloud as "the probability of Y given X." The vertical bar means "given" — everything to the right of it is something you already know for certain.

Here is the one mental image that makes conditional probability click, and it never leaves you: conditioning means restricting the world to a smaller room and re-measuring inside it. Normally you ask "what fraction of everyone is Y?" When you condition on X, you throw away everyone who isn't X, walk into the room containing only the X-people, and ask "what fraction of them is Y?" The denominator shrinks from "everyone" to "everyone who is X."

Let's make it concrete with a frequency picture — the single most powerful trick for taming probability. Forget percentages; imagine an actual crowd of 100 people standing in a field. We'll sort them by two yes/no facts and just count heads.

Suppose among these 100 people: 20 own a dog, and of those dog owners 15 also own a leash, while 5 own a dog but no leash. The other 80 own no dog (some own a stray leash, doesn't matter). Now ask: given that a randomly chosen person owns a dog, what's the probability they own a leash?

You do not divide by 100. You walk into the "dog owner" room — that's 20 people — and count leashes inside it: 15. So P(leash | dog) = 15/20 = 0.75. The denominator is 20, not 100, because conditioning on "dog" already discarded the 80 dogless people. That shrinking denominator is the whole idea.

Conditioning = shrinking the denominator

100 dots. Warm dots own a dog; among dog owners, ringed dots own a leash. Click Condition on "dog" to dim everyone outside the room and watch the denominator shrink from 100 to 20.

Now the formula, which is just that counting procedure written with symbols. The fraction "leashes among dog owners" is (people with dog and leash) divided by (people with dog):

P(leash | dog) = P(dog and leash)
P(dog)

In our crowd P(dog and leash) = 15/100 = 0.15 and P(dog) = 20/100 = 0.20, so P(leash | dog) = 0.15 / 0.20 = 0.75. Identical to the head-count, because dividing both counts by 100 (to turn them into probabilities) doesn't change their ratio. The general statement, with any two events A and B, is the definition of conditional probability:

P(A | B) = P(A and B)
P(B)
  (valid whenever P(B) > 0)

Two things to bolt down. First, P(A and B) is symmetric — "dog and leash" is the same group as "leash and dog" — but P(A | B) is not symmetric. P(leash | dog) = 0.75 here, while P(dog | leash) depends on how many leash owners there are total, which could be completely different. Conditioning is directional. That asymmetry is exactly what trips people in Chapter 0: P(positive | sick) is not P(sick | positive).

Second, rearrange the definition by multiplying both sides by P(B) and you get the multiplication rule, which we'll lean on in two chapters:

P(A and B) = P(A | B) · P(B)

Read it as a recipe: to be in both groups, first be in B (probability P(B)), then within that room be in A (probability P(A | B)). 0.75 × 0.20 = 0.15 — the dog-and-leash fraction, recovered. Hold onto this; Bayes' rule falls right out of it.

The room analogy, once more. Unconditional probability measures a group against the whole population. Conditional probability walks into a side room (everyone who is B) and measures the group against just the people in that room. Same numerator idea, smaller denominator. Every "given" you ever read is an instruction to change which room you're standing in.
Misconception: "and" vs "given" are the same. They are not. P(sick and positive) asks "what fraction of everyone is both sick and positive?" P(sick | positive) asks "what fraction of the positives are sick?" The first divides by the whole crowd; the second divides only by the positives. Confusing them inflates a tiny "and" probability into a scary-looking "given," or vice versa. Always check which denominator you're standing on.

One hand calculation to seal it. A deck of 52 cards: what's P(King | face card)? Face cards are J, Q, K — 12 of them. Kings among them: 4. Condition on "face card" means the denominator is 12, not 52. So P(King | face card) = 4/12 = 1/3. Compare P(King) unconditional = 4/52 = 1/13 ≈ 0.077. Knowing it's a face card raised the probability from about 8% to 33%, just by shrinking the room from 52 cards to 12.

In a class of 100, 30 students play an instrument and 12 of those also sing. What is P(sing | plays instrument)?

Chapter 2: Bayes' Rule — Flipping the Conditional

We ended Chapter 1 with the directional problem: I can easily measure P(positive | sick) — just test a bunch of sick people and count — but what I actually want is P(sick | positive). The arrow points the wrong way. Bayes' rule is the machine that reverses the arrow. And it isn't a new axiom dropped from the sky; it falls out of the definition we already wrote, in three lines of algebra.

Start from the multiplication rule, written two ways for the same joint event. Using events Y (the hidden cause, e.g. "sick") and X (the observed evidence, e.g. "positive"):

P(X and Y) = P(X | Y) · P(Y)
P(Y and X) = P(Y | X) · P(X)

The left sides are the same event — "X and Y" is the same crowd as "Y and X," order doesn't matter. So the right sides must be equal too:

P(Y | X) · P(X) = P(X | Y) · P(Y)

Now divide both sides by P(X) to isolate the thing we want, P(Y | X), and we have Bayes' rule:

P(Y | X) = P(X | Y) · P(Y)
P(X)

That's it. The entire lesson rests on this one line, and you just derived it yourself from "and is symmetric." Every piece has a name worth memorizing, because thinking in these names is what separates fluent reasoning from formula-plugging:

TermNamePlain meaning
P(Y)PriorWhat you believed before seeing evidence — the base rate.
P(X | Y)LikelihoodHow well the cause Y explains the evidence X (the test's hit rate).
P(X)EvidenceHow likely the evidence X was overall, across all causes — the normalizer.
P(Y | X)PosteriorYour updated belief in Y after seeing X. The answer.

Read the rule as a sentence: your updated belief (posterior) equals how well the cause explains the evidence (likelihood) times what you believed before (prior), divided by how surprising the evidence was overall (evidence). Belief flows in, gets reweighted by how well each hypothesis predicted what you saw, and flows out re-normalized.

The one-line intuition. Posterior ∝ Likelihood × Prior. The "∝" (proportional to) hides the evidence term P(X), whose only job is to make the posteriors over all hypotheses sum to 1. So Bayes' rule does two things: (1) reweight each hypothesis by how well it predicted the data (likelihood), starting from how plausible it already was (prior), then (2) renormalize so it's a valid probability. Update, then normalize.

The evidence term P(X) usually isn't handed to you directly — you compute it by adding up every way the evidence could have happened. If Y is yes/no (sick / not sick), then X (positive) happens either via a sick person testing positive, or a healthy person testing positive:

P(X) = P(X | Y) · P(Y) + P(X | not Y) · P(not Y)

This is the law of total probability: the total chance of the evidence is the sum of its chances under each hypothesis, each weighted by that hypothesis's prior. It's the denominator's full form, and notice the numerator of Bayes' rule, P(X | Y)·P(Y), is literally the first term of this sum. So the posterior is "the sick-and-positive piece, divided by the all-positive total" — exactly the 16-out-of-32 head-count from Chapter 0, now in symbols.

Watch belief flow: prior → reweight → posterior

Two hypotheses, "sick" and "healthy." Drag the prior. The bars show prior, then prior×likelihood (un-normalized), then the normalized posterior. Evidence reshuffles the weights; normalizing makes them sum to 1.

Prior P(sick) 0.20
Misconception: "the likelihood is the answer." The likelihood P(X | Y) — the test's 80% hit rate — is not your posterior. It's only one of three ingredients. People quote the likelihood ("the test is 80% accurate!") as if it were P(sick | positive), skipping the prior and the normalizer entirely. That skip is the base-rate trap. Bayes' rule exists precisely to stop you from mistaking a likelihood for a posterior.

Quick hand calculation to anchor the names before the full worked problem. A bag has a fair coin (heads 50%) and a trick coin (heads 100%), picked at random. You pull one, flip it, get heads. P(trick | heads)? Prior P(trick) = 0.5. Likelihood P(heads | trick) = 1.0. Evidence P(heads) = 1.0·0.5 + 0.5·0.5 = 0.5 + 0.25 = 0.75. Posterior = (1.0 × 0.5) / 0.75 = 0.5 / 0.75 = 2/3 ≈ 0.667. One heads nudged you from 50% to 67% — not certainty, because a fair coin lands heads too.

In P(Y | X) = P(X | Y)·P(Y) / P(X), which term is the prior — your belief before seeing the evidence?

Chapter 3: The Disease Test — Worked Fully, Every Step

Now we cash in. This is the exact problem from the opening, and we'll grind through every arithmetic step until the famous answer — 0.5, not 0.8 — is undeniable. The numbers come from a real assessment question, and they're chosen so the symmetry makes the punchline crisp.

The setup, stated precisely so we know which conditional is which:

QuantitySymbolValue
Prevalence (prior you're sick)P(D)0.20  (1 in 5)
Sensitivity: P(+ if sick)P(+ | D)0.80
Specificity: P(− if healthy)P(− | ¬D)0.80
False-positive rate: P(+ if healthy)P(+ | ¬D)1 − 0.80 = 0.20
Prior you're healthyP(¬D)1 − 0.20 = 0.80

Two new words, defined: sensitivity is the test's true-positive rate — of all sick people, what fraction does it correctly flag (0.80 here). Specificity is the true-negative rate — of all healthy people, what fraction does it correctly clear (also 0.80). "80% accurate" in the problem means both of these are 0.80. The false-positive rate is whatever's left over from specificity: 1 − 0.80 = 0.20.

We want the posterior P(D | +) — probability you're sick given a positive test. Bayes' rule:

P(D | +) = P(+ | D) · P(D)
P(+)

Step 1 — the numerator (the sick-and-positive piece). Likelihood times prior:

P(+ | D) · P(D) = 0.80 × 0.20 = 0.16

Sixteen percent of all people are both sick and test-positive. In the 100-person crowd that's 16 people — the true positives.

Step 2 — the evidence P(+), the denominator, using the law of total probability. A positive can come two ways:

P(+) = P(+ | D)·P(D) + P(+ | ¬D)·P(¬D)
P(+) = (0.80 × 0.20) + (0.20 × 0.80)
P(+) = 0.16 + 0.16 = 0.32

The first 0.16 is sick-and-positive (true positives); the second 0.16 is healthy-and-positive (false positives). They happen to be equal here, which is the heart of the punchline: the test produces exactly as many false alarms as true catches. In the crowd: 16 true positives + 16 false positives = 32 positives total.

Step 3 — divide:

P(D | +) = 0.16
0.32
= 0.50

Fifty percent. Of the 32 people who tested positive, only 16 are genuinely sick — a coin flip. The test was informative: it lifted your belief from the 20% prior up to 50%, a 2.5× jump. But "80% accurate" plus "positive" does not equal "80% sick." It equals 50%. The whole gap is the 16 false positives the base rate dragged in.

The answer is 0.5, not 0.8. Sensitivity (0.8) is P(+ | sick). The posterior (0.5) is P(sick | +). They differ because the 80 healthy people, even with a low 20% false-positive rate, contribute 16 false positives — exactly matching the 16 true positives from the 20 sick people. Equal numbers of true and false positives means a 50/50 posterior. That equality is the slickest way to see why the answer is precisely one half.
The 2×2 confusion grid — recolor it live

100 people split into four boxes by truth (sick / healthy) and test (+ / −). The posterior is the warm "true positive" box divided by the whole positive column. Drag the sliders and watch the boxes and the posterior change.

Prevalence P(D) 0.20
Sensitivity P(+|D) 0.80
Specificity P(−|¬D) 0.80

Play with the showcase grid above — it's the centerpiece. Set prevalence to 20, sensitivity to 80, specificity to 80 and read off 0.50. Now crank specificity to 99 (fewer false positives) and watch the posterior leap toward 95%: specificity, not sensitivity, is what a screening test most needs. Then drop prevalence to 1% and watch the posterior collapse even with a great test — that's Chapter 4 in one drag.

Misconception: "raise sensitivity to fix a bad screen." Tempting, but wrong lever. With a rare disease, almost everyone tested is healthy, so the flood of positives is dominated by false positives from the healthy majority. Pushing sensitivity from 0.8 to 0.99 catches a few more of the rare sick people but does little to the false-positive deluge. The high-leverage knob is specificity — shrinking the per-healthy-person false-positive rate — because it's multiplied by the huge healthy population. Try it on the grid: bump sensitivity vs bump specificity and see which moves the posterior more.

One more hand pass with different numbers, to prove it's the procedure not the magic 0.5. Suppose prevalence 0.10, sensitivity 0.90, specificity 0.90. Numerator = 0.90 × 0.10 = 0.09. Evidence = 0.09 + (0.10 × 0.90) = 0.09 + 0.09 = 0.18. Posterior = 0.09 / 0.18 = 0.50 again — because once more the true-positive and false-positive masses tie. The "coin flip" appears whenever sensitivity·prevalence equals false-positive-rate·(1−prevalence). Bayes' rule tells you exactly when your evidence is worth a coin flip and when it's worth more.

The grid above draws the four boxes for you. Now build the crowd yourself — assign all 196 people to their box from the prior, sensitivity, and specificity, then read the posterior straight off the picture by counting.

For prevalence 0.20 and an 80%-accurate test, P(+) = 0.16 + 0.16 = 0.32. What does the second 0.16 represent?

Chapter 4: The Base-Rate Effect — Why Screening Rare Things Is Brutal

We've nailed one case (prevalence 20%). The deeper lesson is what happens as the disease gets rarer. Hold the test fixed at 80% accurate and sweep the prevalence from common down to one-in-a-thousand. The posterior — the meaning of a positive test — doesn't drift gently. It collapses.

Let's hand-compute three points on that curve, all with sensitivity = specificity = 0.80, so the false-positive rate stays 0.20.

Prevalence 20% (1 in 5): numerator 0.80×0.20 = 0.16; evidence 0.16 + 0.20×0.80 = 0.32; posterior 0.16/0.32 = 0.500. (Our familiar coin flip.)

Prevalence 5% (1 in 20): numerator 0.80×0.05 = 0.040; false positives 0.20×0.95 = 0.190; evidence 0.040 + 0.190 = 0.230; posterior 0.040/0.230 ≈ 0.174. A positive now means only a 17% chance you're sick.

Prevalence 1% (1 in 100): numerator 0.80×0.01 = 0.0080; false positives 0.20×0.99 = 0.198; evidence 0.0080 + 0.198 = 0.206; posterior 0.0080/0.206 ≈ 0.039. A positive test, from an "80% accurate" test, means under 4% chance you're actually sick. You are 25× more likely to be a false alarm than a real case.

See the pattern? As prevalence shrinks, the numerator (true positives) shrinks with it, but the false-positive term barely moves — it's anchored to the enormous healthy population, which is now ~99% of everyone. The denominator becomes almost entirely false positives, and the true positives are a vanishing sliver of it. The posterior tracks true positives / (true + false positives), and when false positives dominate, that ratio crawls toward zero.

Sweep the base rate — the collapse

Posterior P(sick | +) versus prevalence, with the test fixed. The teal diagonal is "posterior = prior" (a useless test). The warm curve is the real posterior. The vertical line marks your current prevalence; drag it.

Prevalence (log-ish) 20.0%
Test accuracy (sens=spec) 0.80
The screening paradox. The rarer the condition, the less a positive test means — no matter how good the test sounds in isolation. This is why doctors don't mass-screen the whole population for rare cancers: you'd terrify and over-treat a mountain of healthy people for every real case found. It's also why a positive on a rare condition is almost always followed by a second, different test: the first test's posterior becomes the second test's prior, and stacking evidence is the only escape (Chapter 5).

There's a beautiful way to feel why the curve falls so fast: the odds form of Bayes' rule. Define the likelihood ratio LR = sensitivity / false-positive-rate = 0.80 / 0.20 = 4. Then a positive test multiplies your odds (not probability) of being sick by 4:

posterior odds = LR × prior odds = 4 × prior odds

At prevalence 20%, prior odds are 20:80 = 1:4 = 0.25. Posterior odds = 4 × 0.25 = 1, i.e. 1:1, which is probability 1/(1+1) = 0.50. At prevalence 1%, prior odds are 1:99 ≈ 0.0101. Posterior odds = 4 × 0.0101 ≈ 0.0404, probability 0.0404/1.0404 ≈ 0.039 — matching our long calculation. A positive test is a fixed 4× multiplier on the odds; if you start at terrible odds, 4× terrible is still terrible.

The cleanest mental model. Evidence multiplies odds, not probability. Each independent test or clue with likelihood ratio LR multiplies your odds by LR. A 4× multiplier turns 1:4 into 1:1, but only turns 1:99 into 4:99. Same evidence, wildly different conclusions — because the starting odds (the base rate) are doing half the work. Forgetting the base rate is forgetting where the multiplication starts.

Misconception worth a callout, since it's the practical takeaway:

Misconception: "a more accurate test fixes the rare-disease problem." Partly, but slowly. Going from 80% to 95% accurate raises the likelihood ratio from 4 to 19 — better, but at prevalence 0.1% your posterior on a single positive still only reaches about 2%. There is no test accuracy that makes a single positive trustworthy for a truly rare condition; you fundamentally need either a higher-prior subpopulation (screen only at-risk people) or a second independent test. Drag the accuracy slider to 99 at prevalence 0.5% on the sweep above — the posterior is still far from 1.

The sweep canvas plots the collapse as a curve. Now build the posterior that drives it, and watch a live town empty of sick people while the test keeps screaming positives — the screening paradox, frame by frame.

As a disease gets rarer (prevalence drops), what happens to the chance that a positive test means you're truly sick, holding the test fixed?

Chapter 5: Beyond — Chaining Evidence, Spam, and Classifiers

One positive test gave a coin flip. So what do you actually do? You collect more evidence — and Bayes' rule is built to chain. The key move is the engine of all rational learning: today's posterior becomes tomorrow's prior.

Take a second, independent test, also 80% accurate, and suppose it's also positive. After test one, your belief was 0.50 (that's your new prior). Run Bayes again with prior 0.50: numerator 0.80×0.50 = 0.40; evidence 0.40 + 0.20×0.50 = 0.40 + 0.10 = 0.50; posterior 0.40/0.50 = 0.80. Two independent positives carry you from 50% to 80%. In odds form it's effortless: each positive multiplies odds by LR = 4, so two positives multiply by 16. Prior odds 1:4 → 16 × 0.25 = 4 → probability 4/5 = 0.80. Same answer, one multiplication.

Bayesian updating. Belief is not a one-shot verdict; it's a running total you revise with each new clue. Posterior ← prior, observe evidence, multiply by the likelihood ratio, renormalize, repeat. This loop is the backbone of Kalman filters tracking a rocket, robots localizing in a hallway, and A/B tests deciding which button wins. Every one of them is Bayes' rule applied over and over.

The same machine powers a naive Bayes classifier — the algorithm behind classic spam filters. The "hypothesis" Y is "this email is spam"; the "evidence" X is the words in it. For each word, you precompute a likelihood ratio (how much more often "viagra" appears in spam than in ham), and you multiply them all onto the prior odds. "Naive" means it pretends the words are independent — usually false, but it works shockingly well. A 12-word email is just twelve likelihood-ratio multiplications on the spam odds.

python
# Bayesian updating in odds form — chain any number of clues.
def update_odds(prior_odds, likelihood_ratio):
    return prior_odds * likelihood_ratio        # multiply, that's the whole update

def odds_to_prob(o):
    return o / (1 + o)

# Disease test: prevalence 20% -> prior odds 0.2/0.8 = 0.25; each + has LR = 0.8/0.2 = 4
odds = 0.25
for result in ["+", "+"]:        # two independent positive tests
    odds = update_odds(odds, 4.0)
print(odds_to_prob(odds))            # 0.8 — two positives -> 80%

# Naive Bayes spam filter: multiply a word's LR for every word seen.
def spam_prob(prior_odds, word_LRs):
    odds = prior_odds
    for lr in word_LRs:        # "free":8, "meeting":0.2, ...
        odds *= lr
    return odds_to_prob(odds)

And the direct probability-form version, computing the same disease posterior from scratch, so you can see they agree:

python
def posterior(prior, sens, spec):
    # P(disease | +) via Bayes' rule + law of total probability
    p_pos = sens * prior + (1 - spec) * (1 - prior)   # the evidence P(+)
    return sens * prior / p_pos

print(posterior(0.20, 0.80, 0.80))   # 0.5 — one positive
print(posterior(0.50, 0.80, 0.80))   # 0.8 — second positive (prior is now 0.5)
Chain the evidence — click to add a positive or negative test

Start at the prevalence prior. Each + multiplies odds by 4; each multiplies by 0.25 (the likelihood ratio of a negative). Watch belief climb and fall as evidence accumulates.

The canvas above chains one sequence you click out. Now build the odds update itself and run it on a whole cohort of patients at once — watch belief fan out, some climbing toward "sick," some sinking toward "healthy," each one just a string of multiplications.

Where this lesson plugs into the rest of the site:

You know...You want...Use
P(evidence | cause), priorP(cause | evidence)Bayes' rule (this lesson)
Many independent cluesCombined beliefMultiply likelihood ratios (odds form)
Belief that evolves in timeTrack a moving targetBayes / Kalman filter
Words → spam/hamA classifierNaive Bayes
What you can now do. You can flip any conditional, compute a posterior by hand, explain why an 80%-accurate positive test means 50% (or 4%, for a rare disease), chain multiple pieces of evidence with the odds trick, and recognize Bayes' rule lurking inside spam filters, Kalman filters, and every "smart" system that updates beliefs from data. That's the whole rule — one line of algebra, infinite reach.
Feynman's closing thought. “What I cannot create, I do not understand.” You derived Bayes' rule from the definition of conditional probability, computed the famous 0.5 every step by hand, watched it collapse for rare diseases, and chained evidence in odds form. The Code Labs in each chapter — build the crowd, build the collapsing posterior, build the odds chain — and the Workbook below are where you prove you can create it, not just recognize it.
After one positive test your belief is 0.50. A second independent 80%-accurate test is also positive. What's your new belief?

Exercises & Workbook

The lesson showed you Bayes' rule four ways: a crowd, a 2×2 grid, an algebraic derivation, and odds. This workbook makes you apply it — every exercise asks you to derive, trace, build, design, or debug, never just recall. Work them with a blank sheet and a pencil; the answers check your arithmetic, and every reveal walks the full path. Nothing is a placeholder.

Mastery
0 / 32 exercises (0%)
0
Day Streak
Best: 0

Part A — Conditional probability, the smaller room

Before Bayes, you must read a "given" correctly: shrink the denominator to the room you conditioned on.

A.1 — What the bar means Trace
P(A | B) is read "the probability of A given B." What does conditioning on B do?
A.2 — Count in the room Derive

Of 100 people, 40 own a bike and of those 10 own a helmet. Compute P(helmet | bike). Enter the decimal.

P
Show derivation
Condition on "bike" → denominator is 40, not 100.
P(helmet | bike) = 10 / 40 = 0.25

The 60 non-bikers are irrelevant once you've conditioned on bike.

A.3 — Card conditioning Derive

From a 52-card deck, what is P(Queen | face card)? Face cards are J, Q, K. Enter the decimal (3 dp).

P
Show derivation
12 face cards; 4 are Queens. P(Q | face) = 4/12 = 1/3 ≈ 0.333

Unconditional P(Queen) = 4/52 ≈ 0.077; the "face card" room raised it to 0.333.

A.4 — The directional trap Trace
Which statement is true about P(A | B) versus P(B | A)?
A.5 — Multiplication rule Derive

P(rain) = 0.30 and P(umbrella | rain) = 0.90. Using P(A and B) = P(A | B)·P(B), find P(rain and umbrella). Enter the decimal.

P
Show derivation
P(rain and umbrella) = P(umbrella | rain) · P(rain) = 0.90 × 0.30 = 0.27
A.6 — Spot the "and"/"given" swap Debug

A student computes P(sick | positive) from 100 people. Exactly one line confuses "and" with "given." Click it.

100 people: 20 sick, 80 healthy
true positives (sick & +) = 0.8 * 20 = 16
false positives (healthy & +) = 0.2 * 80 = 16
P(sick | +) = 16 / 100 = 0.16   # divide by everyone
# should divide by the positives (16+16=32), giving 0.5
Part A engine. "Given" = shrink the denominator to the conditioning room. "And" divides by everyone. Mixing them is the original sin of probability errors — and the seed of the base-rate trap.

Part B — Bayes' rule and its parts

Now flip the conditional. Name every term, then compute posteriors from scratch.

B.1 — Name the prior Trace
In P(Y|X) = P(X|Y)P(Y)/P(X), the term P(Y) is the...
B.2 — Name the likelihood Trace
Which term is the likelihood — how well the cause explains the evidence?
B.3 — Compute the evidence Derive

Prior P(D)=0.10, sensitivity P(+|D)=0.90, false-positive P(+|¬D)=0.20. By total probability, P(+) = 0.90·0.10 + 0.20·0.90. Enter P(+).

P(+)
Show derivation
P(+) = (0.90 × 0.10) + (0.20 × 0.90) = 0.09 + 0.18 = 0.27
B.4 — The famous posterior Derive

Prior 0.20, sensitivity 0.80, specificity 0.80. Compute P(D | +). Enter the decimal.

P
Show derivation
numerator = 0.80 × 0.20 = 0.16
P(+) = 0.16 + (0.20 × 0.80) = 0.16 + 0.16 = 0.32
P(D | +) = 0.16 / 0.32 = 0.50

0.5, not 0.8 — equal true and false positives.

B.5 — A better test Derive

Same prior 0.20, but sensitivity 0.90 and specificity 0.95 (false-positive 0.05). Compute P(D | +), 2 dp.

P
Show derivation
numerator = 0.90 × 0.20 = 0.18
P(+) = 0.18 + (0.05 × 0.80) = 0.18 + 0.04 = 0.22
P(D | +) = 0.18 / 0.22 ≈ 0.818

Raising specificity from 0.80 to 0.95 jumped the posterior from 0.50 to 0.82.

B.6 — Trick-coin posterior Derive

A fair coin (heads 0.5) and a two-headed coin (heads 1.0), picked 50/50. You flip heads. P(two-headed | heads)? 3 dp.

P
Show derivation
P(heads) = 1.0·0.5 + 0.5·0.5 = 0.5 + 0.25 = 0.75
P(2H | heads) = (1.0 × 0.5) / 0.75 = 0.5/0.75 = 2/3 ≈ 0.667
B.7 — The sign-error bug Debug

A student computes the evidence P(+) for prior 0.2, sens 0.8, spec 0.8. One line has the wrong false-positive rate. Click it.

prior = 0.2;  sens = 0.8;  spec = 0.8
true_pos = sens * prior            # 0.8 * 0.2 = 0.16
false_pos = spec * (1 - prior)     # WRONG: uses spec, not (1-spec)
P_pos = true_pos + false_pos
posterior = true_pos / P_pos
B.8 — Order the Bayes recipe Design

Put the steps of computing a posterior in order.

?
?
?
?
divide numerator by evidence numerator = likelihood × prior write down prior & likelihoods evidence = sum over all causes
Part B engine. Posterior = (likelihood × prior) / evidence, where evidence = sum of likelihood×prior over every hypothesis. The numerator is the first term of the evidence sum. Always: name, compute numerator, compute evidence, divide.

Part C — The base-rate effect

Hold the test fixed, shrink the prior, and watch a positive lose its meaning.

C.1 — Rare-disease posterior Derive

Prevalence 0.01, sens 0.80, spec 0.80. Compute P(D | +), 3 dp.

P
Show derivation
numerator = 0.80 × 0.01 = 0.0080
false pos = 0.20 × 0.99 = 0.198
P(+) = 0.0080 + 0.198 = 0.206
P(D | +) = 0.0080 / 0.206 ≈ 0.039
C.2 — The likelihood ratio Derive

For sensitivity 0.80 and false-positive rate 0.20, the likelihood ratio of a positive is sens / FP-rate. Enter it.

LR
Show derivation
LR = 0.80 / 0.20 = 4 — a positive multiplies your odds by 4.
C.3 — Odds form Derive

Prevalence 0.20 → prior odds 0.25. Positive test, LR = 4. Posterior odds = 4×0.25 = 1. Convert to probability: odds/(1+odds). Enter it.

P
Show derivation
posterior odds = 4 × 0.25 = 1 = 1:1
P = 1 / (1 + 1) = 0.50 — agrees with the long calculation.
C.4 — Why screening rare things fails Trace
For a rare disease, why does a positive test mean so little even with a good test?
C.5 — The wrong fix Trace
To make a positive trustworthy for a rare disease, the highest-leverage single change is to...
C.6 — Design a precise screen Design

Order these moves from most to least effective for raising the posterior on a rare-disease positive.

?
?
?
nudge sensitivity up a little screen only a high-risk (higher-prior) subgroup drive specificity way up
C.7 — Odds-form bug Debug

A student updates odds for a positive test. Exactly one line is wrong. Click it.

prior_prob = 0.2
prior_odds = prior_prob / (1 - prior_prob)   # 0.2/0.8 = 0.25
LR = sens / (1 - spec)                        # 0.8/0.2 = 4
post_odds = prior_odds + LR                   # WRONG: should multiply
post_prob = post_odds / (1 + post_odds)
Part C engine. Evidence multiplies odds by the likelihood ratio. Rare condition = tiny prior odds; even a 4× or 19× multiplier leaves you small. Fix it by raising the prior (target the screen) or the specificity (cut false positives), not by chasing sensitivity.

Part D — Applications & chaining

Bayes' rule isn't about diseases — it's about evidence. Spam, fraud, and stacked tests are the same machine.

D.1 — Spam filter Derive

10% of mail is spam. The word "free" appears in 60% of spam and 5% of ham. An email contains "free." P(spam | "free")? 2 dp.

P
Show derivation
numerator = P("free"|spam)·P(spam) = 0.60 × 0.10 = 0.060
P("free") = 0.060 + 0.05×0.90 = 0.060 + 0.045 = 0.105
P(spam | "free") = 0.060 / 0.105 ≈ 0.571
D.2 — Two positive tests Derive

After one positive your belief is 0.50. A second independent 80%-accurate test is positive too. P(sick | both +)? 2 dp.

P
Show derivation
new prior = 0.50; numerator = 0.80×0.50 = 0.40
P(+) = 0.40 + 0.20×0.50 = 0.50; posterior = 0.40/0.50 = 0.80

Odds shortcut: 0.25 × 4 × 4 = 4 → 4/5 = 0.8.

D.3 — A negative result Derive

Prior 0.20, sens 0.80, spec 0.80. The test is negative. P(sick | −)? 3 dp. (Use P(−|D)=0.20, P(−|¬D)=0.80.)

P
Show derivation
numerator = P(−|D)·P(D) = 0.20 × 0.20 = 0.04
P(−) = 0.04 + 0.80×0.80 = 0.04 + 0.64 = 0.68
P(sick | −) = 0.04 / 0.68 ≈ 0.059

A negative lowered you from 0.20 to about 0.06 — reassuring, but not zero.

D.4 — Fraud detection Derive

0.5% of transactions are fraud. The detector flags 95% of fraud and 2% of legit transactions. A transaction is flagged. P(fraud | flag)? 3 dp.

P
Show derivation
numerator = 0.95 × 0.005 = 0.00475
P(flag) = 0.00475 + 0.02×0.995 = 0.00475 + 0.0199 = 0.02465
P(fraud | flag) = 0.00475 / 0.02465 ≈ 0.193

Even a 95%/98% detector flags ~5 false alarms per true fraud — the base rate strikes again.

D.5 — Naive Bayes chip Trace
What does "naive" mean in a naive Bayes spam filter?
D.6 — Posterior becomes prior Trace
What is the core loop of Bayesian updating across multiple pieces of evidence?
D.7 — Chaining bug Debug

A student chains two tests. One line resets belief instead of carrying it forward. Click it.

belief = 0.20            # prevalence prior
belief = posterior(belief, 0.8, 0.8)   # after test 1 -> 0.5
belief = 0.20            # WRONG: throws away test 1's result
belief = posterior(belief, 0.8, 0.8)   # after test 2
print(belief)            # should be 0.8, prints 0.5
D.8 — Design the chained pipeline Design

Order the steps to combine two independent tests.

?
?
?
update again using that posterior as the new prior start from the prevalence prior apply Bayes with test 1's result

Part E — Conceptual mastery

The last set tests whether you can explain, not just compute.

E.1 — Likelihood ≠ posterior Trace
"The test is 80% accurate" gives you which quantity directly?
E.2 — Role of the evidence term Trace
What is the job of P(X), the evidence, in Bayes' rule?
E.3 — When is the posterior exactly 0.5? Derive

The posterior is 0.5 when true positives equal false positives. With sens 0.6 and FP-rate 0.2, at what prevalence (2 dp) does sens·p = FP·(1−p) hold? Enter p.

p
Show derivation
0.6p = 0.2(1−p) ⇒ 0.6p = 0.2 − 0.2p ⇒ 0.8p = 0.2 ⇒ p = 0.25
E.4 — The base-rate fallacy named Trace
A juror hears "the DNA match is 99.9% accurate" and concludes the defendant is 99.9% guilty. What's the error?
E.5 — Two clues, odds form Derive

Prior odds 0.25. Clue 1 has LR 4 (raises belief); clue 2 has LR 0.5 (lowers it). Posterior odds = 0.25×4×0.5. Convert to probability, 3 dp.

P
Show derivation
posterior odds = 0.25 × 4 × 0.5 = 0.5
P = 0.5 / (1 + 0.5) = 0.333
E.6 — Reasoning bug Debug

A reasoner walks through the disease case. Exactly one line is a logical error. Click it.

The test is 80% accurate.
It came back positive.
Most positives at 20% prevalence are false alarms unless we check.
P(sick | positive) = P(positive | sick) = 0.8   # WRONG: flips the conditional
We must fold in the 20% prior, giving 0.5.
Mastery check. If you finished Parts A–E you can: read any conditional, derive Bayes' rule, compute posteriors for tests, spam, and fraud, explain the base-rate effect, chain evidence in odds form, and catch the conditional-flip error in someone else's reasoning. That is the rule, fully owned.