N-Gram Language Models
A language model assigns a probability to a sequence of words and, equivalently, predicts the next word from its history. The n-gram model makes this tractable by truncating the history to the last few words, estimates the resulting conditional probabilities by counting, and is scored by perplexity.
╌╌╌╌
Some word sequences are more probable than others. Its water is so transparent that the is ordinary English; its water is so transparent that which is not. A model that could put a number on that difference — that could say the is a likely next word here and which is not — would be useful almost everywhere in language technology: it picks the fluent transcription in speech recognition, the natural phrasing in translation, the plausible completion in text prediction.
A language model does precisely that job: it assigns a probability to a sequence of words, and equivalently predicts the next word given the words so far.1 The two views are equivalent: a model that scores whole sequences can rank candidate next words, and a model that predicts each next word can multiply those predictions into a sequence probability. The n-gram is the simplest such model, and though neural language models have replaced it in practice, every concept here — the prediction task, maximum likelihood from counts, perplexity, the zero-probability problem — carries forward unchanged.
The prediction task
Start with the conditional form. Let be a history, the words seen so far, and a candidate next word. We want
One estimate comes straight from counts: take a very large corpus, count how often the history appears, count how often it is followed by , and divide.2
This estimate fails in practice. Language is creative — new sentences are coined constantly — so even a corpus the size of the web will not contain most histories even once, and the ratio is . The same problem affects the sequence view: to get directly we would count how often that exact sequence occurs among all sequences of the same length, and long sequences essentially never repeat. We need a way to estimate these probabilities without having seen the exact string.
Notation
Write a sequence of words as , and abbreviate the span as . So is the entire history before position . Counts are written : is the number of times the bigram appears in the corpus. The vocabulary is , and is its size.
The chain rule and the Markov approximation
Counting a whole sequence directly is infeasible. Two steps make the problem tractable: rewrite the sequence probability exactly as a product of next-word predictions, then approximate each prediction by conditioning on only the most recent words.
The joint probability of a sequence factors exactly, with no approximation, by the chain rule of probability: each word is conditioned on everything before it.
This is exact but does not yet help: the last factor conditions on the full history, the very quantity we cannot estimate. The chain rule has only converted one uncountable quantity (a whole sequence) into a product of uncountable quantities (each word on its entire past).
The n-gram idea is to approximate each factor by conditioning on only the last few words rather than the full history. The bigram model keeps just the single preceding word:
The assumption that the next word depends only on a bounded window of recent words is the Markov assumption.3 Generalizing from one word of context to gives the general n-gram approximation:
Substituting the approximation back into the chain rule gives the model's estimate of a whole sequence — for the bigram case,
The figure below shows the two views side by side: the same product of factors, read left-to-right as next-word prediction or collected as a sequence score.
Estimating the probabilities: maximum likelihood
An n-gram probability is estimated by maximum likelihood estimation (MLE): take counts from a corpus and normalize them to lie between and .4 For a bigram, count the two-word sequence and divide by the count of all bigrams that share the same first word — which is just the unigram count of that word:
The general n-gram case divides the count of the full n-gram by the count of its -word prefix:
This ratio, an observed frequency over the frequency of a prefix, is a relative frequency, and using relative frequencies as probability estimates is what makes this MLE: the resulting parameters maximize the probability the model assigns to the training corpus.
A worked bigram example
Take a three-sentence corpus. Each sentence is padded with a start token <s> and an
end token </s> — the start token supplies a left context for the first real word,
and the end token lets the model assign probability to where a sentence stops, so
that sequence probabilities of different lengths remain comparable.
<s> I am Sam </s><s> Sam I am </s><s> I do not like green eggs and ham </s>Reading counts off the corpus, some of the bigram MLE probabilities are
I is followed by am twice and by do once out of its three occurrences, so the
three bigrams starting with I split as .
Counts and probabilities on a larger corpus
The pattern is clearer on real data. The tables below use eight words from the Berkeley Restaurant Project, a spoken-query corpus of sentences over a vocabulary of words.5 First the raw bigram counts — rows are the first word, columns the second:
| i | want | to | eat | chinese | food | lunch | spend | |
|---|---|---|---|---|---|---|---|---|
| i | 5 | 827 | 0 | 9 | 0 | 0 | 0 | 2 |
| want | 2 | 0 | 608 | 1 | 6 | 6 | 5 | 1 |
| to | 2 | 0 | 4 | 686 | 2 | 0 | 6 | 211 |
| eat | 0 | 0 | 2 | 0 | 16 | 2 | 42 | 0 |
| chinese | 1 | 0 | 0 | 0 | 0 | 82 | 1 | 0 |
| food | 15 | 0 | 15 | 0 | 1 | 4 | 0 | 0 |
| lunch | 2 | 0 | 0 | 0 | 0 | 1 | 0 | 0 |
| spend | 1 | 0 | 1 | 0 | 0 | 0 | 0 | 0 |
Most cells are zero — and this matrix was hand-picked to cohere; a random set of
eight words would be sparser still. Dividing each row by that word's unigram count
(for these rows: i , want , to , eat , chinese ,
food , lunch , spend ) turns the counts into the bigram
probabilities :
| i | want | to | eat | chinese | food | lunch | spend | |
|---|---|---|---|---|---|---|---|---|
| i | 0.002 | 0.33 | 0 | 0.0036 | 0 | 0 | 0 | 0.00079 |
| want | 0.0022 | 0 | 0.66 | 0.0011 | 0.0065 | 0.0065 | 0.0054 | 0.0011 |
| to | 0.00083 | 0 | 0.0017 | 0.28 | 0.00083 | 0 | 0.0025 | 0.087 |
| eat | 0 | 0 | 0.0027 | 0 | 0.021 | 0.0027 | 0.056 | 0 |
| chinese | 0.0063 | 0 | 0 | 0 | 0 | 0.52 | 0.0063 | 0 |
| food | 0.014 | 0 | 0.014 | 0 | 0.00092 | 0.0037 | 0 | 0 |
| lunch | 0.0059 | 0 | 0 | 0 | 0 | 0.0029 | 0 | 0 |
| spend | 0.0036 | 0 | 0.0036 | 0 | 0 | 0 | 0 | 0 |
With a few extra probabilities — , , — the model scores a whole sentence by multiplying the appropriate bigrams:
Some of these numbers are syntactic (whatever follows to tends to be a verb;
), some reflect the task (people starting
sentences with i), and some are cultural (chinese food more probable than english).
The bigram model has absorbed all three from counts alone.
The next-word distribution
Fix a context and a bigram model induces a probability distribution over the whole vocabulary for what comes next. Most of the mass concentrates on a handful of words and the long tail is near zero. That distribution is the model's prediction.
<s> i want. Probability mass concentrates on a few continuations (to, a, some) and is near zero on the rest; heights are the conditional probabilities .Evaluating a language model: perplexity
How good is a model? The gold standard is extrinsic evaluation: embed the model in an application — a speech recognizer, a translator — and measure whether the application improves.6 That is the only test that certifies a real gain, but it is slow and expensive. For quick iteration we want an intrinsic metric that scores the model on its own, independent of any application.
The setup is the usual one. Split the data into a training set, a development set for tuning, and a held-out test set the model never sees during training. The better of two models is the one that assigns higher probability to the test set: a model that predicts the test data more tightly fits it better. (Letting test sentences leak into training inflates this number dishonestly — never train on the test set.)
Rather than raw probability, the standard intrinsic metric is perplexity, the inverse probability of the test set normalized by the number of words. For a test set ,
Expanding the joint probability by the chain rule, and then applying the bigram approximation, gives the forms actually computed:
The exponent (the -th root) normalizes for length so that longer test sets
do not automatically look worse; counts every token including the end-of-sentence
marker </s> but not the start marker <s>.
Perplexity as a branching factor
There is a second, more intuitive reading: perplexity is the weighted average branching factor of the language — roughly, how many words are plausible continuations under the model at each step.7 Consider a toy language of the ten digits, each equally likely, . For any test string of digits,
The perplexity is exactly — the true branching factor, since any of ten digits is
equally possible next. Now make the digit 0 far more frequent than the rest. The
branching factor is still nominally ten, but a good model that has learned 0 is
common will predict it confidently, so the weighted branching factor — the
perplexity — drops below ten. Perplexity rewards a model for concentrating its
probability on the words that actually occur.
The figure contrasts a flat next-word distribution (high perplexity, many equally likely continuations) with a peaked one (low perplexity, the model commits).
The effect shows up when comparing model orders. Trained on million words of the Wall Street Journal and tested on million held-out words, higher-order n-grams carry more context and reach lower perplexity:
| Model | Unigram | Bigram | Trigram |
|---|---|---|---|
| Perplexity | 962 | 170 | 109 |
Two cautions. Perplexity is only comparable across models that share the same vocabulary — shrink the vocabulary and perplexity drops artificially. And an intrinsic gain in perplexity is only a proxy; it usually tracks extrinsic quality but should be confirmed by a real end-to-end evaluation before you trust it.
Where part one leads
The n-gram model so far: factor a sequence by the chain rule, truncate the history with the Markov assumption, estimate each conditional by counting, and score with perplexity. This procedure breaks as soon as the test set contains a word sequence the training corpus never included: the count is zero, the probability is zero, and perplexity is undefined. Smoothing and Backoff repairs those zeros by moving probability mass from seen events to unseen ones, works Kneser-Ney by hand, and ends with the neural models that avoid the zero problem entirely.
Footnotes
- Jurafsky & Martin, Speech and Language Processing (3rd ed.), Ch. 3 — N-gram Language Models: models that assign probabilities to word sequences are language models; the n-gram is the simplest, and a foundational tool for the concepts that carry into neural models. ↩
- Jurafsky & Martin, §3.1 — N-Grams: estimating from relative frequency counts, and why even a web-sized corpus is too sparse to count long histories directly. ↩
- Jurafsky & Martin, §3.1 — the Markov assumption and the n-gram approximation , generalizing the bigram to arbitrary order. ↩
- Jurafsky & Martin, §3.1 — maximum likelihood estimation of n-gram parameters as normalized counts, worked on the Sam/ham mini-corpus and the Berkeley Restaurant Project. ↩
- Jurafsky & Martin, §3.1, Figs. 3.1–3.2 — bigram counts and normalized probabilities for eight words of the Berkeley Restaurant Project corpus ( sentences, ). ↩
- Jurafsky & Martin, §3.2 — Evaluating Language Models: extrinsic vs. intrinsic evaluation, the train/dev/test split, and why the better model assigns higher probability to a held-out test set. ↩
- Jurafsky & Martin, §3.2.1 — Perplexity: the inverse test-set probability normalized by length, and its reading as the weighted average branching factor of the language. ↩
╌╌ END ╌╌