DigitHelm

Bayes' Theorem Calculator

Calculate posterior probability P(A|B) from prior, sensitivity, and false positive rate. Includes natural frequencies, Bayes factor, and step-by-step derivation.

Quick Presets

Enter values as:

Base rate: how common is "Hypothesis A"?

Prob. of "Evidence B" when "Hypothesis A" is true

Prob. of "Evidence B" when "Hypothesis A" is false

What Is the Bayes' Theorem Calculator?

This calculator applies Bayes' theorem to compute the posterior probability P(A|B), that is, how likely your hypothesis is after observing a piece of evidence. You provide three numbers:

  • P(A), Prior: the base rate of hypothesis A before any evidence. For a medical test, this is the disease prevalence in the population.
  • P(B|A), Sensitivity: how often evidence B occurs when A is true. For a diagnostic test, this is the true positive rate.
  • P(B|¬A), False Positive Rate: how often B occurs when A is false. This is 1 minus the test's specificity.

The calculator returns the posterior P(A|B), a natural frequency confusion matrix, the Bayes factor, and a prior-sensitivity table so you can see how your answer would change if the base rate were different.

Formula

Bayes' Theorem

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

Law of Total Probability

P(B) = P(B|A) × P(A) + P(B|¬A) × P(¬A)

Complement

P(¬A) = 1 − P(A)

P(¬A|B) = 1 − P(A|B)

Bayes Factor (Likelihood Ratio)

BF = P(B|A) / P(B|¬A)

SymbolNameDescription
P(A)Prior probability, your initial belief in hypothesis A before seeing evidence (e.g. disease prevalence)
P(B|A)Likelihood / Sensitivity, probability of observing evidence B when A is true (e.g. true positive rate)
P(B|¬A)False positive rate, probability of observing B when A is false (1 − Specificity)
P(B)Marginal probability of B, calculated via the law of total probability
P(A|B)Posterior probability, updated belief in A after observing evidence B (the main answer)
P(¬A|B)Posterior complement, probability that A is false given B; equals 1 − P(A|B)
BFBayes factor, ratio P(B|A) / P(B|¬A); measures how strongly evidence supports A over ¬A

How to Use

  1. 1
    Choose a preset or enter custom values: Pick "Medical Test", "Drug Screen", "Spam Filter", or "Cancer Screen" to auto-fill a realistic example, or type your own values directly.
  2. 2
    Select your input format: Toggle between Decimal (0–1) and Percentage (%), the calculator converts existing values automatically when you switch.
  3. 3
    Label your hypothesis and evidence (optional): Type custom names like "Has disease" and "Test positive" to personalise the results table and probability bars.
  4. 4
    Enter the three probabilities: Fill in P(A), P(B|A), and P(B|¬A). Press Enter or click Calculate.
  5. 5
    Read the results: The posterior P(A|B) is shown prominently. Scroll down for the natural frequency table, Bayes factor, prior-sensitivity rows, and the full step-by-step derivation.
  6. 6
    Explore the sensitivity table: See how the posterior probability would change if the prior (base rate) were 0.1%, 1%, 5%, 10%, etc. The current prior is highlighted for easy comparison.

Example Calculation

Medical Test, Disease Prevalence 1%, Sensitivity 95%, FPR 10%

Imagine a disease affects 1% of people (prior P(A) = 0.01). A test correctly identifies 95% of sick patients (sensitivity = 0.95) and falsely flags 10% of healthy people (false positive rate = 0.10). You test positive, what is the actual probability you have the disease?

Step 1, Total probability of a positive test:

P(B) = 0.95 × 0.01 + 0.10 × 0.99

= 0.0095 + 0.0990 = 0.1085

Step 2, Apply Bayes' theorem:

P(A|B) = (0.95 × 0.01) / 0.1085

= 0.0095 / 0.1085 ≈ 0.0876

Posterior P(A|B) ≈ 8.76%

The Base Rate Paradox

A test that is 95% accurate sounds impressive, but a positive result only carries an 8.76% chance of disease. Why? Because the disease is rare (1% prior). For every 10,000 people tested:

  • 100 have the disease → 95 test positive (true positives)
  • 9,900 are healthy → 990 test positive (false positives)
  • Total positives: 1,085, of which only 95 are real cases (8.76%)

Understanding Bayes' Theorem

A Brief History of Bayes' Theorem

Thomas Bayes, an 18th-century English minister and mathematician, developed the core idea around 1740, but never published it. The theorem was found among his papers after his death in 1761 and published posthumously in 1763 by his friend Richard Price. Pierre-Simon Laplace independently derived and extended the result, giving it the rigorous mathematical form we use today.

For nearly two centuries the theorem was largely a mathematical curiosity. It gained practical power in World War II when Alan Turing and his team at Bletchley Park used a Bayesian approach, called the Banburismus method, to crack the German Enigma machine, estimating the probability that intercepted messages shared the same daily settings.

Why the Prior Matters So Much

The most counterintuitive result in Bayesian reasoning is how strongly the prior shapes the posterior. When a condition is rare, a disease affecting 1 in 10,000, even a highly accurate test will produce mostly false positives. This happens because the pool of healthy people who might trigger a false alarm vastly outnumbers the small group of true cases.

This is not a flaw in the test, it is a mathematical reality. Understanding it prevents the base rate fallacy: ignoring the prior and treating a positive test as nearly certain proof. Physicians, forensic analysts, and security engineers all need to account for base rates when interpreting diagnostic or detection results.

Interpreting the Bayes Factor

The Bayes factor (BF) is the ratio of the likelihood of evidence under hypothesis A to its likelihood under ¬A: BF = P(B|A) / P(B|¬A). It tells you how much the evidence alone shifts your belief, independent of the prior.

Bayes FactorInterpretation
< 1/30Very strong evidence against A
1/30 – 1/10Strong evidence against A
1/10 – 1/3Moderate evidence against A
1/3 – 1Weak evidence against A
1 – 3Weak evidence for A
3 – 10Moderate evidence for A
10 – 30Strong evidence for A
> 30Very strong evidence for A

A Bayes factor of 9.5 (as in the medical test example) falls in the "moderate evidence for A" range, the positive test does update your belief, but the low prior means the posterior is still below 9%.

Natural Frequencies: A Clearer Way to Think

Gerd Gigerenzen and colleagues showed that people understand Bayesian reasoning far better when it is expressed as natural frequencies (actual counts of people) rather than conditional probabilities. Instead of saying "P(A|B) = 8.76%", say "of 1,000 people who test positive, about 88 actually have the disease".

The natural frequency table in this calculator does exactly that, it takes your probabilities and shows the corresponding counts out of 10,000 people, making the true positive, false positive, false negative, and true negative numbers immediately tangible.

Real-World Applications

  • Medical diagnosis: Computing positive predictive value (PPV) and negative predictive value (NPV) for laboratory tests, imaging, and genetic screening.
  • Spam and fraud detection: Email services use Bayesian classifiers, updated with each new keyword, to flag spam while keeping false positive rates low enough to be usable.
  • Forensic evidence: The "prosecutor's fallacy" is a legal base-rate error. Bayes' theorem correctly accounts for how rare certain DNA profiles are in the general population.
  • Machine learning: Naïve Bayes classifiers, Bayesian neural networks, and probabilistic graphical models are all built on this foundation.
  • A/B testing: Bayesian A/B testing updates the probability that a variation beats the control continuously as data arrives, rather than waiting for a fixed sample size.
  • Security: Intrusion detection systems weight alerts by prior probability of attack; Bayesian reasoning keeps false alarm rates manageable in networks with millions of benign events per day.

Bayesian vs. Frequentist Statistics

Frequentist statistics treats probability as long-run frequency, only random, repeatable events have a probability. The Bayesian framework treats probability as a degree of belief that can apply to any uncertain proposition, whether repeatable or not. Bayes' theorem is the formal update rule: prior belief + evidence = posterior belief.

Neither approach is universally superior. Frequentist methods (p-values, confidence intervals) dominate classical hypothesis testing. Bayesian methods shine when you have strong prior knowledge, need to incorporate sequential evidence, or want to express uncertainty in a form that is directly interpretable as probability.

Frequently Asked Questions

What is Bayes' theorem in simple terms?

Bayes' theorem is a formula for updating a probability estimate when you learn new information. You start with a prior belief, observe evidence, and apply the theorem to get a revised posterior probability.

In plain English: the more likely the evidence is when A is true (relative to when A is false), the more a positive result should raise your confidence in A, but only scaled by how common A was to begin with.

Think of it as a rational belief-updating machine: each new piece of evidence nudges your probability estimate up or down.

Why does a highly accurate test still give mostly false positives?

This is the base rate paradox. When a condition is rare, the large pool of healthy people generates many false positives, even with a low false positive rate, because that small percentage applies to a huge number.

Example with 1% prevalence, 95% sensitivity, 10% FPR per 10,000 people:

  • 100 true cases → 95 true positives
  • 9,900 healthy → 990 false positives
  • Only 95 / 1,085 ≈ 8.76% of positives are real cases

The fix is to improve specificity (lower FPR) or to screen populations with higher base rates, not just increase sensitivity.

What is the difference between prior, likelihood, and posterior?

These three terms describe the three stages of Bayesian reasoning:

  • Prior P(A): your belief before any evidence, e.g., disease prevalence in the population.
  • Likelihood P(B|A): how probable the observed evidence is if the hypothesis A were true.
  • Posterior P(A|B): your updated belief after combining prior and likelihood.

The posterior from one calculation becomes the prior for the next, this is how Bayesian reasoning accumulates evidence over time.

What is the Bayes factor and how do I interpret it?

The Bayes factor is the ratio of the probability of evidence under hypothesis A to its probability under ¬A:

BF = P(B|A) / P(B|¬A)

Key interpretations:

  • BF = 1: evidence is equally consistent with both hypotheses, doesn't help.
  • BF = 10: the evidence is 10× more likely under A, strong support.
  • BF = 0.1: the evidence is 10× more likely under ¬A, evidence against A.

A large BF does not mean A is probable, it means the evidence strongly updates your belief. The prior still matters.

What is the base rate fallacy?

The base rate fallacy happens when you ignore the prior probability (base rate) and focus only on the test's accuracy.

Classic example: a psychologist claims that 85% of people accused of a crime are guilty, because their profile matches a guilty person. But if only 1 in 1,000 people commit that crime, even a highly accurate profile will flag many innocent people.

It appears in many domains:

  • Medical: overconfidence in a diagnostic test for a rare disease
  • Legal: the prosecutor's fallacy, treating DNA match probability as guilt probability
  • Security: treating every intrusion alert as a real attack when 99.9% are false alarms

The fix is to always anchor your reasoning in a realistic prior before interpreting evidence.

What is Positive Predictive Value (PPV) and how does Bayes' theorem relate?

Positive Predictive Value (PPV) is the proportion of positive test results that are genuine (true positives). It is exactly what Bayes' theorem computes:

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

Similarly, Negative Predictive Value (NPV) is 1 − P(A|¬B), the probability that a negative result means the condition is truly absent.

Both depend heavily on prevalence. A test with fixed sensitivity and specificity will have:

  • High PPV in high-prevalence populations (e.g. symptomatic patients)
  • Low PPV in low-prevalence populations (e.g. general screening)

Can I use Bayes' theorem with more than two hypotheses?

Yes. The generalised form handles any number of mutually exclusive hypotheses H₁, H₂, …, Hₙ:

P(Hᵢ|B) = P(B|Hᵢ) × P(Hᵢ) / Σⱼ [P(B|Hⱼ) × P(Hⱼ)]

This is used in:

  • Naïve Bayes classifiers (many category labels, each with its own prior and likelihood)
  • Medical differential diagnosis (multiple competing diseases)
  • Bayesian model comparison (comparing several statistical models)

This calculator solves the standard binary case (A vs ¬A). For multi-hypothesis problems, the same logic applies, just add a row for each hypothesis.

How is Bayesian statistics different from frequentist statistics?

The key differences:

AspectFrequentistBayesian
ProbabilityLong-run frequencyDegree of belief
ParametersFixed, unknown constantsRandom variables with distributions
Prior knowledgeNot usedIncorporated via prior
Outputp-value, confidence intervalPosterior distribution
Update ruleN/ABayes' theorem

Does the calculator save my inputs?

Yes, your inputs are saved automatically to your browser's localStorage as you type. This means:

  • If you accidentally close the tab, your values are restored when you come back.
  • The input mode (decimal vs percentage) and custom labels are also remembered.
  • Data stays in your browser, nothing is sent to any server.

To clear everything and start fresh, click the Reset All button, it wipes both the form and the saved data.

What are some limitations of Bayes' theorem?

Bayes' theorem is mathematically exact, but its practical usefulness depends on the quality of your inputs:

  • Prior accuracy: A wrong prior produces a misleading posterior. If the base rate is unknown or contested, the result is sensitive to your assumption.
  • Conditional independence: The simple two-hypothesis form assumes P(B|A) and P(B|¬A) fully describe the evidence. Real evidence can be correlated with other factors.
  • Defining events precisely: Vague hypotheses ("the defendant is guilty") lead to poorly defined likelihoods.
  • The choice of prior: Informative priors encode strong assumptions. When prior data is scarce, choosing a prior is subjective, different analysts may reach different posteriors.

Despite these limitations, Bayes' theorem remains the correct framework for rational belief update under uncertainty when the inputs are reliable.

Related Calculators