Bayes' theorem
Bayes' theorem — how to update a prior probability to a posterior probability using new evidence (used in diagnostics, classification, and decision-making).
Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.
Why it matters
Bayes' theorem is used by engineers when they must update beliefs or probabilities after seeing new data — for example, deciding whether a component is faulty given a test result, or a classifier deciding if an email is spam given words it contains. It connects prior knowledge with observed evidence to give a corrected probability.
Key ideas
Start from first principles and simple definitions, as exams expect clear definitions before use.
Probability: a number between 0 and 1 that measures how likely an event is to occur. Probabilities are unitless.
Sample space (S): the set of all possible outcomes of an experiment. Example: for a coin toss S = {H, T}.
Event (A, B, ...): any subset of the sample space. Example: A = {H}.
Intersection (A ∩ B): outcomes in both A and B. Draw two overlapping circles for A and B inside a rectangle labelled S; shade the overlap for A ∩ B.
Union (A ∪ B): outcomes in A or B or both.
Complement (A^c): outcomes not in A.
Conditional probability: the probability of A given B, written P(A | B), is the probability that A occurs when we know B has occurred. By definition:
- P(A | B) = P(A ∩ B) / P(B), provided P(B) > 0. State this assumption in exam answers.
From the definition of conditional probability we get the multiplication rule:
- P(A ∩ B) = P(A | B) · P(B) = P(B | A) · P(A).
- Derivation step-by-step universities expect: start with P(A | B) = P(A ∩ B)/P(B) and P(B | A) = P(A ∩ B)/P(A), rearrange either to P(A ∩ B) = P(B | A)P(A) and substitute as needed.
Total probability theorem (for a partition {A1, A2, ..., An} of S): if the Ai are disjoint and cover S and P(Ai) > 0, then for any event B:
- P(B) = Σ_i P(B | Ai) · P(Ai).
- On a diagram draw a tree: root splits into branches Ai with probabilities P(Ai); each Ai has branches for B and B^c with conditional probabilities P(B | Ai) and 1 − P(B | Ai).
Bayes' theorem (exact statement): for events A and B with P(B) > 0,
- P(A | B) = P(B | A) · P(A) / P(B).
- Using the total probability theorem for P(B) when {Ai} partition S,
- P(Aj | B) = P(B | Aj) · P(Aj) / Σ_i P(B | Ai) · P(Ai).
- Definitions used here: prior = P(Aj) (initial belief); likelihood = P(B | Aj) (how probable evidence is if Aj were true); evidence or normalizing constant = P(B) = Σ_i P(B | Ai)P(Ai); posterior = P(Aj | B) (updated belief after seeing B).
Independence: A and B are independent if P(A | B) = P(A) (equivalently P(A ∩ B) = P(A)P(B)). If independent, Bayes' update is trivial: P(A | B) = P(A).
Note on language and uncertainty: I am confident in the derivations and definitions above. Please verify that the choice of numerical examples (sensitivities/specificities) in the worked examples is appropriate for your course context.
Formulas
P(A | B) = P(A ∩ B) / P(B) — P(A | B) is conditional probability of A given B; unitless; requires P(B) > 0.
P(A ∩ B) = P(B | A) · P(A) — multiplication rule; all symbols unitless; requires P(A) > 0 so that P(B | A) is defined.
P(B) = Σ_i P(B | A_i) · P(A_i) — total probability theorem for partition {A_i}; P(B) unitless; requires {A_i} to be disjoint, cover S, and P(A_i) > 0 so that each P(B | A_i) is defined.
P(A_j | B) = P(B | A_j) · P(A_j) / Σ_i P(B | A_i) · P(A_i) — Bayes' theorem for a partition; all terms unitless; requires denominator > 0.
Posterior odds = Prior odds × Likelihood ratio — P(A | B)/P(A^c | B) = [P(B | A)/P(B | A^c)] · [P(A)/P(A^c)] (requires all denominators > 0).
f_{X|Y}(x | y) = f_{Y|X}(y | x) · f_X(x) / f_Y(y) — continuous/density form; requires f_Y(y) > 0.
Naive Bayes: P(C | x_1,...,x_n) ∝ P(C) · Π_i P(x_i | C) — assumes conditional independence of features given class; normalize over classes to obtain posteriors.
Independence: P(A ∩ B) = P(A) · P(B) — definition of independence; unitless; if true then P(A | B) = P(A).
Definitions (bold in Key ideas): prior = P(A_j); likelihood = P(B | A_j); posterior = P(A_j | B); evidence = P(B).
Worked examples
Example 1 — Medical test (numerical, carry units as "unitless") Problem: A disease has prevalence 1% in a population. A diagnostic test has sensitivity 99% and specificity 95%. A person tests positive. What is the probability they actually have the disease? Given:
- P(Disease) = P(D) = 0.01 (prior)
- Sensitivity = P(+ | D) = 0.99 (likelihood)
- Specificity = P(- | D^c) = 0.95 so false positive rate = P(+ | D^c) = 0.05
We want P(D | +).
Formula (Bayes):
P(D | +) = P(+ | D)·P(D) / [P(+ | D)·P(D) + P(+ | D^c)·P(D^c)]Compute step-by-step:
- Numerator = 0.99 × 0.01 = 0.0099 (unitless).
- Denominator = 0.0099 + 0.05 × 0.99 = 0.0099 + 0.0495 = 0.0594 (unitless).
- Posterior = 0.0099 / 0.0594 = 0.166666... = 1/6 ≈ 0.1667. Final answer: P(Disease | positive) ≈ 16.67%. Comment: This shows a highly accurate test can still give a low posterior when disease is rare.
Example 2 — Urn / box problem (classic conditional probability) Problem: Two boxes chosen with equal probability. Box A contains 2 white and 1 black ball. Box B contains 1 white and 2 black balls. You choose a box uniformly at random then draw a ball at random which turns out white. What is the probability the chosen box was A? Given:
- P(A) = 0.5, P(B) = 0.5
- P(white | A) = 2/3, P(white | B) = 1/3
We want P(A | white).
Formula:
P(A | white) = P(white | A)·P(A) / [P(white | A)·P(A) + P(white | B)·P(B)]Compute:
- Numerator = (2/3) × 0.5 = 1/3 ≈ 0.333333.
- Denominator = 1/3 + (1/3) × 0.5 = 1/3 + 1/6 = 1/2 = 0.5.
- Posterior = (1/3) / (1/2) = 2/3 ≈ 66.6667%. Final answer: P(chosen box is A | white) = 2/3 ≈ 66.67%.
Example 3 — Short model-answer style (derivation question) Question: Derive Bayes' theorem from conditional probability and state the assumptions. Model answer (what to write): Start with P(A | B) = P(A ∩ B)/P(B) (assume P(B) > 0). Also P(B | A) = P(A ∩ B)/P(A) (assume P(A) > 0). Rearranging the second gives P(A ∩ B) = P(B | A)P(A). Substitute into the first to obtain P(A | B) = P(B | A)P(A)/P(B). Mention assumption P(B) > 0 and, if using partitions, that the partition events are disjoint and cover S. Also state P(A) > 0 if you write P(B | A).
Common mistakes
- Using Bayes without checking P(B) > 0, or using impossible events.
- Misplacing sensitivity and specificity (sensitivity = P(+ | disease), specificity = P(- | no disease)).
- Assuming independence without justification; independence makes Bayes trivial and often wrong in practice.
- Arithmetic errors in the denominator when using the total probability theorem (forgetting one partition term).
- Not stating assumptions (e.g., partition is complete and disjoint) in derivations.
For your exam
Types of questions you will get and what full-marks answers include:
- Definitions: define probability, conditional probability, prior, likelihood, posterior, evidence, independence. Give symbols and state P(B) > 0 requirement for conditional probability.
- Short notes: explain total probability theorem and when to use it; include a small tree diagram (draw root → branches for partition Ai with P(Ai), then branches for B and B^c under each Ai with P(B | Ai)).
- Derivations: derive Bayes' theorem from definitions step-by-step and explicitly state assumptions (P(B) > 0; partitions are disjoint and exhaustive if used).
- Numericals: show each step, write the formula first, substitute numbers with units (probabilities are unitless), carry units through, show final answer as a percentage and decimal.
- Diagrams: if asked draw either a Venn diagram (rectangle S, circles A and B, label A ∩ B) or a tree diagram as above. Label probabilities on branches.
- Long/essay: discuss an application (diagnostics or classifier), show a worked numeric example and interpret the posterior (what it means practically).
Full-marks answers must: define terms, show the required algebra, state assumptions, compute denominator via total probability when needed, and interpret the result in plain English.
Quick check
- Define conditional probability in one line.
- If P(A) = 0.4, P(B) = 0.5 and P(A ∩ B) = 0.2, what is P(A | B)?
- A disease has prevalence 20%. Test sensitivity = 80%, specificity = 90%. If a person tests positive, what is probability they have the disease? (Give percentage to two decimal places.)
Answers: 1) P(A | B) = P(A ∩ B)/P(B) (requires P(B) > 0). 2) 0.2/0.5 = 0.4. 3) Numerator = 0.8·0.2 = 0.16, Denominator = 0.16 + 0.1·0.8 = 0.16 + 0.08 = 0.24, Posterior = 0.16/0.24 = 2/3 = 66.67%.
Finished this topic? Mark it so your progress, study plan and readiness keep up.
Stuck on something here?