← Back to HMM Trader

Lesson 04

HMM Foundations

A Hidden Markov Model has three components: initial probabilities, a transition matrix, and emission distributions. Together they define a complete probability model of how hidden market regimes generate observable returns.

Hidden vs observable

The key insight of the HMM: the market's true state (Bull Run, Bear, Chop) is hidden. We never observe it directly. What we do observe is the return at each time step — the HMM must infer the hidden state from the observable data.

State machine — 3-state HMM (simplified view)
The Markov property. Each state depends only on the previous state, not the full history. This is what makes the model tractable: P(St | St-1, St-2, ...) = P(St | St-1). Markets have memory, but the Markov assumption is a useful simplification.

The three components: λ = (π, A, B)

An HMM is fully defined by three sets of parameters, collectively written as λ (lambda).

π

Initial probabilities

Probability of starting in each state. A vector of length K (7 for our model). Sums to 1.

A

Transition matrix

K×K matrix. A[i][j] = P(state j at t+1 | state i at t). Each row sums to 1.

B

Emission distributions

One multivariate Gaussian per state. B(x|k) = N(x; μ_k, Σ_k). Probability of observing features x while in state k.

λ = (π, A, B)
πk = P(S1 = k)     initial state probability
Aij = P(St+1=j | St=i)   transition probability
Bk(x) = N(x; μk, Σk)     emission probability

The transition matrix

Each cell A[i][j] is the probability of moving from regime i to regime j on the next bar. High values on the diagonal mean regimes are sticky — once you're in a Bull Run, you tend to stay there.

Transition matrix A — hover a cell to see its probability

Regime persistence is critical for trading. A sticky transition matrix (high diagonal values) means regimes last for hours or days. This gives the trading system time to act. A flippy matrix would cause constant whipsawing between entry and exit signals — exactly what our hysteresis rule guards against.

Putting it together: generating a sequence

Click "Simulate" to watch the HMM generate a sequence: it samples from π to choose a starting state, then emits an observation from that state's Gaussian, then transitions to a new state using the transition matrix, and repeats.

Simulated regime sequence and observations
Observations (returns)