All courses
Courses › How LLMs really work · foundation

Conditional probability

Use p(b|a), the chain rule p(a,b,c) = p(a)p(b|a)p(c|a,b), and Bayes' rule.

Builds on: Probability and distributions

▶ Watch this lesson Start the whole course

What the animation says

Shrink the world

Here are a hundred emails. Twenty are spam, eighty are normal. Ten of the spam emails contain the word free. So do four of the normal ones. For the chance of free given spam, throw away everything except the twenty spam emails. Ten of those twenty contain free. Ten out of twenty is one half. Dividing by the kept group rescales it, so chances inside it add up to one. Now flip it. For spam given free, which group do we keep?

Same ten, different divisor

Keep only the fourteen free emails. Ten of them are spam: about point seven one. Same ten emails as before, divided by a different group. So the two answers differ. In general, the chance of b given a is the chance of both, divided by the chance of a. Turn it around: the chance of both equals the chance of a, times b given a. Check: spam is point two, free given spam is one half. Both together: point one, ten emails in a hundred.

One word at a time

That rearranged rule chains. Each new word multiplies in its chance, given all the words before it. A predictor says: the, point one. Cat given the, point zero two. Sat given the cat, point three. Multiply: point one times point zero two is point zero zero two. Times point three gives point zero zero zero six: the chance a text starts with the cat sat. Now add the word on, with chance one half given the cat sat. What's the new score?

Flip it with Bayes

Point zero zero zero three: every word multiplies, so scores shrink. A finished text also multiplies in an end marker. Now flip what is given. Spam's base rate is point two; not spam, point eight. Free appears in half of spam, but in only one in twenty normal emails. Two paths lead to free. Point two times one half is point one. Point eight times point zero five is point zero four. Together, free has chance point one four. Which number goes on top for spam given free?

Never swap them

Point one over point one four: about point seven one. That's exactly ten out of fourteen, from our email grid. Free given spam was one half. Spam given free is point seven one. Never assume they're equal. So: conditioning keeps one group and divides by its size. Multiplying in order gives a joint chance. Bayes swaps what is given.

How likely is a whole sentence?

A text predictor must score whole sentences like *the cat sat*, but there are far too many sentences to list each one's probability. Sometimes we also need the reverse: we see a clue (an email contains *free*) and want the chance of its cause (spam). Both problems rest on one idea: the probability of something **given** that something else is already known.

Conditioning: shrink the world, then rescale

Take 100 emails: 20 are spam, and 10 of those contain *free*. Of the 80 normal ones, 4 contain *free*. 'Probability of *free* given spam' looks only at the 20 spam emails: 10 of 20 = 0.5. 'Spam given *free*' looks only at the 14 *free* emails: 10 of 14 ≈ 0.71. Dividing by the size of the group you kept is the rescaling step: it makes the probabilities inside that group sum to 1. Both answers use the same 10 emails, but they divide by different groups, so they differ.

Three formulas, one idea

$p(b\mid a)$ reads 'probability of $b$ given $a$'. With $p(a,b)$ the joint probability that both happen (and $p(a)>0$): $$p(b\mid a)=\frac{p(a,b)}{p(a)}$$ Rearranging gives $p(a,b)=p(a)\,p(b\mid a)$. Applying the same rule to the pair $(a,b)$ and then $c$ gives $p(a,b,c)=p(a,b)\,p(c\mid a,b)$. Together these form the **chain rule**: $p(a,b,c)=p(a)\,p(b\mid a)\,p(c\mid a,b)$. **Bayes' rule** flips what is given: $$p(a\mid b)=\frac{p(b\mid a)\,p(a)}{p(b)}$$ where $p(b)=p(b\mid a)p(a)+p(b\mid\text{not }a)\,p(\text{not }a)$.

Scoring a sentence one word at a time

A predictor gives: $p(\text{the})=0.1$, $p(\text{cat}\mid\text{the})=0.02$, $p(\text{sat}\mid\text{the, cat})=0.3$. Chain rule: $0.1\times0.02=0.002$, then $0.002\times0.3=0.0006$. So $p(\text{the cat sat})=0.0006$ is the probability that a text starts with *the cat sat*. Each factor is one entry from the predictor's distribution over the next word, given the words so far. To score a finished text, the predictor multiplies in one more factor: the probability of an end marker. Text predictors score whole texts in exactly this way.

Bayes with numbers: spam given free

Base rate $p(\text{spam})=0.2$. Also $p(\text{free}\mid\text{spam})=0.5$ and $p(\text{free}\mid\text{not spam})=0.05$. Step 1: $p(\text{free})=0.5\times0.2+0.05\times0.8=0.1+0.04=0.14$. Step 2: $p(\text{spam}\mid\text{free})=\frac{0.5\times0.2}{0.14}=\frac{0.1}{0.14}\approx0.71$. This matches the 10-out-of-14 count from the email table.

What to remember

$p(b\mid a)$ is the share of $a$-cases in which $b$ also holds. Multiply conditional probabilities in order to get a joint probability (chain rule). Use Bayes' rule to swap what is given, and never assume $p(a\mid b)=p(b\mid a)$.

On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.