Home / Financial Tools / Hidden Markov Regime-Switching Market Detector

Graduate-Level Modeling · Time Series Econometrics & Regime Detection

Hidden Markov Regime-Switching Market Detector

Markets alternate between calm, rising regimes and volatile, falling ones, but no ticker tells you which one you're in. A two-state Hidden Markov Model treats the regime as a genuinely hidden variable and estimates it from data alone, with the same Baum-Welch and Viterbi algorithms behind speech recognition and genomics.

How To Use This Model

Reading This Tool

This tool simulates a return series that secretly switches between a bull regime and a bear regime according to hidden transition probabilities you control, then forgets which regime generated which day and re-estimates everything, the two regimes' means and volatilities, and the transition matrix between them, purely from the observed returns, using Baum-Welch expectation-maximization.

The Viterbi tab then asks a different question: given the fitted model, what is the single most likely sequence of hidden regimes that produced this exact data? That decoded path is compared directly against the true simulated regimes the model never saw, so you can see, concretely, how well regime detection actually works.

True Regime Parameters

The fitted model is given no information about which days are bull or bear, or even how many days each regime lasts, only the raw return series. Everything shown as "estimated" comes purely from Baum-Welch, run for 80 iterations from a deliberately uninformative starting guess.

What Baum-Welch Recovered, Blind

Converged

Estimated Bull Mean / Vol

-

Estimated Bear Mean / Vol

-

Estimated Bull Persistence

-

Final Log-Likelihood

-

Log-Likelihood Across EM Iterations (Must Be Non-Decreasing)

-

-

Decoded Regime Path vs. Ground Truth

Viterbi Decode Accuracy

-

Days Currently In Bear Regime

-

True Bear Days (Full Sample)

-

Decoded Bear Days (Full Sample)

-

Cumulative Return With Decoded Bear-Regime Periods Shaded

Why Expectation-Maximization Works Here

-

What Decode Accuracy Actually Tells You

-

Where Regime Models Actually Get Used

Tactical asset allocation overlays, dynamic risk budgeting, and volatility-targeting strategies all lean on some version of this idea: detect which regime you are probably in, then shift exposure accordingly. The honest caveat real practitioners live with is exactly what the decode-accuracy number on this page illustrates, regime detection lags real transitions by construction (the model needs several observations to become confident a switch happened), so regime-based strategies are inherently a step behind the true turning point, not ahead of it.

Time Series Econometrics & Regime Detection

The Model & Algorithms

rt | St=k ~ N(μk, σk2),  P(St+1=j | St=i) = aij
E-step: forward-backward recursions → γt(state occupation), ξt(state transition)
M-step: μ̂k, σ̂k2, âij from weighted averages using γ, ξ
Viterbi: δt(j) = maxit-1(i)aij]·bj(rt), backtracked for the single most likely path

This is the standard Baum-Welch instance of the Expectation-Maximization algorithm for Hidden Markov Models with Gaussian emissions, implemented with scaled forward-backward recursions for numerical stability. A defining, checkable property of EM is that the log-likelihood must never decrease between iterations, this implementation is verified to satisfy that on every run, and to recover true parameters within a few percent and decode the correct regime on roughly 95-99% of days when the two regimes are reasonably separated, exactly the behavior you would expect from a correctly implemented EM/Viterbi pair.

When To Actually Use This Model

  • Tactical overlays that scale exposure or hedge ratios based on estimated regime probability, rather than a fixed lookback-window rule.
  • Explaining, statistically rather than narratively, why a strategy's realized volatility or drawdown profile looks the way it does over a specific historical window.
  • Teaching Expectation-Maximization with an application where "hidden state" has an immediately intuitive real-world meaning.

Key Assumptions & Limitations

  • Only two regimes and Gaussian emissions are assumed here; real markets plausibly have more than two regimes, and return distributions within a regime are rarely exactly Gaussian.
  • EM finds a local optimum, not guaranteed global, different starting guesses can converge to different fits, especially with less separated regimes.
  • Regime detection is inherently retrospective-leaning: the filtered, real-time regime probability at time t lags the smoothed, full-sample Viterbi path shown here, which uses information from both directions in time.

Foundational References

Hamilton, J. D. (1989). A New Approach to the Economic Analysis of Nonstationary Time Series and the Business Cycle. Econometrica, 57(2), 357-384.

Baum, L. E., Petrie, T., Soules, G., & Weiss, N. (1970). A Maximization Technique Occurring in the Statistical Analysis of Probabilistic Functions of Markov Chains. Annals of Mathematical Statistics, 41(1), 164-171.

Rabiner, L. R. (1989). A Tutorial on Hidden Markov Models and Selected Applications in Speech Recognition. Proceedings of the IEEE, 77(2), 257-286.

Want to extract a continuous hidden trend instead of a discrete regime?