Open Family Office Datasets Tickers Strategies Portfolios Research Posts About Changelog

Detecting Market Regimes: Hidden Markov Model

Source

Citation. David Borst (datadave1). "Detecting Market Regimes: Hidden Markov Model". Medium, 26 August 2025, an 11-minute read. datadave1.medium.com/detecting-market-regimes-hidden-markov-model-2462e819c72e. A blog post with its code inline, and no repository or data. It follows the author's post on K-means regimes (17 August 2025). Its prices come from Financial Modeling Prep's API, which needs a key.

Status. Not applicable as a method. The post fits every step on the whole sample and runs no test, so it has no result for the repo to use. It is a worked example of the faults that hmmlearn.md lists, and my check below shows what they produce on data with no regimes.

Read. Read on 2026-11-28 from a PDF of the post that the maintainer supplied, 26 pages. I read the whole post, its code and its tables. Medium refuses the fetcher, so I didn't see the live page or the post's 14 responses beyond the three in the PDF. I ran the post's pipeline myself on simulated random walks, as described below.

Summary

Claim. A Gaussian hidden Markov model finds more market regimes than K-means does. It gives transition probabilities and a filtered probability of today's regime, which can guide risk.

Method.

Findings, as the source reports them.

Setting Number Value
BIC from K = 3 to K = 9 values 153,963 falling every step to 138,526
The same chosen K 9
The best state, Viterbi, in sample annualised return, volatility 34.0%, 11.4%
The worst state annualised return, volatility −53.6%, 60.3%
The last day filtered probability state 2 at 0.56, state 5 at 0.44

Where the claims go beyond the evidence

BIC never chose K. BIC fell at every step and stopped at the top of the range tried, K = 9. A criterion that is still falling at the edge has found no minimum. On five simulated random walks, which have one regime by construction, the same pipeline also chose K = 9 every time.

The states' returns are partly built in. Returns over four horizons and drawdowns are among the features, so the model groups days by their returns. Sorting states by their mean return in sample then shows a spread whatever the market did. On the random walks, the spread between the best and worst state was 118 to 506 points of annual return. The post's spread is 88 points.

Every step saw the whole sample. The scaler, the PCA and the HMM are fitted on all 4,440 days, and the states are read back over the same days. The post says the filtered probability "doesn't use future data", but its parameters do. The filtered value for today is causal only in its last step.

No test and no baseline. The post runs no backtest, charges no costs and compares with nothing. Its closing list says to train models by regime, weight strategies by the filtered probabilities and backtest by regime, but it tries none of them. One response under the post names the problem: detecting a change of regime in real time "without any lookahead bias".

A private method. The forward filter calls hmm._compute_log_likelihood, which hmmlearn doesn't promise to keep (hmmlearn.md).

Credit. The post explains why the Viterbi path is wrong for "now" and writes out a correct forward recursion in log space. It shows its code, its BIC values and its state table, so each of these points can be checked.

Use in this repo

My check: a regime that isn't there. I fitted the post's pipeline to the first 70% of each random walk, and filtered the rest with the parameters frozen. The state that had the highest return in training didn't predict a better next day. Across five walks, its next-day mean was −21 to 13 basis points, against −6 to 22 for the worst state. A regime model is a hypothesis to test out of sample, and its in-sample table is no evidence.

The repo's rules already cover it. hmmlearn.md sets out the causal recipe: a forward filter, parameters refitted on an expanding window, and labels sorted by a key chosen in advance. It also gives the check that a signal's past values don't change when later data are added (pointintime.py). The post fails each one, which makes it a good test case for that check. The repo already has a regime model: econometrics.regimes (#315) returns only filtered probabilities. A backtest calls it through expanding, so its parameters are fitted on the past too.

Choose K out of sample. Use the likelihood on the next window, not BIC on the fitted one, and count each K, feature set and seed as a trial (trials.py). The HMM against HSMM review (hmm-vs-hsmm-regimes.md) shows why: refitted each month out of sample, a richer model's in-sample advantage "largely disappears".

Don't feed returns into the regime model that is meant to predict them. If regimes are to condition a strategy, build them from volatility, credit and the term structure, not from the returns the strategy trades. Then the states' returns are a result to test, not a sort of the inputs.