---
title: Bayes' rule
heading: Reasoning backward from what you observed
eyebrow: A11 · Lesson 5 · Bayes' rule
xp: 40
---

::: hook
A board tester catches 99% of bad boards and wrongly flags only 2% of good ones. One percent of boards are bad. The tester flags a board: what's the chance it's really bad? Most people say about 99%. The answer is one in three. Bayes' rule is the calculation that gets it right, and it's how every receiver decides which symbol was sent, every spam filter scores a message, and every fault detector weighs an alarm.
:::

::: play
- The curve is the chance that a flagged board is really bad, for each possible share of bad boards (the horizontal axis, on a log scale).
- Start with 1% bad boards: about $\frac{1}{3}$. Make the defects rarer and watch the answer collapse.
- Which helps more when defects are rare: catching more of them (sensitivity) or flagging fewer good boards (false alarms)?
:::
:::

```sim plot
view: graph
x: q = 0.0001..1 log
y: P = 0..1
slider: s = 0.5..1 step 0.01 = 0.99
slider: f = 0.001..0.2 = 0.02 log
slider: q0 = 0.0001..0.5 = 0.01 log
plot: s*q/(s*q + f*(1 - q)) | label: P(bad given flagged)
marker: x = q0 | label: share of bad boards
readout: P(bad given flagged) = s*q0/(s*q0 + f*(1 - q0)) | digits: 3
readout: flagged boards, of all = s*q0 + f*(1 - q0) | digits: 3
```

::: predict
## With 1% of boards bad, 99% detection and 2% false alarms, what fraction of flagged boards are bad?

- [ ] 99%
- [ ] 98%
- [ ] 50%
- [x] About 33%

::: reveal
About 33%. Out of 10,000 boards, 100 are bad and the tester flags 99 of them. Of the 9,900 good boards it flags 2%, which is 198. So 297 boards are flagged and only 99 of those are bad: one in three. When the thing you're looking for is rare, false alarms from the large majority swamp the true detections.
:::
:::

::: nameit
## Bayes' rule

Write the multiplication rule both ways, $P(A \cap B) = P(B)P(A \mid B) = P(A)P(B \mid A)$, and solve:

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

The denominator comes from total probability. With $B$ and its complement:

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

The names:

| Term | Meaning | Board tester |
|---|---|---|
| $P(B)$ | **prior**: belief before the evidence | 1% of boards are bad |
| $P(A \mid B)$ | **likelihood**: how well $B$ explains the evidence | 99% of bad boards are flagged |
| $P(B \mid A)$ | **posterior**: belief after the evidence | $\frac{1}{3}$ of flagged boards are bad |

## The odds form

The **odds** of $B$ are $\frac{P(B)}{P(B^c)}$. Bayes' rule in odds is one multiplication:

$$\text{posterior odds} = \frac{P(A \mid B)}{P(A \mid B^c)} \times \text{prior odds}$$

The ratio in the middle, the **likelihood ratio**, measures how strongly the evidence points to $B$. For the tester it's $\frac{0.99}{0.02} = 49.5$; prior odds of 1 to 99 become 49.5 to 99, or 1 to 2, a probability of $\frac{1}{3}$.

## Repeated evidence

If two tests are independent given the board's true state, apply the rule twice: each positive result multiplies the odds by 49.5. Two positives give odds of $\frac{49.5^2}{99} = 24.75$, a probability of 0.96. Retesting flagged boards is how real production lines cope with low base rates.

## Receivers

A receiver sees a noisy symbol and must guess what was sent. The priors are how often each symbol is sent, the likelihoods come from the channel's error rates, and the best guess is the symbol with the largest posterior (lesson 42 develops this).
:::

::: example
## A binary channel sends 1 with probability 0.3. A sent 1 is read as 1 with probability 0.9; a sent 0 is read as 1 with probability 0.05. A 1 is received. What is the chance a 1 was sent?

::: solution
1. **Chance of reading a 1.**
   $$\begin{aligned} P(\text{read }1) &= 0.3 \times 0.9 \\ &\quad + 0.7 \times 0.05 \\ &= 0.27 + 0.035 \\ &= 0.305 \end{aligned}$$
2. **Bayes's rule.**
   $$\begin{aligned} P(\text{sent }1 \mid \text{read }1) &= \frac{0.27}{0.305} \\ &= 0.885 \end{aligned}$$
3. **Why it's below 0.9.** Even though the channel reads a 1 correctly 90% of the time, the rarity of sent 1s pulls the posterior down a little, and the 0s misread as 1s account for the rest.

**Answer:** given a received 1, the chance a 1 was sent is 0.885.
:::
:::

::: example
## A flagged board is retested on an independent tester with the same rates and flagged again. What is the chance now that it's bad?

::: solution
1. **Odds per positive.** Each positive multiplies the odds by
   $$\frac{0.99}{0.02} = 49.5$$
2. **Two positives.** Starting from odds of $\frac{1}{99}$:
   $$\frac{49.5^2}{99} = 24.75$$
3. **Back to a probability.**
   $$\begin{aligned} P &= \frac{24.75}{25.75} \\ &= 0.961 \end{aligned}$$

**Answer:** after two flags, the board is bad with chance 0.961.
:::
:::

::: example
## Using the suppliers above, a part is found defective. What is the chance it came from B?

::: solution
1. **Bayes's rule.**
   $$\begin{aligned} P(B \mid \text{bad}) &= \frac{P(\text{bad} \mid B)P(B)}{P(\text{bad})} \\ &= \frac{0.03 \times 0.4}{0.018} \\ &= \frac{0.012}{0.018} \\ &= 0.667 \end{aligned}$$
2. **What it means.** B supplies 40% of the parts but two thirds of the defects. Bayes's rule weighs each supplier's share by how strongly a defect points to it.

**Answer:** a defective part came from B with chance 0.667, two thirds.
:::
:::

::: check
given: q = 0.01 | 0.05; s = 0.95 | 0.99; f = 0.02 | 0.05
answer: s*q/(s*q + f*(1 - q))
tolerance: 0.5%

## A fraction {q} of parts are faulty. A test flags faulty parts with probability {s} and good parts with probability {f}. Given a flag, what is the chance the part is faulty?

::: right
Right. $\frac{sq}{sq + f(1 - q)}$ = {answer}.
:::

::: misses
- [s] That's $P(\text{flag} \mid \text{faulty})$; you need the reverse.
- [s*q] Divide by the chance of a flag, from faulty and good parts alike.
- [any] Not yet. Bayes' rule, with total probability in the denominator.
:::
:::

::: check
given: q = 0.01 | 0.05; s = 0.95 | 0.99; f = 0.02 | 0.05
answer: s*q + f*(1 - q)
tolerance: 0.5%

## Same test: faulty fraction {q}, detection {s}, false alarms {f}. What fraction of all parts gets flagged?

::: right
Right. Total probability: {answer}.
:::

::: misses
- [s*q] Good parts get flagged too: add $f(1 - q)$.
- [any] Not yet. Flags from faulty parts plus flags from good ones.
:::
:::

::: check
given: a = 0.3 | 0.6; e1 = 0.1 | 0.05; e0 = 0.05 | 0.03
answer: a*(1 - e1)/(a*(1 - e1) + (1 - a)*e0)
tolerance: 0.5%

## A link sends 1 with probability {a}. A sent 1 is misread as 0 with probability {e1}; a sent 0 is misread as 1 with probability {e0}. A 1 is received. What is the chance a 1 was sent?

::: right
Right. {answer}.
:::

::: misses
- [1 - e1] That's the chance a sent 1 reads as 1. Reverse it with Bayes' rule.
- [a] That's the prior. The received 1 is evidence; update it.
- [any] Not yet. $\frac{P(\text{read }1 \mid \text{sent }1)P(\text{sent }1)}{P(\text{read }1)}$.
:::
:::

::: check
given: q = 0.01 | 0.001; L = 50 | 100
answer: L*q/(1 - q)/(1 + L*q/(1 - q))
tolerance: 0.5%

## An event has prior probability {q}. Evidence arrives with likelihood ratio {L}. What is the posterior probability?

::: right
Right. The odds go from $\frac{q}{1 - q}$ to $L$ times that; converting back gives {answer}.
:::

::: misses
- [L*q] Multiply the odds, not the probability.
- [any] Not yet. Odds $o = \frac{q}{1 - q}$, then $Lo$, then $\frac{Lo}{1 + Lo}$.
:::
:::

::: check
given: q = 0.01 | 0.02; s = 0.99 | 0.95; f = 0.02 | 0.05
answer: s^2*q/(s^2*q + f^2*(1 - q))
tolerance: 0.5%

## Two independent tests, each with detection {s} and false alarm rate {f}, both flag a part. A fraction {q} of parts are faulty. What is the chance this one is faulty?

::: right
Right. Each test multiplies the odds by $\frac{s}{f}$: {answer}.
:::

::: misses
- [s*q/(s*q + f*(1 - q))] That uses only one of the two results.
- [any] Not yet. The likelihoods of two positives are $s^2$ and $f^2$.
:::
:::

::: check
kind: choice

## Faults are rare (0.1%). Which change raises $P(\text{faulty} \mid \text{flag})$ the most?

- [ ] Raising detection from 95% to 99%
- [x] Cutting false alarms from 2% to 0.2%
- [ ] Testing parts in a different order
- [ ] Neither matters; only the prior does

::: right
Right. With rare faults, nearly every flag comes from the huge pool of good parts, so the false alarm rate sets the answer. Cutting it tenfold multiplies the posterior odds by ten; better detection gains only a few percent.
:::

::: misses
- [1] Detection appears in the numerator and denominator; it changes the answer by only about 4%.
- [3] The order of testing doesn't change any probability here.
- [4] The likelihoods matter as much as the prior; that's what the rule multiplies.
:::
:::

::: check
kind: parts
given: pa = 0.6 | 0.7; ra = 0.01 | 0.02; rb = 0.03 | 0.05

## A fraction {pa} of parts come from A (defect rate {ra}), the rest from B (defect rate {rb}). A part is defective.

::: part
answer: (1 - pa)*rb/(pa*ra + (1 - pa)*rb)
tolerance: 0.5%

### What is the chance it came from B?

::: misses
- [1 - pa] That's before you knew it was defective.
- [any] Bayes: $\frac{P(\text{bad} \mid B)P(B)}{P(\text{bad})}$.
:::
:::
:::

::: check
kind: derivation
vars: p = 0.001..0.05, s = 0.9..0.99, f = 0.01..0.1

## A test detects a fault (present in a fraction $p$ of boards) with probability $s$ and false-alarms on good boards with probability $f$.

::: step
answer: s*p + f*(1 - p)

### What fraction of boards are flagged?
:::

::: step
answer: s*p/(s*p + f*(1 - p))

### What fraction of flagged boards are really faulty?
:::

::: right
Right. When faults are rare, false alarms can outnumber real detections.
:::
:::

::: explain
## Why is the posterior so low when a reliable test flags a rare fault?

::: model
The posterior compares two ways of getting a flag: a faulty part being caught, and a good part being falsely flagged. Even a small false alarm rate applies to the vast number of good parts, so it can produce many more flags than the few faulty parts do. The flag then most likely came from a good part. The prior sets how big each pool is; the likelihoods set what fraction of each pool raises a flag.
:::
:::

::: practice

::: check
given: q = 0.02 | 0.1; s = 0.9 | 0.98; f = 0.01 | 0.03
answer: s*q/(s*q + f*(1 - q))
tolerance: 0.5%

## Prior {q}, detection {s}, false alarm rate {f}. A flag appears. What is the posterior?

::: misses
- [s] Reverse the conditioning with Bayes' rule.
- [any] $\frac{sq}{sq + f(1 - q)}$.
:::
:::

::: check
given: s = 0.9 | 0.99; f = 0.01 | 0.05
answer: s/f
tolerance: 0.5%

## A test has detection {s} and false alarm rate {f}. What is the likelihood ratio of a positive result?

::: misses
- [f/s] Detection over false alarms.
- [any] $\frac{P(+ \mid \text{faulty})}{P(+ \mid \text{good})}$.
:::
:::

::: check
given: q = 0.1 | 0.2 | 0.25 | 0.4 | 0.6 | 0.75 | 0.8
answer: q/(1 - q)
tolerance: 0.5%

## What are the odds of an event with probability {q}?

::: misses
- [any] $\frac{q}{1 - q}$.
:::
:::
:::
