Hidden Markov Models › The HMM Framework

Michael BrenndoerferUpdated May 7, 20256 min read
Language AI HandbookAll chapters
Part 6 of 67Sequence Labeling
Chapter 40Hidden Markov Models › The HMM Framework
Reading section 2 of 13 · The HMM Framework · 6 minSection 2 of 13 · 6 min

Book contents

Section 2 of 13 · 6 min read

Hidden Markov Models › The HMM FrameworkOne section · Sections in this chapter2 / 13 ↓
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

An HMM defines a joint probability distribution over two sequences: a hidden state sequence and an observable output sequence. In POS tagging, the hidden states are the part-of-speech tags, and the observations are the words. The hidden states are "hidden" in a literal sense: you never observe them directly. You see only the output words, and everything about the underlying grammar must be inferred from those observations.

Hidden Markov Model

A Hidden Markov Model is a probabilistic sequence model with two components: an unobservable (hidden) state sequence that evolves according to Markov dynamics, and an observable output sequence where each output depends only on the current hidden state. The "hidden" refers to the fact that you cannot directly observe the states, only infer them from the observations.

The model has five components:

  • States (SS): A finite set of hidden states. For POS tagging, these are the tags like NN, VB, DT, JJ. The state set defines the vocabulary of labels the model can assign.
  • Observations (VV): A finite vocabulary of observable symbols. These are the words in your corpus. Every word you will ever encounter at training or test time must belong to this set (with unknown words handled separately by smoothing).
  • Initial probabilities (π\pi): The probability of starting in each state. For sentences, this captures which tags typically begin sentences. English sentences far more often begin with a determiner or noun than with a verb or preposition, and π\pi encodes exactly that regularity.
  • Transition probabilities (AA): The probability of moving from one state to another. This captures tag-to-tag patterns like "determiners are often followed by nouns." The full transition matrix AA encodes a compressed statistical description of English syntax.
  • Emission probabilities (BB): The probability of generating each observation from each state. This captures which words are associated with which tags. The word "the" has very high emission probability from DET and near-zero from VERB; "run" has meaningful probability from both NOUN and VERB. This reflects lexical ambiguity.

The beauty of this decomposition is that each component captures a different kind of linguistic knowledge. The transition matrix captures syntax: how grammatical categories sequence. The emission matrix captures lexical associations: which words belong to which categories. The initial distribution captures positional tendencies: how sentences typically begin. Together they form a compact and learnable representation of a language's statistical structure.

Let's define these components mathematically. For a state sequence s1,s2,…,sns_1, s_2, \ldots, s_n and observation sequence o1,o2,…,ono_1, o_2, \ldots, o_n, we define three probability distributions.

The initial probability captures how likely each state is to appear at the start of a sequence:

πi=P(s1=i)\pi_i = P(s_1 = i)

where πi\pi_i is the probability that the first state in the sequence is state ii.

The transition probability captures how states follow one another:

aij=P(st=j ∣ st−1=i)a_{ij} = P(s_t = j \,|\, s_{t-1} = i)

where:

  • aija_{ij}: the probability of transitioning from state ii to state jj
  • sts_t: the state at position tt in the sequence
  • st−1s_{t-1}: the state at the previous position

The emission probability captures how each state generates observations:

bi(o)=P(ot=o ∣ st=i)b_i(o) = P(o_t = o \,|\, s_t = i)

where:

  • bi(o)b_i(o): the probability of observing symbol oo when in state ii
  • oto_t: the observation at position tt
  • sts_t: the hidden state at position tt

These three distributions fully characterize an HMM's behavior. The initial probabilities determine how sequences start, transitions determine how states evolve, and emissions determine what we observe. Notice that the parameters are not hand-crafted: they are estimated automatically from annotated data. This is what makes HMMs powerful. You do not need a linguist to write grammar rules. You need labeled sentences, and the model learns the statistical regularities on its own.

To make these concepts concrete, let's build a toy HMM for POS tagging. We'll use just three states (determiner, noun, verb) and five words, which is enough to see the key patterns while keeping the numbers tractable.

In[3]:
Code
# A toy HMM for POS tagging with 3 states and 5 words
states = ["DT", "NN", "VB"]  # Determiner, Noun, Verb
vocab = ["the", "dog", "cat", "runs", "sleeps"]

# Initial probabilities: sentences often start with determiners
pi = {"DT": 0.6, "NN": 0.3, "VB": 0.1}

# Transition probabilities
# P(next_state | current_state)
A = {
    "DT": {"DT": 0.1, "NN": 0.8, "VB": 0.1},  # DT usually followed by NN
    "NN": {"DT": 0.1, "NN": 0.3, "VB": 0.6},  # NN often followed by VB
    "VB": {"DT": 0.3, "NN": 0.4, "VB": 0.3},  # VB can lead to various
}

# Emission probabilities
# P(word | state)
B = {
    "DT": {"the": 0.9, "dog": 0.02, "cat": 0.02, "runs": 0.03, "sleeps": 0.03},
    "NN": {"the": 0.01, "dog": 0.45, "cat": 0.45, "runs": 0.05, "sleeps": 0.04},
    "VB": {"the": 0.01, "dog": 0.02, "cat": 0.02, "runs": 0.50, "sleeps": 0.45},
}
Out[4]:
Console
HMM Components for POS Tagging
==================================================

States (Tags): ['DT', 'NN', 'VB']
Vocabulary: ['the', 'dog', 'cat', 'runs', 'sleeps']

Initial Probabilities π:
  P(start with DT) = 0.60
  P(start with NN) = 0.30
  P(start with VB) = 0.10

Sample Transition Probabilities A:
  P(NN | DT) = 0.8  (determiners often precede nouns)
  P(VB | NN) = 0.6  (nouns often precede verbs)

Sample Emission Probabilities B:
  P('the' | DT) = 0.9  (determiners emit 'the' frequently)
  P('dog' | NN) = 0.45  (nouns emit 'dog')
  P('runs' | VB) = 0.5  (verbs emit 'runs')

The transition matrix encodes grammatical patterns. Determiners (DT) are followed by nouns (NN) with probability 0.8, capturing the common English pattern "the dog" or "a cat." The emission matrix encodes lexical associations. The word "the" is emitted by DT with probability 0.9, while "runs" is emitted by VB with probability 0.5.

In practice, these probabilities need not be set by hand. The numbers above are illustrative, but when you train on a large corpus the same patterns emerge automatically from counting. The model "discovers" that determiners precede nouns because the data contains thousands of examples of exactly that pattern. This is maximum likelihood estimation at its simplest: the best description of the data is the one that assigns the highest probability to the observed events.