Bayes' Theorem Calculator
Calculate posterior probability P(A|B) using Bayes' theorem. Enter a prior, sensitivity, and false positive rate to see how evidence updates your belief — with step-by-step working and a Venn diagram.
Calculating posterior probability...
Enter a prior probability, sensitivity, and false positive rate, then click Calculate to see the posterior probability.
What Is Bayes' Theorem?
Bayes' theorem is a mathematical formula that describes how to update a probability estimate when new evidence arrives. Named after Reverend Thomas Bayes, the formula connects the probability of a hypothesis being true before seeing evidence (called the prior) with the probability after seeing the evidence (called the posterior). It is the mathematical foundation of conditional probability and is used in medicine, artificial intelligence, spam filters, and scientific research.
The core insight is simple but counterintuitive: how trustworthy a positive result is depends not just on the accuracy of the test, but also on how common the condition is in the first place. A highly accurate test applied to a very rare condition still produces many false positives.
The Bayes' Theorem Formula Explained
The standard formula is:
How Bayes' Theorem Components Connect
Your initial belief in the hypothesis before seeing any evidence. Also called the base rate or prevalence.
Probability of observing the evidence if the hypothesis is true. Equal to the test's sensitivity or true positive rate.
Updated probability after seeing the evidence. This is what the calculator returns — the answer to the Bayesian question.
Frequency Tree: Why the Posterior Is So Low
Prior 1% · Sensitivity 95% · Specificity 90% — imagining 10,000 people tested
How to Calculate Posterior Probability
To use Bayes' theorem, you need three numbers. First, the prior probability P(A) — how likely is the hypothesis before any test? Second, the sensitivity P(B|A) — if the hypothesis is true, how often does the evidence appear? Third, the false positive rate P(B|¬A) — if the hypothesis is false, how often does the evidence still appear?
Multiply the sensitivity by the prior to get the probability of a true positive. Compute the total probability of positive evidence P(B) by adding the true positive probability and the false positive probability. Divide the true positive probability by P(B) to get the posterior.
Example: a medical condition affects 1 in 100 people (prior = 0.01). A test has 95% sensitivity and 90% specificity (false positive rate = 0.10). P(B) = 0.95×0.01 + 0.10×0.99 = 0.1085. Posterior = 0.0095 / 0.1085 ≈ 8.76%. Despite the test's high sensitivity, only 8.76% of people who test positive actually have the condition.
Bayes' Theorem in Medical Testing
Medical testing is the most widely cited application of Bayes' theorem, and for good reason. Doctors and patients routinely misinterpret test results because they ignore the base rate. This cognitive error is called the base rate fallacy or base rate neglect.
Consider a COVID-19 rapid test with 90% sensitivity and 95% specificity used when community prevalence is 2%. Even with those solid accuracy figures, a positive result carries only a 27% posterior probability that the person is actually infected. If prevalence drops to 0.5%, the posterior falls to just 8.3%. The rarer the condition, the less a positive result should be trusted.
The same reasoning applies to cancer screening, HIV testing, drug testing, and any binary diagnostic procedure. Sensitivity tells you how good the test is at catching true cases. Specificity tells you how well it avoids false alarms. Neither number alone tells you what a positive result means — for that, you need the base rate and Bayes' theorem.
Common Bayes' Theorem Examples
Spam filters: An email arrives. The prior probability that any email is spam is 30%. Words like "free" and "click here" appear in 99% of spam but only 5% of legitimate email. Bayes' theorem updates the posterior probability that this specific email is spam to about 89%.
Drug testing in sport: If 5% of athletes use a banned substance and a test has 98% sensitivity and 97% specificity, a positive test carries a posterior probability of roughly 63% — meaning about one in three positives is a false alarm, even with a highly accurate test.
Machine learning classifiers: Naive Bayes classifiers use Bayes' theorem to assign class probabilities for text classification, sentiment analysis, and document categorisation. The "naive" assumption is that features are independent given the class, but despite that simplification the algorithm performs remarkably well in practice.
Legal evidence: If DNA evidence from a crime scene matches a suspect, the posterior probability of guilt depends not just on the match probability but also on the prior probability that the suspect was at the scene. Ignoring the prior — treating a match probability as a guilt probability — is called the prosecutor's fallacy.
Common Bayes' Theorem Reference Table
| Scenario | Prior | Sensitivity | Specificity | Posterior P(A|B) |
|---|---|---|---|---|
| Rare disease (1 in 100) | 1% | 95% | 90% | 8.76% |
| Common condition (1 in 10) | 10% | 90% | 95% | 67.67% |
| Spam filter (30% spam) | 30% | 99% | 95% | 89.44% |
| Drug test (10% users) | 10% | 90% | 95% | 67.67% |
| Security intrusion (1% events) | 1% | 99% | 99% | 50.00% |
Bayes' Theorem FAQ
What is Bayes' theorem?
Bayes' theorem is a formula that calculates the probability of a hypothesis being true after observing evidence. It combines the prior probability (initial belief before evidence), the likelihood (how probable the evidence is if the hypothesis is true), and the marginal probability (overall probability of the evidence) to produce the posterior probability — the updated belief after the evidence.
What is the difference between prior and posterior probability?
The prior is your belief in a hypothesis before seeing any new evidence. The posterior is your updated belief after incorporating that evidence through Bayes' theorem. For example, if 2% of the population has a disease (prior = 0.02) and you test positive, the posterior might be 15% — the evidence raised your probability, but the low prior kept it far below the test's raw accuracy.
Why does a positive medical test not always mean you have the condition?
Because the prior probability — how common the condition is — determines how many false positives exist in the population. If only 1 in 1,000 people have a condition, even a 99% accurate test will produce many more false positives than true positives among all tested people. The posterior probability (the chance you actually have the condition given a positive test) can be far lower than the test's accuracy percentage.
What is sensitivity and specificity in Bayes' theorem?
Sensitivity (true positive rate) is P(B|A) — the probability the test is positive when the condition is truly present. Specificity (true negative rate) is P(¬B|¬A) — the probability the test is negative when the condition is truly absent. The false positive rate used in Bayes' theorem equals 1 minus specificity. High sensitivity reduces missed cases; high specificity reduces false alarms. Both matter, and neither alone determines the posterior probability without the prior.
What is the base rate fallacy?
The base rate fallacy occurs when people focus on the accuracy of a test while ignoring how rare or common the condition is. Humans are naturally bad at this calculation — we tend to treat a 95% accurate test as meaning a 95% probability the result is correct. Bayes' theorem corrects that intuition by forcing the prior into the calculation. The rarer the condition, the more false positives swamp true positives, and the less each individual positive result should be trusted.
How is Bayes' theorem used in machine learning?
Naive Bayes classifiers apply Bayes' theorem to classify text, emails, and data records. The algorithm calculates the posterior probability of each class label given the observed features, then assigns the most likely label. It is computationally efficient and works well for spam detection, sentiment analysis, and medical diagnosis systems. Modern Bayesian neural networks also use probabilistic weights to express uncertainty in predictions rather than outputting single deterministic values.
What does P(A|B) mean?
P(A|B) is read as "the probability of A given B." It means: assuming B has already occurred or been observed, what is the probability that A is also true? In Bayes' theorem, A is typically the hypothesis (e.g. having a disease) and B is the evidence (e.g. a positive test result). The vertical bar "|" means "given that" or "conditional on."
Related Calculators
Probability Calculator
Compute simple, joint, and conditional probabilities
Bayesian Logic Calculator
Interactive slider-based true positive probability tool
Normal Distribution Calculator
Probabilities under the Gaussian bell curve
Binomial Distribution Calculator
Probability of k successes in n Bernoulli trials
P-Value Calculator
Statistical significance testing from test statistics
Chi-Square Calculator
Goodness-of-fit and independence tests