Bayes' theorem
Bayes' theorem reverses a conditional probability. We have , the probability of given each hypothesis . We want , the probability of hypothesis given that we have observed . Bayes is the formula that does this reversal cleanly.
It is the cornerstone of statistical inference, medical diagnostics, machine learning, and any form of rational belief-updating.
Statement
Let be a partition of the sample space, and let be an event with . Then for each :
The denominator is just from the law of total probability.
Derivation
Start from the multiplication rule: . Solve for : Replace by the total-probability expansion to get the full Bayes formula.
Terminology
In Bayesian language:
- is the prior probability of (before observing ).
- is the likelihood of under .
- is the posterior probability of (after observing ).
- is the marginal or normalising constant.
When to use it
Bayes is the right tool whenever you have a "cause and effect" structure: the 's are possible causes (hypotheses), is the observed effect, and you want to know which cause is most likely given the effect.
Classic settings:
- Medical diagnosis: is having (or not having) the disease; is the test result.
- Quality control: is which machine produced the item; is whether it is defective.
- Spam filtering: is "spam" or "not spam"; is the appearance of certain words.
Worked examples
Example 1. A factory makes light bulbs on three machines , producing , with defect rates . A random bulb is found defective. What is the probability it came from ?
Priors: , , . Likelihoods: , , .
Marginal: .
.
Example 2. The classic medical test. Disease affects 1% of population. Test: true positive rate, true negative rate (so false positive). A person tests positive. What is the probability they have the disease?
Priors: , . Likelihoods: , .
Marginal: .
, i.e. about .
The surprising conclusion: despite a "95% accurate" test, the conditional probability of disease given a positive result is only ~9%. This is because the disease is rare and false positives dominate.
Example 3. Two urns: U1 has 3W, 2B; U2 has 1W, 4B. An urn is picked at random and a ball drawn , turns out to be white. Probability it came from U1?
Priors: . Likelihoods: , .
Marginal: .
.
Example 4. A coin is selected from a jar: , . The selected coin is flipped and shows heads. Probability it is the two-headed coin?
, .
.
.
Example 5. Three drivers (call them D1, D2, D3) split a fleet's mileage: 40%, 35%, 25%. Their accident rates are 0.02, 0.04, 0.08 per year. An accident occurs; probability it was driver D3?
Priors: . Likelihoods: .
Marginal: .
.
Example 6. Spam filter: of emails are spam. The word "free" appears in of spam, of non-spam. An email contains "free" , probability it is spam?
.
Try it yourself
- Box A: 4R/6B; box B: 7R/3B. Choose box uniformly; draw red. .
- Three machines: 30%, 30%, 40%; defective rates 1%, 2%, 3%. A defective item , .
- Two coins, fair and biased (). Choose at random and flip , heads. .
- A test: , . Prevalence . Find .
- A student answers correctly. Probability she knew = . If guesses, . Find .
- Bag A: 3W/5B. Bag B: 6W/2B. Choose with probability ; draw white. .
- Two factories produce in ratio 3:2. Defect rates 4% and 2%. A defective , .
- Three urns, all equally likely: U1 has 2W/3B, U2 has 4W/1B, U3 has 3W/2B. Draw a white. .
- A communication channel: probability of bit being 1 is 0.6. Probability of correct transmission is 0.95. Receive 1 , .
- Two batches of fruits. Batch 1: 95% good, contributes 60% of total. Batch 2: 80% good. Pick a good fruit , .
- Three coins: with prior probabilities . Flip and get H. Posterior?
- Bag with 3 red, 2 white, then add another set of 5 red, 5 white. Choose first bag w.p. . Draw red , .
- A taxi cab: 85% Yellow, 15% Green. Witness identifies Green with accuracy when actually Green, and Green falsely with . Identify Green , actual probability it was Green?
- Insurance: 70% of policies are low risk, accident rate . 30% are high risk, accident rate . Given an accident, ?
Pitfalls and tricks
- Identify priors, likelihoods, and the marginal. Lay out the data clearly before plugging in.
- Bayes formula has a clear structure: posterior prior likelihood. Don't lose sight of the structure.
- Mediasource fallacy: never confuse with . The whole point of Bayes is that they are usually different.
- Rare diseases trap. Even a highly accurate test for a rare condition has surprisingly low PPV (positive predictive value).
- Always normalise. The denominator is the law of total probability , sum of prior × likelihood over all .