← Back to list

Hidden Markov Models for regime detection look simple until your backtest silently lies to you.

Nicolae Filip Stanciu · 2026-03-12 22:54 · 67 claps · 2.0 min read paywalled
#finance #data-science #algorithms #research #backtesting
Open on Medium ↗
Wiki topics: ML · Machine Learning ECO · Economy · General 💻 · Programming 🔬 · Science · General

Hidden Markov Models for regime detection look simple until your backtest silently lies to you.

Most quant implementations fail not because the model is wrong – but because the data pipeline feeds it information it should never have. Here is every leakage point worth knowing.

Feature standardisation is not optional

Raw features going into a Gaussian HMM violate the emission assumption. OU Half-Life has an extreme right tail – trending windows produce values of 200+ days while mean-reverting windows produce 8. Feed that raw and you corrupt the covariance matrix of every state. Log-scale before z-scoring.

Use expanding z-score, not rolling. Rolling normalisation assumes mean and variance are constant over the window, which fails exactly when you need the model most – during regime transitions.

hmmlearn has three inference methods. Two of them are look-ahead.

predict() uses Viterbi – full sequence, forward and backward. score_samples() with smoothed posteriors uses the forward-backward algorithm. Both know what state the model ended up in at t+10 when they assign the label at t. Your live system does not.

The only valid option for production scoring is the forward algorithm on a single observation. One bar, one forward pass, no future data.

Frequent retraining creates leakage even without look-ahead

Refit every 5 days with no embargo and you have a problem. EM concentrates gradient on the most recent observations – those closest to the training cutoff. Score the next observation and the model is responding to patterns it just memorised from the days immediately before. Not classical look-ahead, but parameter leakage in practice.

The fix: a hard embargo between training cutoff and scored observation. Train on [0, t-21], withhold [t-21, t] entirely, score at t. The 21-day buffer breaks the leakage path regardless of how frequently you retrain within it.

Walk-Forward Analysis needs two embargos, not one!

The HMM-internal embargo handles parameter leakage. The fold-level embargo handles something different: threshold selection leakage. If you grid-search an entry threshold on IS data [0, T_k] and evaluate immediately on [Tk, T{k+1}] with no gap, the optimisation can overfit to the boundary between the two windows. Add a fold-level embargo of at least 21 days between the IS optimisation cutoff and the first OOS observation.

WFE – Walk-Forward Efficiency – is the metric that tells you whether any of this worked. It is simply mean(OOS Calmar) / mean(IS Calmar) across folds. WFE near 1.0 means IS performance transfers. WFE near 0 or negative means you built a sophisticated curve-fitting machine.

The uncomfortable truth is that a model with correct leakage controls will almost always look worse in-sample than one without them. That degradation is not a problem to fix – it is the signal that you are measuring something real.

You can visit: https://ventajaquant.com/en/pages/market-regime/ for more information

QuantFinance #AlgoTrading #MachineLearning #HMM #RiskManagement​​​​​​​​​​​​​​​​ #VentajaQuant.com


메타데이터
post_id
9d8d0cf02099
slug
hidden-markov-models-for-regime-detection-look-simple-until-your-backtest-silently-lies-to-you-9d8d0cf02099
url
https://medium.com/@NFS303/hidden-markov-models-for-regime-detection-look-simple-until-your-backtest-silently-lies-to-you-9d8d0cf02099
canonical_url
https://medium.com/@NFS303/hidden-markov-models-for-regime-detection-look-simple-until-your-backtest-silently-lies-to-you-9d8d0cf02099
author_url
https://medium.com/@NFS303
status
ok
fetched_at
2026-06-10 15:53:41