Basic Prerequisites: 01 · From Zero and Econometrics & Time Series (AR models, maximum likelihood).


1. Intuition & Practical Objective

The Markov-switching (MS) model (Hamilton 1989) is the workhorse of regime detection. The idea: the parameters of a time-series model are the outcome of a discrete-state Markov process you do not observe. Concretely, model returns as

where the regime follows a first-order Markov chain with transition probabilities . This one specification does two jobs at once: (a) it estimates the regime parameters () and the persistence matrix by maximum likelihood, and (b) it produces, as a byproduct of the same recursion, the filtered probability of being in each regime right now - a live, objective bull/bear gauge.

Hamilton’s 1989 paper is the canonical template. Applied to quarterly US real GNP growth (1952–1984), the MLE splits into negative-growth (recession) and positive-growth (expansion) states: /qtr vs /qtr, with the negative state associated with a permanent drop in the level of GNP. The filtered probability of the negative-growth state matches NBER business-cycle dating to within a quarter for most episodes - evidence the model is finding a real structure, not just fitting noise.

This page is the mathematical spine of the folder. It gives the full likelihood, the Hamilton filter recursion, and a complete EM implementation that estimates the parameters from returns and recovers the planted regimes.


2. Mathematical Ground Truth & Derivations

The Markov chain. Transition matrix with rows summing to . In Hamilton’s GNP application the states are ordered so state = fast growth: , . Two quantities matter throughout:

The likelihood. Marginalizing over the unobserved regimes, the sample likelihood is (Hamilton §4.2):

with . This is not a product of independent Gaussian densities - the predictive probabilities couple adjacent observations through the chain.

The Hamilton filter evaluates that likelihood and the state probabilities in one forward pass:

  • Predict:
  • Density:
  • Update:

Starting the filter from the stationary distribution gives probabilities that remain in and sum to by construction.

Estimation by EM (Baum–Welch). Because the regimes are hidden, direct gradient maximization works but EM is cleaner and standard (it is the HMM/Baum–Welch algorithm of 04 · HMM). Iterate:

  • E-step: run forward–backward to get responsibilities .
  • M-step: re-estimate, weighted by responsibilities,

Cross-check against Hamilton (Tsay Ch 4, verified). Tsay’s Markov-switching application to US real GNP (via EM/Hamilton and MCMC/Gibbs) finds contraction ≈ 3.7 quarters, expansion ≈ 11.3 quarters - the same structure Hamilton got with a different estimator, reinforcing the result’s robustness.


3. Computational Implementation - estimate a 2-state MS model end-to-end

We generate a 400-period 2-regime return series with known truth, run the Hamilton filter with the true parameters (inference check), then estimate everything back from the raw returns via EM and compare. Stdlib only.

The bull mean () and both vols (, ) are recovered well from raw returns alone. The bear→bull transition () is over-estimated vs - the bear regime had only 88 observations, so its persistence is noisily identified (a real, first-principles limitation, not a coding bug).


4. Failure Modes & First-Principles Breakdowns

  1. State-count uncertainty. The number of regimes is not estimated by the filter; it is chosen (Tsay notes the general framework supports states, with input a vector of or elements). Adding states always raises the likelihood - see the overfitting BIC demo in 05 · Failure Modes.
  2. Identification limits (Hamilton’s own warning). Separating the Markov-chain parameters () from the Gaussian component () “depends on nonlinearities in the data”; there is a practical ceiling on how rich both sets of dynamics can be. The over-estimate above is exactly this.
  3. Label switching. The states are unlabeled by construction; without a constraint the EM can return an identical model with swapped labels (verified in 05). Always fix an ordering (e.g. ).
  4. Startup/persistent regimes. If is near , the chain rarely visits state , so its parameters are estimated from very few effective observations - high variance regardless of sample size.

5. References

  • Hamilton (1989), Econometrica 57(2)
  • Tsay, Analysis of Financial Time Series
  • Ang & Timmermann (2012)
  • Hamilton, James D.: Time Series Analysis (1994)

6. Connected Graph Bridges