Bayes' Theorem Calculator

Update a probability when new evidence arrives. Enter a prior and the likelihood of the evidence to get the posterior probability P(A|B), with every step shown, a probability tree and a natural-frequency table to check it against.

How likely A is before the evidence.
Probability of the evidence B if A is true.
Overall probability of B, whether or not A is true. It cannot be zero.
Probability of the evidence B if A is false (the false-alarm rate). P(B) is worked out for you.
Share of the group that has the condition.
P(positive | has the condition).
P(negative | no condition). The false positive rate is 1 minus this.
Only used for the natural-frequency table. Leave blank for 10,000.
The priors must add up to 100% (or 1). Hypotheses are treated as mutually exclusive and exhaustive.
How likely A is before the evidence.
P(B|A) / P(B|not A). Above 1 favours A, between 0 and 1 favours not A. Not a probability, so enter it as a plain number.

Enter every value above and the result appears here automatically.

Reset calculator?

Are you sure you want to reset? Your entered values and the current result will be cleared.

What this Bayes' theorem calculator does

Bayes' theorem, also called Bayes' rule, shows how to revise the probability of a hypothesis after observing evidence. You supply the probability before the evidence (the prior) and how likely the evidence is under the hypothesis (the likelihood), and the calculator returns the updated probability (the posterior).

Short answer: P(A|B) = P(B|A) × P(A) / P(B). The posterior is the likelihood times the prior, divided by the overall probability of the evidence.

The calculator covers five situations: the evidence probability P(B) is known, only the two likelihoods are known, a diagnostic test described by prevalence, sensitivity and specificity, several competing hypotheses, and the odds form with a likelihood ratio. It works as a posterior probability calculator for general problems and as a conditional probability tool whenever you need to reverse a conditional probability. For a single conditional probability from joint counts, see the Conditional Probability Calculator.

Choosing an input mode and a number format

Pick the mode that matches the numbers you already have:

  • P(A), P(B|A) and P(B): use this when a source gives you the overall probability of the evidence directly.
  • P(A), P(B|A) and P(B|not A): the most common case. You do not need P(B); the calculator works it out.
  • Diagnostic test: enter prevalence, sensitivity and specificity. You also get the positive and negative predictive values.
  • Several competing hypotheses: two to six hypotheses, each with a prior and a likelihood.
  • Prior and likelihood ratio: for results quoted as a likelihood ratio, as in the Post-Test Probability Calculator.

Probabilities can be entered as percentages (5 means 5%) or as decimals (0.05 means 5%). Choose the format first; changing it later converts the numbers already entered. Every probability must lie between 0 and 100% (or between 0 and 1). The likelihood ratio and the optional population size are plain numbers, so the format menu does not affect them.

Probability notation and what each input means

Statistics textbooks and websites label the same quantities in different ways. This is how the inputs are named on this page.

P(A): prior probability
The probability of the hypothesis A before the evidence is considered. It is also called the base rate, and for a medical test it is the prevalence.
P(not A)
The complement of the prior, 1 − P(A). The calculator works it out for you.
P(B|A): likelihood
The probability of observing the evidence B if A is true. For a test, this is the sensitivity, also called the true positive rate.
P(B|not A)
The probability of observing B if A is false. For a test, this is the false positive rate, which equals 1 − specificity.
P(B): evidence probability
The overall probability of observing B, whether A is true or not. It is also called the marginal probability of the evidence.
P(A|B): posterior probability
The probability of A after B has been observed. This is the number the calculator returns.

P(A|B) and P(B|A) are different questions. The probability that a person with a cold has a cough is not the probability that a person with a cough has a cold. Swapping the two is one of the most common mistakes in probability, and Bayes' theorem is the tool that converts one into the other.

The Bayes' theorem formula

The general formula multiplies the prior by the likelihood and divides by the probability of the evidence. When A and not A are the only two possibilities, the law of total probability expands the denominator, which gives the second and third forms below.

  • P(A|B) = P(B|A) × P(A) / P(B)
  • P(B) = P(B|A) × P(A) + P(B|not A) × P(not A), with P(not A) = 1 − P(A)
  • P(A|B) = P(B|A) × P(A) / [P(B|A) × P(A) + P(B|not A) × (1 − P(A))]
  • P(Hi|E) = P(E|Hi) × P(Hi) / Σ P(E|Hj) × P(Hj)
  • Posterior odds = prior odds × likelihood ratio, where odds = p / (1 − p) and likelihood ratio = P(B|A) / P(B|not A)

The numerator, P(B|A) × P(A), is the probability that A is true and B is observed. The denominator is the probability that B is observed in any way at all. The posterior is therefore the share of all the ways B can happen that also involve A. A strong likelihood pushes the posterior up, but a small prior pulls it back down.

Calculating the evidence probability P(B)

B can occur in two ways: A is true and B is observed, or A is false and B is observed. Adding the two probabilities gives P(B). This is the law of total probability, and P(not A) is simply 1 − P(A).

With P(A) = 0.01, P(B|A) = 0.9 and P(B|not A) = 0.09, the evidence probability is 0.9 × 0.01 + 0.09 × 0.99 = 0.009 + 0.0891 = 0.0981. If you enter P(B) directly instead, the calculator also reports the P(B|not A) it implies, and it warns you when your numbers cannot all be true together.

Reading the result and the input checks

The result panel shows the posterior probability as a decimal and a percentage, then the substituted numerator, the denominator and the final division so you can check each line by hand. A plain-language sentence compares the prior with the posterior. Below that, a probability tree and a table of natural frequencies (expected counts out of 10,000 cases) show the same calculation in a different way. The decimal places menu changes only what is displayed; the arithmetic always uses full precision.

Instead of showing a misleading number, the calculator stops and explains the problem when:

  • a probability is outside the valid range, or a field is not a number;
  • P(B) is zero, because the posterior is undefined when the evidence has zero probability;
  • the inputs contradict each other, for example P(B) smaller than P(B|A) × P(A), which no valid probability model could produce;
  • the priors of several hypotheses do not add up to 100%.

Prior versus posterior probability

The prior is what you believe before looking at the evidence. The likelihood says how expected that evidence would be if the hypothesis were true. The posterior combines the two. Evidence that is much more likely under A than under not A raises the probability of A; evidence that is equally likely either way leaves it unchanged. Once you have a posterior, it can serve as the prior for the next piece of evidence, as described under sequential updating below.

Worked example: a diagnostic test

Suppose a condition affects 1% of a population. A test returns positive for 90% of people who have it (sensitivity 90%) and returns negative for 91% of people who do not (specificity 91%, a 9% false positive rate). These figures are illustrative and are not taken from any real test.

In diagnostic mode, enter prevalence 1, sensitivity 90 and specificity 91. In the two-likelihood mode, the same example is P(A) = 1%, P(B|A) = 90% and P(B|not A) = 9%. The results are:

  • After a positive result: P(positive) = 0.9 × 0.01 + 0.09 × 0.99 = 0.0981, so the positive predictive value is 0.009 / 0.0981 ≈ 0.0917, about 9.17%.
  • After a negative result: P(negative) = 0.1 × 0.01 + 0.91 × 0.99 = 0.9019. The probability of having the condition is 0.001 / 0.9019 ≈ 0.0011 (0.11%), and the negative predictive value is 0.9009 / 0.9019 ≈ 0.9989 (99.89%).

The negative predictive value is the probability of not having the condition after a negative result. The probability of having it after a negative result is the complement, 1 − NPV. They answer opposite questions, and it is easy to quote the wrong one, so the calculator shows both. Natural frequencies make the same example concrete:

Expected counts for 10,000 people: 1% prevalence, 90% sensitivity, 91% specificity
GroupTest positiveTest negativeTotal
Has the condition9010100
No condition8919,0099,900
Total9819,01910,000

Of the 981 positive results, 90 come from people who have the condition: 90 / 981 ≈ 9.17%. The false positive rate is the likelihood P(B|not A); for background on test accuracy measures see the Sensitivity and Specificity Calculator.

Base rates and common mistakes

Sensitivity says that 90 of the 100 people with the condition test positive. It says nothing about the 9,900 people without it, 891 of whom also test positive. Because the healthy group is so much larger, false positives outnumber true positives by roughly ten to one, even though the test looks accurate. This effect is known as the base rate fallacy, and the False Positive Paradox Calculator explores it further. If the same test were used where prevalence is 30%, the positive predictive value would rise to about 81%, so a result has to be read against the group being tested.

Common mistakes to avoid:

  • Treating sensitivity P(B|A) as if it were the positive predictive value P(A|B).
  • Reversing the conditional probabilities, for example entering P(A|B) where P(B|A) is needed.
  • Using specificity where the false positive rate is needed. P(B|not A) = 1 − specificity.
  • Using P(B|A) in the denominator instead of the total probability P(B).
  • Ignoring the prior or base rate, which is what drives the result when it is very small or very large.
  • Typing 5 in decimal mode or 0.05 in percentage mode. Check the input format menu.

A nonmedical example: which machine made the defective part?

A factory has three machines. Machine 1 makes 50% of the parts, machine 2 makes 30% and machine 3 makes 20%. Their defect rates are 1%, 2% and 4%. A part is picked at random and it is defective. Which machine most likely made it? Select the several hypotheses mode to try it.

Which machine made a defective part?
MachineShare of output (prior)Defect rate (likelihood)Prior × likelihoodPosterior
Machine 150%1%0.00526.32%
Machine 230%2%0.00631.58%
Machine 320%4%0.00842.11%
Total100%P(defective) = 1.9%0.019100%

The overall defect rate is P(defective) = 0.005 + 0.006 + 0.008 = 0.019. Machine 1 makes the most parts but is the least likely source of a defective one, because its defect rate is lowest. The hypotheses here are mutually exclusive (each part comes from one machine) and exhaustive (every part comes from one of the three), which is what the method requires.

Advanced calculation modes

Several hypotheses

For more than two hypotheses H1 to Hn, each posterior is P(Hi|E) = P(E|Hi) × P(Hi) divided by the sum of P(E|Hj) × P(Hj) over all hypotheses. The hypotheses must be mutually exclusive and exhaustive, so the priors must add up to 1. The posteriors then add up to 1 as well.

Likelihood ratio and the odds form

The odds form multiplies prior odds by the likelihood ratio to get posterior odds. Odds are p / (1 − p), so a 20% probability has odds of 0.25 (1 to 4). The likelihood ratio is P(B|A) / P(B|not A): above 1 supports A, below 1 supports not A, and exactly 1 changes nothing. A prior of 20% and a likelihood ratio of 4 give posterior odds of 1, which is a probability of 50%. Odds convert back to a probability with p = odds / (1 + odds).

Sequential updating

The posterior from one calculation can become the prior for the next. After a result, use the update buttons to carry it forward. This is valid only when the pieces of evidence are conditionally independent given the hypothesis, meaning that once you know whether A is true, one observation tells you nothing extra about the other. With the 1%, 90%, 91% test above, a second independent positive result would lift the probability from about 9.17% to about 50.25%. Repeating a test on the same sample often shares errors with the first result, in which case the real probability is lower than this method suggests.

Assumptions and limitations

  • The result is only as reliable as the inputs. Priors, sensitivity and specificity are often estimates, and the calculator does not account for their uncertainty.
  • Sensitivity and specificity are assumed to apply to the group you are describing. A test evaluated in one population may behave differently in another.
  • The several hypotheses mode assumes the hypotheses are mutually exclusive and exhaustive. Sequential updating assumes conditional independence.
  • This calculator provides a mathematical result from the values you enter. It is an informational and educational tool, not medical, legal, financial or other professional advice. For decisions that matter, consult a qualified professional.

Formula sources

The formulas on this page were checked against the following independent references:

Frequently asked questions

How do I calculate probability using Bayes' theorem?

Multiply the prior probability P(A) by the likelihood P(B|A) to get the numerator, then divide by P(B), the overall probability of the evidence. The result is the posterior probability P(A|B). If P(B) is not given, work it out as P(B|A) × P(A) + P(B|not A) × (1 − P(A)). The calculator does these steps and shows each one.

Which probabilities do I need to enter into a Bayes' theorem calculator?

You always need a prior and a likelihood. After that it depends on what you know: either the overall probability of the evidence P(B), or the probability of the evidence when A is false, P(B|not A). For a diagnostic test, enter prevalence, sensitivity and specificity. For several hypotheses, enter a prior and a likelihood for each one.

What is the difference between prior and posterior probability?

The prior probability is your starting probability for a hypothesis before the new evidence is considered. The posterior probability is the updated probability after the evidence is taken into account. Bayes' theorem is the rule that turns one into the other.

What is the difference between P(A|B) and P(B|A)?

P(A|B) is the probability of A given that B has happened, and P(B|A) is the probability of B given that A has happened. They answer different questions and are usually different numbers. Bayes' theorem converts P(B|A) into P(A|B) by accounting for the prior P(A) and the overall probability P(B).

How do I calculate P(B) if it is not given?

Use the law of total probability: P(B) = P(B|A) × P(A) + P(B|not A) × P(not A), where P(not A) = 1 − P(A). In the calculator, choose the mode that asks for P(B|not A) and it works out P(B) for you.

Should I enter probabilities as percentages or decimals?

Either works. Use the input format menu to choose percentages (0 to 100) or decimals (0 to 1); 5% is entered as 5 or as 0.05 respectively. Switching the menu converts the values already entered. Do not mix the two formats in one calculation. The likelihood ratio and population size are plain numbers, not probabilities.

How do I calculate the probability of having a condition after a positive test?

That probability is the positive predictive value. Choose the diagnostic test mode and enter prevalence, sensitivity and specificity. It is calculated as sensitivity × prevalence divided by (sensitivity × prevalence + (1 − specificity) × (1 − prevalence)).

How do I calculate the probability of having a condition after a negative test?

Use the same diagnostic test mode. The probability of having the condition after a negative result is (1 − sensitivity) × prevalence divided by ((1 − sensitivity) × prevalence + specificity × (1 − prevalence)). This is not the negative predictive value: the NPV is the probability of not having the condition after a negative result, which is 1 minus this figure. The calculator shows both.

Why can a highly accurate test still produce a low probability that a positive result is correct?

Because of the base rate. When few people have a condition, even a small false positive rate applies to a very large group of healthy people and can produce more false positives than true positives. With 1% prevalence, 90% sensitivity and 91% specificity, only about 9% of positive results are true positives.

Can Bayes' theorem compare more than two hypotheses?

Yes. With any number of mutually exclusive and exhaustive hypotheses, the posterior for each one is its prior times its likelihood, divided by the sum of that product across all hypotheses. This calculator supports two to six hypotheses.

Can I use the result of one calculation as the prior for another test?

Yes, if the two pieces of evidence are conditionally independent given the hypothesis. The posterior from the first update becomes the prior for the second. The Use this posterior as the new prior button does this for you. If the tests share the same sources of error, repeated updating overstates the certainty.

What happens if the probability of the evidence is zero?

Bayes' theorem divides by P(B), so the posterior probability is undefined when P(B) = 0. If the evidence is impossible under your model, you cannot condition on it. The calculator reports this instead of showing a number; check the inputs, because a real observation should not have zero probability.