Search

Search papers and lessons

Bayes' Rule

Level 2 · 9 min read

Bayes' rule turns the chance of evidence given a hypothesis into the chance of a hypothesis given the evidence, using a prior belief as the starting point.

On this page

  • Colour = topic mix
  • Probability
  • Sign in to see your progress

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 P(sick∣positive)P(\text{sick} \mid \text{positive}) and P(positive∣sick)P(\text{positive} \mid \text{sick}) 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 HH be a hypothesis and EE be some evidence, with P(E)>0P(E) \gt 0. Then

P(H∣E)=P(E∣H) P(H)P(E).P(H \mid E) = \frac{P(E \mid H)\, P(H)}{P(E)}.

Here P(H)P(H) is the prior: how likely HH was before seeing EE. P(E∣H)P(E \mid H) is the likelihood: how likely the evidence is if HH is true. P(H∣E)P(H \mid E) is the posterior. The denominator P(E)P(E) is the total chance of the evidence, under all explanations together.

The rule is nothing more than the definition of conditional probability applied twice. P(H∩E)=P(H∣E) P(E)P(H \cap E) = P(H \mid E)\,P(E) and also P(H∩E)=P(E∣H) P(H)P(H \cap E) = P(E \mid H)\,P(H). Set the two equal and divide by P(E)P(E).

The denominator, built by total probability

We rarely know P(E)P(E) directly. But if the hypotheses H1,…,HnH_1, \dots, H_n split every possibility cleanly, the law of total probability gives

P(E)=∑i=1nP(E∣Hi) P(Hi).P(E) = \sum_{i=1}^{n} P(E \mid H_i)\, P(H_i).

Substituting this in gives the form you will use most often:

P(Hk∣E)=P(E∣Hk) P(Hk)∑i=1nP(E∣Hi) P(Hi).P(H_k \mid E) = \frac{P(E \mid H_k)\, P(H_k)}{\sum_{i=1}^{n} P(E \mid H_i)\, P(H_i)}.

The denominator is the same for every kk. 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, HH and not-HH, dividing one posterior by the other removes the denominator entirely:

P(H∣E)P(¬H∣E)=P(E∣H)P(E∣¬H)⋅P(H)P(¬H).\frac{P(H \mid E)}{P(\neg H \mid E)} = \frac{P(E \mid H)}{P(E \mid \neg H)} \cdot \frac{P(H)}{P(\neg H)}.

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

Example
A medical test

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 DD be "sick" and TT be "tests positive".

  1. Prior: P(D)=0.01P(D) = 0.01, so P(¬D)=0.99P(\neg D) = 0.99.
  2. Likelihoods: P(T∣D)=0.90P(T \mid D) = 0.90 and P(T∣¬D)=0.05P(T \mid \neg D) = 0.05.
  3. Numerator for sick: 0.90×0.01=0.0090.90 \times 0.01 = 0.009.
  4. Numerator for healthy: 0.05×0.99=0.04950.05 \times 0.99 = 0.0495.
  5. Denominator P(T)=0.009+0.0495=0.0585P(T) = 0.009 + 0.0495 = 0.0585.
  6. Posterior: P(D∣T)=0.009/0.0585≈0.154P(D \mid T) = 0.009 / 0.0585 \approx 0.154.

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.

Example
The same test in a population where it is common

Suppose the disease is much more common here, with a prior of P(D)=0.5P(D) = 0.5. Keep the same test: P(T∣D)=0.90P(T \mid D) = 0.90, P(T∣¬D)=0.05P(T \mid \neg D) = 0.05.

  1. Numerator for sick: 0.90×0.5=0.450.90 \times 0.5 = 0.45.
  2. Numerator for healthy: 0.05×0.5=0.0250.05 \times 0.5 = 0.025.
  3. Posterior: 0.45/(0.45+0.025)=0.45/0.475≈0.9470.45 / (0.45 + 0.025) = 0.45 / 0.475 \approx 0.947.

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.

Example
A spam filter with one word

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?

  1. Prior: P(spam)=0.2P(\text{spam}) = 0.2, P(ham)=0.8P(\text{ham}) = 0.8.
  2. Numerator for spam: 0.6×0.2=0.120.6 \times 0.2 = 0.12.
  3. Numerator for ham: 0.05×0.8=0.040.05 \times 0.8 = 0.04.
  4. Posterior: 0.12/(0.12+0.04)=0.750.12 / (0.12 + 0.04) = 0.75.

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.

Example
Updating twice

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 1/21/2 each. You flip it and get heads.

  1. Numerator for biased: 0.9×0.5=0.450.9 \times 0.5 = 0.45. Numerator for fair: 0.5×0.5=0.250.5 \times 0.5 = 0.25.
  2. Posterior: P(biased∣H)=0.45/0.70≈0.643P(\text{biased} \mid H) = 0.45 / 0.70 \approx 0.643.

Flip again and get another heads. The posterior from step 2 is now the prior.

  1. Numerator for biased: 0.643×0.9≈0.5790.643 \times 0.9 \approx 0.579. Numerator for fair: 0.357×0.5≈0.1790.357 \times 0.5 \approx 0.179.
  2. Posterior: 0.579/(0.579+0.179)≈0.7640.579 / (0.579 + 0.179) \approx 0.764.

Each heads raises the belief in the biased coin, but by less each time, because the fair coin was already becoming less plausible.

00.20.40.60.8100.20.40.60.81prior P(sick)probability
  • posterior P(sick | positive)
  • prior (no test)
For a test that catches 90% of sick people and falsely flags 5% of healthy ones, a positive result pushes the belief well above the prior. At a 1% prior it reaches about 15%, and at a 50% prior about 95%.

Tradeoffs and failure modes

Bayes' rule is exact, so the failures come from its inputs, not its algebra.

SituationWhat goes wrongBetter choice
The prior is a guess and is far offThe posterior inherits the error, and enough data eventually fixes it only slowlyUse 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 twiceModel 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 formSampling (MCMC) or variational inference
Only the posterior mode is reportedA single number hides how uncertain the belief isReport the full posterior or a credible interval
The base rate is ignoredLikelihoods are read as if they were posteriors, the base-rate neglect errorAlways 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 0.99×0.01=0.00990.99 \times 0.01 = 0.0099. The numerator for no condition is 0.01×0.99=0.00990.01 \times 0.99 = 0.0099. The posterior is 0.0099/(0.0099+0.0099)=0.50.0099 / (0.0099 + 0.0099) = 0.5. A positive result is only a coin flip here, which shows how strongly the base rate matters.

QuestionWhy is the denominator P(E)P(E) the same for every hypothesis, and why can you often skip computing it?Show answer

The denominator is the total probability of the evidence, ∑iP(E∣Hi) P(Hi)\sum_i P(E \mid H_i)\, P(H_i), 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 8×(1/3)=8/38 \times (1/3) = 8/3. Converting odds 8/38/3 to a probability gives (8/3)/(1+8/3)=8/11≈0.727(8/3)/(1 + 8/3) = 8/11 \approx 0.727.

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