Regime-Switching Model Calculator — Hamilton Markov Filter
Run a two-regime Hamilton Markov-switching model: filtered and smoothed regime probabilities, Viterbi decoding, log-likelihood, and expected regime durations.
Regime Switching Hamilton Markov Engine
A clear path from inputs to results.
Fit a two-regime Hamilton Markov-switching model to a return series and watch the machinery that recovers the hidden state. A latent Markov chain switches between a calm regime and a turbulent one, each with its own mean and volatility, and the observable is conditionally normal. The engine runs the Hamilton filter, the Kim smoother, and the Viterbi decoder on a deterministic synthetic series, reporting the log-likelihood, the probability of being in the high-volatility regime, the decoded path accuracy, and the expected time spent in each regime.
How to use
- Pick a scenario (balanced, recession, volatile, or bull) or set the parameters directly; each scenario preloads a plausible pair of regime means and volatilities.
- Set the two regime means mu0 and mu1 and the two regime volatilities sigma0 and sigma1, with regime 1 typically the higher-volatility state.
- Set the persistence probabilities p00 and p11, the chances of staying in regime 0 and regime 1 from one period to the next; higher values make regimes longer-lived.
- Set the initial probability of starting in regime 1 and the horizon (number of periods) for the synthetic series.
- Read the Hamilton filtered probability of the high-volatility regime over time, the Kim smoothed probability that uses the whole sample, and the Viterbi most-likely regime path.
- Read the log-likelihood, the Viterbi decoding accuracy against the known hidden states, and the expected regime durations 1/(1-p00) and 1/(1-p11).
The formula
Reference: Hamilton (1989), "A New Approach to the Economic Analysis of Nonstationary Time Series and the Business Cycle", Econometrica 57(2), 357-384; Kim (1994) smoothing recursion; Hamilton, Time Series Analysis (1994), ch. 22.
Worked example
Balanced two-regime model over 180 periods
- Regime 0 (calm): mean mu0 = -0.08, volatility sigma0 = 0.55. Regime 1 (turbulent): mean mu1 = 0.42, volatility sigma1 = 1.1. Persistence p00 = 0.92, p11 = 0.91; initial probability of regime 1 = 0.35; horizon 180 periods; synthetic-data seed 20260624.
- 1
The transition matrix is P = [[0.92, 0.09], [0.08, 0.91]] (columns are the previous state), giving expected durations 1/(1-0.92) = 12.5 periods in the calm regime and 1/(1-0.91) = 11.11 periods in the turbulent one. Its stationary distribution solves pi = P*pi: pi0 = (1-p11)/((1-p00)+(1-p11)) = 0.09/0.17 = 0.5294 and pi1 = 0.08/0.17 = 0.4706. A deterministic xorshift32 generator then produces the 180-point series, and the Hamilton filter runs prediction xi_{t|t-1} = P*xi_{t-1|t-1}, Gaussian emission weighting, and renormalization at each step, accumulating log L = sum of log of the normalizing constants = -223.696254 (average -1.2428 per period). The Kim smoother and Viterbi decoder then run backward; the decoded path matches the true hidden regime 79.4% of the time, and the final filtered and smoothed probabilities of the turbulent regime are both 0.1161.
Log-likelihood -223.70 (average -1.2428 per period); final probability of the high-volatility regime 0.1161 (filtered and smoothed agree at the last point); Viterbi decoding accuracy 79.4%; expected durations 12.5 and 11.1 periods; stationary regime split 52.9% calm / 47.1% turbulent.
Interpreting the result
The filtered probability is the real-time estimate of the regime using data up to each point; the smoothed probability revises it with the benefit of the full sample and is what you read for a historical regime chronology. They coincide at the final period by construction, since there is no future data to add there. The log-likelihood is the objective a real calibration would maximize to fit the parameters, and the expected durations translate the persistence probabilities into an intuitive average regime length. A Viterbi accuracy well above the 50% coin-flip baseline shows the two regimes are statistically separable given their means and volatilities.
Assumptions & limitations
- Exactly two regimes with Gaussian emissions; the observable is conditionally normal with a regime-specific mean and volatility, and richer dynamics (AR terms, fat tails, more than two states) are not modeled.
- The regime follows a first-order, time-homogeneous Markov chain, so transition probabilities are constant and the next regime depends only on the current one.
- The series analyzed is deterministic synthetic data generated from a seeded xorshift32 PRNG, so results are exactly reproducible but are an illustration, not a fit to your own data.
- Parameters are supplied, not estimated: the tool runs the filter, smoother, and decoder at the given parameters rather than maximizing the likelihood over them.
- Expected durations assume geometric regime lengths implied by constant persistence, so 1/(1-p_jj) is a mean, not a fixed dwell time.
Frequently asked questions
What is the difference between the filtered and smoothed probabilities?
The filtered probability at time t uses only observations up to t, so it is the best real-time estimate of the regime as data arrives. The smoothed probability uses the entire sample, including later observations, so it is sharper and is the right quantity for dating past regimes. The two necessarily agree at the final observation, because there is no later data to incorporate.
What does the Viterbi path add over the probabilities?
The filtered and smoothed outputs are marginal probabilities period by period. The Viterbi algorithm instead finds the single most likely sequence of regimes for the whole sample, respecting the transition dynamics, which can differ from stringing together the per-period maxima. Here that decoded path matches the true hidden states 79.4% of the time, a measure of how separable the two regimes are.
What is the stationary distribution and how is it found?
The stationary distribution is the long-run fraction of time spent in each regime, the vector pi satisfying pi = P*pi. For a two-state chain it has the closed form pi0 = (1-p11)/((1-p00)+(1-p11)), giving 52.9% calm and 47.1% turbulent here. It is the regime split you would expect over a very long sample, independent of the starting state.
How is the log-likelihood computed?
At each step the Hamilton filter predicts the regime probabilities, weights them by the Gaussian likelihood of the current observation in each regime, and the sum of those weighted terms is the one-step predictive density. The log-likelihood is the sum of the logs of those normalizing constants across all periods, -223.70 here. A real calibration maximizes exactly this quantity over the means, volatilities, and transition probabilities.
How do the persistence probabilities relate to regime length?
If a regime stays with probability p each period, its length is geometric with mean 1/(1-p). So p00 = 0.92 implies the calm regime lasts about 12.5 periods on average and p11 = 0.91 implies the turbulent regime lasts about 11.1 periods. Raising a persistence probability lengthens that regime and makes switches rarer.
Can I use this on my own return data?
This engine runs on a deterministic synthetic series so its output is reproducible and the mechanics are transparent. Applying Hamilton switching to your own data additionally requires estimating the parameters by maximizing the log-likelihood (often via the EM algorithm), which this tool illustrates but does not perform. The filter, smoother, and decoder shown here are the same routines such an estimation would call internally.
Methodology last reviewed 2026-10-06. Results are informational and not financial or engineering advice — see our disclaimer.