impera Open Impera

IB Diploma Mathematics AA · Statistics and probability

Bayes' theorem (HL)

1Reversing a condition

Often you know $P(B \mid A)$ but want $P(A \mid B)$. For example, a test tells you $P(\text{positive} \mid \text{disease})$, but a patient wants $P(\text{disease} \mid \text{positive})$.

Bayes' theorem reverses the condition:

$$P(A \mid B) = \frac{P(B \mid A)\,P(A)}{P(B \mid A)\,P(A) + P(B \mid A')\,P(A')}$$

The bottom line is just $P(B)$, found by adding both ways that $B$ can happen.

Check yourself

What does Bayes' theorem let you do?

  • Calculate a binomial probability

  • Find P(A ∩ B) for independent events

  • Find P(A | B) when you know P(B | A)

  • Add two probabilities

Show the answer

Find P(A | B) when you know P(B | A)

It turns a condition around.

2Bayes on a tree diagram

Bayes' theorem on a tree diagram: add the positive paths, then divide

A tree diagram makes Bayes' theorem easy:

  • The tree goes in the order you are told: cause first, then result.
  • $P(B)$ is the sum of every path that ends in $B$.
  • $P(A \mid B)$ is the path through $A$ and $B$, divided by $P(B)$.
Worked example

A screening test

  1. 1% of people have a condition. The test is positive for 90% of people with it and for 5% of people without it.
  2. $P(C \cap +) = 0.01 \times 0.90 = 0.009$.
  3. $P(C' \cap +) = 0.99 \times 0.05 = 0.0495$.
  4. $P(+) = 0.009 + 0.0495 = 0.0585$.
  5. $P(C \mid +) = \frac{0.009}{0.0585} = 0.154$.

Only 15% of people who test positive actually have the condition. When a condition is rare, false positives dominate.

Check yourself

In Bayes' theorem, how do you find P(B) on a tree diagram?

  • Take the first branch only

  • Multiply all of the branches together

  • Add all the paths that end in B

  • Subtract P(A) from the number 1

Show the answer

Add all the paths that end in B

B can happen through A or through A′.

3More than two causes

Bayes' theorem works with any number of causes $A_1, A_2, A_3, \ldots$ that cover all possibilities:

$$P(A_i \mid B) = \frac{P(B \mid A_i)\,P(A_i)}{\sum_j P(B \mid A_j)\,P(A_j)}$$

Worked example

Three machines

  1. A factory's bolts come from machines A (50%), B (30%) and C (20%). They are faulty 2%, 3% and 5% of the time.
  2. $P(\text{faulty}) = 0.5(0.02) + 0.3(0.03) + 0.2(0.05) = 0.010 + 0.009 + 0.010 = 0.029$.
  3. A bolt is faulty. $P(C \mid \text{faulty}) = \frac{0.010}{0.029} = 0.345$.
  4. Machine C makes only 20% of bolts but 34.5% of the faulty ones.
Check yourself

In the factory example, why is P(C | faulty) larger than P(C)?

  • Machine C makes the most bolts overall

  • Machine C has the highest fault rate

  • Faulty bolts are rare in the factory

  • The probabilities were rounded up

Show the answer

Machine C has the highest fault rate

Faults point more strongly to the worst machine.

4Prior and posterior

In Bayes' language:

  • $P(A)$ is the prior: your belief before the evidence.
  • $P(A \mid B)$ is the posterior: your belief after seeing evidence $B$.

A second piece of evidence can be added by using the posterior as the new prior.

Worked example

Testing twice

  1. After one positive test, $P(C) = 0.154$.
  2. A second, independent test is also positive. Now the prior is $0.154$.
  3. $P(C \cap +) = 0.154 \times 0.9 = 0.1386$ and $P(C' \cap +) = 0.846 \times 0.05 = 0.0423$.
  4. $P(C \mid ++) = \frac{0.1386}{0.1809} = 0.766$.

Two positive tests make the condition much more likely.

Check yourself

In Bayes' theorem, what is the prior?

  • The probability of the evidence

  • The probability after the new evidence

  • Always 0.5

  • The probability before the new evidence

Show the answer

The probability before the new evidence

It is updated into the posterior.

5Medical testing and false positives

Bayes' theorem shows why positive test results for rare conditions can be misleading.

Worked example

A test for a rare disease

  1. 1% of people have the disease.
  2. The test detects 95% of cases (sensitivity).
  3. It wrongly gives a positive result for 5% of healthy people.
  4. $P(\text{positive}) = 0.01 \times 0.95 + 0.99 \times 0.05 = 0.0095 + 0.0495 = 0.059$.
  5. $P(\text{disease} \mid \text{positive}) = \frac{0.0095}{0.059} \approx 0.161$.
  6. Only about 16% of people who test positive actually have the disease.
Check yourself

Why can a positive result for a rare disease still be likely to be false?

  • Bayes' theorem does not apply to medical tests, so the result is uncertain

  • The disease is common, so most people who test positive are actually healthy

  • Medical tests are never accurate, so a positive result means very little

  • Healthy people far outnumber sick ones, so false positives outnumber true ones

Show the answer

Healthy people far outnumber sick ones, so false positives outnumber true ones

Base rates matter.

6Updating probabilities

Bayes' theorem lets us update a probability with new evidence. The updated probability (posterior) becomes the prior for the next piece of evidence.

Worked example

Testing twice

  1. After one positive test, $P(\text{disease}) \approx 0.161$.
  2. A second, independent positive test: $P(\text{positive}) = 0.161 \times 0.95 + 0.839 \times 0.05 \approx 0.153 + 0.042 = 0.195$.
  3. $P(\text{disease} \mid \text{two positives}) \approx \frac{0.153}{0.195} \approx 0.785$.
  4. A second test greatly increases confidence.
Check yourself

What is a prior probability?

  • The probability after new evidence is considered

  • The probability before new evidence is considered

  • The sensitivity of a diagnostic test

  • A probability equal to zero before a test

Show the answer

The probability before new evidence is considered

Before updating.

7Going deeper: a screening test, three causes and updating twice

Bayes' theorem reverses a condition: $P(B \mid A) = \frac{P(B)\,P(A \mid B)}{P(B)\,P(A \mid B) + P(B')\,P(A \mid B')}$. The denominator is $P(A)$, found by adding the routes through a tree.

Worked example (screening): 1% of people have a condition. The test is positive for 95% of those who have it and for 4% of those who do not.

  1. $P(\text{has it and positive}) = 0.01 \times 0.95 = 0.0095$.
  2. $P(\text{does not and positive}) = 0.99 \times 0.04 = 0.0396$.
  3. $P(\text{positive}) = 0.0491$.
  4. $P(\text{has it} \mid \text{positive}) = \frac{0.0095}{0.0491} \approx 0.193$.
  5. Only about 19% of positives are true positives. The condition is rare, so false positives from the large healthy group outnumber true positives.
  6. In whole numbers: of 10,000 people, 100 have it and 95 test positive; of the 9,900 without it, 396 test positive. $\frac{95}{491} \approx 0.193$.

Worked example (updating): the same person takes an independent second test and is positive again.

  1. The new prior is 0.193.
  2. Numerator: $0.193 \times 0.95 \approx 0.184$.
  3. Other route: $0.807 \times 0.04 \approx 0.0323$.
  4. Posterior: $\frac{0.184}{0.184 + 0.0323} \approx 0.85$.
  5. Each piece of evidence updates the probability. Yesterday's posterior is today's prior.

Worked example (three causes): machines A, B and C make 50%, 30% and 20% of the output, with defect rates 1%, 2% and 5%.

  1. $P(\text{defective}) = 0.5(0.01) + 0.3(0.02) + 0.2(0.05) = 0.005 + 0.006 + 0.010 = 0.021$.
  2. $P(C \mid \text{defective}) = \frac{0.010}{0.021} \approx 0.476$.
  3. $P(A \mid \text{defective}) = \frac{0.005}{0.021} \approx 0.238$.
  4. C makes a fifth of the items and nearly half of the defective ones.

Vocabulary

  • Prior: the probability before the evidence.
  • Likelihood: the probability of the evidence under each hypothesis.
  • Posterior: the probability after the evidence.
  • Base rate: how common the condition is. Ignoring it is the base rate fallacy.

Common mistakes

  • Confusing $P(\text{positive} \mid \text{ill})$ with $P(\text{ill} \mid \text{positive})$.
  • Leaving a route out of the denominator.
  • Rounding intermediate products too early, which distorts a small numerator.
Check yourself

Bag X holds 3 red and 1 blue counter; bag Y holds 1 red and 3 blue. A bag is chosen at random and a red counter is drawn. What is the probability it came from bag X?

  • 0.25

  • 0.75

  • 0.5

  • 0.375

Show the answer

0.75

(0.5 × 0.75) ÷ (0.5 × 0.75 + 0.5 × 0.25) = 0.375 ÷ 0.5.

Now test yourself

This lesson has a 8-question quiz and 5 exam-style questions with mark schemes in Impera. Free, with your progress saved.

Practise in Impera