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 tokens just before it and from nothing else, and it learns these predictions by counting. A bigram model () predicts each word from the previous word, and a trigram model () 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 and a vocabulary of predicted tokens, with the end marker </s> and an unknown-word token <unk>. Each sentence , with , is padded on the left with start markers <s>, written . The history of token is the tuple of the tokens before it.
The model is a table that gives, for each history , a distribution on . The probability of a sentence is
This is the chain rule of a language model with the Markov assumption that only the last tokens matter. The model is a Markov chain whose states are the histories; the unigram model () 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 be the number of positions where history is followed by , and . Then
For each the likelihood 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 .
A word example. Train a bigram model on the three sentences "a b", "a c" and "b c". The maximum likelihood rows are:
| history | ||||
|---|---|---|---|---|
<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 , and . The model also gives to "a b c" and 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 , , and C and T get 0. Laplace smoothing with pseudocounts of 1 changes these to , , and .
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, of the mean of 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 tokens has at most distinct n-grams of each order, so a model of order stores at most counts, plus the backoff or interpolation weights per history. The number of possible n-grams, , is far larger.
- Time. Counting takes one pass over the corpus, with expected time when the n-grams are kept in a hash table or a trie. Scoring a token takes lookups for a smoothed model that combines the orders .
- Data. The fraction of new n-grams in fresh text from the same source is estimated by , 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
- Smoothed n-gram models. Laplace smoothing adds a constant to every count. Good-Turing estimation and simple Good-Turing reestimate the counts from the counts of counts. Katz backoff uses discounted counts for seen n-grams and the shorter history for the rest. Deleted interpolation, Witten-Bell smoothing and absolute discounting mix the orders with weights per history. Kneser-Ney smoothing and modified Kneser-Ney smoothing add a lower order built from the number of distinct contexts.
- Large-scale variants. Stupid backoff drops normalization to scale to web corpora, entropy-based pruning removes n-grams whose loss changes the model least, and a cache language model mixes in the counts of the recent text.
- Biology and compression. The k-mer Markov models of DNA are n-gram models on bases, and the interpolated Markov model of gene finders mixes orders as deleted interpolation does. Prediction by partial matching in text compression is an n-gram model whose escape probabilities gave rise to Witten-Bell smoothing.
- Neural models. The neural probabilistic language model keeps the fixed window of tokens but shares parameters through word vectors, so similar histories get similar predictions. The recurrent neural network language model and the transformer drop the fixed window. The maximum entropy language model uses n-grams as features of a log-linear model.
- Hidden Markov models. A hidden Markov model has a Markov chain on hidden states, with the tokens as emissions. In an n-gram model the states are the visible histories.
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 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
- 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
- Shannon 1951, the entropy of English letters estimated from n-gram statistics and from human prediction
- 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
- 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)
- 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
- Absolute discounting
- Brown clustering
- Cache language model
- Deleted interpolation
- HCLG decoding graph
- Joint-sequence grapheme-to-phoneme model
- Katz backoff
- Kneser-Ney smoothing
- Laplace smoothing
- Lattice-free MMI
- Lattice rescoring
- Markov password model
- Maximum entropy language model
- Neural probabilistic language model
- Out-of-vocabulary word
- Phrase-based translation model
- Shallow fusion
- Stack decoding
- Stupid backoff
- Time-synchronous Viterbi beam search
- Truecasing
- Witten-Bell smoothing
- Word lattice
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.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W4 complete |
|
Tag NGSSK3 Edit this page History Report an error Markdown