Home / Probability

Bayes' Theorem

A test is 99% accurate and you test positive. Are you 99% likely to be ill? Almost certainly not — and this grid of 10,000 people shows exactly why.

Controls

base rate P(D)1.0%
sensitivity P(+|D)99%
specificity P(−|not D)99%

has it & tests positive
healthy but tests positive
has it, missed by test
healthy, correctly negative

10,000 People, One Square Each

natural frequencies

Live Calculation

true positives
0
false positives
0
all positives
0
P(ill | positive)
0%

Bayes' Theorem

How to update a belief when evidence arrives — and why "99% accurate" is not what you think.

What this is

Bayes' theorem takes a prior belief and revises it in light of new evidence. It is the mathematics of changing your mind correctly.

The Direction Everyone Confuses

The whole difficulty is that P(+ | D) and P(D | +) are different questions:

  • P(+ | D)given that you are ill, how likely is a positive test? This is the test's sensitivity, the number printed on the box.
  • P(D | +)given a positive test, how likely is it that you are ill? This is what you actually want to know.

They can differ enormously. Swapping them is called the base rate fallacy, and studies repeatedly find that most doctors get this exact question wrong.

Count People, Not Percentages

The grid shows 10,000 people, one square each. With a 1% base rate and a 99% accurate test:

  • 100 people actually have it. The test catches 99 of them.
  • 9,900 are healthy. The test wrongly flags 1% of them — 99 people.
  • So 198 positives in total, of which only 99 are real. P(ill | positive) = 50%.

A coin flip — from a test that is genuinely 99% accurate. The reason is simply that there are so many more healthy people that even a tiny error rate applied to them produces as many false alarms as there are true cases. Presenting the problem as counts instead of percentages makes this obvious, which is why this format is called natural frequencies.

The Base Rate Dominates

Drag the base rate slider from 0.1% upward and watch the answer climb. The test never changed — only how common the condition is. This is why:

  • Mass screening for rare diseases produces mostly false positives, and follow-up testing is essential.
  • A positive result means far more when you already had symptoms — symptoms raise your prior before the test is even run.
  • Specificity matters more than sensitivity for rare conditions: false positives are drawn from the huge healthy majority. Try dropping specificity by just 1% and watch the answer collapse.

Where It Appears in ML

Bayes underpins the Naive Bayes classifier, which asks P(class | features) by flipping it into P(features | class) × P(class). The same asymmetry explains why a fraud detector with excellent accuracy still drowns analysts in false alarms — fraud is rare, so the base rate works against you. It is also why accuracy is a poor metric on imbalanced data, and why precision and recall exist.

Updating a belief with evidence

Bayes' theorem is the rule for revising what you believe when new information arrives.

P(A|B) = P(B|A) × P(A) / P(B)

Each piece has a name and a job:

  • P(A) — the prior. What you believed before the evidence.
  • P(B|A) — the likelihood. How expected this evidence would be if A were true.
  • P(B) — the marginal. How expected the evidence is overall.
  • P(A|B) — the posterior. What you believe now.

In words: new belief = old belief × how well the evidence fits, normalised.

The medical test, worked through

A disease affects 1 in 1,000 people. A test is 99% accurate in both directions: it catches 99% of cases and produces a false positive 1% of the time.

You test positive. What is the probability you have the disease?

Most people say 99%. Work it through on 100,000 people:

 Has diseaseNo diseaseTotal
Test positive999991,098
Test negative198,90198,902
Total10099,900100,000

Of the 1,098 positive results, only 99 are real. So the probability is 99 / 1,098 = 9%.

The test is genuinely 99% accurate. The answer is still 9%, because the disease is rare and the 99,900 healthy people generate ten times as many false positives as there are true cases.

This is base rate neglect, and it is the single most consequential probability error in medicine, security screening, fraud detection and machine learning evaluation. Whenever the positive class is rare, the prior dominates.

Count it yourself

The article's 50% is arithmetic, not rhetoric. Change base_rate and run it again — the test's accuracy never moves.

example_01.pyNumPy
Output

The base rate, swept

The same calculation across six base rates. Nothing about the test changes; the answer moves from 9% to 99%.

example_02.pyNumPy
Output

Reading it as an odds update

There is a version of the theorem that is easier to compute in your head:

posterior odds = prior odds × likelihood ratio

For the example: prior odds are 1:999. The likelihood ratio is 0.99 / 0.01 = 99. So the posterior odds are 99:999, which is about 1:10 — the same 9%.

Two useful habits fall out of this form. A likelihood ratio near 1 means the evidence barely moves your belief, however dramatic it sounds. And updates chain by multiplication, so several independent pieces of evidence multiply their ratios together — which is exactly what Naive Bayes does with words in a document.

Where it appears in machine learning

  • Naive Bayes classifiers apply it directly, with the naive assumption that features are independent given the class.
  • Every probabilistic classifier's output is a posterior: P(class | features).
  • Bayesian optimisation uses it to decide which hyperparameters to try next, updating a model of the objective as results come in.
  • A/B testing in its Bayesian form reports "the probability B is better than A", which is what stakeholders actually asked for — unlike a p-value.
  • Regularisation has a Bayesian reading: ridge regression is the posterior mode under a normal prior on the coefficients, and lasso under a Laplace prior.
  • Spam filtering was the first mass-deployed application and is still a clean example.

Guided experiments

  1. Start at the default (1% base rate, 99/99 test) and read the answer: about 50%, not 99%.
  2. Drop the base rate to 0.1%. The posterior falls to roughly 9% — a positive result now means you are still probably fine.
  3. Raise the base rate to 40%. Now a positive is highly meaningful. Same test, completely different conclusion.
  4. Return to 1% and drop specificity to 95%. False positives swamp the true ones and confidence collapses — a small change in specificity matters far more than sensitivity here.
  5. Count the amber squares against the red ones in the grid. That visual ratio is the answer.

Summing up

Evidence updates a prior; it does not replace it. When a condition is rare, even an excellent test produces mostly false positives, because the healthy majority is so much larger. Always ask how common the thing was before the evidence arrived.

Priors, and the objection to them

The usual criticism of Bayesian methods is that the prior is subjective — two people can start from different beliefs and reach different conclusions from the same data.

Three responses, all practical:

Data overwhelms the prior. With enough evidence, any two reasonable priors converge to the same posterior. Disagreement only persists when the data is thin — which is exactly when honesty about prior assumptions is most valuable.

The alternative is a hidden prior. Choosing a model, a feature set and a regularisation strength are all prior assumptions. Bayesian methods write them down.

Weak priors exist. When you genuinely know little, a wide, uninformative prior lets the data do nearly all the work.

Where priors earn their keep is small data. A conversion rate estimated from 3 clicks in 10 visits is 30% by the naive calculation; with a sensible prior from historical rates it comes out far more plausibly, and with an honest interval around it.

A second worked example: spam

A word appears in 8% of spam emails and 1% of legitimate ones. Spam is 40% of all mail. An email contains the word — how likely is it to be spam?

  • Prior: P(spam) = 0.40.
  • Likelihood: P(word | spam) = 0.08.
  • Marginal: P(word) = 0.08×0.40 + 0.01×0.60 = 0.032 + 0.006 = 0.038.
  • Posterior: 0.032 / 0.038 = 84%.

One word moved the belief from 40% to 84%. A second independent word would multiply the odds again — which is how a filter reaches near-certainty from a handful of weak signals, and why removing one incriminating word rarely fools it.

Questions people ask

What is the difference between P(A|B) and P(B|A)? Everything. The probability of a positive test given the disease is not the probability of the disease given a positive test, and confusing the two is the error the medical example above is built on.

Where does the prior come from? Historical data, domain knowledge, or a deliberately weak default. It should be stated, not hidden.

Is Bayesian statistics better than frequentist? They answer different questions. Bayesian methods give the probability of a hypothesis given the data; frequentist methods give the probability of the data given a hypothesis. The second is what a p-value is, and it is routinely misread as the first.

Why is Naive Bayes called naive? Because it assumes all features are independent given the class, which is almost never true — and works anyway for ranking.

How do I compute P(B) in practice? By summing over all the ways B can happen: P(B) = Σ P(B|Aᵢ)P(Aᵢ). In classification it is the same for every class, so it can be ignored while ranking.

What is a conjugate prior? A prior whose posterior has the same mathematical form, which makes the update a closed-form calculation rather than a simulation. Beta with binomial is the classic pair.

Recap in one screen

  • Posterior ∝ prior × likelihood: new belief is old belief adjusted by how well the evidence fits.
  • Rare conditions produce mostly false positives, however accurate the test — always start from the base rate.
  • The odds form (posterior odds = prior odds × likelihood ratio) is the easiest to compute and to chain.
  • P(A|B) and P(B|A) are different numbers, and swapping them is the classic error.
  • Naive Bayes, Bayesian optimisation, Bayesian A/B tests and the probabilistic reading of regularisation all rest on this one line.

Recall check

0 of 3

Say the answer out loud before you reveal it — recalling it is what makes it stick, and rereading it is not.

  1. What does this module say about “What this is”?

  2. What does this module say about “Count People, Not Percentages”?

  3. What does this module say about “The Base Rate Dominates”?

Cheat sheet

Bayes' Theorem

A test is 99% accurate and you test positive. Are you 99% likely to be ill? Almost certainly not — and this grid of 10,000 people shows exactly why.

MATHS · vizlearn.in/maths/bayes_theorem.html

About the author

Ashish Jangra builds and maintains VizLearn. Every module here is written and the visualisation behind it hand-built, so the numbers in a readout come from the same code that draws the picture. Corrections are genuinely welcome and get priority over everything else — if a page states something wrong, or an animation misrepresents what the algorithm does, get in touch.