Bayes' Theorem Calculator
Calculate posterior probability P(A|B) using Bayes' theorem. Enter prior probability, sensitivity, and specificity to update beliefs with new evidence.
Results
Bayes' Theorem
Bayes' theorem provides a mathematically rigorous way to update probabilities when new evidence arrives: P(A|B) = P(B|A) × P(A) / P(B). In words: the posterior probability of A given B equals the likelihood of B given A times the prior probability of A, divided by the total probability of B.
The Medical Testing Example
Suppose a disease affects 1% of the population. A test has 99% sensitivity (correctly identifies 99% of true cases) and 99% specificity (correctly identifies 99% of non-cases, meaning 1% false positive rate). If you test positive, what's the probability you actually have the disease?
Intuition says "99%" — but Bayes' theorem gives ~50%. Why? Of 10,000 people: 100 have the disease (99 test positive). 9,900 don't (99 false positives). Total positives: 198. True positives: 99. P(disease | positive) = 99/198 = 50%.
Prior Matters
This is the base rate fallacy — ignoring the prior probability. Rare conditions have low prior probability, so even accurate tests yield many false positives. This is why mass screening for rare diseases requires careful analysis, and why doctors order confirmatory tests.
Worked Example: Email Spam Filter
A spam filter knows: 30% of emails are spam (prior P(spam) = 0.30). The word "free" appears in 80% of spam but only 10% of legitimate emails.
Using Bayes' theorem to find P(spam | "free"):
P(B) = P("free" | spam) × P(spam) + P("free" | not spam) × P(not spam) = 0.80 × 0.30 + 0.10 × 0.70 = 0.24 + 0.07 = 0.31.
P(spam | "free") = P("free" | spam) × P(spam) / P(B) = (0.80 × 0.30) / 0.31 = 0.24 / 0.31 ≈ 77.4%.
So an email containing "free" has a 77.4% probability of being spam — higher than the base rate of 30%, but not 100%.
Frequently Asked Questions
P(A|B) = P(B|A)×P(A) / P(B). It tells you how to update the probability of A after observing evidence B. P(A) is prior belief; P(A|B) is posterior belief after seeing B. Evidence that is more likely under A than under ¬A increases P(A|B).
Sensitivity = P(positive test | disease present) = true positive rate. Specificity = P(negative test | no disease) = true negative rate. High sensitivity minimizes missed cases; high specificity minimizes false alarms. The perfect test has 100% of both.
Because the prior (prevalence) matters. For a rare disease (1% prevalence) even a 99% accurate test gives ~50% chance of truly having it on a positive result. For a common condition (50% prevalence) the same test gives ~99% chance. Context is everything.
Frequentist statistics treats probability as a long-run frequency and doesn't incorporate prior beliefs. Bayesian statistics treats probability as a degree of belief and explicitly updates it with Bayes' theorem as evidence accumulates. Both frameworks have practical uses.
When testing for a rare event, even a highly accurate test produces more false positives than true positives. Example: a disease with 0.1% prevalence tested with 99% accuracy: in 100,000 people, 100 have the disease (99 test positive) and 99,900 do not (999 false positives). Of 1,098 total positives, only 99 are true — just 9% accuracy in practice.
The likelihood ratio (LR) = P(evidence | hypothesis) / P(evidence | not hypothesis). An LR of 10 means the evidence is 10 times more likely under the hypothesis than without it. In medicine, a test's positive LR = sensitivity / (1 − specificity). Higher LR = stronger evidence.