Hidden Markov Models
Suppose you see the word sequence "time flies like an arrow." You must tag each word with its part of speech. The word "flies" could be a noun (insects) or a verb (moving quickly). The correct choice depends on invisible grammatical structure you cannot observe directly. You can only observe the words themselves. This is the central problem a Hidden Markov Model (HMM) solves. It recovers a hidden sequence of states from a visible sequence of observations. It uses probability to arbitrate ambiguity. HMMs formalize an idea that is everywhere in real systems. The data we observe is a noisy or indirect signal. It reflects some underlying process we actually care about. Speech recognizers observe acoustic frames but want phonemes. Part-of-speech taggers observe words but want syntactic categories. Bioinformatics pipelines observe DNA base pairs but want gene/intergenic boundaries. Before neural sequence models dominated, HMMs were the workhorse of statistical Part-of-Speech Tagging, speech recognition, and gene finding. They remain the cleanest illustration of generative sequence modeling with latent structure. This is a template that reappears in conditional random fields, structured perceptrons, and even the attention patterns of modern sequence-to-sequence models. The big picture is this: an HMM assumes a hidden Markov chain wanders through states over time. At each time step it emits a visible symbol. That symbol's distribution depends only on the current hidden state. Two probability tables define this process: transitions between states and emissions from states. Together with an initial-state distribution, they define a complete generative story for how observed sequences arise. Once you have that story, three classic questions become well-posed and computable. How likely is this observed sequence? What is the single best hidden path that explains it? How do we learn the transition and emission probabilities from data? This article builds the HMM from its independence assumptions upward. It gives exact pseudocode for the forward algorithm (probability of an observation sequence). It lays the groundwork for the decoding and learning algorithms that build directly on it. It ends with the historical context that connects Markov's original chains to modern statistical NLP. It previews how The Viterbi Algorithm and applications like Named-Entity Recognition extend these ideas.
- An HMM is a generative model: a hidden Markov chain of states emits observable symbols at each time step.
- Two independence assumptions make it tractable: the Markov assumption (states) and the output-independence assumption (emissions).
- Three parameter tables define it: initial state probabilities, transition probabilities, and emission probabilities.
- The forward algorithm computes the likelihood of an observation sequence in time via dynamic programming, not brute-force .
- Decoding (finding the best hidden path) and learning (estimating parameters from data) reuse the same trellis structure, previewing the Viterbi algorithm and Baum-Welch/EM.
- HMMs power classical part-of-speech tagging, speech recognition, and named-entity recognition, and remain a conceptual scaffold for later sequence models.
An HMM models an observed sequence as generated by a hidden Markov chain whose current state alone determines both the next state and the current emission, and dynamic programming exploits this locality to make otherwise exponential computations run in polynomial time.
The idea
A Hidden Markov Model is a probabilistic generative model for sequences in which an unobserved (hidden) sequence of discrete states evolves over time, and each state produces a visible observation . The model is specified by three components: a set of states with an initial distribution over which state starts the chain; a transition matrix where gives the probability of moving from state to state ; and an emission matrix where gives the probability that state emits observation . Together fully specifies the model. Two independence assumptions make the model tractable and give it its name. The Markov assumption says the probability of the next state depends only on the current state, not the entire history: . This is the same first-order Markov property used in N-gram Language Models, just applied to a hidden chain rather than an observed one. The output independence assumption says the current observation depends only on the current hidden state, not on neighboring observations or states: . These two assumptions factor an otherwise intractable joint distribution over long sequences into a product of small local terms, which is exactly what makes exact dynamic-programming algorithms possible. It helps to contrast an HMM with the tools you already know. A n-gram model treats the observed words themselves as a Markov chain — there's no hidden layer. Naive Bayes treats a document's words as conditionally independent given a single global class label, with no notion of a sequence of latent states evolving over time. The HMM generalizes both: it keeps a Naive-Bayes-style emission model (observations depend only on a hidden category) but replaces the single static label with a whole sequence of labels linked by a Markov chain — precisely the structure needed for tagging problems like Part-of-Speech Tagging, where the hidden "class" of a word (its tag) depends on the tag before it.
How it works
An HMM answers three canonical questions, traditionally called the "three problems" of HMMs (Rabiner's formulation): 1. Likelihood (evaluation). Given a model and an observation sequence , compute . Solved by the forward algorithm. 2. Decoding. Given and , find the most probable hidden state sequence . Solved by the Viterbi algorithm (the subject of its own dedicated treatment). 3. Learning. Given only observation sequences (and a fixed state set), estimate . Solved by Baum-Welch, an instance of Expectation-Maximization, which itself leans on the forward and a companion backward algorithm. This article works through the first problem in full detail, since it introduces the trellis structure and the recurrence that both later algorithms reuse almost verbatim.
Why brute force fails. The literal definition of likelihood is a sum over every possible hidden path:
There are possible hidden sequences of length over states, so naively evaluating every term costs time — exponential in the sequence length. For a part-of-speech tagger with 45 tags and a 20-word sentence, that is terms: computationally impossible.
The forward algorithm's key insight. Define the forward variable
the probability of seeing the observations up to time and being in state at time , summed over all the ways of getting there. Because of the Markov and output-independence assumptions, can be computed recursively from alone — you never need to remember the full path, only the total probability mass funneling into each state at each time step. This is the dynamic-programming insight: overlapping subproblems (many paths share a common prefix) are computed once and reused, collapsing paths into a table of size . The recurrence:
``` ALGORITHM Forward(observations O[1..T], states S[1..N], A, B, pi) // A[i][j] = P(state j | state i); B[j][o] = P(observation o | state j) // alpha[t][j] = P(o_1..o_t, q_t = s_j | lambda) alpha <- array of size (T x N), initialized to 0 // --- Initialization step (t = 1) --- for j <- 1 to N: alpha[1][j] <- pi[j] B[j][ O[1] ] // --- Recursion step (t = 2 .. T) --- for t <- 2 to T: for j <- 1 to N: sum <- 0 for i <- 1 to N: sum <- sum + alpha[t-1][i] A[i][j] alpha[t][j] <- sum B[j][ O[t] ] // --- Termination step --- total <- 0 for j <- 1 to N: total <- total + alpha[T][j] return total, alpha // total = P(O | lambda) ``` Each cell `alpha[t][j]` is filled using only the previous column `alpha[t-1][]`, so the table can be built left to right, column by column — the same "fill a table using only already-filled entries" discipline used in edit distance and matrix chain multiplication. A companion backward algorithm defines via an analogous recursion run right-to-left; and together are what Baum-Welch needs to estimate transition and emission counts during learning.
Complexity & correctness
Time complexity. The forward algorithm fills a table of columns and rows. Computing each cell `alpha[t][j]` requires summing over predecessor states, so each cell costs work. Total cost:
This replaces the brute-force with a polynomial-time algorithm — for tags and words, roughly operations instead of . This exponential-to-polynomial reduction is the entire reason HMMs were usable at scale for real corpora.
Space complexity. Storing the full table is . If only the final probability is needed (not the full table, e.g. for decoding), a rolling pair of columns suffices, reducing space to — though the full table is normally kept because Viterbi and Baum-Welch need it.
Correctness argument. The proof is an induction on that formalizes the DP invariant. Claim: `alpha[t][j]` as computed by the recurrence equals . Base case (): by the chain rule and the output-independence assumption. This matches the initialization step exactly. Inductive step: assume `alpha[t-1][i]` correctly equals for every state . Partition the event by which state occupied at the previous step (the law of total probability over a partition of the sample space):
The middle factor uses the Markov assumption (the transition to depends only on , not the earlier observations or states), and the last factor uses the output-independence assumption (the emission depends only on ). Substituting and for these factors, and for the first factor (true by the inductive hypothesis), yields exactly the recurrence used by the algorithm:
Termination: , again by total probability over the final hidden state — exactly what the termination step computes. This completes the induction: every cell of the table is provably the quantity it claims to be, and no probability mass from any hidden path is double-counted or omitted, because the partition at each step is over a mutually exclusive, exhaustive set of predecessor states.
Honest tradeoffs. The polynomial-time gain is bought entirely by the Markov and output-independence assumptions; if a domain genuinely needs longer-range dependencies (a word's tag depending on a tag three positions back, or an emission depending on the surrounding words as in log-linear taggers), the plain HMM is a biased model — it will assign probability incorrectly, no matter how much data it sees. Discriminative alternatives (CRFs, neural taggers) relax these independence assumptions at the cost of losing the simple closed-form generative story and the exact EM-style unsupervised learning HMMs support.
Worked example
Consider a toy weather-inference HMM (the classic ice-cream illustration), reused here for tagging-flavored intuition: hidden states are `{HOT, COLD}`, and the observation each day is the number of ice creams eaten: `{1, 2, 3}`. Parameters:
| HOT | COLD | |
|---|---|---|
| (initial) | 0.8 | 0.2 |
| → HOT | → COLD | |
|---|---|---|
| HOT | 0.7 | 0.3 |
| COLD | 0.4 | 0.6 |
| 1 | 2 | 3 | |
|---|---|---|---|
| HOT | 0.2 | 0.4 | 0.4 |
| COLD | 0.5 | 0.4 | 0.1 |
Observation sequence: — three ice creams, then one.
Step , :
Step , :
Termination:
A trace table makes the DP structure explicit:
| 1 | 3 | 0.3200 | 0.0200 |
| 2 | 1 | 0.0464 | 0.0540 |
Notice that although observing "3 ice creams" makes day 1 look overwhelmingly HOT (), by day 2 — after observing only "1 ice cream" — the COLD path has caught up and overtaken the HOT path (). This is exactly the ambiguity-resolving behavior an HMM is built for: no single day's observation determines the hidden state; the whole sequence, weighted through transition probabilities, does. Decoding (finding the single best hidden path, rather than summing over all of them) is precisely what the forthcoming The Viterbi Algorithm computes, by replacing each sum in the recurrence above with a max.
Common pitfalls & traps
| Pitfall | Why it happens | Fix |
|---|---|---|
| Confusing forward probability with the best-path probability | sums over all paths reaching ; it is not the probability of the single best path | Use Viterbi's max-based recurrence when you need the single best hidden sequence, not the forward algorithm |
| Zero-probability emissions from unseen words/symbols | Maximum-likelihood emission estimates assign probability 0 to unseen observation symbols, collapsing the whole sequence probability to 0 | Apply smoothing (e.g., add-one, Good-Turing, or unknown-word models) to , exactly as in n-gram smoothing |
| Underflow from multiplying many small probabilities | shrinks geometrically with ; for long sequences it underflows floating point | Work in log-space (log-sum-exp for the summation step) or rescale columns at each step |
| Treating / rows as unnormalized counts | Forgetting to normalize transition/emission counts into probabilities during parameter estimation | Always divide raw counts by the correct denominator (row sums) before use |
| Assuming a first-order HMM captures all context needed | Real tag dependencies (e.g. "to VB" vs "to NN") sometimes span more than one previous state | Consider a higher-order (trigram) HMM or a richer feature-based model such as a CRF |
Pioneers & history
Origins · 1 minAndrey Markov and how this was discovered
Markov developed the theory of chains of dependent random variables in the early 1900s, originally to model letter sequences in Pushkin's poetry — an early, informal ancestor of statistical language modeling. His name now labels the "memoryless" independence assumption at the heart of every HMM: the future depends on the present, not the full past.
Rabiner's 1989 tutorial on HMMs for speech recognition became the standard reference that popularized the "three problems" framing (evaluation, decoding, learning) used throughout this article, cementing forward-backward and Viterbi as the canonical algorithmic pair for any hidden Markov process.
Where this matters
HMMs were the backbone of statistical Part-of-Speech Tagging for decades: tag sequences form the hidden Markov chain, and words are the emissions, with the forward and Viterbi algorithms giving both sequence probabilities and best-tag-sequence decoding. In speech recognition, HMMs (often combined with Gaussian mixture models or, later, neural acoustic models) modeled phoneme sequences as hidden states emitting acoustic feature vectors, forming the core of systems from the 1980s through the early 2010s before end-to-end neural architectures took over. In bioinformatics, profile HMMs detect gene structures and protein family membership by treating coding/non-coding regions as hidden states emitting nucleotide or amino-acid sequences. HMMs are also the direct conceptual predecessor of conditional random fields, which drop the generative/independence assumptions but keep the same trellis-and-dynamic-programming machinery, and they remain the standard first example taught alongside Named-Entity Recognition pipelines, where hidden states correspond to entity tags (e.g., `B-PER`, `I-ORG`, `O`) emitting the observed tokens. Even in the era of large neural sequence models, the HMM's clean separation of transition dynamics and emission likelihoods, and its exact polynomial-time inference algorithms, remain the clearest teaching example of how structured probabilistic assumptions convert an exponential search problem into a tractable one.
Ask the tutor: How does the Viterbi algorithm's max-based recurrence differ in its correctness proof from the forward algorithm's sum-based one?
What's the difference between a Markov chain and a Hidden Markov Model?
A plain Markov chain models a sequence of observed states directly — the states themselves are what you see, as in an n-gram model over word sequences. An HMM adds a layer of indirection: the Markov chain governs hidden states you never observe directly, and each hidden state stochastically emits a visible observation via the emission matrix . This extra layer is exactly what lets HMMs model problems like part-of-speech tagging, where you observe words but want to infer unobserved grammatical tags, or speech recognition, where you observe acoustic signals but want unobserved phonemes.
Why can't we just use Naive Bayes instead of an HMM for tagging?
Naive Bayes assigns a single class label to an entire document based on conditionally independent features, with no notion of a sequence of labels influencing one another. An HMM generalizes this by chaining together many "Naive-Bayes-like" emission decisions, one per time step, and linking the hidden labels themselves with a Markov chain. This lets an HMM capture the fact that adjacent tags are correlated (a determiner is almost always followed by a noun or adjective), which plain Naive Bayes has no mechanism to represent at all.
What exactly does the forward algorithm compute, and why do we need it?
It computes , the total probability that the model assigns to an observed sequence, summed over every possible hidden path consistent with it. This quantity is needed to compare or evaluate models (e.g., picking the HMM parameters, among several candidates, that best explain training data) and as a computational building block inside the Baum-Welch learning algorithm, which needs both forward and backward probabilities to re-estimate transition and emission counts.
Is the forward algorithm the same as the Viterbi algorithm?
No, though they share almost identical recurrence structure. The forward algorithm sums over all incoming paths at each trellis cell to compute a total probability; the Viterbi algorithm replaces that sum with a max and keeps backpointers, so it finds the single most probable hidden state sequence rather than the total probability mass over all sequences. Both run in time using the same dynamic-programming trellis, but they answer different questions (evaluation vs. decoding).
How are the transition and emission probabilities actually learned?
If labeled training data is available (e.g., a corpus where every word already has its correct part-of-speech tag), and can be estimated directly by counting: is the relative frequency of tag following tag , and is the relative frequency of word given tag , both typically smoothed to avoid zero probabilities. When no labeled tags are available, the Baum-Welch algorithm (a special case of Expectation-Maximization) iteratively re-estimates and using forward and backward probabilities computed under the current parameter guess, provably increasing the data likelihood at every iteration until convergence to a local optimum.
What are the main limitations of HMMs compared to modern neural sequence models?
HMMs assume a rigid first-order Markov dependency among hidden states and that each observation depends only on its own hidden state, ignoring surrounding context and arbitrary overlapping features. Neural sequence models and conditional random fields relax these assumptions, allowing rich, overlapping, and long-range features to influence predictions, generally yielding higher accuracy on complex tagging and recognition tasks. HMMs remain valuable, however, for their transparency, small parameter count, exact and efficient inference, and ability to learn from unlabeled data via EM, which neural models often cannot do as cleanly.
Why is the Markov assumption called an "assumption" rather than a fact — does it hurt accuracy?
It is an assumption because real language and real hidden processes often violate strict first-order dependence — a word's correct tag can depend on context several tokens away, not just the immediately preceding tag. This is a genuine source of model bias: no amount of data fixes an HMM's blindness to dependencies its structure cannot express. In practice this cost is often acceptable given the enormous efficiency gain from polynomial-time inference, and richer variants (higher-order HMMs, or discriminative models like CRFs) exist precisely to relax this assumption when accuracy demands it.