Skip to content
induwara.lk
induwara.lkStatistics · Probability

Bayes' Theorem Calculator

Enter a prior P(A), a true-positive rate P(B|A) and a false-positive rate P(B|¬A) to get the posterior probability P(A|B), the evidence P(B), a step-by-step substitution and a probability tree. It is the fastest way to settle the classic false-positive paradox, runs entirely in your browser, and needs no signup.

By Induwara AshinsanaUpdated Sep 11, 2026
Bayes' theorem calculator
Input format

Base rate of A before any evidence.

True-positive rate / sensitivity: chance of B when A is true.

Chance of B when A is false. Equals 1 − specificity.

Presets
Posterior P(A|B)
16.67%
Exact: 0.1667
Complement P(¬A|B)
83.33%
Exact: 0.8333
Evidence P(B)
5.94%
Exact: 0.0594 (the normaliser)

Observing B raises the probability of A from 1.0% (prior) to 16.7% (posterior). Even with a 99.0% true-positive rate, the posterior can stay modest when A is rare, false positives outnumber true positives.

Step-by-step substitution

P(¬A) = 1 − 0.0100 = 0.9900

P(B) = 0.9900·0.0100 + 0.0500·0.9900 = 0.0594

P(A|B) = (0.9900·0.0100) / 0.0594 = 0.0099 / 0.0594 = 0.1667

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

Probability tree

BranchOutcomeJointProbability
A → BTrue positive0.9900·0.01000.0099
A → ¬BFalse negative(1−0.9900)·0.01000.0001
¬A → BFalse positive0.0500·0.99000.0495
¬A → ¬BTrue negative(1−0.0500)·0.99000.9405
Total (must equal 1)1.0000

Cross-check. The odds form gives prior odds 0.0101 × likelihood ratio 19.8000 = posterior odds 0.2000, i.e. P(A|B) = 0.1667. It matches the direct calculation, as it must, the result is verified.

Method: P(A|B) = P(B|A)·P(A) / [P(B|A)·P(A) + P(B|¬A)·P(¬A)] (Bayes' theorem with the law of total probability). Sources cited below the calculator. No data leaves this page.

How it works

Bayes' theorem updates a prior belief about a hypothesis A once you observe evidence B. It rests on two simpler rules of probability and combines them into a single formula. Throughout, let A be the event of interest (“has the condition”, “is spam”) and B the observed evidence (“tests positive”, “contains the word”).

The full statement, with the denominator expanded, is:

P(A|B) = P(B|A)·P(A) / [ P(B|A)·P(A) + P(B|¬A)·P(¬A) ]

  1. Complement the prior. P(¬A) = 1 − P(A). This is the basic complement axiom of probability (Grinstead & Snell).
  2. Find the evidence by total probability. The chance of seeing B at all splits across the two ways it can happen, when A is true and when A is false:

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

    This marginal P(B) is the normalising constant. Source: NIST/SEMATECH e-Handbook; Grinstead & Snell §4.
  3. Apply Bayes' theorem. Divide the true-positive term by the evidence to get the posterior P(A|B) = P(B|A)·P(A) / P(B). This is the same rule scikit-learn states for Naive Bayes, P(y|x) = P(y)·P(x|y) / P(x).
  4. Complement the posterior. P(¬A|B) = 1 − P(A|B), the probability that A is false despite the evidence.

The calculator above also recomputes the posterior a second, independent way, the odds form. The prior odds P(A)/P(¬A) are multiplied by the likelihood ratio P(B|A)/P(B|¬A) to give the posterior odds, which convert back to a probability via odds / (1 + odds). Because this uses a ratio of ratios rather than the normalisation above, agreement between the two routes is a genuine correctness check, and the four joint probabilities of the tree are confirmed to sum to 1. When a likelihood is 0 the evidence can vanish (P(B) = 0); the tool then reports the posterior as undefined rather than dividing by zero.

A key intuition the tool makes visible: the posterior depends heavily on the base rate. A highly sensitive test applied to a rare condition still produces mostly false positives, because the large healthy population contributes many false alarms while the small affected population contributes few true ones. That is the false-positive paradox, and the probability tree shows exactly where the numbers come from.

Where the formula comes from

Bayes' theorem is not an extra assumption; it falls straight out of the definition of conditional probability. By definition P(A|B) = P(A∩B) / P(B) and P(B|A) = P(A∩B) / P(A). Both share the joint term P(A∩B), so P(A∩B) = P(B|A)·P(A). Substituting that into the first definition gives P(A|B) = P(B|A)·P(A) / P(B). Grinstead & Snell derive it the same way in their chapter on conditional probability. The only extra ingredient is the denominator: because A and ¬A split every outcome into two non-overlapping groups, P(B) is the sum of the two branches of the tree that end in B.

Updating more than once

The posterior from one piece of evidence can become the prior for the next, provided the two pieces of evidence are conditionally independent given A (for example, two separate tests whose errors are unrelated). Run the calculator, copy P(A|B) back into the prior field, and apply the second likelihoods. In odds form this is simply multiplying the prior odds by one likelihood ratio per test. If the tests share a cause of error, such as the same lab or the same sample, this shortcut overstates your confidence.

Edge cases the calculator handles

  • P(A) = 0 or 1. A prior of exactly 0 or 1 can never move: no evidence changes a certainty. The odds form is undefined at P(A) = 1 (the odds are infinite), so only the direct formula is shown.
  • P(B|¬A) = 0. If B never happens without A, one observation of B proves A: the posterior is 1 for any non-zero prior. The likelihood ratio is infinite, so the odds cross-check is skipped.
  • P(B|A) = P(B|¬A). The likelihood ratio is 1, the evidence tells you nothing, and the posterior equals the prior. This is a quick way to spot a useless test.
  • Both likelihoods 0. P(B) = 0 and the posterior is undefined, as described above.

If you are starting from a table of counts (true positives, false positives and so on) rather than rates, the diagnostic test statistics calculator turns them into sensitivity, specificity and predictive values. Its positive predictive value is the same number this page calls the posterior.

Worked examples

Rare-disease screening, posterior 16.67% (the Demo preset)

  1. Prior P(A) = 0.01, sensitivity P(B|A) = 0.99, false-positive P(B|¬A) = 0.05
  2. P(¬A) = 1 − 0.01 = 0.99
  3. P(B) = 0.99·0.01 + 0.05·0.99 = 0.0099 + 0.0495 = 0.0594
  4. P(A|B) = 0.0099 / 0.0594 = 0.1667 (16.67%)
  5. P(¬A|B) = 1 − 0.1667 = 0.8333 (83.33%)
  6. Read-out: a positive result still means only ~17% chance of the disease

Naive-Bayes spam filter, posterior 84.21%

  1. Prior P(spam) = 0.40, P(word|spam) = 0.80, P(word|ham) = 0.10
  2. P(¬spam) = 1 − 0.40 = 0.60
  3. P(word) = 0.80·0.40 + 0.10·0.60 = 0.32 + 0.06 = 0.38
  4. P(spam|word) = 0.32 / 0.38 = 0.8421 (84.21%)
  5. Seeing this word pushes the spam probability from 40% up to ~84%

The boundaries, impossible and certain priors

  1. Impossible prior: P(A) = 0, P(B|A) = 0.90, P(B|¬A) = 0.10
  2. P(B) = 0.90·0 + 0.10·1 = 0.10 → P(A|B) = 0 / 0.10 = 0.0000 (if A can't occur, B never implies it)
  3. Certain prior: P(A) = 1, P(B|A) = 0.50, P(B|¬A) = 0.20
  4. P(B) = 0.50·1 + 0.20·0 = 0.50 → P(A|B) = 0.50 / 0.50 = 1.0000
  5. Undefined edge: both likelihoods 0 make P(B) = 0, so the posterior is shown as 'undefined' rather than dividing by zero

A/L-style factory question: which machine made the defective item? (63.16%)

  1. Machine X makes 30% of output with a 4% defect rate; machine Y makes 70% with a 1% defect rate
  2. Let A = 'made by X', B = 'defective': P(A) = 0.30, P(B|A) = 0.04, P(B|¬A) = 0.01
  3. P(¬A) = 1 − 0.30 = 0.70
  4. P(B) = 0.04·0.30 + 0.01·0.70 = 0.012 + 0.007 = 0.019
  5. P(A|B) = 0.012 / 0.019 = 0.6316 (63.16%)
  6. X makes under a third of the items but about 63% of the defective ones

Retesting after a positive result: 16.67% → 79.84%

  1. Start from example 1: after one positive test, P(A|B) = 0.1667 (exactly 1/6)
  2. Use it as the new prior for an independent second test with the same accuracy: P(A) = 1/6, P(B|A) = 0.99, P(B|¬A) = 0.05
  3. P(B) = 0.99·(1/6) + 0.05·(5/6) = 0.1650 + 0.0417 = 0.2067
  4. P(A|B) = 0.1650 / 0.2067 = 0.7984 (79.84%)
  5. Odds check: prior odds 0.2 × LR 19.8 = 3.96 → 3.96 / 4.96 = 0.7984 ✓
  6. A second independent positive moves the probability from about 1 in 6 to about 4 in 5

Common mistakes when applying Bayes' theorem

Most wrong answers to Bayes problems come from a small set of habits. Check your working against these before trusting a result.

  1. Entering specificity instead of the false-positive rate. A test described as “95% specific” has P(B|¬A) = 0.05, not 0.95. Entering 0.95 makes the evidence almost useless and the posterior collapses toward the prior.
  2. Ignoring the base rate. Using the test's accuracy as the answer (“the test is 99% accurate, so I am 99% likely to be sick”) skips the prior entirely. The same test gives very different posteriors in a population where the condition is common and in one where it is rare.
  3. Mixing percentages and decimals. Typing 1 for a 1% prior in probability mode means certainty. Switch the input format to percentage mode, or divide by 100 first.
  4. Treating dependent tests as independent. Repeating the same test on the same sample usually repeats the same error, so chaining posteriors (as in the retesting example) is only valid when the errors are unrelated.
  5. Reading odds as probabilities. Posterior odds of 3.96 mean 3.96 to 1, which is a probability of 0.7984, not 3.96. If you work with odds ratios from studies, the odds ratio and relative risk calculator shows how the two scales relate.

The same arithmetic sits behind classifier evaluation. A spam filter or a medical model produces a confusion matrix, and the precision in that matrix is Bayes' posterior for a positive prediction. You can build one from raw counts with the confusion matrix calculator. When one trial becomes many, for example the chance of at least k positives out of n independent tests, the binomial distribution calculator picks up from there.

Frequently asked questions

Sources & references

The formulas on this page were last cross-checked against these sources on 2026-09-11. Bayes' theorem is a stable mathematical identity, so this tool needs no rate or schedule updates, only the worked examples are periodically re-reconciled.

Related tools

Comments & feedback

Spotted a bug or want an improvement? Tell us, our team reviews every comment, and good ideas get built. Comments are public and anonymous.

Found a bug, edge case, or want to suggest an improvement?

Email me at [email protected] , most fixes ship within 24 hours.