0008 β€” Conditional Probability & Bayes' Theorem

Probability Pillar Β· ~25 minutes

🧩 Puzzle Piece: A language model computes P(next_token | previous_tokens) β€” the probability of a token given the context. This vertical bar "|" is conditional probability, and it's the fundamental operation of every autoregressive model. The attention mechanism itself computes something closely related: "given what I've seen so far, which past tokens are most relevant?" This lesson teaches you the mathematics of "given that."
"The probability that it will rain tomorrow" and "the probability that it will rain tomorrow, given that it's cloudy today" are different questions. The second is conditional β€” and it's almost always the better question. AI models don't answer "what's the most common word?" They answer "what's the most likely word given everything said so far?"

Conditional Probability

P(A | B) = P(A ∩ B) / P(B)

In words: the probability of A given B is the fraction of B's probability mass that also satisfies A. You're zooming in on the part of the sample space where B is true, and measuring A's share of that subspace.

Example: P("on" | "The cat sat") vs. P("on" | "The cat sat on the") β€” the second probability should be near-zero (you don't say "on the on the mat"), even though "on" is a common word. The context changes everything.

Bayes' Theorem

P(A | B) = P(B | A) Β· P(A) / P(B)

Bayes' theorem tells you how to update your beliefs when you observe evidence:

Bayes' theorem is the mathematical engine of learning. Start with prior beliefs β†’ observe data β†’ compute posterior (updated beliefs). Repeat with new data. This iterative updating β€” prior β†’ evidence β†’ posterior β†’ (becomes new prior) β†’ more evidence β†’ … β€” is exactly how both humans and AI systems learn from experience.

Autoregressive Decomposition: The Chain Rule

How does a language model compute P("The cat sat on the mat")? By breaking it into conditional probabilities:

P(w₁, wβ‚‚, ..., wβ‚™) = P(w₁) Β· P(wβ‚‚ | w₁) Β· P(w₃ | w₁,wβ‚‚) Β· ... Β· P(wβ‚™ | w₁,...,wₙ₋₁)

Each factor is exactly what the transformer computes at each step: the probability of the next token given all previous tokens. The joint probability of the entire sequence is the product of these conditional probabilities. This is the chain rule of probability, and it's why autoregressive generation works.

Generation is just repeated conditional probability. Step 1: P(token₁). Step 2: P(tokenβ‚‚ | token₁). Step 3: P(token₃ | token₁, tokenβ‚‚). The transformer's entire job, at every position, is to compute one conditional probability distribution. Then you sample from it, append the result, and repeat.

Check Your Understanding

Q1: P(A | B) = 0.8 and P(A) = 0.3. What does this tell you?

A. A and B are independent B. B makes A much more likely β€” B is strong evidence for A C. A causes B D. The numbers are inconsistent β€” P(A|B) can't exceed P(A)

Q2: A transformer computes P("mat" | "The cat sat on the"). Which mathematical concept does this directly use?

A. Matrix multiplication B. Conditional probability C. Eigenvalue decomposition D. Gradient descent

Q3: Why does autoregressive generation use the chain rule P(w₁,...,wβ‚™) = ∏ P(wα΅’ | w₁,...,wᡒ₋₁) instead of trying to compute the joint probability directly?

A. The chain rule is faster to type B. Direct joint probability over all possible sequences of length n would require |V|ⁿ entries β€” impossibly large. The chain rule reduces it to n conditional distributions over |V| tokens each C. The chain rule gives more accurate results D. It's a legal requirement for AI systems
πŸ“Ί Primary Source Watch: Bayes theorem β€” 3Blue1Brown (15 min, the single best explanation of Bayes ever made)
πŸ“– Recommended Reading Blitzstein & Hwang, Introduction to Probability β€” Chapter 2 (Conditional Probability)
Conditional probability is the "|" in P(token | context). If you internalize nothing else from probability, internalize this: the vertical bar means "zooming in on the world where the right side is true." Everything in AI is conditional on something.
← 0007: Probability Fundamentals Next: 0009 β€” Random Variables & Distributions β†’