Skip to content
mlmentorship

Hidden Markov models

A latent Markov chain emits observations through a per-state distribution. Forward-backward, Viterbi, Baum-Welch. The classical sequence model toolkit.

Published · 6 min read ·Role-specific ·Intermediate

Visual quick review

Visual first · depth when needed

Trace how an HMM carries temporal dependence through a first-order hidden-state chain while each observation is generated only from its same-time state.

Preparing the visual…

Summary

A Hidden Markov Model is a latent-variable sequence model with: (a) a discrete latent state evolving as a first-order Markov chain with transition matrix , and (b) per-state emission distributions producing observations .

HMMs were the dominant sequence model from the 1970s through the early 2010s for speech recognition, part-of-speech tagging, gene finding, and many time-series problems. They have largely been displaced by neural sequence models (RNNs, transformers) for tasks with abundant data, but remain useful for:

  • Small-data sequence labeling.
  • Settings with strong domain structure (gene finding still uses HMMs).
  • Online filtering with limited compute.
  • As a teaching example of latent-variable inference.

The three classical HMM problems. Likelihood, decoding, learning. And their solutions (forward, Viterbi, Baum-Welch) are core probabilistic ML.

The model

  • Discrete latent states .
  • Initial distribution .
  • Transition matrix .
  • Emission distributions . Typically Gaussian or categorical.

The joint:

Learning objective: trace how temporal dependence travels through the hidden-state chain while each observation is generated only from its same-time state.

Hidden dynamics, local evidence

Only the latent chain carries memory across time.

A four-timestep hidden Markov model factorization Four circular hidden states z one through z four form a directed horizontal chain. The initial distribution pi points to z one, and each transition arrow is labelled A. Each hidden state points down through an emission arrow labelled p of x t given z t to one rounded rectangular observation x at the same timestep. There are no arrows between observations. State z two has a double ring: conditioning on it separates the past variables z one, x one, and x two from the future variables z three, z four, x three, and x four. A factor strip spells out the initial and emission factor at time one followed by the product of one transition and one local emission at later times. t=1 t=2 t=3 t=4 HIDDEN π A A A z₁z₂z₃z₄ OBS. p(x₁|z₁)p(x₂|z₂)p(x₃|z₃)p(x₄|z₄) x₁x₂x₃x₄ no x-to-x edges observations depend across time only through z₁ → z₂ → z₃ → z₄ Condition on the double-ringed z₂ {z₁, x₁, x₂} ⟂ {z₃, z₄, x₃, x₄} | z₂ READ THE JOINT ONE LOCAL FACTOR AT A TIME π(z₁) p(x₁|z₁) × ∏ [p(zt|zt−1) p(xt|zt)] initial state + one emission, then transition + emission per step
Read it this way: follow the top row to sample the hidden trajectory, then move down once at each timestep to emit the observation. There is no direct arrow from one xt to the next: distant observations are related because their hidden causes are related. Condition on the double-ringed z2, and that state blocks the path between past and future. The graph therefore reads directly as the joint's initial factor followed by one transition and one emission per timestep. Original schematic checked against Rabiner's HMM tutorial and the hmmlearn model description.

The three classical problems

1. Likelihood: forward algorithm

Compute by marginalizing over . Naive sum is . The forward algorithm uses dynamic programming:

Complexity: . Final likelihood: .

2. Decoding: Viterbi algorithm

Find the most likely sequence . Same DP structure but replace sum with max:

Backtrack from . Complexity: .

3. Learning: Baum-Welch (EM)

Estimate from observations alone. E-step: compute posterior over latents using forward-backward. M-step: weighted MLE on transitions and emissions. This is EM applied to HMMs; converges to local optimum of the log-likelihood.

Forward-backward

The forward variable and backward variable together give:

  • Posterior over single state: .
  • Posterior over consecutive pair: needed for the EM transition update.

Forward-backward is the HMM analog of message passing on a chain. Exact in .

Connection to other models

ModelRelation to HMM
Mixture of GaussiansHMM with
Linear-Gaussian state space (Kalman filter)Continuous-state HMM
CRFDiscriminative HMM (model directly)
Linear-chain RNNNeural generalization with continuous latents
TransformerReplaces Markov assumption with attention over full sequence

When to use HMMs in 2026

SettingHMM vs. alternatives
Phoneme alignment in TTS / ASR forced alignmentHMM still standard
Bioinformatics (gene finding, profile HMMs)HMMs dominant
Small-data sequence labelingHMM or CRF baseline
Modern NLP (NER, POS)Transformers win
Speech recognition (end-to-end)RNN-T or transformer encoder + CTC

Common pitfalls

  • Numerical underflow. shrinks geometrically; use log-space or scaling.
  • EM local optima. Multiple random restarts; initialize emission means with k-means.
  • Treating HMMs as state-of-the-art for general sequence tasks. They are not for any task with abundant data.
  • Confusing first-order Markov with the model’s expressiveness. The latent is first-order Markov; the observations can have arbitrary long-range structure mediated by latents (which is why HMMs work at all).