Quant research & trading·Python·Published
Regime Detector
I wanted a volatility regime label I could trust across refits, so this fits a 3-state Gaussian HMM on daily log returns and then labels the internal states low_vol, mid_vol and high_vol by their emission standard deviation in ascending order rather than by state index. That one decision is the whole point: hmmlearn's state numbering is arbitrary and can permute between fits, so labelling by index gives you a detector that silently flips its meaning after a retrain, while labelling by emission std keeps the semantics stable. It exposes per-observation labels, posterior probability vectors reordered to match the label order, and predict_current for the last observation with its probability vector. GARCHForecaster fits GARCH(1,1) with Student-t innovations through the arch library and returns a one-step-ahead daily vol forecast in percent plus the annualized figure. detect_regime_changes flags label transitions with an optional min_consecutive confirmation to suppress single-bar flickers, and the CLI exits with code 3 when the current regime is high_vol, which makes it a cron alert with no wrapper script. Tests use synthetic GARCH and two-regime return series, so state recovery and parameter recovery are checked against known ground truth.



- HMM state indices are arbitrary and can permute between fits, so any label derived from the index is unstable; sorting states by emission standard deviation is what makes the label mean the same thing after every retrain.
- A regime change detector without a min_consecutive confirmation produces a stream of one-bar flickers at every boundary, which is useless for anything that acts on the signal.
- GARCH(1,1) with Student-t innovations matters for equity returns because the Gaussian version understates tail risk exactly in the high-vol regime you most want to detect.
Scale 0–100: how much this project relied on each skill.