Hidden Markov models for partially observed sequences

← Back to Blog & Articles

A Hidden Markov Model (HMM) describes a sequence of observations generated by an unobserved (hidden) Markov chain of states.

It is defined by an initial state distribution π, a transition matrix A (A_ij = P(state_t=j | state_{t-1}=i)), and an emission distribution B (B_i(o) = P(observation=o | state=i)). HMMs underlie part-of-speech tagging, speech recognition, and gene sequence analysis — any task where an underlying discrete process is only observed indirectly.

The forward algorithm computes P(o_1, ..., o_T) — the probability of an observation sequence — efficiently via dynamic programming. Define α_t(i) = P(o_1, ..., o_t, state_t = i). Then:

α_1(i) = π_i B_i(o_1)

α_t(j) = [Σ_i α_{t-1}(i) A_ij] B_j(o_t)

Summing α_T(i) over all i gives P(o_1, ..., o_T) in O(T·N²) time (N states), versus the O(N^T) cost of naively summing over every possible hidden state sequence — the same dynamic-programming principle (reusing overlapping sub-computations) that makes A* and value iteration tractable.

The Viterbi algorithm finds the single most likely hidden state sequence, replacing the forward algorithm's sum with a max: δ_t(j) = max_i [δ_{t-1}(i) A_ij] B_j(o_t), tracking backpointers to reconstruct the optimal path — structurally identical to the forward algorithm with sum replaced by max, a substitution that recurs throughout dynamic programming (compare to how UCS becomes A* by adding a heuristic, or how sum-product and max-product belief propagation share one derivation).

Continue with the book

Explore the full treatment, worked examples, and exercises in The AI Algorithms Compendium.


Companion article for The AI Algorithms Compendium, published by Choral Software. Article text and original examples © 2026 Choral Software. All rights reserved.

Scroll to Top