TL;DR
Bayes' rule tells you how to flip a conditional probability around. If you know how likely some evidence is when a hypothesis is true, and you know how likely the hypothesis was before you saw the evidence, you can work out how likely the hypothesis is now. The answer is the prior, scaled up or down by how well the hypothesis explains the evidence, then divided by the total chance of seeing that evidence at all.
Where you'll see it: in interpreting a medical test result, in a spam filter that updates its guess as words arrive, and in the posterior distribution of the hidden variables in a latent-variable model.
Intuition
Reversing a question
Doctors, judges and spam filters all face the same reversed question. The data is easy to describe in one direction. A test is positive in 90% of sick people. A spam email contains the word "free" in 60% of cases. But what you need is the other direction. Given a positive test, how likely is the person to be sick? Given the word "free", how likely is the email to be spam?
These two quantities are different. A test can be accurate in the first direction and still give mostly false alarms in the second, if the disease is rare. Conditional probability says that and are different numbers. Bayes' rule is the bridge between them.
A belief that updates
The rule has a second, more important reading. Before any evidence arrives, you hold a belief about each possible explanation. That is the prior. When evidence comes in, each explanation is judged by how well it predicts that evidence. Explanations that predict the evidence well gain weight. Explanations that predict it poorly lose weight. The result is the posterior, your updated belief.
flowchart LR P[Prior: belief before evidence] --> M[Multiply by how well each hypothesis predicts the evidence] M --> N[Normalise so the beliefs add up to 1] N --> Q[Posterior: belief after evidence] Q -. becomes the prior for the next piece of evidence .-> P
That loop is why the rule is central to learning from data. Each observation nudges the belief a little, and yesterday's posterior is today's prior.
The math
Definition
Let be a hypothesis and be some evidence, with . Then
Here is the prior: how likely was before seeing . is the likelihood: how likely the evidence is if is true. is the posterior. The denominator is the total chance of the evidence, under all explanations together.
The rule is nothing more than the definition of conditional probability applied twice. and also . Set the two equal and divide by .
The denominator, built by total probability
We rarely know directly. But if the hypotheses split every possibility cleanly, the law of total probability gives
Substituting this in gives the form you will use most often:
The denominator is the same for every . It is a normalising constant that forces the posteriors to add up to 1. This is why, in practice, you often compute only the numerators and divide at the end.
Odds form
With two hypotheses, and not-, dividing one posterior by the other removes the denominator entirely:
The left side is the posterior odds. The first fraction on the right is the likelihood ratio, the strength of the evidence. The second is the prior odds. Evidence multiplies the odds. This form is very useful for quick mental arithmetic and for adding evidence one piece at a time.
Worked examples
A disease affects 1% of a population. A test catches 90% of sick people and wrongly flags 5% of healthy people. A person tests positive. What is the chance they are sick?
Let be "sick" and be "tests positive".
- Prior: , so .
- Likelihoods: and .
- Numerator for sick: .
- Numerator for healthy: .
- Denominator .
- Posterior: .
Interpretation: a positive result raises the chance of disease from 1% to about 15%. The test is decent, but the disease is rare enough that most positives are healthy people who were unlucky.
Suppose the disease is much more common here, with a prior of . Keep the same test: , .
- Numerator for sick: .
- Numerator for healthy: .
- Posterior: .
The same test now gives a 95% chance of disease. Nothing about the test changed. Only the prior did. A sensible reader keeps both facts in view: the test's accuracy and the base rate.
Suppose 20% of incoming email is spam. The word "free" appears in 60% of spam and in 5% of legitimate email. An email contains "free". What is the chance it is spam?
- Prior: , .
- Numerator for spam: .
- Numerator for ham: .
- Posterior: .
Seeing the word moves the belief from 20% to 75%. A real filter does this for hundreds of words at once, which is what the naive Bayes classifier does.
A bag holds a fair coin and a coin that lands heads 90% of the time. You pick one at random, so the prior is each. You flip it and get heads.
- Numerator for biased: . Numerator for fair: .
- Posterior: .
Flip again and get another heads. The posterior from step 2 is now the prior.
- Numerator for biased: . Numerator for fair: .
- Posterior: .
Each heads raises the belief in the biased coin, but by less each time, because the fair coin was already becoming less plausible.
- posterior P(sick | positive)
- prior (no test)
Tradeoffs and failure modes
Bayes' rule is exact, so the failures come from its inputs, not its algebra.
| Situation | What goes wrong | Better choice |
|---|---|---|
| The prior is a guess and is far off | The posterior inherits the error, and enough data eventually fixes it only slowly | Use a weakly informative prior, or check sensitivity to the prior |
| Evidence is not independent (two "free" words in one email) | Multiplying likelihoods counts the same evidence twice | Model the dependence, or accept overconfidence and temper the result |
| The denominator is intractable (many hidden variables) | You cannot normalise, so the posterior has no closed form | Sampling (MCMC) or variational inference |
| Only the posterior mode is reported | A single number hides how uncertain the belief is | Report the full posterior or a credible interval |
| The base rate is ignored | Likelihoods are read as if they were posteriors, the base-rate neglect error | Always write down the prior first |
The base-rate neglect in the last row is the most common human error with this rule. People see "90% accurate" and conclude "90% sure", forgetting that a rare condition makes most positive results false.
History
Thomas Bayes, a Presbyterian minister in England, worked on the problem of inverse probability. His essay on the topic was sent to the Royal Society after his death in 1761 and published in 1763 by his friend Richard Price, in Philosophical Transactions. It asked how to learn about the chances of an event from observed outcomes, which is exactly the reversed question above.
Pierre-Simon Laplace independently developed the same idea in a 1774 memoir, "On the probability of causes, as deduced from events," and applied it widely in astronomy and demography. He stated the general form and used it to estimate quantities from data, often with a uniform prior. For much of the 19th and early 20th centuries the approach was out of favour, partly because the choice of prior seemed arbitrary.
It came back in the 20th century through work on decision theory, and then through computing. Once machines could sum over many hypotheses, the denominator became manageable, and Bayesian methods spread into spam filtering, medical diagnosis, and eventually the probabilistic models behind modern machine learning. Its appeal is that it keeps a clear record of what was assumed and how the belief changed.
Check yourself
QuestionA test catches 99% of people with a condition and falsely flags 1% of those without it. The condition has a 1% prevalence. What is the posterior probability of the condition given a positive result?Show answer
The numerator for the condition is . The numerator for no condition is . The posterior is . A positive result is only a coin flip here, which shows how strongly the base rate matters.
QuestionWhy is the denominator the same for every hypothesis, and why can you often skip computing it?Show answer
The denominator is the total probability of the evidence, , which does not depend on which hypothesis you are looking at. It only normalises the numerators so the posteriors sum to 1. If you need only the ranking of hypotheses, or you can normalise at the end, you never need its value separately.
QuestionIn the odds form, the likelihood ratio is 8 and the prior odds are 1 to 3. What are the posterior odds, and what is the posterior probability?Show answer
The posterior odds are . Converting odds to a probability gives .
QuestionYou update a belief on 50 independent pieces of evidence and then find the evidence was actually one piece copied 50 times. What went wrong, and what effect does it have on the posterior?Show answer
Multiplying likelihoods assumes independent evidence. Copying one piece 50 times multiplies its likelihood ratio 50 times, which makes the posterior far too confident. The fix is to model the dependence, or count the evidence once and temper the update.
Sources
- Bayes' theorem (Wikipedia): https://en.wikipedia.org/wiki/Bayes%27_theorem
- Bayesian inference (Wikipedia): https://en.wikipedia.org/wiki/Bayesian_inference
- Bishop, Pattern Recognition and Machine Learning, §1.2.3: https://www.microsoft.com/en-us/research/publication/pattern-recognition-machine-learning/