Regime-Aware Mean-Variance Optimization using Hidden Markov Models + LightGBM + Markowitz MVO
Financial markets alternate between latent regimes — bull, bear, sideways — that alter return distributions and asset correlations. Classical Markowitz ignores this, leading to poor out-of-sample performance.
This project chains three ML components:
SPY Returns ──► [GaussianHMM] ──► Market Regime (Bull / Bear / Sideways)
│
Stock Features ──► [LightGBM] ──► Expected Returns μ
│
[Regime-Conditioned Σ + μ] ──► [Markowitz MVO] ──► Weights
| Strategy | Ann. Return | Sharpe | Sortino | Max DD |
|---|---|---|---|---|
| ML + Regime MVO | 18.4% | 1.61 | 2.34 | −13.2% |
| Classical MVO | 15.1% | 1.17 | 1.71 | −17.4% |
| Equal Weight | 13.9% | 0.99 | 1.40 | −22.1% |
| SPY Benchmark | 14.2% | 0.97 | 1.39 | −33.9% |
30-stock S&P 500 universe, monthly rebalancing, strict walk-forward validation.
git clone https://github.com/aashshahh/portfolio-optimization-ml
cd portfolio-optimization-ml
pip install -r requirements.txt
# Run full pipeline
jupyter notebook notebooks/portfolio_optimization_pipeline.ipynb
# Launch interactive demo
streamlit run app.pyportfolio-optimization-ml/
├── notebooks/
│ └── portfolio_optimization_pipeline.ipynb ← Full end-to-end pipeline
├── data/ ← Generated by notebook
├── results/ ← Saved plots
├── docs/index.html ← GitHub Pages demo site
├── app.py ← Streamlit interactive app
└── requirements.txt
1. HMM Regime Detection — GaussianHMM (3 states) on SPY return/vol/momentum features. No labels required. Learns bull/bear/sideways from data.
2. LightGBM Walk-Forward — 14 features per stock (momentum, RSI, vol, beta, regime dummies). Strict walk-forward: 3Y train → 1Y test, 5 folds. Metric: Information Coefficient (Spearman rank correlation).
3. Regime-Conditioned MVO — max w'μ - λ/2 · w'Σw where μ = ML predictions, Σ = covariance estimated from same-regime historical returns + Ledoit-Wolf shrinkage. Solved via scipy SLSQP.
- No transaction costs modeled
- Survivorship bias in universe selection
- Regime detection is backward-looking
- Not validated for live trading
- Hamilton (1989) — HMM for regime switching
- Ang & Timmermann (2012) — Regime changes in financial markets
- Ledoit & Wolf (2004) — Covariance shrinkage
- Markowitz (1952) — Portfolio selection
Built for educational purposes. Not financial advice.