ElemedexA pocket encyclopedia of AI architectures
No. 002

n-gram language model

Markov language model

Word Counter

It never looks more than a couple of words back, and still it writes almost passable English. Its first trainer was a mathematician who opened books at random. Faced with a phrase it has never seen, it declares it impossible, unless someone has taught it to add one.

Height
— (a table of counts)
Weight
from a page of tables to billions of counts
Parameters
up to Vⁿ conditional probabilities
Context
n − 1 words (typically 1–4)
Training data
Shannon: books · Google: 10¹² words (≈ 10⁹ five-grams)
Year
1948
Generation
1 · Cybernetics and symbols
Creators
Claude E. Shannon (Bell Labs), after Andrei A. Markov (1913)
Kind
Model

Base stats

Total
255

Approximate, ordinal scales meant for comparing species with each other; the hard numbers are in the data sheet.

Weaknesses

  • Sparsity: the vast majority of possible sequences never occur in the corpus.
  • Combinatorial explosion: with a vocabulary of V words there are Vⁿ possible contexts.
  • Myopia: it misses dependencies longer than its window, such as long-distance agreement.
  • Each word is an isolated symbol: learning something about “cat” teaches it nothing about “dog”.

Habitat

It was born among letter-frequency tables taken from a cryptography handbook and books opened at random. It spent decades inside speech recognizers, statistical translators and spelling checkers. Today it survives as a baseline, in corpus linguistics, and in the question LLMs keep answering: what comes next?

Moves

Abilities

Markov assumption
The probability of each word depends only on the previous n − 1: the whole past is reduced to a fixed window.
Learning is counting
Probabilities are estimated as relative frequencies in the corpus; there are no weights to tune and no gradients.
Smoothing
Adding pseudocounts (Laplace, Dirichlet and variants) so that an unseen sequence does not get probability zero.

Anatomy

lookupText corpusCount n-gramsNormalize and smoothTable P(w | context)“…the cat sat”Look up the context rowSample the next word“on”TrainingGeneration

A trigram model (n = 3): the context is just the last two words. Training fills the table by counting; generating means looking up the context row, sampling a word from its probabilities, sliding the window one step and repeating.

Evolution chain

002 n-gram language model

An n-gram model is the simplest kind of language model: it estimates the probability of the next word by looking only at the previous n − 1 words, and it does so by counting. It is the direct ancestor of the question we still put to large models: given this fragment, which words are likely to come next? (Shanahan, 2024, p. 70).

How it works

By the chain rule, the probability of a sentence is the product of the probabilities of each word given everything before it. The n-gram model simplifies this with the Markov assumption: only the last n − 1 words matter (a bigram conditions on one, a trigram on two). In the terms of Witten et al. (2017), it is a Markov model of order n − 1 (2017, pp. 403–404).

Training is counting: for a trigram, P(sat | the cat) = C(the cat sat) / C(the cat), the relative frequency in the corpus. To generate, you look up the context row, sample the next word, slide the window one step and repeat: autoregressive generation generation in its barest form. The model is evaluated by its perplexity on unseen text.

Markov, Shannon and a book opened at random

In 1913 Andrei Markov counted by hand the first 20,000 letters of Pushkin's Eugene Onegin to study how vowels and consonants follow one another (Markov, 1913/2006). Thirty-five years later, Claude Shannon carried the idea over to English with a "series of approximations" running from equiprobable letters through digrams, trigrams and, finally, words chosen according to the preceding word (Shannon, 1948, sec. 3). That last order already yields things like "THE HEAD AND IN FRONTAL ATTACK ON AN ENGLISH WRITER THAT THE CHARACTER OF THIS POINT…".

The sampling was done by hand: open a book at random, read until the current letter (or word) turns up, write down the one that follows, and carry on from another page. Shannon observed that going one order further would take an enormous amount of labor (1948, sec. 3), and called these sources "discrete Markoff processes" (1948, sec. 4). Meaning had been left out on purpose: semantic aspects, he writes, are irrelevant to the engineering problem (1948, p. 379).

Limits

The central problem is sparsity. With a vocabulary of V words there are Vⁿ possible sequences, and almost none appear in a real corpus; left uncorrected, a single unseen sequence zeroes out the probability of the whole sentence. Hence smoothing (the simplest scheme, the Laplace estimator, adds one to every count) is critical at higher orders (Witten et al., 2017, pp. 99, 404). The other answer was scale: Google released counts for about a billion five-word sequences drawn from a trillion-word corpus (2017, p. 404).

The second limit is myopia. A bigram model can learn that keys are is more frequent than keys is, but not the agreement in "The keys to the old, wooden kitchen cabinet are on the table," with six words between subject and verb (Mahowald et al., 2024). This was the target of Chomsky (1957): no finite-state model generates English, and statistical approximation does not define grammaticality (colorless green ideas sleep furiously) (1957, chs. 2–3). The second half of the argument aged worse: a class-based bigram model trained on newspaper text gives that sentence about 200,000 times the probability of its reversal (Pereira, 2000), a result Piantadosi (2024) turns against Chomskyan skepticism about probability (2024, pp. 368–369).

Offspring

The third limit is that each word is an isolated symbol. The neural probabilistic language model of Bengio et al. (2003) replaced counts with a network over learned word vectors, so that similarity could spread the evidence around; until then, n-grams had been one of the leading approaches (Alammar & Grootendorst, 2024, p. 88). LLMs inherited Shannon's task and changed almost everything else: unlike an n-gram model, GPT-2 mostly produces sequences of five or more words that do not occur in its training data (Mahowald et al., 2024).

Sources

  • Alammar, J., & Grootendorst, M. (2024). Hands-on large language models: Language understanding and generation. O’Reilly Media. §
  • Bender, E. M., & Koller, A. (2020). Climbing towards NLU: On meaning, form, and understanding in the age of data. In D. Jurafsky, J. Chai, N. Schluter, & J. Tetreault (Eds.), Proceedings of the 58th Annual Meeting of the Association for Computational Linguistics (pp. 5185–5198). Association for Computational Linguistics. https://doi.org/10.18653/v1/2020.acl-main.463 §
  • Bengio, Y., Ducharme, R., Vincent, P., & Jauvin, C. (2003). A neural probabilistic language model. Journal of Machine Learning Research, 3, 1137–1155. https://www.jmlr.org/papers/v3/bengio03a.html §
  • Chomsky, N. (1957). Syntactic structures. Mouton. https://doi.org/10.1515/9783112316009 §
  • Firth, J. R. (1957). A synopsis of linguistic theory, 1930–1955. In Studies in linguistic analysis (pp. 1–32). Blackwell. §
  • Harris, Z. S. (1954). Distributional structure. Word, 10(2–3), 146–162. https://doi.org/10.1080/00437956.1954.11659520 §
  • Mahowald, K., Ivanova, A. A., Blank, I. A., Kanwisher, N., Tenenbaum, J. B., & Fedorenko, E. (2024). Dissociating language and thought in large language models. Trends in Cognitive Sciences, 28(6), 517–540. https://doi.org/10.1016/j.tics.2024.01.011 §
  • Markov, A. A. (2006). An example of statistical investigation of the text Eugene Onegin concerning the connection of samples in chains (G. Custance & D. Link, Trans.). Science in Context, 19(4), 591–600. https://doi.org/10.1017/S0269889706001074 (Original work published 1913) §
  • Mitchell, M. (2019). Artificial intelligence: A guide for thinking humans. Pelican Books. §
  • Pereira, F. (2000). Formal grammar and information theory: Together again? Philosophical Transactions of the Royal Society of London. Series A: Mathematical, Physical and Engineering Sciences, 358(1769), 1239–1253. https://doi.org/10.1098/rsta.2000.0583 §
  • Piantadosi, S. T. (2024). Modern language models refute Chomsky’s approach to language. In E. Gibson & M. Poliak (Eds.), From fieldwork to linguistic theory: A tribute to Dan Everett (pp. 353–414). Language Science Press. https://doi.org/10.5281/zenodo.12665933 §
  • Shanahan, M. (2024). Talking about large language models. Communications of the ACM, 67(2), 68–79. https://doi.org/10.1145/3624724 §
  • Shannon, C. E. (1948). A mathematical theory of communication. Bell System Technical Journal, 27(3), 379–423; 27(4), 623–656. https://doi.org/10.1002/j.1538-7305.1948.tb01338.x §
  • Witten, I. H., Frank, E., Hall, M. A., & Pal, C. J. (2017). Data mining: Practical machine learning tools and techniques (4th ed.). Morgan Kaufmann. https://doi.org/10.1016/C2015-0-02071-8 §