Hidden Markov Models for Regime Detection

Markets behave differently in different regimes — bull, bear, choppy, high-volatility — and the same strategy can be profitable in one regime and disastrous in another. Hidden Markov Models (HMMs) are the standard probabilistic tool for inferring which regime the market is in right now, given only observable price behavior.

The Core Idea

HMM assumes there is an unobservable "state" (the regime) that the market is in at each point in time. Each state has its own statistical signature — its own mean return, its own volatility, its own typical duration. The HMM doesn't see the state directly; it only sees observations (returns, volatility, range). From these observations, it computes the probability that the market is currently in each state.

The key assumption: state transitions follow a Markov chain. The probability of moving from state A to state B depends only on the current state, not on the history of states. This is restrictive but tractable, and it captures most of the regime-persistence empirically observed in markets.

Two-State and Three-State Regimes

The simplest HMM has two states: low-volatility / high-volatility, or trending / mean-reverting. Three-state models add a "neutral" or "transitioning" state. More states give finer resolution but require much more data to estimate reliably. Two-state HMMs are the practical default for most retail use cases.

What the Model Estimates

Given a return series, an HMM with K states fits:

  • K mean-return parameters (μ_1, …, μ_K) — one per state.
  • K volatility parameters (σ_1, …, σ_K) — one per state.
  • A K×K transition matrix — probabilities of moving between any two states.
  • An initial distribution over states.

Once fitted (typically via the Baum-Welch EM algorithm), the model can produce per-bar posterior probabilities: given everything observed up to today, what is the probability we are in state 1, state 2, etc.?

Using HMM States in Strategies

Two practical patterns:

Regime Filter

Run a strategy only when the HMM says the favorable regime is most likely. Example: a mean-reversion strategy only trades when the HMM probability of "low-vol mean-reverting" state is > 0.7. Trades are filtered out during high-volatility or trending regimes where mean-reversion typically fails.

Regime-Conditional Allocation

Run different strategies in different regimes. In bull regimes, trade trend-following. In choppy regimes, switch to mean-reversion. The HMM probabilities drive the allocation. This is a more aggressive use that requires high confidence in the regime classifications.

What Makes HMMs Work in Practice

  • Right number of states. Too few states miss real distinctions; too many states overfit. Use AIC/BIC information criteria or cross-validation to pick.
  • Right observables. Returns alone are not enough — pair them with realized volatility, range, or other features so the HMM can distinguish low-vol from high-vol states cleanly.
  • Long enough history. HMM parameter estimation needs many regime transitions to estimate the transition matrix. A 1-year sample with 1 regime transition is not informative.
  • Probabilistic outputs, not hard classifications. Treat HMM output as a probability distribution. Trade only when probabilities are decisively above threshold; abstain when ambiguous.

Failure Modes

  • State drift. The "low-vol state" the HMM learned in 2017 may not be the same statistical regime that exists in 2023 — the means and volatilities of states can themselves shift over decades. Refit periodically.
  • Hindsight is 20/20. The HMM's posterior probabilities computed using the FULL history (smoothed) are very accurate looking backward. The forward-looking (filtered, online) probabilities are noisier and harder to trade. Use the latter for live decisions.
  • Spurious states. A 4-state HMM can produce states that look meaningful but are noise — distinct in the fitted parameters, but not statistically separable in OOS data.
  • Overfit to the training period. If you try many state counts and pick the best, you're back to multiple-testing.

QuanterLab's Approach

The Regime Optimizer (RG001RGMO) detects regimes via HMM and then runs full strategy optimization per regime. It then constructs a composite "regime-aware" strategy whose parameters change based on the current regime probability. This is the right structure: the HMM identifies regimes, then a per-regime grid search finds the best parameters for each, and the live strategy switches between them based on real-time regime classification.

The Bottom Line

HMMs are the principled way to formalize "market is in regime X right now." Used as a filter or allocator on top of strategies, they can dramatically reduce drawdowns and avoid the worst-of-regime catastrophes. Used as a strategy themselves (trade based on HMM signals alone), they tend to underperform — the regime classification is the input, not the alpha.

Further Reading

Foundational papers

  • Hamilton, J. D. (1989). A New Approach to the Economic Analysis of Nonstationary Time Series and the Business Cycle. Econometrica, 57(2), 357–384.
  • Ang, A. & Bekaert, G. (2002). International Asset Allocation with Regime Shifts. Review of Financial Studies, 15(4), 1137–1187.
  • Guidolin, M. & Timmermann, A. (2007). Asset Allocation under Multivariate Regime Switching. Journal of Economic Dynamics and Control, 31(11), 3503–3544.

Textbook references

  • Hamilton, J. D. (1994). Time Series Analysis. Princeton University Press.
  • Tsay, R. S. (2010). Analysis of Financial Time Series (3rd ed.). Wiley.

Related QuanterLab articles

Try it in QuanterLab

Open RG001RGMO on a long-history index. Watch how the regime probabilities evolve through 2008, 2018, 2020. Strategies that only trade in one regime will look very different in their per-regime performance breakdown.

Back to QuanterLab
Report
Loading report...
Article
Loading article...