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

Counting & Combinatorics: How Big Is the Space of Possibilities?

Before you can say how likely something is, you often need to know how many things could have happened. This lesson is about counting: why you can never brute-force an LLM's output space, how many ways there are to pick an eval set, and the distribution that turns "k correct out of n" into a probability.

Lessons 3 and 4 quietly assumed you already knew the list of possible outcomes. In practice that list is the hard part. A model with a 50,000-token vocabulary has far more than 50,000 possible outputs, because outputs are sequences. An eval harness has an enormous number of possible test sets you could have drawn. Combinatorics counts those possibilities, and it feeds probability: when outcomes are equally likely, a probability is a count of the outcomes you care about divided by a count of all outcomes.

1. The multiplication rule: how sequences explode

The multiplication rule says: if a process has several steps, and step 1 can go a ways, step 2 can go b ways (whatever happened in step 1), and step 3 can go c ways, then the whole process can go a × b × c ways. Choices at each step multiply; they do not add.

Here is the smallest possible language model: a vocabulary of V = 3 tokens (A, B, C) and outputs of length n = 2. The first token has 3 choices. For each of those, the second token has 3 choices. The tree below draws every path.

start A AA AB AC B BA BB BC C CA CB CC 3 × 3 = 9 sequences of length 2 (V = 3, n = 2, so V^n = 9)

Every path from the start to the bottom row is one distinct output sequence. Add one more level and each of the 9 boxes sprouts 3 more: 27 sequences.

With vocabulary size V and sequence length n, the count is V × V × … × V (n times) = Vn. Now put in real-sized numbers. Take V = 50,000 tokens, a plausible order of magnitude from Lesson 4:

n = 1: 50,000
n = 3: 50,000 × 50,000 × 50,000 = 125,000,000,000,000 (1.25 × 1014)
n = 20: 50,00020 ≈ 9.5 × 1093

A 20-token reply is a short sentence. The number of distinct token strings of that length is a 94-digit number, far too many to list, let alone score. Most of those strings are gibberish and the model gives them near-zero probability, but you cannot enumerate even the sensible ones.

Key Insight Counting sequences is not the same as assigning them probabilities. Vn tells you how many strings exist. The chain of conditional probabilities from Lesson 3 tells you how likely each one is: P(sequence) = P(token 1) × P(token 2 | token 1) × … Only if every string were equally likely would the probability of each be 1/Vn, and for a language model that is nowhere near true.

What this rules out

Exhaustively testing a prompt template (10 slots with 20 fillers each is 2010 = 10,240,000,000,000 variants), searching all outputs for the single best one, and proving a model never says X all fail on this arithmetic. Decoders build answers one token at a time instead, and you estimate how often something happens by sampling (Lesson 5).

2. Permutations vs. combinations: does order matter?

Two questions sound alike and have very different answers. Say a RAG system retrieved 20 candidate chunks and you will paste 5 of them into the prompt.

That division is the whole idea behind the formula for "n choose k", written C(n, k) or ⁿCₖ:

n!
C(n, k) = ───────────
k! × (n − k)!
C(20, 5) = 20! / (5! × 15!) = 15,504
ordered picks = 15,504 × 5! = 15,504 × 120 = 1,860,480

Read it as: n! arranges all n items, (n − k)! cancels the arrangements of the items you did not pick, and k! cancels the arrangements within the group you did pick. Check by hand: choose 3 of 10 prompts to review. Ordered: 10 × 9 × 8 = 720; each set appears 3! = 6 times, so C(10, 3) = 120.

Pitfall Decide whether order matters before you count. A training split is a combination; which chunk goes first in a prompt is a permutation. Using C(n, k) when order matters undercounts; using the ordered count when it does not overcounts by exactly k!.

3. Pascal's triangle and the shape of binomial coefficients

The numbers C(n, k) have a pattern you can build with addition alone. To choose k items from n, look at one particular item. Either it is in your group (then choose the other k − 1 from the remaining n − 1) or it is not (then choose all k from the remaining n − 1). So:

C(n, k) = C(n−1, k−1) + C(n−1, k)

Stack these into rows and you get Pascal's triangle, where each entry is the sum of the two above it:

n=0: 1
n=1: 1 1
n=2: 1 2 1
n=3: 1 3 3 1
n=4: 1 4 6 4 1
n=5: 1 5 10 10 5 1 sum = 32 = 25
n=6: 1 6 15 20 15 6 1 sum = 64 = 26

4. The binomial distribution: counting meets probability

This is the bridge to Lesson 4. A Bernoulli is one yes/no trial with success probability p. Now run n independent Bernoulli trials with the same p and ask: how many successes? The count k follows a binomial distribution.

Build the formula from what you already have. One specific pattern with exactly k successes, say the first k trials succeed and the rest fail, has probability pk(1 − p)n−k, because independent probabilities multiply (Lesson 3). But the k successes could land on any k of the n trials, and there are C(n, k) ways to choose which. Every such pattern has the same probability, so:

P(k successes) = C(n, k) × pk × (1 − p)n−k
mean = n × p
variance = n × p × (1 − p)

The mean and variance come from treating the count as a sum of n Bernoulli trials: the mean of a sum is the sum of the means (np), and for independent trials the variances add too (n × p(1 − p)). Lesson 12 derives both rules.

Worked example: k correct out of 10 test prompts

A team runs a model on n = 10 test prompts. Suppose the model's true per-prompt success rate is p = 0.8 (so each prompt is an independent Bernoulli trial with p = 0.8, an assumption we come back to in the pitfall below). What is the chance it gets exactly 8 right?

C(10, 8) = 10! / (8! × 2!) = 45
0.88 = 0.16777216
0.22 = 0.04
P(k = 8) = 45 × 0.16777216 × 0.04 = 0.3020
mean = 10 × 0.8 = 8 variance = 10 × 0.8 × 0.2 = 1.6 sd ≈ 1.26
0 1 2 3 4 0.026 5 0.088 6 0.201 7 0.302 8 0.268 9 0.107 10 k = number of prompts answered correctly out of 10

Binomial with n = 10, p = 0.8. Even when the model is truly 80% accurate, the score it earns on 10 prompts is a spread, not a single number.

Even at the most likely score (8 of 10), the model hits it only about 30% of the time. P(k ≥ 8) = 0.302 + 0.268 + 0.107 = 0.678, and P(k ≤ 6) = 0.121: an honestly 80%-accurate model scores 6/10 or worse about one run in eight.

Run it the other way, the way a real team does. You see 8 of 10 and wonder what the true p is. Under p = 0.6 the same data has probability C(10, 8) × 0.68 × 0.42 = 45 × 0.016796 × 0.16 = 0.121. Under p = 0.95 it is 45 × 0.958 × 0.052 = 0.075. Both are only about 2.5 to 4 times less likely than 0.302 (0.302 / 0.121 ≈ 2.5, 0.302 / 0.075 ≈ 4.0). A small eval makes a 60% or a 95% model less plausible than an 80% one, but it cannot rule either out. Lesson 5 turns this observation into confidence intervals and maximum likelihood.

More prompts shrink the relative noise. With n = 100 and p = 0.8: sd = √(100 × 0.8 × 0.2) = √16 = 4 correct answers, which is 4 percentage points on the accuracy. The sd of the proportion is √(p(1 − p)/n) = 0.04. With 10 prompts the same formula gives 0.126, three times bigger.

Pitfall The binomial needs independent trials with the same p. Eval prompts violate both when they share a template, a source document, or a topic: if the model fails one it likely fails its near-duplicates, and easy and hard prompts do not share one p. Correlated prompts behave like fewer independent trials than you counted, so the real spread is wider than the binomial formula says. The binomial is the right baseline, not a guarantee.

5. Sampling without replacement: eval subsets and splits

When you pick an eval set of 100 prompts out of a pool of 1,000, you do not pick the same prompt twice. That is sampling without replacement, and the count of possible subsets is C(1,000, 100), a 140-digit number (about 10140). The same logic gives you the size of other familiar choices:

ChoiceCountSize
100-prompt eval subset from a pool of 1,000C(1,000, 100)about 10140
200 validation examples from 1,000 (an 80/20 split)C(1,000, 200)216 digits

Two people who each draw a "random" split will essentially never draw the same one, so a single lucky or unlucky split is possible. That is why serious reports show several splits or a confidence interval.

K-fold assignments

Cross-validation cuts the data into k folds of equal size. Count the ways on a small case: 20 examples into 5 labeled folds of 4. Choose 4 for fold 1 from 20, then 4 for fold 2 from the remaining 16, and so on:

C(20,4) × C(16,4) × C(12,4) × C(8,4) × C(4,4) = 20! / (4!)5
= 305,540,235,000

The folds are interchangeable in practice (it does not matter which one you call "fold 1"), so divide by the 5! = 120 relabelings: 305,540,235,000 ÷ 120 = 2,546,168,625 genuinely different ways to partition 20 examples into 5 folds of 4. Any one run of k-fold uses just one of them.

Worked example: how many bad prompts does a sample catch?

This is the without-replacement version of the binomial, the hypergeometric count. A dataset of N = 1,000 prompts contains 100 mislabeled ones (10%). You hand-audit a random 50. On average you expect 50 × 100 / 1,000 = 5 mislabeled prompts. What is the chance your audit finds none? Count the audits that avoid all 100 bad prompts (choose 50 of the 900 good ones) over all possible audits:

P(no bad prompts) = C(900, 50) / C(1,000, 50)
≈ 0.0045 so P(at least one) ≈ 0.9955

A 50-prompt audit almost certainly spots a 10% problem. For a 1% problem (10 bad in 1,000) you expect only 50 × 10 / 1,000 = 0.5 hits, and C(990, 50) / C(1,000, 50) ≈ 0.60 says the audit finds nothing about 60% of the time. An audit sized for common problems says little about rare ones.

Key Insight With replacement, draws are independent and the count is binomial. Without replacement, each draw changes what is left and the count is hypergeometric. When the pool is far larger than the sample, the difference is negligible.

6. Collisions and the birthday effect: duplicate prompts and hash dedup

Counting also explains a surprise: duplicates appear far sooner than intuition suggests. Pick n items at random from N possible values and the number of pairs is n(n − 1)/2, which grows like n2. Each pair collides with probability about 1/N. So the expected number of colliding pairs is roughly:

expected collisions ≈ n(n − 1) / (2N) P(at least one) ≈ 1 − e−n(n−1)/(2N)

This is the birthday effect, and it matters for deduplication. Say a team deduplicates a training corpus by giving each document a 32-bit hash (N = 232 = 4,294,967,296 possible values) and treating equal hashes as equal documents:

Documents nPairs n(n−1)/2Expected colliding pairsP(at least one)
10,00049,995,0000.01161.2%
100,0004,999,950,0001.1668.8%

With a hash space of over 4 billion values, 100,000 documents already collide about 69% of the time. Counting it exactly, the 50% mark is reached at 77,164 documents, about √N rather than N. Practical result: a corpus of millions of documents will have distinct documents treated as duplicates under a 32-bit hash. A 64-bit hash (N = 264 ≈ 1.8 × 1019) with 109 documents gives expected collisions of 109 × (109 − 1) / (2 × 264) ≈ 0.027, small enough to ignore.

7. Top-k: counting candidate sets

Top-k sampling restricts each next-token draw to the k highest-probability tokens (Lesson 10 treats the mechanics). Combinatorics tells you two things about it.

8. Why this matters for building AI systems

Why This Matters Counting tells you what you can never check and how much you can learn from what you do check.

The binomial sets the noise floor on your eval

Any pass/fail eval is a binomial count. Before comparing two models that scored 82/100 and 79/100, note that each has an sd of about 4 correct answers, so a 3-point gap is well within noise. Check this first when a "win" shows up on a small test set.

Splits, audits and hashes

Report variability across several splits, size audits for the problem rate you care about, and size hashes from n2/(2N), not from n alone.

Leads into Lesson 12

Here the focus was counting outcomes and the count of successes. The next step is to treat a number attached to a random outcome, such as the count of correct answers, the latency of a request, or the loss on a batch, as an object in its own right. That is a random variable, and its long-run average is its expectation. The binomial's np and np(1 − p) were your first two examples.

Recap

✅ Check Yourself

1. A model has a 1,000-token vocabulary and generates replies exactly 4 tokens long. How many distinct 4-token sequences exist?

4,000 (1,000 × 4)
1,000 × 999 × 998 × 997, because tokens cannot repeat
1,0004 = 1012, because each of the 4 positions has 1,000 independent choices

2. You will choose which 3 of 10 candidate prompts to include in a review batch and the order inside the batch is irrelevant. How many different batches are there?

120, which is 10 × 9 × 8 = 720 ordered picks divided by 3! = 6
720, the number of ordered picks
1,000, which is 103

3. A model's true per-prompt success rate is 0.8 and you test 10 independent prompts. What are the mean and variance of the number of correct answers?

Mean 0.8, variance 0.16
Mean 8, variance 1.6
Mean 8, variance 8

4. A team's eval prompts are 100 near-paraphrases of the same 10 base questions. Why is the binomial formula likely to understate the true spread of their accuracy score?

Binomial variance is always too small for sample sizes under 1,000
Paraphrases make the model's answers more random, which adds variance
The prompts are not independent: a model that fails one paraphrase likely fails its siblings, so there are effectively closer to 10 independent trials than 100

5. You deduplicate 100,000 documents with a 32-bit hash. Roughly how often will at least one pair of different documents share a hash value?

Almost never: 100,000 is tiny compared with 4.29 billion possible hash values
More often than not (about 69%), because the number of pairs grows like n2 and each pair has about a 1 in 4.29 billion chance
Exactly 50%, because a hash either collides or it does not