Bayes' Theorem Calculator
Find out what a test result or a piece of evidence really tells you. See why a good test can still give mostly false alarms, follow the numbers through a picture of real people, and work through second tests, several possible causes and results tables. Every answer shows the working.
What are you trying to understand?
These are illustrative numbers, not figures for any real test. Change them to match your situation.
How common it is among cases like this one. Drag to watch the answer change.
Specificity = 100% minus the false alarm rate. Switching converts the number for you.
For learning only. This can't interpret your own test result. Talk to a doctor or nurse about that.
Assumes the two tests make mistakes independently, which usually means two different kinds of test.
Give each possible cause a starting share and how often it produces the evidence. Leave rows blank if you need fewer.
Also called a confusion matrix. Enter counts from real results.
Type chances as 5%, 0.05 or 1/20.
Results update as you type. Press Calculate or Enter to save one to your recent list.
Your recent calculations
Practice questions
Five fresh questions each time, with worked answers. Teachers can print them as a worksheet with an answer key, or save it as a PDF from the print window.
What Bayes' theorem does
You start with a belief about how likely something is. Then you see some evidence. Bayes' theorem tells you how much that evidence should change your belief. The starting chance is called the prior. The updated chance is called the posterior.
The surprising part is how often the updated chance is lower than people expect. A test can catch 95% of real cases and still give more false alarms than real detections, simply because what it's looking for is rare.
Why it feels wrong
In 1978, Ward Casscells and colleagues asked doctors and medical students at Harvard a question. A disease affects 1 in 1,000 people, and a test for it has a 5% false positive rate. Someone tests positive. What's the chance they have the disease? Almost half answered 95%. The answer, assuming the test catches every real case, is about 2%. Fewer than one in five got it right.
Later work by Gerd Gigerenzer and Ulrich Hoffrage showed that the same question becomes much easier when it's framed as counts of people: "Out of 1,000 people, 1 has the disease. About 50 of the other 999 test positive anyway." That's why this calculator shows a tree and a grid of people alongside the percentages.
The formula, piece by piece
Bayes' theorem
P(A | B) = P(A) × P(B | A) / P(B)
P(A | B) means "the chance of A, given that B happened". P(A) is the starting chance. P(B | A) is how often the evidence shows up when A is true, which for a test is its sensitivity. P(B) is how often the evidence shows up at all.
Building P(B)
P(B) = P(A) × P(B | A) + P(not A) × P(B | not A)
Add the positives from cases where A is true to the false alarms from cases where it isn't.
The odds form
Odds after = odds before × likelihood ratio
The likelihood ratio for a positive result is sensitivity divided by the false positive rate. A test with 95% sensitivity and a 5% false positive rate multiplies the odds by 19. Starting odds of 1 to 99 become 19 to 99, which is 16.1%.
Predictive values
PPV = P(A | positive) NPV = P(not A | negative)
The positive predictive value is the share of positive results that are real. The negative predictive value is the share of negative results that are genuinely clear. In machine learning, PPV is called precision and sensitivity is called recall.
Same test, different starting chance
Take a test that catches 95% of real cases with a 5% false alarm rate. Where 1% of people have the condition, out of 10,000 people 100 have it and 95 of them test positive. Of the 9,900 without it, 495 test positive anyway. Only 95 of the 590 positives are real: 16.1%.
Use exactly the same test where 30% have the condition, and 89.1% of positives are real. The test didn't change. Only the starting chance did. Drag the slider in "One test result" to watch this happen.
What the starting chance means for one person
For a whole population, the starting chance is how common the condition is. For one person it's their chance before the test, and that depends on why they're being tested. Someone with symptoms, or a family history, starts from a much higher chance than someone picked at random. The same positive result means more for them. That's one reason doctors think about who to test, not just which test to use.
Negative results and second tests
Negative results matter too. With the 95% and 5% test and a 1% starting chance, a negative result leaves about a 0.053% chance, around 1 in 1,900. That's reassuring, but it isn't zero.
A positive result can be followed by a second test, using the first result as the new starting chance. After one positive at 16.1%, a positive on a different test with a 90% catch rate and 3% false alarm rate raises the chance to 85.2%. This only works if the two tests make mistakes independently. Repeating the same test often doesn't qualify, because whatever fooled it the first time may fool it again.
Beyond medical tests
The taxi cab problem. In a problem made famous by Amos Tversky and Daniel Kahneman, 15% of a city's cabs are blue and 85% are green. A witness who's right 80% of the time says the cab in an accident was blue. The chance it really was blue is only 41.4%, because there are so many more green cabs to mistake.
Spam filters. If half of all email is spam, and the word "free" appears in 40% of spam but only 2% of genuine email, an email containing "free" has a 95.2% chance of being spam.
Several causes. Machines A, B and C make 50%, 30% and 20% of a factory's parts, with fault rates of 2%, 3% and 5%. A faulty part is equally likely to have come from A or C (34.5% each), and 31.0% likely to have come from B.
Mistakes that give the wrong answer
Confusing the catch rate with the chance a positive is real. A 95% sensitivity says how often the test catches real cases. It says nothing on its own about how many positives are real.
Ignoring the starting chance. The same test gives very different answers in different groups, as the 16.1% and 89.1% example shows.
Trusting "95% accurate". "Accuracy" can mean sensitivity, specificity or the share of all results that are right. Ask which one is meant.
The prosecutor's fallacy. The chance of the evidence if someone is innocent is not the chance they're innocent given the evidence. Mixing up P(evidence | innocent) and P(innocent | evidence) has led to serious errors in court. In the UK, the Royal Statistical Society publicly raised concerns about the statistics used in the 1999 conviction of Sally Clark, which was overturned in 2003.
How we calculate
All results follow directly from Bayes' theorem. When your inputs are simple fractions or percentages, the calculator also shows the exact fraction. The picture of people uses expected counts, rounded to whole numbers, from a group large enough that the smallest group is visible. The simulation creates 10,000 random cases in your browser and counts how many positives are real, so you can see the answer hold up.
The calculator assumes the catch rate and false alarm rate are the same for everyone in the group, and in "A second test", that the two tests' mistakes are independent. Real tests don't always meet these assumptions. Every number in the examples on this page was checked against an independent calculation in Python. If you spot a mistake, let us know and we'll correct it and note the change here.
References
- Ward Casscells, Arno Schoenberger and Thomas Graboys, "Interpretation by Physicians of Clinical Laboratory Results", New England Journal of Medicine, 299, 1978.
- Gerd Gigerenzer and Ulrich Hoffrage, "How to Improve Bayesian Reasoning Without Instruction: Frequency Formats", Psychological Review, 102(4), 1995.
- Gerd Gigerenzer and colleagues, "Helping Doctors and Patients Make Sense of Health Statistics", Psychological Science in the Public Interest, 8(2), 2007.
- Amos Tversky and Daniel Kahneman, "Evidential Impact of Base Rates", in Judgment Under Uncertainty: Heuristics and Biases, Cambridge University Press, 1982.
- Sheldon Ross, A First Course in Probability, 10th edition, Pearson, 2019. Chapter 3 covers conditional probability and Bayes' formula.
Related calculators
- Binomial Probability Calculator, including an exact test of whether a rate differs from what you expected
- Card Probability Calculator, where every draw changes the odds for the next
- Combinations and Permutations Calculator, including the birthday problem
- Coin Flip Probability Calculator and Dice Probability Calculator
- Probability Calculator, the full set of tools
Questions people ask
Why is the chance a positive is real so much lower than the test's sensitivity?
Sensitivity only describes people who have the condition. When the condition is rare, the much larger group without it produces enough false alarms to outnumber the real positives. A test with 95% sensitivity and a 5% false alarm rate gives only 16.1% real positives when 1% of people have the condition.
What's the difference between sensitivity and specificity?
Sensitivity is how often a test catches real cases. Specificity is how often it correctly clears cases that aren't real. Specificity is 100% minus the false positive rate.
Does a second test make the result more reliable?
Usually, if it's a different kind of test whose mistakes are independent of the first. Use the first result as the new starting chance. Repeating the same test helps less, because the same thing that fooled it once can fool it again.
What does a negative result tell me?
It lowers the chance, often a lot, but rarely to zero. With a 1% starting chance and a test that catches 95% of cases with a 5% false alarm rate, a negative result leaves about a 0.053% chance.
Can this tell me what my own test result means?
No. It shows how test results work in general. Your own starting chance and the performance of your particular test depend on things this page doesn't know, so please talk to a doctor or nurse about a real result.
What are precision and recall?
They're machine learning names for the same ideas. Precision is the share of positive predictions that are right, which is the positive predictive value. Recall is the share of real positives that get caught, which is sensitivity.
What is the prosecutor's fallacy?
It's confusing the chance of the evidence if someone is innocent with the chance they're innocent given the evidence. The two can be very different, for the same reason a positive test can be much less certain than the test's accuracy suggests.