N-gram Language Models › The Markov Assumption

Michael BrenndoerferMarch 23, 20254 min read
Language AI HandbookAll chapters
Part 2 of 67Classical Text Representations
Chapter 8N-gram Language Models › The Markov Assumption
Reading section 3 of 15 · The Markov Assumption · 4 minSection 3 of 15 · 4 min

Book contents

Section 3 of 15 · 4 min read

N-gram Language Models › The Markov AssumptionOne section · Sections in this chapter3 / 15 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

The chain rule gives us an exact decomposition, but it demands something impractical: reliable estimates of probabilities conditioned on arbitrarily long histories. The key insight of n-gram models is to make a simplifying assumption that trades some accuracy for tractability.

Markov Assumption

The Markov assumption states that the probability of a word depends only on a fixed-length history of preceding words, not the entire sequence. An n-gram model assumes the probability of word wiw_i depends only on the previous n−1n-1 words.

The assumption is named after the Russian mathematician Andrey Markov, who studied stochastic processes with this "memoryless" property in the early 20th century. In a Markov chain, the probability of the next state depends only on the current state, not on the full history of how you arrived there. In the context of language, it means we assume the future depends only on the recent past, not the distant past.

For a unigram model (n=1n = 1), each word is treated as completely independent of all others:

P(wi∣w1,…,wi−1)≈P(wi)P(w_i|w_1, \ldots, w_{i-1}) \approx P(w_i)

The unigram model ignores all context. It assigns the same probability to "cat" whether the preceding words were "the fluffy" or "the entire concept of." This is obviously a poor approximation, but unigrams are useful as a baseline and as a component in more sophisticated models.

For a bigram model (n=2n = 2), each word depends only on the single word immediately before it:

P(wi∣w1,…,wi−1)≈P(wi∣wi−1)P(w_i|w_1, \ldots, w_{i-1}) \approx P(w_i|w_{i-1})

where:

  • P(wi∣w1,…,wi−1)P(w_i|w_1, \ldots, w_{i-1}): the true conditional probability given the full history (left side)
  • P(wi∣wi−1)P(w_i|w_{i-1}): the bigram approximation using only the immediately preceding word (right side)
  • ≈\approx: indicates this is an approximation, not an equality

For a trigram model (n=3n = 3), the memory extends to two previous words:

P(wi∣w1,…,wi−1)≈P(wi∣wi−2,wi−1)P(w_i|w_1, \ldots, w_{i-1}) \approx P(w_i|w_{i-2}, w_{i-1})

where:

  • wi−2w_{i-2}: the word two positions before the current word
  • wi−1w_{i-1}: the word immediately before the current word
  • The context (wi−2,wi−1)(w_{i-2}, w_{i-1}) forms a two-word history window

In general, for an n-gram model, the Markov approximation is:

P(wi∣w1,…,wi−1)≈P(wi∣wi−n+1,…,wi−1)P(w_i|w_1, \ldots, w_{i-1}) \approx P(w_i|w_{i-n+1}, \ldots, w_{i-1})

where the context window spans exactly n−1n-1 preceding words. We discard information from the distant past to make estimation tractable from finite data.

Why does this help? Consider the difference in what we need to estimate for the sentence "the cat sat on the mat":

  • Full history: We need P(mat∣the cat sat on the)P(\text{mat}|\text{the cat sat on the}), requiring counts of the specific five-word context.
  • Trigram: We need P(mat∣on the)P(\text{mat}|\text{on the}), requiring only counts of the two-word context.
  • Bigram: We need P(mat∣the)P(\text{mat}|\text{the}), requiring only counts of the single word "the."

Shorter contexts appear far more frequently in training data, giving us more reliable probability estimates. The Markov assumption is an approximation, but it works surprisingly well in practice because language has enough local structure that nearby words carry most of the predictive information. The word that most immediately constrains "mat" is likely "the" (or "on the"), not the first word five positions back.

In[5]:
Code
# Compare full history vs Markov approximations
sentence = "the cat sat on the mat"
words = sentence.split()

approximations = [
    (
        "Full history (n=6)",
        "P(mat | the cat sat on the)",
        "exact but impractical",
    ),
    ("Trigram (n=3)", "P(mat | on the)", "considers 2 previous words"),
    ("Bigram (n=2)", "P(mat | the)", "considers 1 previous word"),
    ("Unigram (n=1)", "P(mat)", "ignores all context"),
]
Out[6]:
Console
Markov Approximations for the final word 'mat':
-----------------------------------------------------------------

Full history (n=6):
  P(mat | the cat sat on the)
  -> exact but impractical

Trigram (n=3):
  P(mat | on the)
  -> considers 2 previous words

Bigram (n=2):
  P(mat | the)
  -> considers 1 previous word

Unigram (n=1):
  P(mat)
  -> ignores all context