Skip to content

Markov-Switching Models: Hamilton's Regime Model

Bull and bear markets do not share one set of parameters, but the regime itself is invisible. The Hamilton (1989) model, filtered versus smoothed probabilities, EM estimation, and why the signal you can trade always arrives later than the historical chart suggests.

6 of 12 ~14 min

If you have noticed that economic relationships behave differently in recessions and expansions, that volatility comes in gears, or that correlations change their face in a crisis, you have already run into the problem Markov-switching models address: one set of parameters cannot describe two worlds. The idea itself is simple — parameters change with an unobserved regime, and the regime follows a Markov chain. All the trouble lives in the word “unobserved”: the regime cannot be seen, so the entire methodology revolves around inferring its probability from the data. That inference comes in two versions — using only data up to today (filtering) and using the whole sample (smoothing) — and the distinction will run through this article: estimation depends on it, the true lag of any trading signal depends on it, and so does the gulf between hindsight and real time.

This article derives the Hamilton (1989) Markov-switching model and works through the filtering and smoothing recursions, EM estimation, and the use of regime probabilities. References are Hamilton (1994, Ch. 22), Krolzig (1997), and Tsay (2010, Ch. 5).

The Basic Setup

A two-regime Markov-switching AR(pp) model:

yt=cSt+ϕ1,Styt−1+ϕ2,Styt−2+⋯+ϕp,Styt−p+σStεty_t = c_{S_t} + \phi_{1, S_t} y_{t-1} + \phi_{2, S_t} y_{t-2} + \cdots + \phi_{p, S_t} y_{t-p} + \sigma_{S_t} \varepsilon_t

where St∈{1,2}S_t \in \{1, 2\} is the unobserved regime and εt∼iid(0,1)\varepsilon_t \sim \text{iid}(0, 1). The intercept, the AR coefficients, and the volatility can all switch with the regime — in applied work you often let only some of them switch, say the mean alone or the variance alone.

The regime evolves as a Markov chain with transition matrix

P=(p11p12p21p22)\mathbf{P} = \begin{pmatrix} p_{11} & p_{12} \\ p_{21} & p_{22} \end{pmatrix}

where pij=P(St=j∣St−1=i)p_{ij} = P(S_t = j \mid S_{t-1} = i). The typical reading on financial data: regime 1 is low-volatility, “normal”; regime 2 is high-volatility, “crisis”. Estimated p11p_{11} and p22p_{22} usually come out near 1 — regimes are persistent, and once you enter one you stay for 1/(1−pii)1/(1 - p_{ii}) periods on average. That persistence is exactly what separates this model from a mixture model that flips an independent coin every period, and it is where the model’s forecasting content comes from.

Before doing anything else, deal with an unglamorous problem that bites: without a constraint, the model is unidentified. Swap the labels 1 and 2 everywhere and the likelihood is unchanged, so the optimizer has no reason to settle on either labeling and can even swap labels mid-iteration. The fix is a convention that breaks the symmetry — declare regime 1 the lower-variance one (σ1<σ2\sigma_1 < \sigma_2), or the lower-mean one; most software uses the numerical version of the same idea, reordering the regimes by some criterion at each step. This is not theoretical fussiness; unhandled, it makes EM output uninterpretable.

Hamilton’s 1989 Model

Hamilton’s original application was quarterly US real GNP growth, and his specification was more restrained than the general form above: two regimes, with only the mean switching — AR coefficients and variance fixed across regimes. In demeaned form, with y~t=yt−μSt\tilde{y}_t = y_t - \mu_{S_t},

y~t=∑j=14ϕjy~t−j+σεt\tilde{y}_t = \sum_{j=1}^4 \phi_j \tilde{y}_{t-j} + \sigma \varepsilon_t

Regime 1 is expansion (μ1>0\mu_1 > 0), regime 2 recession (μ2<0\mu_2 < 0). Note that the mean enters the lags through y~t−j=yt−j−μSt−j\tilde{y}_{t-j} = y_{t-j} - \mu_{S_{t-j}}, so the likelihood depends on the current and four lagged regimes jointly — the filter has to track 25=322^5 = 32 composite states. That is why the model is computationally heavier than it looks.

The result is what made the paper famous: with no NBER information whatsoever, the recession probabilities estimated purely from GNP data lined up closely with the recessions the NBER dated after the fact. Both regimes were highly persistent (piip_{ii} near 1) and the recession regime had distinctly negative mean growth. The methodological point matters more than the numbers: the business cycle behaves like discrete regime shifts rather than the symmetric fluctuations of a linear model, and “recession” went from a committee’s retrospective verdict to a statistical object whose probability can be estimated in real time.

Filtering: Computing the Regime Probabilities

The regime is invisible; what can be computed is its conditional probability. The forward pass is the Hamilton filter. Write the predicted probability ξt∣t−1(j)=P(St=j∣yt−1,…,y1)\xi_{t|t-1}(j) = P(S_t = j \mid y_{t-1}, \ldots, y_1) and the filtered probability ξt∣t(j)=P(St=j∣yt,…,y1)\xi_{t|t}(j) = P(S_t = j \mid y_t, \ldots, y_1) — the only difference is whether the current observation has been seen.

Each step of the recursion does two things. First, push last period’s filtered probability through the Markov transition:

ξt∣t−1(j)=∑i=1Mpij ξt−1∣t−1(i)\xi_{t|t-1}(j) = \sum_{i=1}^M p_{ij} \, \xi_{t-1|t-1}(i)

Then update on the current observation. The observation density in regime jj is

f(yt∣St=j,yt−1,…,y1)=ϕ(yt;μj,t,σj2)f(y_t \mid S_t = j, y_{t-1}, \ldots, y_1) = \phi(y_t; \mu_{j,t}, \sigma_j^2)

where μj,t\mu_{j,t} is the conditional mean from the AR(pp) recursion under regime-jj parameters and ϕ(⋅;μ,σ2)\phi(\cdot; \mu, \sigma^2) is the normal density. Then

ξt∣t(j)=f(yt∣St=j) ξt∣t−1(j)∑i=1Mf(yt∣St=i) ξt∣t−1(i)\xi_{t|t}(j) = \frac{f(y_t \mid S_t = j) \, \xi_{t|t-1}(j)}{\sum_{i=1}^M f(y_t \mid S_t = i) \, \xi_{t|t-1}(i)}

which is one standard Bayesian update: prior ξt∣t−1(j)\xi_{t|t-1}(j) times likelihood, normalized. The whole filter is “transition, update” alternating from t=1t = 1 to TT.

The likelihood comes free with the filter. Each period’s integrated density — the predictive density of yty_t with the regime integrated out — is exactly the denominator of the update:

f(yt∣yt−1,…,y1)=∑j=1Mf(yt∣St=j) ξt∣t−1(j)f(y_t \mid y_{t-1}, \ldots, y_1) = \sum_{j=1}^M f(y_t \mid S_t = j) \, \xi_{t|t-1}(j)

The sample likelihood is their product:

L=∏t=1Tf(yt∣yt−1,…,y1)L = \prod_{t=1}^T f(y_t \mid y_{t-1}, \ldots, y_1)

and taking logs,

ℓ=∑t=1Tlog⁡f(yt∣yt−1,…,y1)\ell = \sum_{t=1}^T \log f(y_t \mid y_{t-1}, \ldots, y_1)

One pass of the filter delivers the regime probabilities and the likelihood together.

Smoothing: Looking Back with the Full Sample

Filtered probabilities use only data up to tt. For after-the-fact analysis you hold the whole sample, and the right question is ξt∣T(j)=P(St=j∣yT,…,y1)\xi_{t|T}(j) = P(S_t = j \mid y_T, \ldots, y_1) — the smoothed probability. The Kim smoother (Kim, 1994) runs backward from TT to 11 after the filter:

ξt∣T(i)=ξt∣t(i)∑j=1Mpij ξt+1∣T(j)ξt+1∣t(j)\xi_{t|T}(i) = \xi_{t|t}(i) \sum_{j=1}^M \frac{p_{ij} \, \xi_{t+1|T}(j)}{\xi_{t+1|t}(j)}

The intuition: future observations also have something to say about which regime prevailed at tt — if markets collapse after t+1t+1, the probability that tt was already inside the crisis regime should be revised up. Smoothed probabilities plot much cleaner than filtered ones, with crisper regime boundaries, precisely because they have peeked at the future. That peeking will be billed later.

Estimation by EM

How are the parameters estimated? Direct numerical optimization of ℓ\ell works but is unpleasant: the surface is multimodal, badly so with more than two regimes. The EM algorithm (Dempster, Laird & Rubin, 1977) is more robust on latent-variable models of this kind, and both of its steps have closed forms here.

E-step: with the current parameters, run the filter and smoother to get ξt∣T(j)\xi_{t|T}(j) and the smoothed joint probability of adjacent regimes, P(St=i,St+1=j∣yT,…,y1)P(S_t = i, S_{t+1} = j \mid y_T, \ldots, y_1), which assembles directly from the filter and smoother output:

P(St=i,St+1=j∣yT,…,y1)=ξt∣t(i) pij ξt+1∣T(j)ξt+1∣t(j)P(S_t = i, S_{t+1} = j \mid y_T, \ldots, y_1) = \frac{\xi_{t|t}(i) \, p_{ij} \, \xi_{t+1|T}(j)}{\xi_{t+1|t}(j)}

M-step: treat the smoothed probabilities as weights and update the parameters. The regime-jj AR coefficients are a weighted least squares:

ϕ^j=(∑t=p+1Tξt∣T(j) xt−1xt−1′)−1(∑t=p+1Tξt∣T(j) xt−1yt)\hat{\phi}_j = \left( \sum_{t=p+1}^T \xi_{t|T}(j) \, \mathbf{x}_{t-1} \mathbf{x}_{t-1}' \right)^{-1} \left( \sum_{t=p+1}^T \xi_{t|T}(j) \, \mathbf{x}_{t-1} y_t \right)

with xt−1=(1,yt−1,…,yt−p)′\mathbf{x}_{t-1} = (1, y_{t-1}, \ldots, y_{t-p})'. The variance is a weighted sum of squared residuals:

σ^j2=∑t=p+1Tξt∣T(j) (yt−xt−1′ϕ^j)2∑t=p+1Tξt∣T(j)\hat{\sigma}_j^2 = \frac{\sum_{t=p+1}^T \xi_{t|T}(j) \, (y_t - \mathbf{x}_{t-1}' \hat{\phi}_j)^2}{\sum_{t=p+1}^T \xi_{t|T}(j)}

And the transition probabilities are “expected transitions from ii to jj” over “expected time spent in ii”:

p^ij=∑t=1T−1P(St=i,St+1=j∣yT,…,y1)∑t=1T−1ξt∣T(i)\hat{p}_{ij} = \frac{\sum_{t=1}^{T-1} P(S_t = i, S_{t+1} = j \mid y_T, \ldots, y_1)}{\sum_{t=1}^{T-1} \xi_{t|T}(i)}

Every formula is the softened version of “what you would do if the regimes were visible”: with visible regimes you would regress within each regime; with invisible ones you weight by the regime probabilities. Alternate E and M until convergence. EM is monotone — the likelihood never decreases — but monotone does not mean global; it settles into local maxima all the same.

Which makes initialization a genuine engineering problem. The standard remedy is multiple random starts: run EM from 50-100 random initializations and keep the highest likelihood. Alternatives include K-means clustering on simple statistics (mean, variance) to initialize the regime parameters, or fitting a 2-regime model first and adding regimes one at a time with warm starts. Computationally each EM iteration is O(TM2p)O(T M^2 p) — linear in TT, quadratic in the number of regimes MM. With M=4M = 4 and T=1000T = 1000 an iteration is instant; with M=10M = 10 and T=106T = 10^6 you will wait.

How Many Regimes

AIC and BIC apply in principle (BIC is stingier), but first count how fast the parameters grow: a fully-switching MM-regime MS-AR(pp) has p+2p + 2 parameters per regime (intercept, pp AR coefficients, variance) plus M(M−1)M(M-1) free transition probabilities, for M(p+2)+M(M−1)M(p+2) + M(M-1) in total. A four-regime AR(4) is 4×6+12=364 \times 6 + 12 = 36 parameters — not astronomical, but plenty to overfit with.

What deserves more weight than any information criterion is economic discipline. Financial applications start at M=2M = 2 (low-vol / high-vol), the most common and most stable specification; add a regime only when it has a clear economic interpretation and the data insist. Beware spurious regimes — regimes the sample picks out that never recur out of sample; they are usually absorbing one unrepeatable stretch of history (an oil crisis, a pandemic) rather than describing structure that recurs. The diagnostic is unpretentious: plot the smoothed probabilities and check that each regime’s high-probability episodes correspond to recognizable economic events; check that the parameter differences across regimes tell an economic story; and check that the implied average duration 1/(1−p^ii)1/(1 - \hat{p}_{ii}) is the right order of magnitude — a recession regime’s duration should land near the roughly 10-month average of postwar US recessions. If a regime has no story, retreat to a simpler specification.

Forecasting

Forecasting the future regime is just iterating the transition matrix: ξT+h∣T(j)=∑iξT+h−1∣T(i) pij\xi_{T+h|T}(j) = \sum_i \xi_{T+h-1|T}(i) \, p_{ij}, and as h→∞h \to \infty this converges to the chain’s stationary distribution — the further out you forecast, the less the model knows, until only the unconditional regime shares remain. The point forecast of yT+hy_{T+h} weights the regime-specific conditional means by the predicted regime probabilities:

y^T+h=E[yT+h∣yT,…,y1]=∑j=1MξT+h∣T(j)⋅μj,T+h\hat{y}_{T+h} = E[y_{T+h} \mid y_T, \ldots, y_1] = \sum_{j=1}^M \xi_{T+h|T}(j) \cdot \mu_{j, T+h}

The forecast carries regime uncertainty automatically. Be clear about its fragility too: it is highly sensitive to the most recent regime probabilities, and regime changes tend to be obvious in retrospect and murky in real time — exactly when the model is mid-switch, the filtered probabilities waver most.

Code: Markov Switching in statsmodels

import pandas as pd
from statsmodels.tsa.regime_switching.markov_regression import MarkovRegression


model = MarkovRegression(y, k_regimes=2, trend='c', switching_ar=False)
result = model.fit()
print(result.summary())

# Smoothed regime probabilities
print(result.smoothed_marginal_probabilities.head())

# Regime-specific parameters
for i in range(2):
    print(f"Regime {i}: mean={result.params[f'const[{i}]']:.3f}, "
          f"variance={result.params[f'sigma2[{i}]']:.3f}")

# Forecast
forecast = result.forecast(steps=12)

statsmodels supports the basic Markov-switching models well; for fancier specifications (factor switching, time-varying transition probabilities) you will likely be writing your own.

An Equity Example, and the Filtered-vs-Smoothed Bill

Fit a 2-regime mean-and-variance-switching model (MS-AR(0)) to a long sample of monthly index returns:

rt=cSt+σStεtr_t = c_{S_t} + \sigma_{S_t} \varepsilon_t

The typical shape of the estimates: one regime with positive mean and low volatility — the bull market; the other with negative mean and roughly double the volatility — the bear; both regimes with staying probabilities above 0.9, and the bull regime lasting longer on average than the bear. The specific numbers move with the sample period and the market — do not memorize any particular set as constants. Plot the smoothed probabilities and the bear regime’s high-probability episodes will fall on the recognizable historical drawdowns (mid-1970s, early 1980s, 2000-02, 2008-09, around 2020). That is what the model looks like when it is working.

From here it is tempting to write a rule: cut equity exposure when the bear-regime probability crosses a threshold, add it back when it recedes. You can — but first settle the bill flagged at the start: the clean regime partition in your backtest is the smoothed probability, which used future data; real-time trading only has the filtered probability. Filtered probabilities react to regime changes systematically later than smoothed ones — in the first months of a turn into a bear market, the filter is often still hesitating. So any signal quality assessed on historical smoothed probabilities is an upper bound live trading will not reach; rerun honestly on filtered probabilities and expect the signal later and the performance discounted. That is not a defect of this model — it is a bill every hidden-state model pays — but the Markov-switching chart is unusually good at fooling people, because the smoothed regime edges look so clean.

The same framework becomes a different application with a different variable: on short-term interest rates the two regimes track easing versus tightening, informative for fixed-income allocation; on inflation they track the Great Moderation versus high-volatility inflation, useful for forecasting under structural change; on volatility, MS-GARCH — each regime with its own (ω,α,β)(\omega, \alpha, \beta), Hamilton & Susmel (1994) — serves risk management, raising capital requirements when the crisis-regime probability runs high. All these applications share one discipline: real-time decisions use filtered probabilities; smoothed probabilities are for telling the story afterward.

Variants and Boundaries

A ring of variants around the basic model is worth knowing by name. Time-varying transition probabilities (Diebold, Lee & Weinbach, 1994) let pijp_{ij} depend on observables — useful when regime changes correlate with business-cycle indicators. Markov-switching VAR (Krolzig, 1997) carries the machinery to multivariate systems for business-cycle analysis with several macro variables; add a factor structure and you have Markov-switching dynamic factor models. Looking outward, the Markov-switching model is itself a hidden Markov model (HMM) — the one whose emission distribution happens to be conditionally Gaussian; general HMMs allow any emission distribution, Poisson for count data for instance. Looking toward simplicity, there are two close cousins: threshold AR (TAR) sets the regime by a deterministic rule on past values (St=1S_t = 1 if yt−1>0y_{t-1} > 0, say) — easy to estimate, rigid; smooth transition AR (STAR) makes the regime weight a smooth function of past values, the continuous analog of Markov switching. The dividing line is whether the regime is determined by observables: TAR and STAR regimes are visible; a Markov-switching regime can only ever be inferred.

On the estimation side there is also a Bayesian route (Kim & Nelson, 1999 is the standard reference). MCMC delivers full posteriors for parameters and regime probabilities: uncertainty quantified naturally, label switching handled through prior constraints, small-sample inference more reliable — at the cost of computation and sensitivity to prior specification. In practice ML via EM is the default; reach for Bayes when model uncertainty genuinely matters, as in risk management.

After the Fit: Validation

Fitting is not the finish line. At minimum: hold out the last stretch of the sample (say 20%) and generate genuine out-of-sample one-step forecasts against actuals; compare the model’s predictive likelihood on the holdout against naive benchmarks (constant mean, plain AR) — a regime model that cannot beat the benchmarks does not deserve deployment; when an external regime classification exists (NBER recession dates are ready-made), compute hit rates against it; re-estimate on rolling windows and watch whether regime parameters hold still — large drift signals structural breaks, the one failure mode this model class cannot escape either; and check that standardized residuals are near iid, since anything else means misspecification. Above all, the old rule governs the whole exercise: every out-of-sample evaluation uses filtered probabilities — keep the hindsight of the smoother out of it.

References

  • Dempster, A. P., Laird, N. M., & Rubin, D. B. (1977). Maximum likelihood from incomplete data via the EM algorithm. Journal of the Royal Statistical Society B, 39(1), 1-38.
  • Diebold, F. X., Lee, J.-H., & Weinbach, G. C. (1994). Regime switching with time-varying transition probabilities. In C. Hargreaves (Ed.), Nonstationary Time Series Analysis and Cointegration. Oxford University Press.
  • Hamilton, J. D. (1989). A new approach to the economic analysis of nonstationary time series and the business cycle. Econometrica, 57(2), 357-384.
  • Hamilton, J. D. (1994). Time Series Analysis. Princeton University Press. Ch. 22.
  • Hamilton, J. D., & Susmel, R. (1994). Autoregressive conditional heteroskedasticity and changes in regime. Journal of Econometrics, 64(1-2), 307-333.
  • Kim, C.-J. (1994). Dynamic linear models with Markov-switching. Journal of Econometrics, 60(1-2), 1-22.
  • Kim, C.-J., & Nelson, C. R. (1999). State-Space Models with Regime Switching: Classical and Gibbs-Sampling Approaches with Applications. MIT Press.
  • Krolzig, H.-M. (1997). Markov-Switching Vector Autoregressions: Modelling, Statistical Inference, and Application to Business Cycle Analysis. Springer.
  • Tsay, R. S. (2010). Analysis of Financial Time Series (3rd ed.). Wiley. Ch. 5.