Real systems never produce one number at a time. A request has a type, a length, a latency, and an outcome, all at once. This lesson is about describing them together, and about the factorization trick that lets a language model write one token after another.
So far you have mostly looked at one random quantity at a time. But an AI pipeline emits several at once, and the interesting questions are about how they move together. Do hard queries fail more often? Does slow retrieval predict slow generation? Does the third token depend on the first two? These are questions about a joint distribution: one probability assignment over combinations of values. This lesson builds it from a table, ties it to the conditional probability and independence of Lesson 3 and the correlation of Lesson 6, and ends with the chain rule, which is how an autoregressive LLM defines the probability of a sentence.
Say a team logs every query to a question-answering assistant and tags each with two things: the query type (lookup, summary, or math) and whether the answer was correct against a reference. Counting over a large log and dividing by the total gives the joint distribution, P(type, correct):
| Lookup | Summary | Math | Row total | |
|---|---|---|---|---|
| Correct | 0.36 | 0.24 | 0.12 | 0.72 |
| Wrong | 0.04 | 0.06 | 0.18 | 0.28 |
| Column total | 0.40 | 0.30 | 0.30 | 1.00 |
Each cell is the probability of one pair happening together: 0.12 of all traffic is math queries answered correctly. The rule from Lesson 3 still holds, just stretched across a grid: all cells together sum to 1. If you have three variables, the table becomes a cube, and so on. With the continuous distributions of Lesson 13 the grid becomes a surface (a joint density), but every idea below carries over.
Once you hold the joint table you hold everything. Two operations pull out the single-variable and the “given that” views.
To get P(type) alone, ignore correctness by summing each column. To get P(correct) alone, sum each row. The totals in the margins of the table above are literally why these are called marginal distributions.
To get P(correct | Math), restrict attention to the Math column (the “given” world from Lesson 3) and divide each cell by that column's total so the slice sums to 1 again:
The same formula you already know, P(A|B) = P(A, B) / P(B), now has a concrete reading: joint cell divided by marginal. The headline accuracy of 72% hides the fact that the assistant is right 90% of the time on lookups and 40% of the time on math. Marginals alone would never show you that.
Lesson 3 defined independence as P(A|B) = P(A). On a joint table the same idea has a cleaner test: two variables are independent exactly when every cell equals the product of its two marginals.
Test our table on the Math / Correct cell:
One failing cell is enough. Math answers are correct far less often than the product predicts, which is the dependence a router or a prompt change would want to exploit. Now compare a table built to be independent, using the same marginals (0.40, 0.30, 0.30 and 0.72, 0.28):
In this version P(Correct | any type) is 0.72 everywhere (for example 0.216 / 0.30 = 0.72), so learning the query type tells you nothing about correctness. Real logs are never exactly this clean, so you ask whether an observed gap is bigger than sampling noise; Lesson 15 gives the tools for judging a gap against sampling noise (z-tests on proportions and t-tests on means).
The table works for categories. For two numeric quantities you want a single number that says whether they tend to rise together. Say a team logs five requests through a RAG pipeline, with retrieval time X and generation time Y, both in seconds:
| Request | Retrieval X (s) | Generation Y (s) | X − 0.4 | Y − 1.7 | Product |
|---|---|---|---|---|---|
| 1 | 0.2 | 1.0 | −0.2 | −0.7 | +0.14 |
| 2 | 0.3 | 1.6 | −0.1 | −0.1 | +0.01 |
| 3 | 0.4 | 1.4 | 0.0 | −0.3 | 0.00 |
| 4 | 0.5 | 2.2 | +0.1 | +0.5 | +0.05 |
| 5 | 0.6 | 2.3 | +0.2 | +0.6 | +0.12 |
Covariance is the average of the product of the two deviations from their means (the same deviations that built variance in Lesson 2):
Here is the picture. The dashed lines mark the two means and cut the plot into four quadrants. A point in the upper-right or lower-left quadrant has both deviations the same sign, so its product is positive. A point in the upper-left or lower-right has opposite signs and a negative product. Covariance is the average of those signed products:
Five requests, no point in a red quadrant: slow retrieval goes with slow generation, so Cov(X, Y) = +0.08 s². Point 3 sits on the X-mean line and contributes zero.
The scale problem has a fix you have already used. Divide the covariance by both standard deviations so the units cancel:
That is the Pearson r from Lesson 6: correlation is covariance rescaled to live between −1 and +1. It keeps the sign of the covariance, drops the units, and does not change if you switch from seconds to milliseconds. The same caveat carries over: both measure only linear co-movement, and neither says anything about cause.
With more than two quantities you collect every pairwise covariance into a square table. Add a third column to the same five requests: output length Z, in hundreds of tokens (1, 2, 2, 3, 4; mean 2.4). The covariance matrix puts each variable's variance on the diagonal and each pair's covariance off it:
| X retrieval | Y generation | Z output length | |
|---|---|---|---|
| X retrieval | 0.025 | 0.080 | 0.175 |
| Y generation | 0.080 | 0.300 | 0.600 |
| Z output length | 0.175 | 0.600 | 1.300 |
It is symmetric (Cov(X, Y) = Cov(Y, X)), so you only need half of it. Dividing each entry by the two matching standard deviations turns it into a correlation matrix: r(X, Y) ≈ 0.92, r(X, Z) ≈ 0.97, r(Y, Z) ≈ 0.96. All three features move almost in lockstep.
Lesson 12 gave you the rules for the variance of a random variable. The rule for a sum has a third term, and covariance is what fills it:
Total request latency is T = X + Y. Compute its variance two ways from the five requests. The totals are 1.2, 1.9, 1.8, 2.7, 2.9, with mean 2.1:
Ignoring the covariance understates the spread of total latency by about 18%, which matters for a tail-latency budget (the percentiles of Lesson 2). Slow retrievals line up with slow generations, so the bad cases stack instead of canceling. With negative covariance the sum would be tighter than independence predicts. The same term means averaging many correlated eval runs cuts noise far less than the 1/n you get from independent ones.
The chain rule of probability is the definition of conditional probability applied over and over. For two variables, P(a, b) = P(a) × P(b | a). Extend it to n variables, peeling off one at a time:
This is an identity, not an assumption: no independence is needed, and it holds for any ordering of the variables. An autoregressive language model simply picks the left-to-right order and learns each factor. At every step it reads the tokens so far and outputs a distribution over the next token (the categorical distribution from Lesson 4). Take a three-token reply, “The model answers”, and suppose the model assigns these probabilities:
In practice you add logs instead of multiplying, because products of many small numbers underflow:
The negative average log-probability is the cross-entropy loss of Lesson 7, and its exponential is the perplexity of Lesson 10. Both are the chain rule evaluated on a sentence.
The 72% accuracy in section 1 averaged 90%, 80% and 40%. Build the joint table of condition against correctness whenever you can: P(correct | slice) finds the failure cluster.
Pipeline stages share causes, such as long inputs. Treating them as independent understates the tail, as the 0.570 s versus 0.696 s gap showed.
A correlation matrix is the quick check for duplicated features, and the starting point for reducing embeddings. Scoring a completion, computing perplexity, or running beam search is adding conditional log-probabilities, one per token.
1. In the joint table of query type and correctness, P(Math, Correct) = 0.12 and P(Math) = 0.30. What is P(Correct | Math)?
2. Which observation proves that query type and correctness are NOT independent in that table?
3. Retrieval time and generation time have Cov(X, Y) = +0.08 s² across a set of requests. What does the sign tell you?
4. Two pipeline stages have Var(X) = 0.025, Var(Y) = 0.30 and Cov(X, Y) = +0.08. If you compute the variance of total latency as Var(X) + Var(Y) = 0.325, what is wrong?
5. An autoregressive LLM gives a three-token reply the probability P(x₁) × P(x₂ | x₁) × P(x₃ | x₁, x₂). Why is this a valid way to write P(x₁, x₂, x₃)?