On this page
From a windowless exam hall you can't see the weather, but you can see whether the invigilator carries a wet umbrella. That is a hidden Markov model: the thing you care about is hidden, and you see only its clues. Below we build one small model, fill its forward table by hand, and find the most likely hidden path.
What is a hidden Markov model?
A hidden Markov model (HMM) is a Markov chain over hidden states in which each state emits an observation you can see, as defined in Appendix A of Jurafsky and Martin's Speech and Language Processing. It has four parts:
a set of N hidden states;
a transition matrix A, where a(i, j) is the chance of moving from state i to state j;
emission probabilities b(j, o), the chance that state j produces observation o;
a starting distribution π over the states.
Tomorrow's state depends only on today's, and each observation depends only on the state that produced it. New to Markov chains? Start with solving for a Markov chain's stationary distribution.
The appendix credits Lawrence Rabiner's 1989 tutorial with framing three classic problems: likelihood (how probable are these observations?), decoding (which hidden states explain them best?) and learning (what should A and B be?). The forward algorithm solves the first.
A worked example: weather and an umbrella
These numbers are our own.
Hidden states: Rainy (R) and Sunny (S). Start: 0.5 each.
Transitions: the weather stays the same with probability 0.7 and switches with probability 0.3.
Emissions: on a rainy day the umbrella appears with probability 0.9; on a sunny day, 0.2.
Observations over three days: umbrella, umbrella, no umbrella.
How does the forward algorithm compute P(observations)?
Let α(t, j) be the probability of seeing the first t observations and being in state j at time t. In the recursion from Jurafsky and Martin, you multiply each previous α by the transition into j, add, then multiply by j's emission probability for today's observation.
Day 1 (umbrella): α(1, R) = 0.5 × 0.9 = 0.45. α(1, S) = 0.5 × 0.2 = 0.10.
Day 2 (umbrella): α(2, R) = (0.45 × 0.7 + 0.10 × 0.3) × 0.9 = 0.345 × 0.9 = 0.3105. α(2, S) = (0.45 × 0.3 + 0.10 × 0.7) × 0.2 = 0.205 × 0.2 = 0.041.
Day 3 (no umbrella, so emissions 0.1 for R and 0.8 for S): α(3, R) = (0.3105 × 0.7 + 0.041 × 0.3) × 0.1 = 0.022965. α(3, S) = (0.3105 × 0.3 + 0.041 × 0.7) × 0.8 = 0.09748.
Add the last column: P(observations) = 0.022965 + 0.09748 = 0.120445, about 12%.
Check it the slow way: add the probabilities of all eight weather paths (RRR, RRS and so on) and you get the same 0.120445.
Why is the forward algorithm O(N²T) and not exponential?
With N hidden states and T observations there are N^T possible hidden sequences, and the forward algorithm replaces that sum with an O(N²T) dynamic-programming table, as Jurafsky and Martin explain.
The saving comes from sharing: every path that is Rainy on day 2 continues identically, so one cell stands for all of them. Each cell needs N products, each column has N cells, and there are T columns.
Scale up: with 2 states and 30 days, listing paths means 2^30, which is 1,073,741,824 sequences. The forward table needs on the order of 2 × 2 × 30 = 120 multiply-and-add steps.
How do you find the most likely sequence of hidden states?
This is decoding, solved by the Viterbi algorithm, which Andrew Viterbi published in 1967 as a decoding algorithm for convolutional codes. It fills the same table but takes the maximum instead of the sum, and remembers which earlier state gave it.
Day 1: R = 0.45, S = 0.10.
Day 2: R = max(0.45 × 0.7, 0.10 × 0.3) × 0.9 = 0.2835, from R. S = max(0.45 × 0.3, 0.10 × 0.7) × 0.2 = 0.027, from R.
Day 3: R = 0.2835 × 0.7 × 0.1 = 0.019845, from R. S = 0.2835 × 0.3 × 0.8 = 0.06804, from R.
The best final cell is S at 0.06804. Follow the pointers back: Rainy, Rainy, Sunny.
That one path carries 0.06804 of the total 0.120445, roughly 56%; the other seven paths share the rest. The forward total and the Viterbi best answer different questions: use the sum to ask how likely the observations are, and the max to ask what most probably happened.
How should you practise HMMs?
Redo the table for no umbrella, umbrella, umbrella.
Change one emission probability and predict which way the total moves before computing it.
Revise the law of total expectation, which uses the same "sum over hidden cases" reasoning.
If Markov chains or dynamic programming still feel shaky, the Fermi Concept Library gives each concept in mathematics and computing its own lesson and its place in the map, so you can fill the gap before returning to the trellis.



