The law of total probability
Let hypotheses H1, H2, …, Hn be mutually exclusive and exhaustive (exactly one of them occurs), so P(H1) + … + P(Hn) = 1. Then the probability of an event A that can only happen together with one of the hypotheses is
P(A) = P(H1)·P(A∣H1) + P(H2)·P(A∣H2) + … + P(Hn)·P(A∣Hn).
In words: follow every branch of the tree that ends in A, multiply the probabilities along the branch, and add the results.
Bayes' theorem
If A has already happened, the probability that hypothesis Hi was the cause is
P(Hi∣A) = P(Hi)·P(A∣Hi) / P(A).
P(Hi) is the prior probability (before the observation) and P(Hi∣A) is the posterior (after learning that A occurred). The denominator is the total probability from the first formula, so all P(Hi∣A) add up to 1.
How to solve a problem
- List the hypotheses: "the part comes from factory 1", "the patient is sick". Check that they are mutually exclusive and sum to 1.
- Define the event A: "the part is defective", "the test is positive".
- Write down P(A∣Hi) for every hypothesis: the defect rate of the factory, the sensitivity of the test.
- If the question is "how likely is the event", use the law of total probability. If it is "how likely is hypothesis Hi given that the event happened", use Bayes' theorem.
Example: three factories and defects
Factory 1 makes 50% of the parts, factory 2 makes 30% and factory 3 makes 20%. The defect rates are 1%, 2% and 5%. A part is picked at random. The probability that it is defective is 0.5·0.01 + 0.3·0.02 + 0.2·0.05 = 0.005 + 0.006 + 0.01 = 0.021, that is 2.1%. If the part is defective, it comes from factory 3 with probability 0.01/0.021 ≈ 47.6%, although that factory makes only a fifth of the output: it produces the most defects.
The false positive paradox
1% of people have a disease. The test detects the disease in 99% of sick people (sensitivity) and correctly returns negative for 95% of healthy people (specificity). A person tests positive. How likely is it that they are sick? Intuition says about 95%, but Bayes' theorem gives only about 16.7%.
Count it out for 10,000 people. 100 are sick and the test finds 99 of them. 9,900 are healthy, and 5% of them test positive by mistake: 495 people. That makes 99 + 495 = 594 positive results, and only 99 of those are truly sick, so 99/594 ≈ 16.7%. Healthy people are so numerous that even a small false positive rate yields more positive results than there are true patients. This is why one positive screening for a rare disease calls for a retest, not a diagnosis. If the person has symptoms or belongs to a risk group, the prior is higher and the same test is far more convincing.
What the calculator shows
- A check that the hypothesis probabilities sum to 1, plus a "Complete to 1" button for the last hypothesis.
- P(A) and every P(Hi∣A) with a step-by-step solution and a table.
- A probability tree with P(Hi), P(A∣Hi) and 1 − P(A∣Hi) written on the branches.
- A link to the calculation: the data is stored in the address, so it is easy to share.