Search

Search papers and lessons

Hidden Markov Model

Level 3 · 10 min read

A hidden Markov model explains a sequence of observations as the output of a hidden chain of states, and lets you work out the most likely states from what you can see.

On this page

  • Colour = topic mix
  • Probability
  • Discrete math
  • Calculus
  • Linear algebra
  • Stochastic processes
  • Reinforcement learning
  • ReadReady to readNot yet

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.

  1. 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.

  2. 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.

  3. 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.

  4. 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

Definition: Hidden Markov model

An HMM has NN hidden states 1,…,N1, \dots, N, observations drawn from a finite alphabet, and three parts of parameters: the starting probabilities πi\pi_i, the transition probabilities aija_{ij} (the chance of moving from state ii to state jj on the next step), and the emission probabilities bj(o)b_j(o) (the chance that state jj produces observation oo). 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, N=2N = 2. Let o1,o2o_1, o_2 be the observed umbrella events and qtq_t the hidden state on day tt. The parameters are πSunny=0.5\pi_{\text{Sunny}} = 0.5, aSunny,Rainy=0.3a_{\text{Sunny},\text{Rainy}} = 0.3, and bRainy(umbrella)=0.8b_{\text{Rainy}}(\text{umbrella}) = 0.8, among others.

Step 2 in symbols: the day-1 weights

Define the forward quantity αt(j)\alpha_t(j) as the probability of the observations up to day tt together with the hidden state jj on day tt. This is a Joint Distribution over the observations and the state. On day 1:

α1(j)=πj bj(o1)\alpha_1(j) = \pi_j \, b_j(o_1)

So α1(Sunny)=0.5⋅0.1=0.05\alpha_1(\text{Sunny}) = 0.5 \cdot 0.1 = 0.05 and α1(Rainy)=0.5⋅0.8=0.40\alpha_1(\text{Rainy}) = 0.5 \cdot 0.8 = 0.40.

Step 3 in symbols: the day-2 recursion

Each later weight sums over the ways to arrive in state jj, then multiplies by the emission. This is the forward recursion:

αt+1(j)=(∑i=1Nαt(i) aij)bj(ot+1)\alpha_{t+1}(j) = \left( \sum_{i=1}^{N} \alpha_t(i)\, a_{ij} \right) b_j(o_{t+1})

For Sunny on day 2: α2(Sunny)=(0.05⋅0.7+0.40⋅0.4)⋅0.1=0.0195\alpha_2(\text{Sunny}) = (0.05 \cdot 0.7 + 0.40 \cdot 0.4) \cdot 0.1 = 0.0195. The result is the same as in the walkthrough. The total probability of the observations is P(o1,o2)=∑jα2(j)=0.0195+0.204=0.2235P(o_1, o_2) = \sum_j \alpha_2(j) = 0.0195 + 0.204 = 0.2235.

Step 4 in symbols: the most likely path

Viterbi replaces the sum with a maximum and records the best predecessor. Let δt(j)\delta_t(j) be the probability of the most likely state path that ends in state jj on day tt:

δt+1(j)=max⁡i(δt(i) aij)bj(ot+1)\delta_{t+1}(j) = \max_{i} \left( \delta_t(i)\, a_{ij} \right) b_j(o_{t+1})

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

Example
1. One day, by Bayes' rule

On day 1 the umbrella is observed. What is the chance that the weather is Rainy?

  1. Use the weights from Step 2: Sunny 0.05, Rainy 0.40.
  2. Add them to get the total evidence: 0.05 + 0.40 = 0.45.
  3. 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.

Example
2. Two days, most likely path

The same two umbrella days, now asking for the most likely weather on both days (Viterbi).

  1. Day 1 scores: Sunny δ1=0.05\delta_1 = 0.05, Rainy δ1=0.40\delta_1 = 0.40.
  2. 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: δ2(Sunny)=0.016\delta_2(\text{Sunny}) = 0.016.
  3. 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: δ2(Rainy)=0.192\delta_2(\text{Rainy}) = 0.192.
  4. The best final state is Rainy (0.192 is larger than 0.016). Following the recorded predecessors back gives Rainy, Rainy.
  5. 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.

Example
3. Realistic scale, and why the forward algorithm matters

A speech system uses an HMM with 5 hidden states over 1,000 time frames of audio.

  1. Brute force scores every possible state path. There are 510005^{1000} paths, which is about 1069910^{699}. No computer can add those up.
  2. The forward algorithm handles each frame by looking at all pairs of states once: N2=25N^2 = 25 multiply-adds per frame.
  3. Over 1,000 frames that is 25×1,000=25,00025 \times 1{,}000 = 25{,}000 multiply-adds.
  4. 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: 32×500=9×500=4,5003^2 \times 500 = 9 \times 500 = 4{,}500.

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

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.

Sources