Skip to main content
Core Pattern · Interactive Simhard•DECEMBER 2026•20 MIN

Hidden Markov Models (HMM): Generative Sequences, Viterbi & Baum-Welch

Deep-dive into Hidden Markov Models: how row-stochastic initial, transition, and emission probabilities define P(O, Q), how Forward-Backward computes data likelihood P(O | λ), and how Baum-Welch EM learns without labels.

generative sequence modelmarkov chainviterbi decodingforward-backwardbaum-welch emclassical mlnlpgraphical modelstime series

1. Problem & Generative Framing

A Hidden Markov Model (HMM) is the sequence generalization of Naive Bayes—just as a Linear-Chain CRF is the sequence generalization of Logistic Regression.

An HMM models the joint probability P(O, Q | λ) of an observed sequence O = (o_1, ..., o_T) and a hidden state sequence Q = (q_1, ..., q_T) using three locally normalized probability tables λ = (π, A, B):

  1. Initial state distribution π: π_s = P(q_1 = s) (Σ_s π_s = 1).
  2. Transition matrix A: A_{ij} = P(q_t = j | q_{t-1} = i) (every row sums to 1.0).
  3. Emission matrix B: B_{s, v} = P(o_t = v | q_t = s) (every row sums to 1.0).
P(O, Q | λ) = π_{q_1} · B_{q_1, o_1} · Π_{t=2..T} [ A_{q_{t-1}, q_t} · B_{q_t, o_t} ]

2. Scoring & Probability Matrices

Because probabilities multiply along a path, HMM decoding works in log-space (log π + log B + Σ (log A + log B)) to prevent floating-point underflow.

Interactive HMM Matrices & Log-Viterbi Trellis

Click any node in the trellis (or any cell in Emission B) to inspect the exact log δ_t(s) candidates and see how transition probabilities A resolve ambiguous words like "flies".

LOADING HMM MATRICES & VITERBI TRELLIS...

3. From Joint Probabilities to P(O | λ) & Posterior Smoothing

To evaluate how likely an unlabeled observation sequence O is under the model (P(O | λ)), we must marginalize out all K^T hidden state sequences Q:

P(O | λ) = Σ_Q P(O, Q | λ)

Just like CRF's Z(x), brute-force summation is O(K^T), so HMMs use the Forward (α) and Backward (β) passes in O(T · K^2):

Forward:
  α_1(s) = π_s · B_{s, o_1}
  α_t(s) = [ Σ_{i} α_{t-1}(i) · A_{i, s} ] · B_{s, o_t}
  P(O | λ) = Σ_s α_T(s)

Backward:
  β_T(s) = 1
  β_t(s) = Σ_j A_{s, j} · B_{j, o_{t+1}} · β_{t+1}(j)

Posterior State Marginal (Smoothing):
  γ_t(s) = P(q_t = s | O, λ) = α_t(s) · β_t(s) / P(O | λ)

Interactive Forward-Backward Posterior Explorer

Click any (token, state) cell to inspect α_t(s), β_t(s), P(O | λ), and the smoothed posterior γ_t(s).

LOADING HMM FORWARD-BACKWARD POSTERIORS...

4. Inference & Decoding: Viterbi vs. Posterior Decoding

There are two ways to decode states from an HMM:

  • Viterbi (argmax_Q P(Q | O, λ)): Finds the single globally consistent path Q* in O(T · K^2) using max and backpointers. Guarantees no illegal zero-probability transitions (A_{ij} = 0) appear in Q*.
  • Posterior Decoding (argmax_s γ_t(s) per step): Maximizes expected per-token accuracy, which can occasionally stitch together two states across a zero-probability transition.

5. Training & Optimization: Baum-Welch (EM)

When training data has labeled state sequences (O, Q), HMM training is trivial: just count transitions and emissions and normalize each row!

When training data is unlabeled (only observations O are known), we cannot count transitions directly because Q is hidden. Baum-Welch applies Expectation-Maximization (EM):

  1. E-Step: Run Forward-Backward with current (π, A, B) to compute expected state occupancies γ_t(s) and expected transition counts ξ_t(i, j) = P(q_t = i, q_{t+1} = j | O, λ).
  2. M-Step: Re-normalize expected counts into updated probabilities:
    A'_{i, j} = Σ_{t=1..T-1} ξ_t(i, j) / Σ_{t=1..T-1} γ_t(i)
  3. Guarantee: Every EM iteration is guaranteed to monotonically increase (or hold) log P(O | λ) until reaching a local maximum (unlike CRF's convex NLL).

Interactive Baum-Welch (EM) Training Lab

Step through EM iterations 0 → 10 starting from a near-uniform transition matrix A and watch log P(O | λ) climb monotonically as A sharpens.

LOADING HMM BAUM-WELCH EM TRAINING...

6. Comparison & Failure Modes

DimensionHMM (Generative Sequence)Linear-Chain CRF (Discriminative Sequence)
Distribution modeledJoint P(O, Q)Conditional P(Q | O)
NormalizationLocal per-row (Σ_j A_{ij} = 1, Σ_v B_{sv} = 1)Global Z(x) over entire sequence
Overlapping input featuresAwkward (independence assumption on o_t)Native (w · f(y_t, x, t))
Unsupervised trainingYes (Baum-Welch EM)Hard (requires marginalizing latent Q + Z)
ConvexityNon-convex under EM (local optima)Strictly convex under L2-regularized NLL

Common Failure Modes

  • Local optima in Baum-Welch: Because EM is non-convex, initialization matters—uniform or poorly seeded A, B can get stuck in symmetry or poor state alignments.
  • Underflow on long sequences: Always scale α_t at each step (or work in log-space with logSumExp) for sequences longer than ~20 tokens.
  • Zero-probability emissions: Unseen vocabulary tokens in test sequences cause B_{s, v} = 0 unless smoothed (Dirichlet/Laplace).

Related Lab Entries