Basic Prerequisites: 03 · The EM Algorithm and Probability & Measure Theory (Markov chains, Bayes’ rule).
1. Intuition & Practical Objective
A GMM (02) assumes each day’s regime is an independent draw. But regimes persist: a calm market stays calm for weeks; a crash clusters in days. A Hidden Markov Model (HMM) fixes this by placing a Markov chain over the hidden regime: today’s regime depends on yesterday’s through a transition matrix , and - given the regime - the observation is drawn from a regime-specific distribution. It is, in one sentence, a GMM whose latent class follows a Markov chain.
The practical objective: fit the HMM’s parameters (transition matrix , initial probs , emission means/vols ) by Baum–Welch - EM specialized to the HMM - then decode the hidden state path. Three outputs matter:
- Filtered - online, uses only the past: what you can know live (the honest regime call).
- Smoothed - uses the whole sample: the best post-mortem regime call (and, carelessly, the source of look-ahead, 05).
- Viterbi path - the single most probable joint state sequence , via dynamic programming (the “MAP decoding”).
The one-sentence essence. “An HMM is a GMM whose regime membership is a Markov chain; Baum–Welch (EM) fits it, forward–backward gives per-time regime probabilities, and Viterbi decodes the single best state path - all three are just Bayes’ rule organized over time.”
2. Mathematical Ground Truth & Derivations
The generative model (Rabiner 1989; ESL Ch 14; Tsay Ch 4). Latent state , Markov in time:
with Gaussian emissions .
Forward–backward (the HMM’s E-step). Define and :
The smoothed state posterior (responsibility) and the two-time transition posterior are
Baum–Welch (the M-step) - the EM updates specialized. Maximizing the expected complete-data log-likelihood gives closed forms:
Viterbi (MAP joint path). Dynamic programming: and
keeping the argmax back-pointer at each step, then tracing back from . This is exact - it finds the single most probable sequence, not just per-time marginals. (In practice one uses log-probabilities and optional scaling to avoid underflow on long series.)
3. Computational Implementation - fit a 2-state HMM by Baum–Welch, decode with Viterbi
Simulate a persistent two-regime return series, fit the HMM from scratch (forward–backward + Baum–Welch), and measure how well the decoded path matches the truth. Stdlib only.
The HMM recovers both regime volatilities (, vs true , ), the transition matrix (diagonals / vs true / - the persistence that a GMM cannot express), and the log-likelihood rises monotonically . Viterbi decodes the hidden state path with 95.8% agreement with the truth - because regimes persist, the Markov structure lets the model smooth away the noise that an independent per-day classifier (01) cannot. That persistence is the entire added value of the HMM over a GMM on time series.
4. Failure Modes & First-Principles Breakdowns
- Look-ahead in smoothed/decoded labels. The smoothed and the Viterbi path use future observations . Used at time they leak information - measured as a pt phantom advantage in 05. Fix: trade with the filtered posterior, not the smoothed/decoded one.
- Label switching. Same permutation symmetry as the GMM; two Baum–Welch runs can return swapped states with identical likelihood (05). Fix: order-constrain .
- Number of states . Too many states → regime cloning; BIC penalization picks the right (verified in 05).
- Gaussian emission misfit. Fat-tailed returns violate the normal-emission assumption; heavy tails can fool the model into inventing a third “spike” state. The t-distribution emission (or a GMM emission per state) is the robust upgrade.
- Initialization & local optima. EM is local; a bad start lands on a poor fit. Multi-start or a K-means warm start helps.
5. References
- Rabiner, “A Tutorial on Hidden Markov Models and Selected Applications in Speech Recognition,” Proceedings of the IEEE 77(2), 1989
- Hamilton, “A New Approach…,” Econometrica 57(2), 1989
- Tsay, Analysis of Financial Time Series
- Hastie, Tibshirani & Friedman, The Elements of Statistical Learning
6. Connected Graph Bridges
- Back: 03 · The EM Algorithm · 02 · GMM
- Forward: 05 · Failure Modes · 06 · Regime-Conditional ML
- Sibling (econometric twin): Regime Detection · HMM (filter view) · Signal Processing & Kalman Filter