Algorithm Search⌘K

Modelintro

N-gram language model

Also called n-gram model, Markov language model.

A language model that conditions each token only on the n - 1 tokens before it, with probabilities estimated from counts in a corpus and smoothed to fill the zeros.

An n-gram language model predicts each token of a sequence from the n−1n - 1 tokens just before it and from nothing else, and it learns these predictions by counting. A bigram model (n=2n = 2) predicts each word from the previous word, and a trigram model (n=3n = 3) from the previous two. The same model on DNA, with the four bases as tokens, is the k-mer Markov model used in gene finding and sequence analysis (see k-mer and interpolated Markov model). It is the oldest practical language model, it is fast to train and to query, and its central problem is that most possible n-grams never occur in the training data, which is what the smoothing methods of this area solve.

The machine

Fix the order n≥1n \ge 1 and a vocabulary VV of predicted tokens, with the end marker </s> and an unknown-word token <unk>. Each sentence w1,…,wmw_1, \ldots, w_m, with wm=⟨/s⟩w_m = \langle /s \rangle, is padded on the left with n−1n - 1 start markers <s>, written w2−n,…,w0w_{2-n}, \ldots, w_0. The history of token wiw_i is the tuple ui=wi−n+1,…,wi−1u_i = w_{i-n+1}, \ldots, w_{i-1} of the n−1n - 1 tokens before it.

The model is a table that gives, for each history uu, a distribution P(⋅∣u)P(\cdot \mid u) on VV. The probability of a sentence is

P(w1,…,wm)=∏i=1mP(wi∣wi−n+1,…,wi−1).P(w_1, \ldots, w_m) = \prod_{i=1}^{m} P(w_i \mid w_{i-n+1}, \ldots, w_{i-1}).

This is the chain rule of a language model with the Markov assumption that only the last n−1n - 1 tokens matter. The model is a Markov chain whose states are the histories; the unigram model (n=1n = 1) has one state and treats a sentence as a bag of independent tokens.

The maximum likelihood estimate from a training corpus is a ratio of counts. Let c(u w)c(u\,w) be the number of positions where history uu is followed by ww, and c(u)=∑w∈Vc(u w)c(u) = \sum_{w \in V} c(u\,w). Then

PML(w∣u)=c(u w)c(u).P_{\mathrm{ML}}(w \mid u) = \frac{c(u\,w)}{c(u)}.

For each uu the likelihood ∏wP(w∣u)c(uw)\prod_w P(w \mid u)^{c(u w)} is largest at these values, by Gibbs' inequality, and the histories do not share parameters, so this is the joint maximum. It gives probability 0 to every n-gram not seen in training, and it is undefined for a history not seen. A smoothed model replaces it with an estimate that is positive on all of VV.

A word example. Train a bigram model on the three sentences "a b", "a c" and "b c". The maximum likelihood rows are:

history uu P(⟨/s⟩∣u)P(\langle /s \rangle \mid u) P(a∣u)P(a \mid u) P(b∣u)P(b \mid u) P(c∣u)P(c \mid u)
<s> 0 2/3 1/3 0
a 0 0 1/2 1/2
b 1/2 0 0 1/2
c 1 0 0 0

So P(a b)=23⋅12⋅12=16P(\text{a b}) = \frac23 \cdot \frac12 \cdot \frac12 = \frac16, P(a c)=13P(\text{a c}) = \frac13 and P(b c)=16P(\text{b c}) = \frac16. The model also gives 16\frac16 to "a b c" and 16\frac16 to "b", which it never saw, because it combines bigrams from different sentences. It gives 0 to "c", because no sentence starts with c. These five sentences have total probability 1.

A DNA example. A model of order 3 on DNA predicts each base from the two before it. If the context CG occurs 5 times in a genome, followed by A twice and by G three times, then PML(A∣CG)=2/5P_{\mathrm{ML}}(A \mid \mathrm{CG}) = 2/5, PML(G∣CG)=3/5P_{\mathrm{ML}}(G \mid \mathrm{CG}) = 3/5, and C and T get 0. Laplace smoothing with pseudocounts of 1 changes these to 3/93/9, 4/94/9, 1/91/9 and 1/91/9.

What it counts

The model is measured by its fit to held-out text and by its size and speed.

  • Fit. The standard measure is the perplexity on held-out text, exp⁡\exp of the mean of −ln⁡P(wi∣ui)-\ln P(w_i \mid u_i) over the predicted tokens. It is infinite for an unsmoothed model as soon as one test n-gram is new.
  • Parameters. Only the seen n-grams need storage. A corpus of NN tokens has at most NN distinct n-grams of each order, so a model of order nn stores at most nNnN counts, plus the backoff or interpolation weights per history. The number of possible n-grams, ∣V∣n|V|^n, is far larger.
  • Time. Counting takes one pass over the corpus, with Θ(nN)\Theta(nN) expected time when the n-grams are kept in a hash table or a trie. Scoring a token takes O(n)O(n) lookups for a smoothed model that combines the orders n,n−1,…,1n, n-1, \ldots, 1.
  • Data. The fraction of new n-grams in fresh text from the same source is estimated by N1/NN_1/N, the share of training tokens whose n-gram was seen exactly once, which is the Good-Turing estimate of the unseen mass.

Relation to other models

Known results in the model

  • The maximum likelihood estimate is a ratio of counts, as shown above, and it is the estimate that the unsmoothed model uses.
  • A smoothed model is a distribution on sentences. If every history gives </s> a positive probability, then, since there are finitely many histories, the probabilities of all finite sentences sum to 1 (see language model).
  • Shannon's approximations. Shannon (1948) generated text from letter and word models of increasing order and showed that it looks more like English as the order grows. Shannon (1951) used n-gram statistics of English letters, and the predictions of human readers, to estimate the entropy of printed English.
  • Sparsity grows with the order. For a fixed corpus, raising nn lowers the perplexity on the training text but raises the share of test n-grams that are new, so on held-out text the best order depends on the amount of data and on the smoothing.
  • Comparisons of smoothing. Chen and Goodman (1999) compared the smoothing methods above over several corpora and training sizes, measured by cross-entropy on held-out text, and found that their modified Kneser-Ney smoothing did best.

References

  1. C. E. Shannon, A mathematical theory of communication, Bell System Technical Journal 27(3), 1948, Part I, the series of approximations to English by letter and word sources of increasing order
  2. Shannon 1951, the entropy of English letters estimated from n-gram statistics and from human prediction
  3. Stanley F. Chen and Joshua Goodman, An empirical study of smoothing techniques for language modeling, Computer Speech and Language 13(4), 1999, pages 359 to 394, the comparison of smoothing methods for n-gram models over several corpora and training sizes
  4. Christopher D. Manning and Hinrich Schütze, Foundations of Statistical Natural Language Processing, MIT Press, 1999, Chapter 6 (statistical inference, n-gram models over sparse data)
  5. Daniel Jurafsky and James H. Martin, Speech and Language Processing, third edition draft, chapter N-gram Language Models (draft of January 2026)

Where this leads

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW4 complete
  • a review of the text: no review record reviews/NGSSK3/text.yaml