Quant Memo
Advanced

Hidden Markov Models

A model where the world flips between a few hidden regimes (calm, stormy), each producing its own kind of data. You never see the regime directly, but you can infer it from the observations it emits.

Prerequisites: Markov Chains, Bayes' Theorem

Markets seem to switch personalities. For weeks they drift calmly, small moves, low volatility, and then something flips and every day is a big, jumpy swing. A hidden Markov model (HMM) is the natural way to capture that. It says the world is always in one of a few hidden regimes, it hops between them at random according to fixed odds, and each regime spits out data with its own characteristic look. You never get told which regime you're in; you have to infer it from the observations, and that inference is the whole game.

It's the discrete-state member of the State-Space Models family. Where the The Kalman Filter tracks a continuous hidden number, an HMM tracks a hidden category, calm or stormy, bull or bear, trending or mean-reverting.

The three ingredients

An HMM is fully described by three things:

  1. Transition probabilities, the odds of hopping between regimes from one step to the next. This is a Markov chain on the hidden states: where you go next depends only on where you are now.
  2. Emission distributions, for each regime, the distribution of the data it produces. The calm regime might emit small, tight returns; the stormy regime, wide, fat-tailed ones.
  3. Initial probabilities, your belief about which regime you start in.
Calm Storm 0.10 0.20 0.90 0.80 small moves big moves observations observations
Two hidden regimes. The curved arrows are the odds of switching (or, via the self-loops, staying), and each regime emits its own flavour of observation. You see only the moves, not the regime, and work backward.

An HMM = a hidden regime that follows a Markov chain (transition probabilities) + each regime emitting data from its own distribution. You observe only the emissions and infer the hidden regime sequence, that's what the forward and Viterbi algorithms do.

Worked example: did the regime just flip?

Use the two regimes in the diagram. Suppose the persistence is high: a calm day stays calm with probability 0.90.9 and turns stormy with 0.10.1; a stormy day stays stormy with 0.80.8. Emissions: a "big move" (say a daily return past ±2%\pm 2\%) happens with probability 0.10.1 in the Calm regime but 0.50.5 in the Storm regime.

Yesterday you were fairly sure it was Calm: P(Calm)=0.8P(\text{Calm}) = 0.8, P(Storm)=0.2P(\text{Storm}) = 0.2. Today a big move prints. What should you now believe? This is one step of the forward algorithm, just Bayes' rule with the emission as evidence:

P(Stormbig move)=P(Storm)P(bigStorm)P(Calm)P(bigCalm)+P(Storm)P(bigStorm).P(\text{Storm}\mid \text{big move}) = \frac{P(\text{Storm})\,P(\text{big}\mid \text{Storm})}{P(\text{Calm})\,P(\text{big}\mid \text{Calm}) + P(\text{Storm})\,P(\text{big}\mid \text{Storm})}.

Plug in the numbers:

=0.2×0.50.8×0.1+0.2×0.5=0.100.08+0.10=0.100.180.56.= \frac{0.2 \times 0.5}{0.8 \times 0.1 + 0.2 \times 0.5} = \frac{0.10}{0.08 + 0.10} = \frac{0.10}{0.18} \approx 0.56.

A single big move drags your belief from 20%20\% stormy to 56%56\% stormy, now more likely than not that the regime has flipped. The model quantifies exactly how much one surprising observation should shift your read on the hidden state, and the persistence in the transition matrix keeps it from over-reacting to every wiggle. Run this update forward day after day and you get a live, probabilistic regime signal.

The three questions HMMs answer

Practitioners lean on three standard algorithms, each answering a different question:

  • "What regime am I in now?" The forward-backward algorithm gives the probability of each hidden state at each time, filtering and smoothing.
  • "What's the single most likely sequence of regimes?" The Viterbi algorithm finds the best path through the hidden states.
  • "What are the model's parameters?" The Baum-Welch algorithm (an EM procedure) learns the transition and emission probabilities from data when you don't know them up front.

Where it misleads

  • Picking the number of regimes is an art. Two states? Three? Too few and you blur distinct regimes together; too many and you overfit, inventing regimes that are just noise. Use out-of-sample checks, not in-sample fit, to choose.
  • Label switching and instability. The fitting can converge to different local optima, and "regime 1" versus "regime 2" is arbitrary, so parameters can be hard to compare across runs. Multiple random starts are essential.
  • Regimes are detected with a lag. By construction, you only recognise a regime shift after the tell-tale observations arrive. An HMM will confirm a crash is underway, but it won't front-run it, don't mistake its smooth in-sample regime plot for real-time foresight.
  • Markov and stationarity assumptions. Real transition odds drift, and the "memoryless" assumption (next regime depends only on the current one) is a simplification. Long, structured regimes can violate it.

An HMM's beautiful in-sample regime chart is hindsight. In real time it detects a regime only after the evidence accumulates, so it lags turning points. Treat it as a confirmation and risk-sizing tool, not a crystal ball.

The core intuition to keep: an HMM assumes a small cast of hidden "moods," each with its own fingerprint in the data, and it uses every new observation to update the odds of which mood you're in. That makes it the standard formal engine behind Regime Detection, and the discrete-state sibling of the The Kalman Filter within the broader State-Space Models world.

Related concepts

Practice in interviews

Further reading

  • Rabiner (1989), A Tutorial on Hidden Markov Models
  • Hamilton (1989), A New Approach to the Economic Analysis of Nonstationary Time Series
  • Bishop, Pattern Recognition and Machine Learning (Ch. 13)
ShareTwitterLinkedIn