Lesson 14 of 19 · Statistics for AI, GenAI & LLMs

Joint Distributions: Several Variables at Once, Independence & Covariance

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.

1. A joint distribution is a table over combinations

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):

LookupSummaryMathRow total
Correct 0.36 0.24 0.12 0.72
Wrong 0.04 0.06 0.18 0.28
Column total 0.400.300.301.00
0.36 + 0.24 + 0.12 + 0.04 + 0.06 + 0.18 = 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.

2. Marginals and conditionals fall out of the table

Once you hold the joint table you hold everything. Two operations pull out the single-variable and the “given that” views.

Marginals: sum along a row or column

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.

P(Math) = 0.12 + 0.18 = 0.30
P(Correct) = 0.36 + 0.24 + 0.12 = 0.72

Conditionals: slice the table, then rescale

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:

P(Correct | Math) = 0.12 / 0.30 = 0.40
P(Correct | Lookup) = 0.36 / 0.40 = 0.90
P(Correct | Summary)= 0.24 / 0.30 = 0.80

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.

Pitfall You can always compute the marginals from the joint, but you can not rebuild the joint from the marginals: many tables share the same totals. A dashboard showing only “72% correct” and “30% math” has lost the fact that math is where the failures live.

3. Independence, tested on the table

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.

independent ⇔ P(type, correct) = P(type) × P(correct) for every cell

Test our table on the Math / Correct cell:

P(Math) × P(Correct) = 0.30 × 0.72 = 0.216
actual P(Math, Correct) = 0.12
0.12 ≠ 0.216 → not independent

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):

Correct row: 0.40×0.72 = 0.288 0.30×0.72 = 0.216 0.30×0.72 = 0.216
Wrong row: 0.40×0.28 = 0.112 0.30×0.28 = 0.084 0.30×0.28 = 0.084

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).

4. Covariance: how two numbers move together

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:

RequestRetrieval X (s)Generation Y (s)X − 0.4Y − 1.7Product
10.21.0−0.2−0.7+0.14
20.31.6−0.1−0.1+0.01
30.41.40.0−0.30.00
40.52.2+0.1+0.5+0.05
50.62.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):

Cov(X, Y) = E[(X − μX)(Y − μY)]
sample version: Σ(x − x̄)(y − ȳ) / (n − 1)
x̄ = 0.4, ȳ = 1.7
sum of products = 0.14 + 0.01 + 0.00 + 0.05 + 0.12 = 0.32
Cov(X, Y) = 0.32 / 4 = 0.08 (seconds²)

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:

product > 0 product > 0 product < 0 product < 0 mean X = 0.4 mean Y = 1.7 0.2 0.3 0.4 0.5 0.6 Retrieval time X (s) 1.0 1.5 2.0 Generation time Y (s)

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.

Reading the sign and the units

5. From covariance to the correlation of Lesson 6

The scale problem has a fix you have already used. Divide the covariance by both standard deviations so the units cancel:

Var(X) = (0.04 + 0.01 + 0 + 0.01 + 0.04) / 4 = 0.025 → SD = 0.158 s
Var(Y) = (0.49 + 0.01 + 0.09 + 0.25 + 0.36) / 4 = 0.30 → SD = 0.548 s
r = Cov(X, Y) / (SD(X) × SD(Y)) = 0.08 / √(0.025 × 0.30)
= 0.08 / 0.0866
r ≈ 0.92

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.

Key Insight Independent variables always have covariance 0, but covariance 0 does not imply independence. A U-shaped relationship can have Cov = 0 while one variable completely determines the other. The table test in section 3 (every cell equals the product of marginals) is the strict one; covariance only checks for a straight-line tendency.

6. The covariance matrix

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 retrievalY generationZ output length
X retrieval0.0250.0800.175
Y generation0.0800.3000.600
Z output length0.1750.6001.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.

Why This Matters If two input features are nearly perfectly correlated, they carry almost the same information, and a regression like Lesson 6's cannot tell their effects apart. An embedding is a vector of hundreds of numbers; the covariance matrix of a set of embeddings shows which directions carry real variation, which is what PCA works on.

7. Variance of a sum, with the covariance term

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:

Var(X + Y) = Var(X) + Var(Y) + 2·Cov(X, Y)

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:

direct: deviations (−0.9, −0.2, −0.3, +0.6, +0.8)
squares (0.81 + 0.04 + 0.09 + 0.36 + 0.64) = 1.94
1.94 / 4 = 0.485
formula: 0.025 + 0.30 + 2 × 0.08 = 0.485 ✓ matches
pretending independence: 0.025 + 0.30 = 0.325
SD(T): true 0.696 s vs assumed 0.570 s

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.

8. The chain rule: how an LLM factorizes a sentence

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:

P(x₁, x₂, …, xₙ) = P(x₁) × P(x₂ | x₁) × P(x₃ | x₁, x₂) × … × P(xₙ | x₁…xₙ₋₁)
= Πᵢ P(xᵢ | x​<ᵢ)

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:

P(“The”) = 0.20 first token, given the start of the reply
P(“model” | “The”) = 0.05
P(“answers” | “The model”) = 0.30
P(sequence) = 0.20 × 0.05 × 0.30 = 0.01 × 0.30
= 0.003

In practice you add logs instead of multiplying, because products of many small numbers underflow:

ln 0.20 + ln 0.05 + ln 0.30 = −1.609 − 2.996 − 1.204 = −5.809
ln 0.003 = −5.809 ✓ same number
average per token: 5.809 / 3 = 1.936 nats → e^1.936 ≈ 6.93
(perplexity of this sequence ≈ 6.9; see Lesson 10)

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.

Key Insight Why factorize at all? With a vocabulary of 50,000 tokens, a full joint table over just three positions has 50,000³ = 125,000,000,000,000 cells (1.25 × 10¹⁴). Nobody can store or estimate that. The chain rule replaces the impossible table with a function: one network that, given any prefix, produces the 50,000-way next-token distribution. Generating a sentence means sampling those conditionals in order, which is why a bad early token changes everything after it.

9. Why this matters for building AI systems

Why This Matters Most questions about an AI system involve two or more quantities at once, and the joint view is where the answers live.

Slice evals by condition

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.

Budget latency with covariances

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.

Audit features and score sequences

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.

Recap

Leads into Lesson 15 You now know how several variables fit together and how a sum of correlated pieces behaves. Lesson 15 asks what happens when you add up many pieces and then need to decide something from the result: the limit theorems that explain why averages settle down and become bell-shaped, and the hypothesis tests that turn “these two numbers differ” into a defensible yes or no.

✅ Check Yourself

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)?

0.12, because that is the cell where Math and Correct meet
0.40, because 0.12 / 0.30 rescales the Math column so it sums to 1
0.30, because that is the marginal for Math

2. Which observation proves that query type and correctness are NOT independent in that table?

All six cells add up to 1.00
P(Correct) = 0.72, which is greater than 0.5
P(Math, Correct) = 0.12, but P(Math) × P(Correct) = 0.30 × 0.72 = 0.216

3. Retrieval time and generation time have Cov(X, Y) = +0.08 s² across a set of requests. What does the sign tell you?

When retrieval is slower than its average, generation also tends to be slower than its average
Slow retrieval causes slow generation
The two are 8% correlated

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?

Nothing; variances of sums always add
It overstates the variance, because positive covariance cancels some spread
It understates the variance; the correct value is 0.325 + 2 × 0.08 = 0.485

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₃)?

Because the model assumes the tokens are independent of each other
Because the chain rule holds for any joint distribution: each factor conditions on everything before it
Because only the last token matters, so the earlier factors can be dropped