Statistics

Bayes' Theorem Calculator

Enter the prior probability P(A), the likelihood P(B | A) of observing B when A is true, and the likelihood P(B | A') of observing B when A is false. The calculator applies the law of total probability for P(B) and Bayes' theorem for the posterior P(A | B).

Bayes' Theorem Calculator

Compute the posterior P(A | B) from a prior P(A) and two likelihoods.

Try:
AnswerP(A | B) = 0.166667, P(B) = 0.0594
  1. GivenP(A) = 0.01, P(B | A) = 0.99, P(B | A') = 0.05
  2. ComplementP(A') = 1 − P(A) = 0.99
  3. Total probability of BP(B) = P(B | A)·P(A) + P(B | A')·P(A') = 0.99·0.01 + 0.05·0.99 = 0.0594
  4. Bayes' theoremP(A | B) = P(B | A)·P(A) / P(B) = (0.99·0.01)/0.0594 = 0.166667

Updating a belief with evidence

Bayes' theorem reverses a conditional probability. You usually know how likely the evidence is given the hypothesis — how often a test flags a sick patient — but what you want is the hypothesis given the evidence: how likely this patient is sick, having tested positive. Those two numbers are not the same, and the gap between them is where most probabilistic intuition fails.

This calculator takes a prior and the two likelihoods that make the picture complete, then reports the total probability of the evidence and the updated posterior.

The tool implements the two-hypothesis case: either A holds or it does not. That covers most textbook problems — disease or not, spam or not, faulty or sound — and keeps the arithmetic short enough to follow line by line.

The intermediate value P(B) matters as much as the answer. It is the overall rate at which the evidence appears, and seeing it computed explicitly is what makes the counterintuitive results feel inevitable rather than arbitrary.

How to use this calculator

  1. Enter the prior P(A) The probability of the hypothesis before any evidence — often a base rate, such as the prevalence of a condition in the population being tested.
  2. Enter P(B | A), the true-positive rate How likely the evidence is when the hypothesis holds. For a diagnostic test this is its sensitivity.
  3. Enter P(B | A'), the false-positive rate How likely the same evidence is when the hypothesis is false. For a test this is one minus the specificity, and it is the field most often left out of an intuitive estimate.
  4. Read P(B) before the posterior The total-probability line shows the two routes to the evidence added together. Comparing their sizes explains the posterior more directly than the final number does.

The formula, and where it comes from

P(B) = P(B|A)·P(A) + P(B|A')·P(A') P(A|B) = P(B|A)·P(A) / P(B)

The first line is the law of total probability. The evidence can arrive by exactly two routes — through A or through not-A — so its overall probability is the sum of those two products. The calculator computes P(A') as 1 − P(A) rather than asking for it.

The second line is Bayes' theorem, which is the definition of conditional probability rearranged. The numerator is the route you care about; dividing by P(B) rescales it against every way the evidence could have arisen.

What each input means

P(A) Prior probability — form field “P(A) — prior probability of A”
Your belief in the hypothesis before the evidence, as a probability between 0 and 1 rather than a percentage. Entering 5 instead of 0.05 is rejected outright.
P(B | A) Likelihood under the hypothesis — form field “P(B | A) — likelihood under A”
The chance of seeing the evidence when A is true. High values mean the evidence is expected under A; a value of 1 means it is certain.
P(B | A′) Likelihood under the alternative — form field “P(B | A') — likelihood under not-A”
The chance of the same evidence when A is false. Its ratio to P(B | A) carries the evidential weight: equal values mean the evidence tells you nothing.

Worked examples

Every number below is produced by the same calculation engine the tool above runs. Nothing here is typed by hand, so the walkthrough cannot drift from what you get when you enter the same values yourself.

The base-rate problem

A condition affects 1% of the population. A test detects it 99% of the time when present, and wrongly flags 5% of healthy people. Someone tests positive — how likely are they to have it?

Inputs P(A) — prior probability of A = 0.01, P(B | A) — likelihood under A = 0.99, P(B | A') — likelihood under not-A = 0.05

  1. Given P(A) = 0.01, P(B | A) = 0.99, P(B | A') = 0.05
  2. Complement P(A') = 1 − P(A) = 0.99
  3. Total probability of B P(B) = P(B | A)·P(A) + P(B | A')·P(A') = 0.99·0.01 + 0.05·0.99 = 0.0594
  4. Bayes' theorem P(A | B) = P(B | A)·P(A) / P(B) = (0.99·0.01)/0.0594 = 0.166667

Result P(A | B) = 0.166667, P(B) = 0.0594

The posterior is near 0.167, so about one positive in six is genuine. Most people guess close to 99%. The total-probability line shows why: 0.0099 of the evidence comes from the sick and 0.0495 from the healthy, a group a hundred times larger, so a small error rate still yields five times more positives than the disease does.

The lesson generalises: whenever the base rate is small, the false-positive rate rather than the detection rate dominates, which is why low-prevalence screening produces so many false alarms.

Evidence that genuinely moves the needle

Now start from an even 50/50 prior, with evidence that appears 95% of the time under the hypothesis and only 5% otherwise.

Inputs P(A) — prior probability of A = 0.5, P(B | A) — likelihood under A = 0.95, P(B | A') — likelihood under not-A = 0.05

  1. Given P(A) = 0.5, P(B | A) = 0.95, P(B | A') = 0.05
  2. Complement P(A') = 1 − P(A) = 0.5
  3. Total probability of B P(B) = P(B | A)·P(A) + P(B | A')·P(A') = 0.95·0.5 + 0.05·0.5 = 0.5
  4. Bayes' theorem P(A | B) = P(B | A)·P(A) / P(B) = (0.95·0.5)/0.5 = 0.95

Result P(A | B) = 0.95, P(B) = 0.5

The posterior is 0.95, matching the likelihood exactly. That coincidence is specific to an even prior and symmetric error rates; change either and the equality breaks.

What drives the update is the ratio between the likelihoods, here 19 to 1. Applied to the 1% prior above, that same ratio still leaves the posterior under a half — evidence strength and prior belief both matter.

Reading the result

The posterior is a probability, not a verdict

A posterior of 0.167 does not mean the hypothesis is false. Among everyone with this evidence, about one in six has the property. Turning that into a decision means weighing the cost of each kind of error, which is outside the calculation.

Quality in, quality out

The posterior inherits every uncertainty in the inputs. A prior from a rough guess gives a posterior no firmer than that guess, however many digits show. Six significant figures is formatting, not confidence.

Why P(B) is reported

P(B) is the unconditional rate of the evidence — useful on its own, since it predicts how many positives a screening programme generates, and it is the denominator that makes the posterior a proper probability.

When you would use this

Interpreting a diagnostic or screening test

With prevalence as the prior, sensitivity as P(B | A) and one minus specificity as P(B | A'), the posterior is the positive predictive value — the number a patient cares about, and the one a headline accuracy figure does not give.

Classification and filtering

Spam filters, fraud alerts and quality inspection all reverse the same conditional. The prior is the incidence of the target class, and the two likelihoods are the detector's hit and false-alarm rates.

Assumptions and limitations

What this calculator assumes

  • Exactly two exhaustive, mutually exclusive hypotheses: A and its complement. P(A') is computed as 1 − P(A) and is not entered separately.
  • All three inputs are probabilities in [0, 1]; values outside that range are rejected with an explicit message.
  • The evidence must be possible: if the computed P(B) is zero, the posterior is undefined and the solver says so rather than dividing by zero.

Where it stops being the right tool

  • Only two hypotheses. Three or more competing explanations need the general form of Bayes' theorem, with a sum over all of them in the denominator.
  • One observation at a time. Chaining updates by feeding the posterior back as the next prior assumes the observations are conditionally independent.

Common mistakes

Entering probabilities as percentages

Why it happens. Prevalence and test accuracy are almost always quoted as percentages, so 99 and 1 are the numbers to hand. The field expects a probability, so 99 is a hundredfold error.

How to avoid it. Divide by 100 before entering: 99% becomes 0.99. The solver rejects anything outside [0, 1], so this mistake produces an error rather than a wrong answer.

Confusing P(B | A) with P(A | B)

Why it happens. Both read as “the probability of one thing given another”, and English blurs the order. But the probability that a test is positive given disease is a property of the test, while the probability of disease given a positive is a property of the patient and the population.

How to avoid it. Say each aloud with the condition first: “given that A is true, how often is B seen?” is the input. “Given that B was seen, how likely is A?” is the output.

Ignoring the false-positive rate

Why it happens. It is the least memorable of the three numbers and often absent from a headline claim of accuracy, so it gets guessed at or assumed negligible.

How to avoid it. Enter it explicitly, even when small. With a low prior it is the dominant term in P(B), and setting it to zero changes the posterior to 1 regardless of everything else.

Frequently asked questions

What does Bayes' theorem actually do?

It reverses a conditional probability. Given how likely the evidence is under each hypothesis, plus how likely the hypothesis was to begin with, it returns the probability of the hypothesis now that the evidence has been seen: P(A | B) = P(B | A)·P(A) / P(B).

Why is the medical-test result so much lower than the test's accuracy?

Because the healthy group is far larger. At 1% prevalence with a 5% false-positive rate, healthy people generate about 0.0495 of positives against 0.0099 from the sick — five times as many — so most positives are false.

What is the prior, and where do I get one?

The prior P(A) is your probability for the hypothesis before this evidence. In screening it is the prevalence in the population actually being tested, which differs from the general population when the group is preselected.

What happens if P(B) comes out as zero?

Bayes' theorem divides by P(B), so a zero makes the posterior undefined. This occurs when both likelihoods are zero, meaning the evidence cannot happen under either hypothesis. The solver reports that explicitly instead of returning a number.