intermediate language-modeling 45 min read

n-Gram Language Models and Smoothing: From Laplace to Kneser–Ney

Every smoother is a guess about the words we have not seen — read three ways as empirical Bayes: a Dirichlet prior fit to the data, Robbins' formula on word counts, and Kneser–Ney as a hierarchical Pitman–Yor posterior

Overview & Motivation

A language model assigns a probability to the next word given the words before it. The simplest kind looks back a fixed distance: a trigram model predicts each word from the two before it, and estimates those predictions by counting. Train one on the finance corpus of the previous topics — 6,1306{,}130 sentences, 69,31269{,}312 predicted tokens — and test it on 100100 fresh documents from the same generator. Of the 23,05523{,}055 held-out trigrams, 32.47%32.47\% never occur in training; so do 22.17%22.17\% of the bigrams and 0.67%0.67\% of the words. The counting model gives every one of them probability zero, and a single zero makes the probability of the whole held-out text zero.

So a language model must reserve probability for events it has never seen, and take it from events it has. How much, from which events, and given to which unseen ones? Each classical smoother is an answer, and this topic reads them as one idea seen three ways — empirical Bayes, estimating a prior from the data it will be applied to:

  1. A Dirichlet prior fit to the data. Adding pseudo-counts is a Bayesian posterior mean. Its one free strength can be fit by maximizing the probability of the training data — Type-II maximum likelihood.
  2. Robbins’ formula on word counts. Good and Turing’s estimate of a word’s true frequency from how many other words share its count turns out to be the empirical-Bayes formula Robbins derived for insurance claims. We prove the identity behind it exactly.
  3. Kneser–Ney as a hierarchical Pitman–Yor posterior. The best classical smoother uses a peculiar count — how many different words a word follows. We derive it from a constraint on marginals, and then show it is exactly what a hierarchical Pitman–Yor model predicts when every word type sits at a single table.

Along the way we prove that Katz backoff normalizes, that the familiar discount n1/(n1+2n2)n_1/(n_1 + 2n_2) is an upper bound on the leave-one-out optimum rather than the optimum itself, and that interpolated Kneser–Ney matches a reference implementation to twelve decimals. A laboratory after the Pitman–Yor section puts every smoother side by side on five contexts. Its second panel plots the one curve that connects them all: how much each method subtracts from a count of rr.


Counting and the zero-count problem

Pad each sentence with n−1n - 1 copies of a start symbol <s> in front and one end symbol </s> behind. Every word and the final </s> is a predicted token; <s> only ever serves as context. The vocabulary VV is the set of training word types together with </s> and an unknown-word symbol <unk>, which stands in for every held-out word never seen in training; here ∣V∣=3,427\lvert V \rvert = 3{,}427.

Definition 1 (n-gram model and its maximum-likelihood estimate).

An order-nn model gives each predicted token wiw_i a probability P(wi∣ui)P(w_i \mid u_i) that depends only on its context ui=(wi−n+1,…,wi−1)u_i = (w_{i-n+1}, \dots, w_{i-1}), the n−1n - 1 tokens before it. Write c(u w)c(u\,w) for the number of times the nn-gram u wu\,w occurs in training, c(u ⋅)=∑wc(u w)c(u\,\cdot) = \sum_w c(u\,w) for the number of times uu occurs as a context, and u−u^- for uu with its oldest word dropped. The maximum-likelihood estimate (MLE) is the relative frequency P^(w∣u)=c(u w)/c(u ⋅)\hat P(w \mid u) = c(u\,w) / c(u\,\cdot).

Lower-order counts are always taken as marginals of the top-order ones, so the bigram count c(v w)c(v\,w) is the number of trigrams ending in v wv\,w. That bookkeeping matters twice below.

Proposition 1 (The MLE is the relative frequency).

For a context uu and any distribution qq on the vocabulary,

∑wc(u w)log⁡P^(w∣u)−∑wc(u w)log⁡q(w)  =  c(u ⋅) KL(P^(⋅∣u) ∥ q)  ≥  0,\sum_w c(u\,w)\log \hat P(w \mid u) - \sum_w c(u\,w)\log q(w) \;=\; c(u\,\cdot)\,\mathrm{KL}\bigl(\hat P(\cdot \mid u)\,\big\|\,q\bigr) \;\ge\; 0,

with equality only at q=P^(⋅∣u)q = \hat P(\cdot \mid u).

Proof.

Substitute c(u w)=c(u ⋅) P^(w∣u)c(u\,w) = c(u\,\cdot)\,\hat P(w \mid u): the difference becomes c(u ⋅)∑wP^(w∣u)log⁡P^(w∣u)q(w)c(u\,\cdot)\sum_w \hat P(w \mid u)\log\frac{\hat P(w \mid u)}{q(w)}, which is c(u ⋅)c(u\,\cdot) times the Kullback–Leibler divergence. Gibbs’ inequality (formalML, KL divergence) makes it nonnegative, and zero only when the two distributions agree.

∎

We judge a model by its held-out log-loss, the average of −log⁡P(wi∣ui)-\log P(w_i \mid u_i) over the predicted tokens of text it did not train on, in nats per token. (Its exponential is perplexity, the subject of the next language-modeling topic.)

Proposition 2 (The zero-frequency catastrophe).

The maximum-likelihood model’s held-out log-loss is infinite if and only if some held-out nn-gram never occurs in training.

Proof.

The log-loss is an average of the terms −log⁡P^(wi∣ui)-\log \hat P(w_i \mid u_i). It is finite exactly when every term is, that is, when P^(wi∣ui)=c(ui wi)/c(ui ⋅)>0\hat P(w_i \mid u_i) = c(u_i\,w_i)/c(u_i\,\cdot) \gt 0 for every held-out token, which says that every held-out nn-gram has a positive training count.

∎

With a third of the held-out trigrams unseen, the catastrophe is not a corner case. On the trigrams that were seen, the MLE’s log-loss is finite; the problem is entirely the unseen ones.


Smoothing as shrinkage

Picture the MLE for a context as a point in the probability simplex — the set of all distributions on VV. A context seen a handful of times puts its point on a low-dimensional face of the simplex: every word not yet seen after it has coordinate zero. Smoothing moves the point into the interior. The cleanest way is to slide it along a straight line toward a sensible default, and the sensible default for a trigram context is the bigram model one order down.

Definition 2 (Hierarchical Dirichlet smoothing).

With a strength αk>0\alpha_k \gt 0 for each order kk, set

Pk(w∣u)  =  c(u w)+αk Pk−1(w∣u−)c(u ⋅)+αk,P1(w)  =  c(w)+α1/∣V∣N+α1,P_k(w \mid u) \;=\; \frac{c(u\,w) + \alpha_k\,P_{k-1}(w \mid u^-)}{c(u\,\cdot) + \alpha_k}, \qquad P_1(w) \;=\; \frac{c(w) + \alpha_1/\lvert V\rvert}{N + \alpha_1},

where NN is the number of predicted tokens. With the uniform distribution in place of Pk−1P_{k-1} this is additive (Lidstone) smoothing with α/∣V∣\alpha/\lvert V\rvert pseudo-counts per word, and Laplace smoothing at one pseudo-count.

This is a posterior mean. Put a Dirichlet(αk Pk−1(⋅∣u−))\mathrm{Dirichlet}(\alpha_k\,P_{k-1}(\cdot \mid u^-)) prior on the next-word distribution of the context uu; after observing the counts c(u ⋅)c(u\,\cdot), the posterior mean is exactly Pk(⋅∣u)P_k(\cdot \mid u) — the theorem behind Dirichlet smoothing in formalRAG’s query-likelihood language models, applied here once per context instead of once per document (MacKay and Peto 1995).

Proposition 3 (Shrinkage is interpolation).

Definition 2 can be written as

Pk(w∣u)  =  (1−λu) P^(w∣u)+λu Pk−1(w∣u−),λu=αkc(u ⋅)+αk,P_k(w \mid u) \;=\; (1 - \lambda_u)\,\hat P(w \mid u) + \lambda_u\,P_{k-1}(w \mid u^-), \qquad \lambda_u = \frac{\alpha_k}{c(u\,\cdot) + \alpha_k},

and λu=1\lambda_u = 1 for a context never seen in training.

Proof.

Split the fraction: c(u w)c(u ⋅)+αk=c(u ⋅)c(u ⋅)+αk⋅c(u w)c(u ⋅)=(1−λu) P^(w∣u)\frac{c(u\,w)}{c(u\,\cdot) + \alpha_k} = \frac{c(u\,\cdot)}{c(u\,\cdot) + \alpha_k}\cdot\frac{c(u\,w)}{c(u\,\cdot)} = (1 - \lambda_u)\,\hat P(w \mid u), and αkc(u ⋅)+αk Pk−1(w∣u−)=λu Pk−1(w∣u−)\frac{\alpha_k}{c(u\,\cdot) + \alpha_k}\,P_{k-1}(w \mid u^-) = \lambda_u\,P_{k-1}(w \mid u^-). If c(u ⋅)=0c(u\,\cdot) = 0, then λu=1\lambda_u = 1 and only the lower order remains.

∎

So the estimate moves along the segment from the MLE to the lower-order model, and how far it moves depends on how much evidence the context has: a context seen ten thousand times barely moves, one seen once moves a long way. What remains is the strength αk\alpha_k. Empirical Bayes picks it by asking which prior makes the training data most probable.

Theorem 1 (The Dirichlet–multinomial evidence).

Under independent Dirichlet(α mu)\mathrm{Dirichlet}(\alpha\,m_u) priors for the contexts, where each context’s center mum_u is a distribution on VV fixed in advance — in Definition 2, mu=Pk−1(⋅∣u−)m_u = P_{k-1}(\cdot \mid u^-) — the probability of the training word sequences is

log⁡p(data∣α)  =  ∑u[log⁡Γ(α)−log⁡Γ(α+c(u ⋅))]  +  ∑(u, w) :  c(uw)>0[log⁡Γ(αmuw+c(u w))−log⁡Γ(αmuw)].\log p(\text{data} \mid \alpha) \;=\; \sum_u \Bigl[\log\Gamma(\alpha) - \log\Gamma\bigl(\alpha + c(u\,\cdot)\bigr)\Bigr] \;+\; \sum_{(u,\,w)\,:\;c(u w) \gt 0} \Bigl[\log\Gamma\bigl(\alpha m_{uw} + c(u\,w)\bigr) - \log\Gamma(\alpha m_{uw})\Bigr].
Proof.

For one context with next-word distribution θ\theta, the probability of its word sequence is ∏wθwc(uw)\prod_w \theta_w^{c(u w)}. Integrate against the Dirichlet density ∏wθwαmuw−1/B(αmu)\prod_w \theta_w^{\alpha m_{uw} - 1}/B(\alpha m_u), where B(β)=∏wΓ(βw)/Γ(∑wβw)B(\beta) = \prod_w \Gamma(\beta_w)/\Gamma(\sum_w \beta_w):

∫∏wθwαmuw+c(uw)−1 dθ / B(αmu)  =  B(αmu+cu)B(αmu),\int \prod_w \theta_w^{\alpha m_{uw} + c(u w) - 1}\,d\theta \,\Big/\, B(\alpha m_u) \;=\; \frac{B(\alpha m_u + \mathbf{c}_u)}{B(\alpha m_u)},

where cu=(c(u w))w∈V\mathbf{c}_u = (c(u\,w))_{w \in V} is the vector of counts after uu. Since ∑wαmuw=α\sum_w \alpha m_{uw} = \alpha, the logarithm is the bracketed context term plus one term per word; a word with c(u w)=0c(u\,w) = 0 contributes log⁡Γ(αmuw)−log⁡Γ(αmuw)=0\log\Gamma(\alpha m_{uw}) - \log\Gamma(\alpha m_{uw}) = 0. Contexts have independent priors, so their log-probabilities add.

∎

Maximizing this evidence over α\alpha is Type-II maximum likelihood — the parametric empirical Bayes of formalStatistics’ hierarchical Bayes and partial pooling (§28.7), where it fits the spread of eight schools’ effects. Fit order by order, each order’s centers mum_u taken from the orders already fitted below it, it gives α^=(1,105.5, 9.589, 25.015)\hat\alpha = (1{,}105.5,\ 9.589,\ 25.015) for unigrams, bigrams, and trigrams, and a held-out log-loss of 3.5343.534 nats per token — far better than Laplace’s 5.7865.786. Laplace here is flat add-one on the trigram counts with no lower orders, so that gap measures the hierarchy as well as the fitted strengths. The companion code checks Theorem 1 against SciPy’s Dirichlet–multinomial distribution.

Remark.

The evidence does not choose the best α\alpha for held-out text. Tuning α\alpha on the held-out data itself — an oracle we cannot use in practice — gives (68,911.6, 591.4, 6.97)(68{,}911.6,\ 591.4,\ 6.97) and a log-loss of 3.2433.243. At the unigram and bigram orders the oracle’s strength is about sixty-two times the evidence’s, and there the difference has a cause: the evidence is computed on training text, which contains no unseen words at all, so it never pays for the mass that <unk> will need, and it under-smooths. At the trigram order the comparison reverses: the oracle’s 6.976.97 is below the evidence’s 25.01525.015, so held-out text wants less trigram smoothing than the evidence chooses. We report that reversal as measured; we do not have a mechanism for it. Type-II maximum likelihood answers the question it is asked — which prior best explains the training data — and that is not the question held-out text asks.


Good–Turing: Robbins’ formula on word counts

The Dirichlet approach assumes a shape for the prior. Turing, working on wartime codebooks, had a different idea, published by his assistant Good (1953): estimate a word’s probability from how many other words share its count. Let NrN_r be the number of distinct types seen exactly rr times in a sample of nn tokens — the spectrum of the Zipf–Heaps topic — and MrM_r the total true probability of those types. Good–Turing replaces a count of rr with

r∗  =  (r+1) Nr+1Nr,r^* \;=\; (r + 1)\,\frac{N_{r+1}}{N_r},

and gives the types never seen the total probability N1/nN_1/n. On our trigrams the spectrum begins with N1,…,N5N_1, \dots, N_5 equal to 19,52119{,}521, 1,7361{,}736, 748748, 444444, 309309; so singleton trigrams are each worth r∗=2⋅1,736/19,521≈0.18r^* = 2 \cdot 1{,}736/19{,}521 \approx 0.18 of an occurrence, and the unseen trigrams share a large part of the mass. Why should the next count up tell us about this one? Because of an exact identity.

Theorem 2 (Good–Turing, exact).

For nn independent draws from any distribution pp on a countable set of types, and every r≥0r \ge 0,

E[Mr(n)]  =  r+1n+1 E[Nr+1(n+1)].\mathbb{E}\bigl[M_r(n)\bigr] \;=\; \frac{r + 1}{n + 1}\,\mathbb{E}\bigl[N_{r+1}(n + 1)\bigr].
Proof.

A type ww is seen exactly rr times with probability (nr)pw r(1−pw)n−r\binom{n}{r} p_w^{\,r}(1 - p_w)^{n - r}, so

E[Mr(n)]  =  ∑wpw(nr)pw r(1−pw)n−r  =  ∑w(nr)pw r+1(1−pw)(n+1)−(r+1).\mathbb{E}[M_r(n)] \;=\; \sum_w p_w \binom{n}{r} p_w^{\,r}(1 - p_w)^{n - r} \;=\; \sum_w \binom{n}{r} p_w^{\,r+1}(1 - p_w)^{(n + 1) - (r + 1)}.

Now (nr)=r+1n+1(n+1r+1)\binom{n}{r} = \frac{r + 1}{n + 1}\binom{n + 1}{r + 1}, so the sum is r+1n+1∑w(n+1r+1)pw r+1(1−pw)(n+1)−(r+1)\frac{r + 1}{n + 1}\sum_w \binom{n + 1}{r + 1} p_w^{\,r+1}(1 - p_w)^{(n + 1) - (r + 1)}, and the last sum is the expected number of types seen exactly r+1r + 1 times in n+1n + 1 draws.

∎

Both sides are linear in the distribution of the sample, so the identity also holds for mixtures of independent draws — exchangeable sequences such as the Pitman–Yor process of the Zipf–Heaps topic. Setting r=0r = 0 says the mass of the unseen types is, in expectation, N1(n+1)/(n+1)N_1(n + 1)/(n + 1). The usable estimate N1(n)/nN_1(n)/n is off by very little:

Corollary 1 (The missing-mass estimate is nearly unbiased).

0  ≤  E[N1(n)]n−E[M0(n)]  =  ∑wpw 2(1−pw)n−1  ≤  1n.0 \;\le\; \frac{\mathbb{E}[N_1(n)]}{n} - \mathbb{E}[M_0(n)] \;=\; \sum_w p_w^{\,2}(1 - p_w)^{n-1} \;\le\; \frac{1}{n}.
Proof.

Expand: E[N1(n)]/n=∑wpw(1−pw)n−1\mathbb{E}[N_1(n)]/n = \sum_w p_w(1 - p_w)^{n-1} and E[M0(n)]=∑wpw(1−pw)n\mathbb{E}[M_0(n)] = \sum_w p_w(1 - p_w)^{n}; their difference is ∑wpw⋅pw(1−pw)n−1\sum_w p_w \cdot p_w(1 - p_w)^{n-1}, which is nonnegative. The function x(1−x)n−1x(1 - x)^{n-1} on [0,1][0, 1] peaks at x=1/nx = 1/n with value 1n(1−1n)n−1≤1n\frac1n(1 - \frac1n)^{n-1} \le \frac1n, so the difference is at most 1n∑wpw=1n\frac1n\sum_w p_w = \frac1n.

∎

The missing mass also concentrates around its estimate (McAllester and Ortiz 2003, cited). The companion code checks Theorem 2 exactly on random distributions and Corollary 1 by simulation.

Remark.

Good–Turing is Robbins’ formula. Let each type’s count be Poisson, cw∼Poisson(λw)c_w \sim \mathrm{Poisson}(\lambda_w), and read the spread of the rates λw\lambda_w across the vocabulary as a prior gg. Robbins’ formula (formalStatistics, Empirical Bayes, §33.3, Theorem 2) gives the posterior mean rate of a type seen rr times as E[λ∣c=r]=(r+1) f(r+1)/f(r)\mathbb{E}[\lambda \mid c = r] = (r + 1)\,f(r + 1)/f(r), where f(r)f(r) is the probability that a type is seen rr times — estimated by NrN_r up to a constant. That is Turing’s r∗r^*: word types in place of insurance policy holders, the spectrum in place of the claim counts. Run on Topic 33’s insurance table, the companion code’s Good–Turing function reproduces its Robbins row, 0.1680.168, 0.3630.363, 0.5270.527, 1.331.33, 1.431.43, 6.006.00, 1.751.75, to the printed digits. The frame is Robbins’ compound decision problem (§33.2): estimate every type’s frequency at once, judged by total error.

The kinship carries a warning with it. Robbins’ estimate is erratic where the next count is small (Topic 33, Remark 5), and so is Turing’s: on our trigrams the discount r−r∗r - r^* runs 0.8220.822, 0.7070.707, 0.6260.626, 0.5200.520 for r=1,…,4r = 1, \dots, 4, then jumps to 1.0391.039, falls to −0.382-0.382 — a negative discount — and climbs to 2.4842.484. Simple Good–Turing (Gale and Sampson 1995) repairs the tail: it averages NrN_r over the gaps between observed counts to get ZrZ_r, fits a straight line to log⁡Zr\log Z_r against log⁡r\log r — plausible because the spectrum is a power law, as the Zipf–Heaps topic showed — and switches from Turing’s r∗r^* to the fitted one at the first rr where the two agree within 1.961.96 standard errors (here r=3r = 3). The resulting discount — Turing’s for r=1,2r = 1, 2, the fit’s from then on — is close to constant: 0.8220.822, 0.7070.707, 0.6480.648, 0.6880.688, 0.7140.714, 0.7330.733, 0.7470.747, 0.7580.758 for r=1,…,8r = 1, \dots, 8. Church and Gale (1991) saw the same thing on English bigrams. Hold on to it; it is the reason for absolute discounting. The companion code’s Simple Good–Turing matches NLTK’s to fifteen decimals; that check runs on the bigram spectrum, through the same function that gives the trigram figures above.


Backoff

Good–Turing says how much mass to take from the seen nn-grams. Backoff says where to send it: to the next order down.

Definition 3 (Katz backoff).

With Turing’s discount ratios drd_r for small counts (Katz 1987 renormalizes them so that counts above a cutoff k=5k = 5 are left alone, dr=1d_r = 1 for r>kr \gt k),

Pkatz(w∣u)  =  {dc(uw)  c(u w)/c(u ⋅)if c(u w)>0,α(u)  Pkatz(w∣u−)otherwise,P_{\text{katz}}(w \mid u) \;=\; \begin{cases} d_{c(u w)}\;c(u\,w)/c(u\,\cdot) & \text{if } c(u\,w) \gt 0, \\[2pt] \alpha(u)\;P_{\text{katz}}(w \mid u^-) & \text{otherwise,} \end{cases}

with Laplace smoothing at the unigram level and α(u)=1\alpha(u) = 1 for a context never seen.

Proposition 4 (The backoff weight that normalizes).

If

α(u)  =  1−∑w : c(uw)>0dc(uw) c(u w)/c(u ⋅)1−∑w : c(uw)>0Pkatz(w∣u−)\alpha(u) \;=\; \frac{1 - \sum_{w\,:\,c(u w) \gt 0} d_{c(u w)}\,c(u\,w)/c(u\,\cdot)}{1 - \sum_{w\,:\,c(u w) \gt 0} P_{\text{katz}}(w \mid u^-)}

and the denominator is positive, then Pkatz(⋅∣u)P_{\text{katz}}(\cdot \mid u) sums to one.

Proof.

Induct on the order; the Laplace unigram sums to one. The seen words of uu carry ∑seend c(u w)/c(u ⋅)\sum_{\text{seen}} d\,c(u\,w)/c(u\,\cdot), the numerator’s complement. The unseen words carry α(u)∑unseenPkatz(w∣u−)=α(u)[1−∑seenPkatz(w∣u−)]\alpha(u)\sum_{\text{unseen}} P_{\text{katz}}(w \mid u^-) = \alpha(u)\bigl[1 - \sum_{\text{seen}} P_{\text{katz}}(w \mid u^-)\bigr] by the induction hypothesis, which is the numerator. The two parts sum to one.

∎

On our corpus every drd_r lies strictly between 00 and 11 — for trigrams 0.1230.123, 0.6230.623, 0.7770.777, 0.8610.861, 0.7780.778 — and the model normalizes for every context tested. But the cutoff has a cost on text like ours, which is generated from templates. Take the context in osaka: it occurs seven times in training, followed by </s> every time. Seven is above the cutoff, so nothing is discounted and the numerator of α(u)\alpha(u) is zero: Katz has no mass to reserve for any other continuation. Here the denominator is zero too — the bigram osaka is also always followed by </s>, and gives it probability one — so α(u)\alpha(u) is 0/00/0, undefined; we keep the MLE, which reserves nothing. Of the 17,26917{,}269 seen contexts (3,4263{,}426 bigram and 13,84313{,}843 trigram), 2,1702{,}170 reserve nothing. In 564564 of them the cutoff does it alone: the numerator is zero, the denominator is not, and α(u)=0\alpha(u) = 0. The other 1,6061{,}606, in osaka among them, are trigram contexts whose seen followers already hold all of the lower order’s mass, so the denominator is zero and α(u)\alpha(u) is undefined. They reserve nothing because we keep the MLE there — 1,2891{,}289 of them would free some mass under discounting, but it would have nowhere to go. Three held-out tokens fall into zero-reserve contexts and get probability zero: after across aerospace and of fairhaven, where the cutoff makes α(u)=0\alpha(u) = 0, and after in osaka, where α(u)\alpha(u) is undefined. Katz’s held-out log-loss is infinite: the zero-count problem, back through the cutoff. On the other 23,05223{,}052 held-out tokens it scores 3.1103.110.


Absolute discounting

Simple Good–Turing found that the discount r−r∗r - r^* is roughly the same for every count. Then why not subtract one constant from every count and interpolate with the lower order, as the Dirichlet model does?

Definition 4 (Interpolated absolute discounting).

With a discount 0<D≤10 \lt D \le 1 and N1+(u ∙)=∣{w:c(u w)>0}∣N_{1+}(u\,\bullet) = \lvert\{w : c(u\,w) \gt 0\}\rvert the number of distinct words seen after uu,

P(w∣u)  =  max⁡(c(u w)−D, 0)+D N1+(u ∙) P(w∣u−)c(u ⋅),P(w \mid u) \;=\; \frac{\max\bigl(c(u\,w) - D,\, 0\bigr) + D\,N_{1+}(u\,\bullet)\,P(w \mid u^-)}{c(u\,\cdot)},

with P(w∣u)=P(w∣u−)P(w \mid u) = P(w \mid u^-) for a context never seen, and the uniform distribution 1/∣V∣1/\lvert V\rvert below the unigram level.

Proposition 5 (Absolute discounting normalizes).

If P(⋅∣u−)P(\cdot \mid u^-) sums to one, so does P(⋅∣u)P(\cdot \mid u).

Proof.

Because D≤1D \le 1, every seen word has max⁡(c(u w)−D,0)=c(u w)−D\max(c(u\,w) - D, 0) = c(u\,w) - D, so the first terms sum to c(u ⋅)−D N1+(u ∙)c(u\,\cdot) - D\,N_{1+}(u\,\bullet). The second terms sum to D N1+(u ∙)D\,N_{1+}(u\,\bullet). The numerators sum to c(u ⋅)c(u\,\cdot); induct down to the uniform base.

∎

Every subtracted unit of count reappears as mass for the lower order. The one parameter is DD, and Ney, Essen, and Kneser (1994) proposed choosing it by leave-one-out: delete each training token in turn, ask how well the model trained on the rest predicts it, and maximize the total. The answer usually quoted is D=n1/(n1+2n2)D = n_1/(n_1 + 2n_2), with nrn_r the number of nn-grams seen rr times. What the leave-one-out argument actually proves is weaker, and more interesting.

Theorem 3 (The leave-one-out discount).

Consider the backing-off form of absolute discounting, in which a seen nn-gram gets (c(u w)−D)/c(u ⋅)(c(u\,w) - D)/c(u\,\cdot) and an unseen one gets the freed mass D N1+(u ∙)/c(u ⋅)D\,N_{1+}(u\,\bullet)/c(u\,\cdot) shared in proportion to a fixed lower-order distribution. Let n1′n_1' count the singletons whose context occurs at least twice. Then the leave-one-out log-likelihood is

F(D)  =  n1′log⁡D  +  ∑r≥2r nrlog⁡(r−1−D)  +  C,F(D) \;=\; n_1'\log D \;+\; \sum_{r \ge 2} r\,n_r \log(r - 1 - D) \;+\; C,

with CC independent of DD. If n1′,n2>0n_1', n_2 \gt 0, then FF is strictly concave on (0,1)(0, 1), has a unique maximizer D∗D^*, and

D∗  ≤  n1′n1′+2n2  ≤  n1n1+2n2.D^* \;\le\; \frac{n_1'}{n_1' + 2n_2} \;\le\; \frac{n_1}{n_1 + 2n_2}.
Proof.

Remove one occurrence of an nn-gram seen rr times. If r≥2r \ge 2 it is still seen, with probability (r−1−D)/(c(u ⋅)−1)(r - 1 - D)/(c(u\,\cdot) - 1). If r=1r = 1 and its context occurs at least twice, it becomes unseen in a context that still exists, and its probability is D (N1+(u ∙)−1)/(c(u ⋅)−1)D\,(N_{1+}(u\,\bullet) - 1)/(c(u\,\cdot) - 1) times a factor from the lower-order distribution; only the factor DD depends on DD. If r=1r = 1 and the context occurred once, the context disappears and the probability comes from the lower order alone, free of DD. Summing logarithms over all occurrences gives FF.

Each log⁡(r−1−D)\log(r - 1 - D) is concave in DD and n1′log⁡Dn_1'\log D is strictly concave, so FF is strictly concave. Its derivative F′(D)=n1′/D−∑r≥2r nr/(r−1−D)F'(D) = n_1'/D - \sum_{r\ge2} r\,n_r/(r - 1 - D) tends to +∞+\infty as D→0D \to 0 and to −∞-\infty as D→1D \to 1 (the r=2r = 2 term), so it has exactly one root D∗D^*. At that root,

n1′D∗  =  ∑r≥2r nrr−1−D∗  ≥  2n21−D∗,\frac{n_1'}{D^*} \;=\; \sum_{r \ge 2}\frac{r\,n_r}{r - 1 - D^*} \;\ge\; \frac{2n_2}{1 - D^*},

which rearranges to D∗≤n1′/(n1′+2n2)D^* \le n_1'/(n_1' + 2n_2). Finally x/(x+2n2)x/(x + 2n_2) increases in xx, and n1′≤n1n_1' \le n_1.

∎

So n1/(n1+2n2)n_1/(n_1 + 2n_2) is an upper bound on the leave-one-out optimum, reached only if the counts above two are ignored and every singleton’s context occurs at least twice, so that n1′=n1n_1' = n_1. The difference is not small here. For bigrams, D∗=0.702D^* = 0.702 against the familiar 0.8050.805; for trigrams D∗=0.644D^* = 0.644 against 0.8490.849, and over half of the trigram singletons (10,30610{,}306 of 19,52119{,}521) sit alone in their context and drop out of n1′n_1' entirely. The companion code checks that FF differs from the exact leave-one-out log-likelihood by a constant, and the bound on random spectra.


Kneser–Ney

Absolute discounting sends the freed mass to the lower-order distribution. Look at what that distribution believes. In the finance corpus, industrial occurs 284284 times, and every one of them follows halvorsen: it is the second word of a company name, Halvorsen Industrial. A unigram model built from raw counts ranks industrial as the 2727th most likely word. But the lower order is only consulted when the trigram and bigram are unseen — that is, in a context that is not halvorsen, where industrial almost never appears. The lower order should answer a different question: not how often does ww occur? but how likely is ww to appear after a context it was not seen in? Kneser and Ney’s answer counts contexts instead of tokens.

Definition 5 (Continuation counts and interpolated Kneser–Ney).

The continuation count of an (n−1)(n-1)-gram vv is N1+(∙ v)=∣{x:c(x v)>0}∣N_{1+}(\bullet\,v) = \lvert\{x : c(x\,v) \gt 0\}\rvert, the number of distinct words that precede it. Interpolated Kneser–Ney is Definition 4 with raw counts at the top order and continuation counts at every lower order:

P(w∣u)  =  max⁡(c(u w)−Dk(c(u w)), 0)+γ(u) P(w∣u−)c(u ⋅),γ(u)=∑w : c(uw)>0Dk(c(u w)),P(w \mid u) \;=\; \frac{\max\bigl(c(u\,w) - D_k(c(u\,w)),\,0\bigr) + \gamma(u)\,P(w \mid u^-)}{c(u\,\cdot)}, \qquad \gamma(u) = \sum_{w\,:\,c(u w) \gt 0} D_k\bigl(c(u\,w)\bigr),

where at a lower order cc means the continuation count. With one discount DkD_k per order, γ(u)=Dk N1+(u ∙)\gamma(u) = D_k\,N_{1+}(u\,\bullet). Modified Kneser–Ney (Chen and Goodman 1999) uses three discounts per order, D1D_1, D2D_2, and D3+D_{3+}, for counts of one, two, and more.

industrial has continuation count 11, and drops from rank 2727 to rank 1,2591{,}259. The choice of count is not a trick, though: it is forced by a requirement.

Theorem 4 (The marginal constraint).

Take the top order as in Definition 5, with discounts satisfying c−D(c)≥0c - D(c) \ge 0 for c≥1c \ge 1, and an unknown lower-order distribution β(⋅∣v)\beta(\cdot \mid v) in place of P(⋅∣u−)P(\cdot \mid u^-). The model reproduces the training data’s marginals,

∑xc(x v ⋅)  P(w∣x v)  =  c(v w)for all v,w,\sum_x c(x\,v\,\cdot)\;P(w \mid x\,v) \;=\; c(v\,w) \qquad \text{for all } v, w,

if and only if

β(w∣v)  =  ∑xD(c(x v w))∑xγ(x v).\beta(w \mid v) \;=\; \frac{\sum_x D\bigl(c(x\,v\,w)\bigr)}{\sum_x \gamma(x\,v)}.

With one discount per order, this is β(w∣v)=N1+(∙ v w)/N1+(∙ v ∙)\beta(w \mid v) = N_{1+}(\bullet\,v\,w)/N_{1+}(\bullet\,v\,\bullet): the continuation distribution.

Proof.

Multiply out:

∑xc(x v ⋅) P(w∣x v)  =  ∑x[c(x v w)−D(c(x v w))]  +  β(w∣v)∑xγ(x v).\sum_x c(x\,v\,\cdot)\,P(w \mid x\,v) \;=\; \sum_x \Bigl[c(x\,v\,w) - D\bigl(c(x\,v\,w)\bigr)\Bigr] \;+\; \beta(w \mid v)\sum_x \gamma(x\,v).

Because lower-order counts are marginals, ∑xc(x v w)=c(v w)\sum_x c(x\,v\,w) = c(v\,w), so the constraint holds exactly when β(w∣v)∑xγ(x v)=∑xD(c(x v w))\beta(w \mid v)\sum_x \gamma(x\,v) = \sum_x D(c(x\,v\,w)). When D(c)=DD(c) = D for every c≥1c \ge 1, the right side is DD times the number of xx with c(x v w)>0c(x\,v\,w) \gt 0, which is D N1+(∙ v w)D\,N_{1+}(\bullet\,v\,w), and ∑xγ(x v)=D N1+(∙ v ∙)\sum_x \gamma(x\,v) = D\,N_{1+}(\bullet\,v\,\bullet).

∎

Kneser and Ney (1995) derived the backing-off version of this approximately; in the interpolated form it is exact. The companion code confirms it on the corpus, and confirms the converse too: with modified Kneser–Ney’s three discounts, the marginal-preserving lower order is the discount-weighted count of Theorem 4, not the continuation count, and continuation counts miss the bigram marginals by more than one percent. Modified Kneser–Ney uses continuation counts anyway; that part of it is a heuristic. Our interpolated Kneser–Ney, configured the way NLTK’s KneserNeyInterpolated is (one discount at every order, no uniform base), equals NLTK’s to twelve decimals on sixty held-out predictions.


Kneser–Ney as a hierarchical Pitman–Yor posterior

Continuation counts came out of a constraint. They also come out of a prior. In the Zipf–Heaps topic, word types arrived through a Pitman–Yor Chinese restaurant with discount dd and strength θ\theta: each customer (token) joins an occupied table with probability proportional to its occupancy minus dd, or opens a new table, whose dish (word type) is drawn from a base distribution, with probability proportional to θ\theta plus dd times the number of tables. Teh (2006) stacks these restaurants into a hierarchy.

Definition 6 (The hierarchical Pitman–Yor language model).

Put a Pitman–Yor restaurant in every context uu, with parameters (dk,θk)(d_k, \theta_k) for contexts of order kk, whose base distribution is the restaurant of u−u^-; the restaurant of the empty context draws from the uniform distribution on VV. Each table in uu sends one customer to u−u^-. Given a seating — cuwc_{uw} customers at tuwt_{uw} tables serving ww in restaurant uu, totals cu⋅c_{u\cdot} and tu⋅t_{u\cdot} — the predictive probability is

P(w∣u)  =  cuw−dk tuwθk+cu⋅  +  θk+dk tu⋅θk+cu⋅ P(w∣u−).P(w \mid u) \;=\; \frac{c_{uw} - d_k\,t_{uw}}{\theta_k + c_{u\cdot}} \;+\; \frac{\theta_k + d_k\,t_{u\cdot}}{\theta_k + c_{u\cdot}}\,P(w \mid u^-).

The predictive already has Kneser–Ney’s shape: subtract dd per table, and give the subtracted mass, plus θ\theta, to the parent. The parent’s counts are tables, not tokens. That is the bridge.

Proposition 6 (Kneser–Ney is the one-table seating).

Let the bottom restaurants hold the raw counts as customers, seat every word at exactly one table in every restaurant, tuw=min⁡(1,cuw)t_{uw} = \min(1, c_{uw}), and set θk=0\theta_k = 0. Then (i) the customers of a restaurant below the top are the continuation counts, cuw=N1+(∙ u w)c_{uw} = N_{1+}(\bullet\,u\,w), and (ii) the predictive of Definition 6 is interpolated Kneser–Ney with Dk=dkD_k = d_k at every order.

Proof.

(i) Restaurant uu receives one customer for each table serving ww in each child restaurant x ux\,u, and with one table per type that is one customer for each xx with cxu,w>0c_{xu,w} \gt 0. Directly below the top, cxu,wc_{xu,w} is the raw count c(x u w)c(x\,u\,w), so cuw=N1+(∙ u w)c_{uw} = N_{1+}(\bullet\,u\,w). Further down, by induction cxu,w>0c_{xu,w} \gt 0 exactly when N1+(∙ x u w)>0N_{1+}(\bullet\,x\,u\,w) \gt 0, which happens exactly when c(x u w)>0c(x\,u\,w) \gt 0 because counts are marginals; so again cuw=N1+(∙ u w)c_{uw} = N_{1+}(\bullet\,u\,w). (ii) With θk=0\theta_k = 0 and tuw=min⁡(1,cuw)t_{uw} = \min(1, c_{uw}), the predictive is [cuw−dk 1{cuw>0}+dk N1+(u ∙) P(w∣u−)]/cu⋅\bigl[c_{uw} - d_k\,\mathbf{1}\{c_{uw} \gt 0\} + d_k\,N_{1+}(u\,\bullet)\,P(w \mid u^-)\bigr]/c_{u\cdot}, which is Definition 5 with Dk=dkD_k = d_k, the same counts by (i), and the same uniform base.

∎

The companion code checks the identity to thirteen decimals on 300300 held-out predictions. One table per type is a single, extreme seating, though. The model’s posterior spreads frequent words over several tables. Teh’s Gibbs sampler explores it: remove each training token from its table in turn and reseat it — at an occupied table with weight c−dc - d, or at a new one with weight (θ+d tu⋅) P(w∣u−)(\theta + d\,t_{u\cdot})\,P(w \mid u^-), which sends a customer to the parent — then resample each order’s (dk,θk)(d_k, \theta_k) given the seating. Started from the Kneser–Ney seating and run for forty sweeps, it gives:

  • Strengths near zero. The posterior means are θ=(0.962, 0.0026, 0.0017)\theta = (0.962,\ 0.0026,\ 0.0017) for orders one to three: above the unigram, the data choose pure discounting — Kneser–Ney’s regime.
  • A discount that grows with the count. A singleton always sits at one table, so its discount is exactly d3=0.775d_3 = 0.775. A trigram seen rr times sits at more tables as rr grows, so its effective discount d3 E[t∣c=r]d_3\,\mathbb{E}[t \mid c = r] climbs: 0.7750.775, 1.1671.167, 1.5031.503, 1.7871.787, 1.9711.971, 2.0042.004, 2.2602.260, 2.6572.657 for r=1,…,8r = 1, \dots, 8 — far past modified Kneser–Ney’s top discount of 0.9840.984. Good–Turing’s measured discount stayed near 0.70.7; the two are not the same quantity, because in the Pitman–Yor model the subtracted mass flows to the parent, which in a templated context predicts the same word again.
  • The best held-out score. Averaging the predictive over sweeps 2121 to 4040 gives 2.8222.822 nats per token, ahead of modified Kneser–Ney’s 2.8412.841. That is one chain from one random seed; the harness does not test how far the score moves from seed to seed.

This is Markov chain Monte Carlo run for forty sweeps; we report averages and claim no convergence guarantee.


The smoothers side by side

The laboratory puts the classical smoothers on five trigram contexts. In panel A, pick a context and a smoother and read the next-word distribution: the eight likeliest words, filled if they followed the context in training and hollow if not, then the remaining mass on words that did and did not. The context in osaka shows Katz reserving nothing; machinery as, seen once, shows how much each method takes from a singleton; the ⟨unk⟩ has only the unigram level left, where absolute discounting ranks industrial in the top 3030 and Kneser–Ney ranks it past 1,0001{,}000. The slider sets one discount DD at every order for absolute discounting and interpolated Kneser–Ney; their held-out log-loss bottoms out near D=0.9D = 0.9 and rises again at D=1D = 1. Panel B plots the discount each method takes from a trigram seen rr times — every curve on this page in one picture. Hover a count to light up, in panel A, the words seen exactly that often after the context.

A · Smooth a context — what follows within industrials ___?
context seen in training
26× · 7 distinct next words
mass on words never seen here
0.1482
“industrial”: probability · rank
7.99e-6 · #1259
whole model, held-out log-loss
2.889 nats/token

Filled bars: words seen after this context (×count). Hollow: never seen here. For reference, the sampled hierarchical Pitman–Yor model reaches 2.822 nats/token.


B · Discount versus count — how much each method takes from a trigram seen r times
○ Turing r − r* (raw)━ Simple Good–Turing┅ interpolated KN (D; Ney's 0.849 ticked)━ modified KN━ HPYLM d·E[t | r]
Hover a count r to read the discounts and light up, in panel A, the words seen exactly r times after the context.

The slider applies one D at every order (the page's tables use Ney's per-order discounts). Turing's raw estimate is erratic once N_(r+1) is small; the HPYLM curve is an average over 20 Gibbs sweeps.

Held-out log-loss in nats per token, with the per-order discounts n1/(n1+2n2)n_1/(n_1 + 2n_2):

smootherheld out, same generator (23,05523{,}055 tokens)EDGAR MD&A (830830 tokens)
maximum likelihood∞\infty∞\infty
Laplace5.7865.7868.133\mathbf{8.133}
hierarchical Dirichlet, Type-II α^\hat\alpha3.5343.53410.78710.787
Katz backoff∞\infty (3.1103.110 on 23,05223{,}052 tokens)∞\infty
absolute discounting3.1673.1679.8509.850
interpolated Kneser–Ney2.8552.8558.7968.796
modified Kneser–Ney2.8412.8418.6028.602
hierarchical Pitman–Yor (sampled)2.822\mathbf{2.822}8.6548.654

In domain the ordering is the textbook one, strictly. The second column is a warning. On three public MD&A excerpts, 57.6%57.6\% of tokens are words the model never saw and 99%99\% of trigrams are new; there, Laplace’s indiscriminate pseudo-counts beat every careful smoother, because the careful ones learned how this generator’s text continues and that knowledge does not transfer.


Finance case study: what each estimate estimates


Where this goes next

  • Perplexity. The held-out log-loss of this page, exponentiated, is perplexity; the next language-modeling topic connects it to cross-entropy and to compression, and handles the subword-versus-word normalization the previous topic raised.
  • Markov chains with hidden states. An nn-gram model is a Markov chain on words. Putting the chain on hidden tags and emitting words from them gives the hidden Markov models of sequence labeling, whose emission and transition tables need exactly the smoothing developed here.
  • Association instead of prediction. Continuation counts measure how many contexts a word completes; collocation measures ask which word pairs occur together more than chance, with likelihood-ratio tests in place of discounts.
  • Overlap metrics. BLEU and ROUGE score generated text by its nn-gram overlap with references — counting again, with its own zero-count problem at higher orders.

Honest caveats


Implementation

The companion notebook (notebookPath) imports n_gram_models_smoothing.py, which owns every number on this page. Running it as a script executes a harness of 2828 assertion-based tests in about fifteen seconds on a laptop CPU, including forty Gibbs sweeps of the hierarchical Pitman–Yor model. The tests cover:

  • Propositions 1–3 and Theorem 1, with the evidence checked against SciPy’s Dirichlet–multinomial distribution.
  • Theorem 2 exactly on random distributions, Corollary 1 exactly and by simulation, and the Robbins row of Topic 33’s insurance table.
  • Simple Good–Turing against NLTK’s implementation, and Katz’s normalization and its zeros.
  • Theorem 3’s concavity and bound on the corpus and on random spectra, and its objective against the exact leave-one-out likelihood.
  • Continuation counts against brute force, Theorem 4’s marginal constraint (and its failure for modified Kneser–Ney), normalization at every order, and agreement with NLTK’s KneserNeyInterpolated.
  • Proposition 6 on held-out predictions, the sampler’s seating invariants, the growth of its discounts, the held-out ordering of all smoothers, and the finance case study.

The laboratory reads its data from a JSON file that one of those tests requires to equal the module’s viz_payload() exactly, so the figure cannot drift from the code.

References & Further Reading