TL;DR
A hidden Markov model (HMM) describes a sequence of observations as the output of a chain of states that you cannot see. The states hop from one to the next with fixed probabilities, and each state produces an observation with its own probabilities. Given the observations, an HMM lets you compute how likely they are and which hidden states most likely produced them.
Builds on: Markov Chain, Bayes' Rule · Leads to: Partially Observable Markov Decision Process, Expectation-Maximization Algorithm
Where it's used
Speech recognition. Early speech recognisers, from the 1970s onward, modelled sounds as hidden phoneme states that emit short slices of audio features. The HMM scored each candidate word sequence by how well it explained the audio, and the best-scoring sequence became the transcript. Rabiner's 1989 tutorial made this approach standard teaching material. Later systems replaced most of this with recurrent networks, but the HMM is still the clearest way to see the idea.
Gene finding and protein families. In bioinformatics, a profile HMM describes a protein family by a chain of positions (match, insert, delete states). Aligning a new sequence to the model shows which positions it fits. Pfam, a large protein-family database, is built on profile HMMs.
Part-of-speech tagging. In early language processing, the hidden states were word categories such as noun or verb, and the observations were the words themselves. The tagger found the most likely category sequence for each sentence.
How it works
Take a toy weather problem. The weather is hidden: each day it is Sunny or Rainy. You cannot see the weather, but you can see whether a person carries an umbrella. Here are the numbers, chosen to be easy to multiply:
- Start: on day 1, Sunny and Rainy are equally likely (0.5 each).
- Weather moves: a Sunny day is followed by Sunny with probability 0.7 and Rainy with 0.3. A Rainy day is followed by Sunny with 0.4 and Rainy with 0.6.
- Umbrella: on a Sunny day the person carries an umbrella with probability 0.1. On a Rainy day, with probability 0.8.
We observe umbrella on day 1 and umbrella on day 2. The question is how likely this is, and which weather is most likely on day 2.
-
Day 1: weigh each hidden state by how well it explains the observation. The weight of a state is its starting probability times the chance of seeing an umbrella in that state. Sunny: 0.5 × 0.1 = 0.05. Rainy: 0.5 × 0.8 = 0.40. Check: Rainy explains the umbrella 8 times better than Sunny does, since 0.40 is eight times 0.05.
-
Day 2: carry the weights across the weather moves. For Sunny on day 2, add up the ways to arrive there: from Sunny (0.05 × 0.7 = 0.035) and from Rainy (0.40 × 0.4 = 0.16). The total is 0.195. Multiply by the chance of an umbrella on a Sunny day: 0.195 × 0.1 = 0.0195. Check: 0.035 + 0.16 = 0.195, and 0.195 × 0.1 = 0.0195.
-
Day 2, Rainy: the same step for the other state. Arrivals at Rainy: from Sunny (0.05 × 0.3 = 0.015) and from Rainy (0.40 × 0.6 = 0.24). The total is 0.255. Multiply by 0.8 for an umbrella on a Rainy day: 0.255 × 0.8 = 0.204. Check: 0.015 + 0.24 = 0.255, and 0.255 × 0.8 = 0.204.
-
Read out the answer. Add the two day-2 weights: 0.0195 + 0.204 = 0.2235. That is the probability of seeing an umbrella on both days, under this model. The larger day-2 weight belongs to Rainy, so Rainy is the more likely hidden state on day 2.
flowchart LR H1["Weather day 1 (hidden)"] --> H2["Weather day 2 (hidden)"] H1 --> O1["Umbrella day 1 (seen)"] H2 --> O2["Umbrella day 2 (seen)"]
The math
An HMM has hidden states , observations drawn from a finite alphabet, and three parts of parameters: the starting probabilities , the transition probabilities (the chance of moving from state to state on the next step), and the emission probabilities (the chance that state produces observation ). The hidden states form a Markov Chain; the observations depend only on the current state.
Step 1 in symbols: the setup
In the weather example, . Let be the observed umbrella events and the hidden state on day . The parameters are , , and , among others.
Step 2 in symbols: the day-1 weights
Define the forward quantity as the probability of the observations up to day together with the hidden state on day . This is a Joint Distribution over the observations and the state. On day 1:
So and .
Step 3 in symbols: the day-2 recursion
Each later weight sums over the ways to arrive in state , then multiplies by the emission. This is the forward recursion:
For Sunny on day 2: . The result is the same as in the walkthrough. The total probability of the observations is .
Step 4 in symbols: the most likely path
Viterbi replaces the sum with a maximum and records the best predecessor. Let be the probability of the most likely state path that ends in state on day :
This is a Dynamic Programming recursion: each day's answer reuses the previous day's answers. In practice the products are added in log space (Logarithm: a sum of logs instead of a product), because multiplying many probabilities below 1 quickly underflows to zero on long sequences.
Step 5 in symbols: learning the parameters
When the parameters are unknown, the Baum-Welch procedure adjusts them to raise the probability of the observations, which is a maximum-likelihood fit. It is an instance of Expectation-Maximization Algorithm, which alternates between estimating the hidden states and refitting the parameters.
Worked examples
On day 1 the umbrella is observed. What is the chance that the weather is Rainy?
- Use the weights from Step 2: Sunny 0.05, Rainy 0.40.
- Add them to get the total evidence: 0.05 + 0.40 = 0.45.
- Divide Rainy by the total: 0.40 / 0.45 ≈ 0.889.
Interpretation: after one umbrella, the posterior probability of rain is about 0.89. This is Bayes' Rule applied to the prior of 0.5 and the evidence of an umbrella.
The same two umbrella days, now asking for the most likely weather on both days (Viterbi).
- Day 1 scores: Sunny , Rainy .
- Day 2, Sunny: the candidate predecessors are Sunny (0.05 × 0.7 = 0.035) and Rainy (0.40 × 0.4 = 0.16). The maximum is 0.16, from Rainy. Multiply by 0.1: .
- Day 2, Rainy: the candidates are Sunny (0.05 × 0.3 = 0.015) and Rainy (0.40 × 0.6 = 0.24). The maximum is 0.24, from Rainy. Multiply by 0.8: .
- The best final state is Rainy (0.192 is larger than 0.016). Following the recorded predecessors back gives Rainy, Rainy.
- Compare with the forward sum from the walkthrough: 0.2235 is the total over all paths, and 0.192 is the single best path.
Interpretation: the most likely weather is Rainy on both days, and that path carries most of the probability.
A speech system uses an HMM with 5 hidden states over 1,000 time frames of audio.
- Brute force scores every possible state path. There are paths, which is about . No computer can add those up.
- The forward algorithm handles each frame by looking at all pairs of states once: multiply-adds per frame.
- Over 1,000 frames that is multiply-adds.
- Viterbi has the same cost, with a maximum instead of a sum.
Interpretation: the recursion turns an impossible sum over paths into a cost that grows linearly with the length of the sequence.
Try it: an HMM has 3 hidden states and a sequence of 500 observations. How many multiply-adds does the forward algorithm need? Answer: .
Common mistakes and tradeoffs
It is tempting to pick the most likely state on each day separately and call that the best answer. In fact the best single path can differ from the best day-by-day choice, because the transitions link the days together. Viterbi finds the best whole path, and the forward sum gives the total probability of the observations.
It is tempting to think the hidden states have real-world meaning just because they are named, like Sunny or Rainy. In fact the states are whatever the parameters make them, and training can produce states that do not match the labels you had in mind. Check the states against data you trust.
It is tempting to think an HMM can remember anything that happened long ago. In fact the next state depends only on the current one, so the model forgets older history except through the current state. This is the Markov assumption, and it is why long-range patterns in language or music are hard for an HMM.
It is tempting to think each observation depends on more than the current hidden state. In fact the model assumes it depends only on that state. When observations depend on earlier observations, a richer model is needed.
When to use an HMM: a small, discrete set of hidden states, a clear sequence structure, and enough data to estimate the tables. When not to use it: long-range dependencies, continuous observations that are hard to discretise, or large state spaces. Alternatives include conditional random fields, which model the labels directly given the observations, and neural sequence models such as recurrent networks and Transformers, which learn long-range dependencies from data.
History
In the late 1960s Leonard Baum and colleagues at the Institute for Defense Analyses studied probabilistic functions of Markov chains, which is the mathematical core of the HMM. In 1967 Andrew Viterbi published the decoding algorithm that bears his name, first for error correction in communication codes. The Baum-Welch method for estimating the parameters followed in the early 1970s. Speech researchers at IBM and Carnegie Mellon adopted the model in the 1970s and 1980s, where it worked far better than hand-built rules. Rabiner's 1989 tutorial explained the three core problems and their solutions clearly, and it made the model widely taught.
Recap
- An HMM has hidden states that follow a Markov Chain and observations that depend only on the current state.
- The forward recursion weighs each state by the observations so far, carrying weights across transitions with the same recursion each day.
- Viterbi replaces the sum with a maximum to find the most likely hidden path.
- Parameters are learned with Expectation-Maximization Algorithm, a close relative of Maximum Likelihood Estimation.
- Read next: Partially Observable Markov Decision Process, where the hidden state is what an agent must infer before acting.
Check yourself
QuestionWhat does the Markov assumption say about an HMM's hidden states?Show answer
The next hidden state depends only on the current hidden state, not on the earlier history. The observation on each day depends only on that day's hidden state.
QuestionAn HMM has start probabilities Sunny 0.5 and Rainy 0.5, and on day 1 the emission for an umbrella is 0.1 in Sunny and 0.8 in Rainy. What is the forward weight for Rainy on day 1?Show answer
The weight is 0.5 × 0.8 = 0.40.
QuestionWhy does the forward algorithm not simply add up every possible hidden path?Show answer
The number of paths grows exponentially with the sequence length, so adding them one by one is impossible for long sequences. The recursion reuses the sums from the previous step, so the cost grows only linearly with the length.
QuestionA model's day-by-day most likely states disagree with the best whole path. Which is the better answer to 'what was the most likely weather sequence', and why?Show answer
The best whole path, found by Viterbi, because the question asks about the whole sequence. Day-by-day choices ignore the transition probabilities that link neighbouring days.