---
title: Conditional probability
heading: Updating chances when you learn something
eyebrow: A11 · Lesson 4 · Conditional probability
xp: 40
---

::: hook
A network loses 1% of its packets. A packet has just been lost: what's the chance the next one is lost too? If losses were unrelated, still 1%. On a real link, where interference comes in bursts, it might be 40%. Knowing that one thing happened changes the chances of another, and conditional probability is the arithmetic of that change. It's also the tool for any process that happens in stages: which factory line made a chip, then whether that chip is bad.
:::

::: play
- Two dice are thrown 20,000 times. Each bar is one of the 36 outcomes, numbered row by row: bars 1 to 6 have the first die showing 1, bars 7 to 12 have it showing 2, and so on.
- Event $B$: the first die shows $b$. Event $A$: the sum is at least $s$. The readouts measure $P(B)$, $P(A \cap B)$ and their ratio.
- Set $b = 6$ and $s = 10$. Knowing the first die is a 6, how likely is a sum of 10 or more? Compare with the chance before you knew.
:::
:::

```sim plot
view: random
x: cell = 0.5..36.5
slider: b = 1..6 step 1 = 6
slider: s = 2..12 step 1 = 10
sample: 6*dice(1) + dice(1) - 6
trials: 20000
bins: 36
density: 1/36
measure: step(cell - 6*(b - 1) - 0.5)*step(6*b + 0.5 - cell) | name: PB
measure: step(cell - 6*(b - 1) - 0.5)*step(6*b + 0.5 - cell)*step(cell - (s + 5*b - 6) + 0.5) | name: PAB
measure: step(floor((cell - 1)/6) + 1 + cell - 6*floor((cell - 1)/6) - s + 0.5) | name: PA
readout: P(B) = PB | digits: 3
readout: P(A and B) = PAB | digits: 3
readout: P(A given B), measured = PAB/PB | digits: 3
readout: P(A given B), counted = (abs(7 + b - s) - abs(1 + b - s) + 6)/12 | digits: 3
readout: P(A), not knowing the first die = PA | digits: 3
```

::: predict
## A chip comes from line 1 (60% of production, 2% defective) or line 2 (40%, 5% defective). What fraction of all chips is defective?

- [ ] 3.5%, the average of the two rates
- [x] 3.2%
- [ ] 7%
- [ ] 0.1%, the product of the two rates

::: reveal
3.2%. Split by line: $0.6 \times 0.02 + 0.4 \times 0.05 = 0.012 + 0.020 = 0.032$. Each defect rate is a conditional probability, the chance of a defect given the line, and the overall rate weights each by how much that line makes. The plain average would be right only if the lines made equal shares.
:::
:::

::: nameit
## Conditional probability

The probability of $A$ **given** $B$ is

$$P(A \mid B) = \frac{P(A \cap B)}{P(B)} \qquad (P(B) > 0)$$

Learning that $B$ happened shrinks the sample space to $B$; the outcomes of $A$ that are still possible are those in $A \cap B$, and dividing by $P(B)$ rescales so the new sample space has probability 1. With equally likely outcomes it's just counting inside $B$: of the 6 outcomes with the first die showing 6, three have a sum of 10 or more, so $P = \frac{3}{6}$, against $\frac{6}{36}$ before.

A conditional probability is a probability: it obeys all the axioms, so for example $P(A^c \mid B) = 1 - P(A \mid B)$.

## The multiplication rule

Turned around, the definition gives the chance that two things both happen, one stage at a time:

$$P(A \cap B) = P(B)\,P(A \mid B)$$

Drawing two parts from 10, of which 3 are bad: $P(\text{both bad}) = \frac{3}{10} \times \frac{2}{9} = \frac{1}{15}$. The second factor is conditional, because the first draw changed what's left.

## Total probability

If $B_1, B_2, \ldots$ split the sample space into pieces that don't overlap (which line made the chip, which symbol was sent), then

$$P(A) = \sum_i P(A \mid B_i)\,P(B_i)$$

Draw it as a tree: the first branches are the $B_i$ with their chances, the second branches are $A$ or not, given each $B_i$. Multiply along a path to get that path's chance, and add the paths that end in $A$.

## Order matters

$P(A \mid B)$ and $P(B \mid A)$ are different numbers. The chance a defective chip came from line 2 is not the chance a line-2 chip is defective. Lesson 5 turns one into the other.
:::

::: example
## A link loses 1% of packets, and 0.4% of consecutive pairs are both lost. Given that a packet was lost, what is the chance the next one is lost?

::: solution
1. **Apply the definition.** Write $L_1$ for this packet lost and $L_2$ for the next one lost:
   $$\begin{aligned} P(L_2 \mid L_1) &= \frac{P(\text{both lost})}{P(L_1)} \\ &= \frac{0.004}{0.01} \\ &= 0.4 \end{aligned}$$
2. **Losses come in bursts.** One loss makes the next 40 times more likely than average.
3. **Why it matters.** A retransmission scheme that assumes independent losses would badly underestimate how often two copies in a row fail.

**Answer:** given a loss, the next packet is lost with chance 0.4.
:::
:::

::: example
## A box has 10 fuses, 3 of them blown. You take two without looking. What is the chance that exactly one is blown?

::: solution
1. **Blown, then good.** This is one of the two paths in the tree that give exactly one blown fuse:
   $$\frac{3}{10} \times \frac{7}{9} = \frac{21}{90}$$
2. **Good, then blown.** This is the other:
   $$\frac{7}{10} \times \frac{3}{9} = \frac{21}{90}$$
3. **Add the two paths.**
   $$\begin{aligned} P &= \frac{21}{90} + \frac{21}{90} \\ &= \frac{42}{90} \\ &= 0.467 \end{aligned}$$
4. **Check by counting.**
   $$\frac{\binom{3}{1}\binom{7}{1}}{\binom{10}{2}} = \frac{21}{45}$$

**Answer:** the chance that exactly one is blown is 0.467.
:::
:::

::: example
## 60% of parts come from supplier A, with 1% defective, and 40% from B, with 3% defective. What fraction of parts are defective?

::: solution
1. **Split by supplier.** By the law of total probability:
   $$\begin{aligned} P(\text{bad}) &= 0.6 \times 0.01 \\ &\quad + 0.4 \times 0.03 \\ &= 0.006 + 0.012 \\ &= 0.018 \end{aligned}$$
2. **Read it.** 1.8% is a weighted average of the two rates.
3. **Turning it around.** Lesson 5 asks the reverse question: given a bad part, how likely is it to be from B?

**Answer:** 1.8% of parts are defective.
:::
:::

::: check
given: pab = 0.05 | 0.12; pb = 0.2 | 0.3
answer: pab/pb
tolerance: 0.5%

## $P(A \cap B)$ = {pab} and $P(B)$ = {pb}. What is $P(A \mid B)$?

::: right
Right. $\frac{P(A \cap B)}{P(B)}$ = {answer}.
:::

::: misses
- [pab*pb] Divide by $P(B)$; don't multiply.
- [pb/pab] Upside down.
- [any] Not yet. $P(A \mid B) = P(A \cap B)/P(B)$.
:::
:::

::: check
given: b = 4 | 5 | 6
answer: (b - 2)/6
tolerance: 0.5%

## Two dice are thrown, and the first shows {b}. What is the chance that the sum is 9 or more?

::: right
Right. The second die needs at least $9 - b$: that's {answer}.
:::

::: misses
- [10/36] That's the chance before you saw the first die. Count only within the 6 outcomes that remain.
- [any] Not yet. Which values of the second die work?
:::
:::

::: check
given: s1 = 0.6 | 0.7; d1 = 0.01 | 0.02; d2 = 0.04 | 0.05
answer: s1*d1 + (1 - s1)*d2
tolerance: 0.5%

## Line 1 makes {s1} of the chips with a defect rate of {d1}; line 2 makes the rest with a defect rate of {d2}. What fraction of all chips is defective?

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

::: misses
- [(d1 + d2)/2] Weight each line's rate by its share of production.
- [d1*d2] Multiply each rate by its line's share, then add.
- [any] Not yet. $P(D) = P(D \mid 1)P(1) + P(D \mid 2)P(2)$.
:::
:::

::: check
given: N = 10 | 20; D = 3 | 4
answer: D/N*(D - 1)/(N - 1)
tolerance: 0.5%

## A box holds {N} parts, {D} of them defective. You draw two without replacement. What is the chance both are defective?

::: right
Right. $\frac{D}{N} \times \frac{D - 1}{N - 1}$ = {answer}.
:::

::: misses
- [(D/N)^2] After the first defective is drawn, only $D - 1$ remain among $N - 1$.
- [any] Not yet. Multiplication rule: first draw, then the second given the first.
:::
:::

::: check
given: p = 0.01 | 0.02; q = 0.003 | 0.008
answer: q/p
tolerance: 0.5%

## A link loses a fraction {p} of its packets, and a fraction {q} of consecutive pairs are both lost. Given a loss, what is the chance the next packet is lost?

::: right
Right. $\frac{q}{p}$ = {answer}, far above {p}: the losses come in bursts.
:::

::: misses
- [p] That assumes the losses are independent; the pair rate says they aren't.
- [q] That's the chance both are lost, before knowing anything.
- [any] Not yet. $P(\text{second} \mid \text{first}) = P(\text{both})/P(\text{first})$.
:::
:::

::: check
kind: choice

## A test catches 99% of bad boards. Does that mean 99% of the boards it flags are bad?

- [ ] Yes, it's the same statement
- [x] No: that depends on how common bad boards are and how often good boards are flagged
- [ ] Yes, if the test is independent of the boards
- [ ] No, it means 1% of flagged boards are bad

::: right
Right. The first is $P(\text{flag} \mid \text{bad})$; the second is $P(\text{bad} \mid \text{flag})$. If bad boards are rare, most flags can come from the many good boards. Lesson 5 shows how to compute it.
:::

::: misses
- [1] Conditioning in the other direction gives a different number.
- [3] Independence isn't the issue; the two probabilities answer different questions.
- [4] That's not implied either; the share of flagged boards that are bad needs more information.
:::
:::

::: 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 supplier A, with defect rate {ra}; the rest from B, with defect rate {rb}.

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

### What fraction of all parts are defective?

::: misses
- [(ra + rb)/2] Weight each rate by its supplier's share.
- [any] Total probability.
:::
:::
:::

::: check
kind: derivation
vars: a = 0.001..0.05, b = 0.0001..0.001

## A link loses a fraction $a$ of packets, and a fraction $b$ of consecutive pairs are both lost.

::: step
answer: b/a

### Given one packet was lost, what is the chance the next one is too?
:::

::: step
answer: b/(a*a)

### How many times more likely is that than if losses were independent?
:::

::: right
Right. A ratio above 1 means losses cluster in bursts.
:::
:::

::: explain
## Why does $P(A \mid B)$ divide by $P(B)$?

::: model
Once you know $B$ happened, the only outcomes still possible are the ones in $B$, so they become the whole sample space. Their chances have to be scaled up so they add to 1, and dividing by $P(B)$ does that. Within that smaller space, the outcomes where $A$ also happens are $A \cap B$, so the new chance of $A$ is $P(A \cap B)$ rescaled: $P(A \cap B)/P(B)$.
:::
:::

::: practice

::: check
given: pb = 0.4 | 0.5; pab = 0.6 | 0.8
answer: pb*pab
tolerance: 0.5%

## $P(B)$ = {pb} and $P(A \mid B)$ = {pab}. What is $P(A \cap B)$?

::: misses
- [pab/pb] Multiply: $P(B)P(A \mid B)$.
- [any] The multiplication rule.
:::
:::

::: check
given: pab = 0.15 | 0.3 | 0.6 | 0.7 | 0.85 | 0.95
answer: 1 - pab
tolerance: 0.5%

## $P(A \mid B)$ = {pab}. What is $P(A^c \mid B)$?

::: misses
- [any] A conditional probability obeys the complement rule.
:::
:::

::: check
given: a = 0.3 | 0.7; pa = 0.1 | 0.2; pb = 0.05 | 0.08
answer: a*pa + (1 - a)*pb
tolerance: 0.5%

## A symbol is sent as 1 with probability {a} and 0 otherwise. A 1 is misread with probability {pa}, a 0 with probability {pb}. What is the overall error rate?

::: misses
- [(pa + pb)/2] Weight each by how often that symbol is sent.
- [any] Total probability.
:::
:::
:::
