1. Why market regimes?
Markets rarely behave the same way across time. Volatility clusters, trends persist, and drawdowns often come in bursts. A regime model compresses this behavior into a small set of states (here: Bear / Neutral / Bull) and lets us ask practical questions: How persistent is a regime? How likely is a switch? What happens over a 20-day horizon?
2. Observed-state regime definition
In an observed-state approach we do not “discover” hidden states (as in Hidden Markov Models). Instead, we define regimes via a simple, reproducible rule on ^GSPC prices.
- Compute a rolling return over a lookback window L (e.g., ~63 trading days ≈ 3 months).
- Bull if the rolling return is above a positive threshold.
- Bear if the rolling return is below a negative threshold.
- Neutral otherwise.
This is “discretionary” only in the sense that you choose L and the thresholds. The advantage is clarity: the pipeline is transparent, easy to stress-test, and easy to reproduce.
3. Transition matrix (Markov chain)
A Markov chain assumes that tomorrow’s regime depends only on today’s regime. Once each day is labeled, we estimate transition probabilities by counting how often we observe each one-step transition (e.g., Bear→Bear, Bear→Neutral, Neutral→Bull) and normalizing row-wise.
In this sample, diagonal entries are close to ~0.90, which indicates strong regime persistence. Direct Bear→Bull and Bull→Bear transitions are near zero, suggesting that the model often transitions through Neutral.
4. Expected duration
The transition matrix implies an average time spent in each regime. Intuitively, the higher the probability of staying in the same state, the longer a typical “run” lasts.
5. Stationary distribution
Over a long horizon, a Markov chain can converge to a stable “mix” of time spent in each regime. This is the stationary distribution: a long-run baseline that is consistent with the transition dynamics.
6. 20-day regime probabilities
One-step probabilities are useful, but many decisions have a multi-week horizon. By propagating transitions forward, we can estimate the probability of each regime after 20 trading days, conditional on today’s regime.
Notice how Neutral acts as a “central” state even at 20 days: many paths flow through it. This is a common outcome when direct Bear↔Bull transitions are rare.
7. Conditional performance by regime
Regime labels become meaningful when we inspect how returns and volatility differ across states. We compute daily return statistics conditional on regime and annualize them for readability.
In this run, the Bear regime shows negative average returns and higher volatility, while Bull displays positive average returns and lower volatility. Neutral sits in between. This is exactly the type of sanity check you want when validating a regime definition.
8. Methodology & reproducibility
- Data: ^GSPC daily closes via yfinance (auto-adjusted).
- Regime rule: rolling return over lookback L with Bull/Bear thresholds; Neutral otherwise.
- Markov estimation: count transitions and normalize per row to obtain the transition matrix.
- Derived metrics: expected duration, stationary distribution, and 20-day probability matrix.
- Performance: conditional daily mean/volatility with standard annualization (252 trading days).
- Stress-test sensitivity: compare multiple L values (63 vs 126 vs 252) and threshold sets.
- Compute transition matrices on rolling windows (time-varying dynamics).
- Upgrade to Hidden Markov Models (HMM) to infer latent states directly from returns.
All charts are descriptive and for educational purposes only. Not investment advice.
Stay in the loop
Quant insights, scripts, and indicators - at most 1-2 emails per month.