Statistics & Probability Probability Fundamentals Bayes' theorem with the law of total probability

Bayes' Theorem Calculator

Enter how common a condition is, how often the test catches it, and how often the test is right when the condition is absent, and this calculator returns the probability that a positive result is genuine. That number is Bayes' theorem applied to a diagnostic test, and it is routinely far lower than people expect: a 99% sensitive, 95% specific test for a condition affecting 1 person in 100 produces a positive result that is correct only one time in six. The natural-frequency table below the result shows why.

Calculator

This calculator runs in your browser. Enable JavaScript for live results — the inputs, formula and worked example below remain fully readable without it.

Inputs this calculator takes, with typical values
InputWhat to enterExample
Prior probability (prevalence)How likely the condition is before testing - the base rate in the group being tested.1 %
Sensitivity, P(positive | condition)The share of people who have the condition that the test correctly flags.99 %
Specificity, P(negative | no condition)The share of people without the condition that the test correctly clears; the false positive rate is 100 minus this.95 %
Population for the frequency tableOnly affects the natural-frequency table; the probabilities are unchanged by it.100000

It returns

  • P(condition | positive test) — The posterior probability - how much a positive result should actually worry you.
  • P(no condition | negative test)
  • P(condition | negative test)
  • P(positive test) overall
  • Positive likelihood ratio
  • Negative likelihood ratio

The formula

P(A|B)=P(B|A)P(A)P(B|A)P(A)+P(B|A)P(A)
LR+=sensitivity1specificity

In plain text: P(A|B) = P(B|A) P(A) / [ P(B|A) P(A) + P(B|A') P(A') ]

  • P(A)Prior probability of the condition before testing (probability)
  • P(B|A)Sensitivity: probability of a positive test given the condition (probability)
  • P(B|A')False positive rate: 1 minus specificity (probability)
  • P(A|B)Posterior probability of the condition given a positive test (probability)

The denominator is the law of total probability: the overall chance of a positive result, counting both the true positives and the false positives.

Updated Category Probability Fundamentals Verified against published test cases Reading time 10 min

What Bayes' theorem does

Bayes' theorem tells you how to revise a probability when new evidence arrives. You start with a prior - how likely something was before you looked. Evidence arrives. The theorem gives you the posterior: how likely it is now.

The medical test is the standard illustration because every quantity has a name. The prior is the prevalence: how common the condition is in the group being tested. The evidence is a positive test result. The posterior is the positive predictive value: given that you tested positive, how likely is it that you actually have the condition?

The reason this needs a theorem rather than intuition is that people confuse two different conditional probabilities. Sensitivity is P(positive test | you have the condition). What a patient wants to know is P(you have the condition | positive test). Those are not the same number and can differ by an order of magnitude. Reading one as the other is called the base rate fallacy, and it is the single most consequential probability error in medicine, law and security screening.

Nothing here is specific to medicine. Substitute spam filtering, fraud detection, drug testing, machine-vision inspection or a criminal-trial forensic match and the arithmetic is identical: a rare condition plus an imperfect test produces mostly false alarms, because the false positives come from the large group and the true positives come from the small one.

The formula, term by term

Written for a diagnostic test, Bayes' theorem is

PPV = (prevalence x sensitivity) / [ prevalence x sensitivity + (1 - prevalence) x (1 - specificity) ]

The numerator is the fraction of everybody who has the condition and tests positive: the true positives. The denominator is the fraction of everybody who tests positive at all, which the law of total probability splits into two groups - the true positives, plus the false positives drawn from the healthy majority. Divide one by the other and you have the share of positive tests that are genuine.

The decisive term is the second one in the denominator. When prevalence is 1%, the healthy group is 99 times larger than the affected group, so even a small false positive rate generates a large absolute number of false alarms. At 95% specificity, 5% of that 99% is 4.95% of the whole population - five times the 0.99% of true positives that a 99% sensitive test finds.

There is a second, more compact way to write the same thing, and it is the form clinicians actually use. Convert the prior to odds, multiply by the likelihood ratio, and convert back:

posterior odds = prior odds x LR+, where LR+ = sensitivity / (1 - specificity)

The likelihood ratio is a single number summarising how much the evidence moves you, independent of the prior. An LR+ of 19.8 multiplies your odds by about 20 whatever they were. An LR+ of 1 means the test carries no information at all - which happens exactly when sensitivity equals the false positive rate. LR+ values above 10 are usually described as strongly diagnostic and values below 2 as weak, and the corresponding likelihood ratio calculator works through that framing in more detail.

Worked example: a rare condition and a very good test

A condition affects 1% of the tested population. The test has 99% sensitivity and 95% specificity. You test positive. Work it out on 100,000 people, which is the easiest way to do it in your head.

  1. Split the population by condition. 1% of 100,000 = 1,000 have the condition; 99,000 do not.
  2. Test the affected group. Sensitivity 99% flags 990 of the 1,000. The other 10 are false negatives.
  3. Test the healthy group. Specificity 95% clears 94,050 of the 99,000. The other 5% - 4,950 people - test positive incorrectly.
  4. Count the positives. 990 + 4,950 = 5,940 positive tests in total.
  5. Divide. PPV = 990 / 5,940 = 16.67%, exactly one in six.

So the excellent test moved you from a 1% prior to a 16.67% posterior. That is a large move - your odds went from 1:99 to 1:5, a factor of 19.8, which is precisely the positive likelihood ratio 0.99 / 0.05 = 19.8. But it still leaves five chances in six that you are fine, because 4,950 false positives swamp 990 true ones.

The negative result is a different story. Of the 94,060 negative tests, 94,050 are genuinely clear, so the negative predictive value is 94,050 / 94,060 = 99.989%. A negative result on this test is close to conclusive; a positive one is a reason for a second, more specific test. That asymmetry is the normal design of a screening programme: screen with a sensitive test, confirm with a specific one.

Change one number and the picture changes completely. Run the same test on a group where prevalence is 30% rather than 1% - people already showing symptoms - and the arithmetic gives 29,700 true positives against 3,500 false ones, a PPV of 89.5%. The test did not improve. The population did.

How to read the posterior

Compare the posterior with the prior. The difference is what the evidence bought you, and the ratio of the odds is exactly the likelihood ratio. If the posterior is close to the prior, the test barely moved you and the result should not change your decision.

Judge the posterior against the decision you face, not against 50%. A 16.67% chance of a serious condition is high enough to justify a confirmatory test in almost any clinical setting, even though it means the positive was probably wrong. Conversely a 90% posterior may not be enough to justify an irreversible intervention. The probability is an input to a decision that also involves the cost of each kind of error.

Watch which population your prevalence describes. Published prevalence figures usually describe a whole country, while the person in front of you belongs to a much narrower group defined by age, symptoms and referral route. Screening an unselected population and testing a symptomatic patient are the same test applied to different priors, and the posterior can differ by a factor of ten. When you do not have a defensible prior, run the calculation across a range of plausible values and see whether your decision changes.

The predictive values are properties of the test and the population. Sensitivity and specificity are properties of the test alone, which is why manufacturers quote them and why they transfer between settings. Never quote a PPV from one study as though it applied to a different population - that is the most common way the base rate fallacy re-enters through the back door.

The same 99%/95% test at different prevalences

Sensitivity 99%, specificity 95% throughout. Only the prevalence changes. Every cell was produced by running this calculator's formula at that prevalence.
PrevalenceTrue positives per 100,000False positives per 100,000PPVNPV
0.01%9.94,999.50.198%99.99989%
0.1%994,9951.943%99.99895%
1%9904,95016.667%99.98937%
5%4,9504,75051.031%99.94463%
10%9,9004,50068.750%99.88318%
30%29,7003,50089.458%99.55090%
50%49,5002,50095.192%98.95833%

The positive likelihood ratio is 19.8 in every row - the test is unchanged. What changes is the prior odds it multiplies. PPV crosses 50% at the prevalence where true and false positives balance, which for this test is about 4.81%.

Where Bayesian reasoning goes wrong

  • Reading sensitivity as the answer. A 99% sensitive test does not mean a positive result is 99% likely to be right. Sensitivity conditions on having the condition; the patient's question conditions on the test result. Swapping the two is the base rate fallacy.
  • Using a population prevalence for a selected patient. The prior should describe the group your subject actually belongs to. Symptoms, exposure history and referral all raise the prior long before the test is run.
  • Assuming a second test is independent of the first. Repeating the same test rarely gives independent evidence - the same biological or technical reason that produced the first false positive is usually still present. Multiplying likelihood ratios requires conditional independence, which is a strong assumption.
  • Setting a prior to exactly 0 or exactly 1. Bayes' theorem multiplies the prior, so a probability of zero stays zero no matter what evidence arrives, and a probability of one stays one. Reserve those values for logical impossibility and certainty.
  • Ignoring the negative result. Predictive value cuts both ways. A highly sensitive test with a modest PPV can still rule a condition out very effectively, which is often the clinically useful half.
  • Quoting specificity when you mean the false positive rate. They are complements: a 95% specificity is a 5% false positive rate. Entering 5 where 95 belongs inverts the entire calculation and usually produces a suspiciously reassuring answer.

Related tools and the wider framework

Sensitivity and specificity come from a validation study in which the test was compared against a reference standard, and the sensitivity and specificity calculator derives them from the raw counts. Going the other way, the PPV and NPV calculator works directly in predictive values, and the likelihood ratio calculator expresses the same evidence as a single multiplier on the odds.

For general probability questions that do not involve a test - unions, intersections, conditional probabilities among arbitrary events - the probability calculator handles the algebra, and the identity behind Bayes' theorem is visible there as the definition of conditional probability. The odds ratio calculator works in the odds scale that makes the likelihood ratio form so convenient.

Bayes' theorem is also the foundation of an entire school of statistical inference. In that setting the prior is a distribution over an unknown parameter rather than a single probability, the likelihood is a function rather than a number, and the posterior is another distribution. The arithmetic on this page is that machinery at its smallest scale - two hypotheses, one binary observation - but the structure is identical: prior times likelihood, normalised.

One practical habit is worth adopting from this page's table. Whenever a conditional probability confuses you, stop doing algebra and count people. Take 100,000 of them, split them by the condition, apply the test to each group, and read the answer off the four resulting cells. Natural frequencies make the base rate visible, and experiments consistently show that people reason far more accurately when the question is posed that way.

Frequently asked questions

Why is the probability so low when the test is 99% accurate?

Because the false positives come from a much larger group. With 1% prevalence, 99,000 of 100,000 people are healthy, so even a 5% false positive rate produces 4,950 wrong positives against only 990 right ones. Accuracy figures describe how the test performs on each group; the predictive value depends on how big those groups are.

What is the difference between sensitivity and positive predictive value?

Sensitivity is P(test positive given the condition) and is a property of the test. Positive predictive value is P(condition given a positive test) and depends on the test and the prevalence. Sensitivity transfers between populations; predictive value does not.

Where do I get the prior?

From the base rate in the group your subject belongs to: published prevalence for a screening programme, or a clinical estimate that already accounts for age, symptoms and exposure. If no defensible figure exists, run the calculation across a range and check whether your decision changes across that range.

How do I combine two test results?

Use the posterior from the first test as the prior for the second, or equivalently multiply the prior odds by both likelihood ratios. This is only valid if the two tests are conditionally independent given the condition. Repeating the same assay usually violates that, because whatever caused the first false positive tends to persist.

What is a likelihood ratio and why is it useful?

The positive likelihood ratio is sensitivity divided by the false positive rate, and it is the factor by which a positive result multiplies your odds. Because it does not depend on prevalence, it summarises the test's evidential strength in one number that transfers between populations. An LR of 1 means the test is uninformative.

Can a positive result make the condition less likely?

Only if the positive likelihood ratio is below 1, which means the test flags healthy people more often than affected ones. That indicates the test is mislabelled or the sensitivity and specificity have been swapped. For any usable test the LR+ exceeds 1, so a positive result raises the probability.

Does this work outside medicine?

Yes, unchanged. Spam filters, fraud alerts, airport screening, quality inspection and forensic matching all have the same structure: a base rate, a detection rate, and a false alarm rate. The base rate fallacy is if anything more damaging in security screening, where the base rate can be one in millions.

What is the natural-frequency table for?

It converts the probabilities into counts of people, which makes the base rate visible. Research on medical decision making has repeatedly found that both clinicians and patients answer conditional probability questions far more accurately when the same information is presented as frequencies rather than percentages.

References

  • An Essay towards solving a Problem in the Doctrine of Chances, Philosophical Transactions 53, 370-418 — Thomas Bayes, communicated by Richard Price, 1763
  • Helping doctors and patients make sense of health statistics, Psychological Science in the Public Interest 8(2), 53-96 — Gigerenzer et al., 2007
  • Clinical Epidemiology: The Essentials, 5th ed. — Lippincott Williams & Wilkins (Fletcher & Fletcher)