Basic Prerequisites: 02 · Markov-Switching Models and Probability & Measure Theory (Bayes, conditioning).
1. Intuition & Practical Objective
A Hidden Markov Model (HMM) is the formal, machine-learning-side statement of Hamilton’s Markov-switching model (the two are the same object; the traditions differ in emphasis). An HMM has three parts:
- Hidden states following a first-order Markov chain with transition matrix .
- Emission densities - e.g., Gaussian with regime mean and vol (this is the HMM’s connection to Gaussian Mixture Models / GMM when the time-dependence is removed).
- The initial distribution .
You observe only (returns). The three canonical inference problems, each with a clean algorithm:
- Filtering / smoothing - for each → the forward–backward algorithm.
- Decoding - the single most-likely state sequence → Viterbi.
- Learning - estimate from data → EM / Baum–Welch (exactly the EM of page 02, formalized).
The practical objective: an HMM is the machinery you use when the regime structure is genuinely latent and you want smoothing (best estimate of past regimes), decoding (a dating of the full regime history), and clean parameter learning all from one estimable object.
2. Mathematical Ground Truth & Derivations
Forward pass (filtering). Define , the joint probability of the data up to and being in state at . It satisfies the recursion (with scaling factor for numerical stability):
The scaled version stores with ; then is the log-likelihood.
Backward pass (smoothing). Define , computed backward:
Smoothing - the marginal posterior probability of state at time given all data:
normalized over . This is the “smoothed regime probability” - the best answer to “which regime were we in?”
Decoding (Viterbi). Instead of the marginal, find the joint-MAP path via dynamic programming:
recording the argmax at each step and backtracking from .
Learning (Baum–Welch EM). The E-step computes responsibilities and pairwise transition responsibilities ; the M-step re-estimates:
Each EM iteration is guaranteed not to decrease the likelihood (ESL Ch 8.5’s EM treatment; Tsay Ch 12’s MCMC approach is the Bayesian alternative).
3. Computational Implementation - smooth, decode, and reconstruct an HMM
Generate a 2-state Gaussian HMM, run forward–backward smoothing, Viterbi decoding, and compare both to the planted states. Stdlib only.
Smoothing (88.6%) beats Viterbi (77.8%) - a genuinely important fact: using all the data (past and future) to estimate each regime is more reliable than committing to a single joint-MAP path. The smoothed probabilities are high and stable during the (truly bullish) opening stretch, showing the smoothing knows the early data was bull with near-certainty.
4. Failure Modes & First-Principles Breakdowns
- Smoothing vs filtering vs decoding are different questions. Filtering (, causal) is what a trading decision can use; smoothing (, non-causal) is better for dating regimes but peeks into the future. Viterbi gives the joint-MAP path, not the marginal probabilities - and (as shown) is less accurate than smoothing. Use each for its correct job.
- Overfitting via too many states. EM only raises the likelihood; a 3-state HMM on 2-state data splits a real state into two clones. Use BIC - see 05 · Failure Modes.
- Label switching / non-identifiability. Permuting state labels leaves the likelihood identical; EM may converge to a swapped labeling. Fix an ordering constraint on a parameter (e.g. ).
- Scale/numerics. Unscaled forward probabilities underflow exponentially in ; always use the scaled () version - the code above scales both passes.
5. References
- Rabiner, Lawrence R.: A Tutorial on Hidden Markov Models and Selected Applications in Speech Recognition, Proc. IEEE 77(2), 257–286 (1989)
- Bishop, Christopher M.: Pattern Recognition and Machine Learning
- ESL (Hastie, Tibshirani & Friedman):
- Tsay, Analysis of Financial Time Series
- Hamilton (1989)
6. Connected Graph Bridges
- Back: 02 · Markov-Switching · Index Hub
- Forward: 05 · Failure Modes · 06 · Advanced Extensions
- ML cross-link: Regime Classification: HMM & GMM (Baum–Welch + Viterbi in the ML pillar)
- Contrast: 03 · Threshold Models (observed-state regimes vs latent-state HMM)