Written and maintained by CASRAI Editorial Board
Last updated
Bayes’ theorem in probability is the rule for reversing a conditional probability: it turns P(B | A), how likely the evidence is when a hypothesis is true, into P(A | B), how likely the hypothesis is once the evidence has been seen. This guide gives the formula and the total-probability form, one fully worked probability-tree example, a reading table, and code for Excel, R and Python.
Bayes’ theorem in probability: definition
Bayes’ theorem of probability states how to update the probability of an event E after learning that event F occurred. The result, P(E | F), is called the posterior. It combines the prior P(E) (what you believed before the evidence), the likelihood P(F | E) (how probable the evidence is if E is true) and a normalization constant P(F) that makes the answer a proper probability. The rule is named for Thomas Bayes, whose essay on the doctrine of chances appeared in the Philosophical Transactions of the Royal Society of London in 1764. It follows directly from the definition of conditional probability, so it is a theorem, not an extra assumption. If you need the underlying definition first, start with how to calculate conditional probability.
Bayes theorem formula (and the total probability form)
Basic form: P(E | F) = P(F | E) × P(E) / P(F), valid only when P(F) > 0.
- P(E | F): probability of E given that F has occurred (posterior).
- P(E): probability of E before seeing F (prior).
- P(F | E): probability of F if E is true (likelihood).
- P(F): overall probability of F, whether or not E is true (normalization constant).
The derivation is two lines. The definition gives P(E | F) = P(E and F) / P(F), and the chain rule gives P(E and F) = P(F | E) × P(E). Substituting the second into the first yields the theorem. The same joint probability can be built either way, which is why the two conditional probabilities are linked (see the joint probability formula).
Total probability and Bayes’ theorem
P(F) is usually not given directly. The law of total probability expands it over E and its complement: P(F) = P(F | E) × P(E) + P(F | not E) × P(not E). Substituting this denominator gives the expanded form of Bayes’ theorem for probability:
P(E | F) = P(F | E) P(E) / [ P(F | E) P(E) + P(F | not E) P(not E) ]
With more than two competing hypotheses H1, H2, …, Hn that are mutually exclusive and together cover every possibility, the denominator becomes the sum of P(F | Hi) × P(Hi) over all i, and P(Hi | F) = P(F | Hi) P(Hi) / that sum. A useful check: the posteriors across all the hypotheses must add to 1 (for two hypotheses, P(E | F) + P(not E | F) = 1).
Worked example with a probability tree
A screening test is used in a population where 0.3% of people have the condition (the prevalence). The test detects 99% of true cases (sensitivity) and correctly clears 99% of people without the condition (specificity). A person tests positive. What is the probability they have the condition? Let E = has the condition and F = tests positive.
Step 1, the inputs. P(E) = 0.003, so P(not E) = 0.997. P(F | E) = 0.99. P(F | not E) = 1 − 0.99 = 0.01 (the false positive rate).
Step 2, the two branches of the tree that end in a positive test.
- Has the condition and tests positive: 0.003 × 0.99 = 0.00297
- Does not have it and tests positive: 0.997 × 0.01 = 0.00997
Step 3, total probability of a positive test. P(F) = 0.00297 + 0.00997 = 0.01294.
Step 4, Bayes’ theorem. P(E | F) = 0.00297 / 0.01294 = 0.2295, or about 22.95%.
The same result in natural frequencies, for 100,000 people: 300 have the condition and 99,700 do not. Of the 300, 99% test positive (297) and 3 are missed. Of the 99,700, 1% test positive (997 false positives) and 98,703 test negative. Positives in total: 297 + 997 = 1,294, of which 297 are true, and 297 / 1,294 = 22.95%.
| Group (100,000 people) | Tests positive | Tests negative | Total |
|---|---|---|---|
| Has the condition | 297 | 3 | 300 |
| Does not have it | 997 | 98,703 | 99,700 |
| Total | 1,294 | 98,706 | 100,000 |
Even with a 99% sensitive and 99% specific test, fewer than one in four positives is a true case, because the condition is rare. This is the base-rate fallacy: ignoring the prior P(E). The same arithmetic underlies positive predictive value in sensitivity vs specificity.
Bayes theorem with three hypotheses (illustrative composite)
This example is an invented illustration, not real data. A workshop gets parts from suppliers A, B and C, which supply 50%, 30% and 20% of parts, with defect rates of 2%, 3% and 5%. A part is found defective. P(defective) = 0.5 × 0.02 + 0.3 × 0.03 + 0.2 × 0.05 = 0.010 + 0.009 + 0.010 = 0.029. Then P(A | defective) = 0.010 / 0.029 = 34.5%, P(B | defective) = 0.009 / 0.029 = 31.0% and P(C | defective) = 0.010 / 0.029 = 34.5%. The three add to 100%, and supplier C, with only 20% of the parts, accounts for as many defects as A.
Interpretation: how to read the result
The posterior depends as much on the prior as on the quality of the evidence. Holding sensitivity and specificity at 99%, the probability that a positive result is a true case changes sharply with prevalence (each value computed from the formula above):
| Prior probability (prevalence) | P(condition | positive) | How to read it |
|---|---|---|
| 0.1% | 9.02% | Most positives are false alarms |
| 0.3% | 22.95% | The worked example above |
| 1% | 50.00% | Positives are true and false in equal measure |
| 5% | 83.90% | Most positives are true cases |
| 20% | 96.12% | A positive is strong evidence |
The ratio P(F | E) / P(F | not E), the true positive rate divided by the false positive rate, is the likelihood ratio: it measures how strongly the evidence favours E, independent of the prior. For the Bayesian-testing counterpart see interpreting Bayes factors, and for the wider framework, Bayesian vs frequentist statistics.
Assumptions and common mistakes
- Swapping the conditional. P(positive | condition) = 0.99 is not P(condition | positive) = 0.2295. Write both out in words before calculating.
- Ignoring the base rate. Using only the likelihood and leaving out P(E) is the base-rate fallacy.
- A partition that is not exhaustive or not exclusive. The total-probability denominator is only correct if the hypotheses cover every possibility without overlapping. If the posteriors do not sum to 1, check this first.
- Using a prior from the wrong population. The prior must describe the population the person or case actually comes from.
- Dividing by zero. The theorem requires P(F) > 0.
- Treating P(F | not E) as 1 − P(F | E). It is a separate input (the false positive rate). Only P(not F | E) equals 1 − P(F | E).
How to report it (APA example)
Report the inputs and the result so a reader can reproduce it. An APA-style sentence: “Given a prevalence of .003, a sensitivity of .99 and a specificity of .99, Bayes’ theorem gave a posterior probability of .23 that a person with a positive result has the condition.” Probabilities cannot exceed 1, so they are written without a leading zero. Cite the source of each input (the prevalence study and the test’s validation study), not just the calculation.
How to calculate Bayes’ theorem in Excel, R or Python
Excel
Put prevalence in B1, sensitivity in B2 and specificity in B3, then in B4:
=B1*B2/(B1*B2+(1-B1)*(1-B3))
With 0.003, 0.99 and 0.99 this returns 0.2295.
R
prior <- 0.003; sens <- 0.99; spec <- 0.99
posterior <- prior * sens / (prior * sens + (1 - prior) * (1 - spec))
round(posterior, 4) # 0.2295
Python
prior, sens, spec = 0.003, 0.99, 0.99
posterior = prior * sens / (prior * sens + (1 - prior) * (1 - spec))
print(round(posterior, 4)) # 0.2295
FAQ
What is Bayes’ theorem in probability?
It is the formula P(A | B) = P(B | A) P(A) / P(B). It converts the probability of the evidence given a hypothesis into the probability of the hypothesis given the evidence.
What is the difference between total probability and Bayes’ theorem?
The law of total probability computes the overall probability of an outcome by summing over the hypotheses that could produce it. Bayes’ theorem uses that total as its denominator to work backwards from the outcome to a hypothesis.
What is a Bayes theorem probability tree?
A tree lists the hypotheses on the first branches (with their priors) and the evidence on the second (with the likelihoods). Multiplying along each path gives a joint probability; the paths that end in the observed evidence are summed for the denominator, and the path of interest is divided by that sum, as in the worked example above.
When can Bayes’ theorem not be used?
When P(F) = 0, or when the prior and likelihoods are unknown and cannot be estimated from a relevant source. Its output is only as good as those inputs.
How does Bayes’ theorem relate to Bayesian statistics?
Bayesian statistics applies the same update to model parameters, with a prior distribution in place of a single prior probability. See summarizing a posterior distribution.
Sources
- Stanford University, CS109 lecture notes LN04, “Conditional probability and Bayes”: https://web.stanford.edu/class/archive/cs/cs109/cs109.1208/lectureNotes/LN04_cond_bayes.pdf (checked 2026-10-29).
- Stanford Encyclopedia of Philosophy, “Bayes’ Theorem”: https://plato.stanford.edu/archives/sum2017/entries/bayes-theorem/ (checked 2026-10-29).
- Bayes, T. (1764). An essay towards solving a problem in the doctrine of chances. Philosophical Transactions of the Royal Society of London, 53, 370-418, as cited in the Stanford Encyclopedia entry above.








