Skip to content

    Under active development. New calculators and features ship every week. Everything currently runs free while the site matures — pricing pages are a preview, checkout is not open yet.

    Finance / Hamilton (1989) two-state Markov-switching filter, smoother and Viterbi

    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.

    ToolCalculatorExecutionIn browserCategoryFinancial
    ZORVLABS · REGIME SWITCHING

    Regime Switching Hamilton Markov Engine

    Hamilton-style discrete-state Markov switching with Hamilton filtering, Kim smoothing, Viterbi decoding, and likelihood surfaces.
    Numerical Integrity
    Hamilton filter + Kim smoother
    Deterministic synthetic series for reproducible filtering and estimation views.
    Final filtered P(state 1)
    0.11605
    ξ̂_{T|T}(s=1)
    Final smoothed P(state 1)
    0.11605
    ξ̂_{T|T*}(s=1)
    Average log-likelihood
    -1.24276
    ℓ/T from Hamilton filter
    Viterbi accuracy
    79.44%
    Most likely regime path
    Expected duration state 0
    12.500
    1/(1-p00)
    Expected duration state 1
    11.111
    1/(1-p11)
    Mean observation
    0.1206
    E[y_t] from synthetic sample
    Sample volatility
    0.8669
    Std. dev. of y_t
    Scenario preset
    View mode
    3D surface
    Display scale
    Regime 0 mean μ0
    Regime 1 mean μ1
    Regime 0 volatility σ0
    Regime 1 volatility σ1
    Persistence p00
    Persistence p11
    Initial P(state 1)
    Horizon T
    3D surface X grid
    3D surface Y grid
    Likelihood grid min
    Likelihood grid max
    Synthetic seed
    Log-likelihood
    -223.69625
    ∑ log f(y_t | Ω_{t-1})
    Average LL
    -1.24276
    Hamilton filter objective
    Filtered P1
    0.11605
    ξ̂_{T|T}(s=1)
    Smoothed P1
    0.11605
    ξ̂_{T|T*}(s=1)
    Expected duration 0
    12.500
    1/(1-p00)
    Expected duration 1
    11.111
    1/(1-p11)
    Viterbi accuracy
    79.44%
    Most likely regime path
    Sample std dev
    0.8669
    Observed y_t dispersion
    Hamilton filter: ξ̂_{t|t} ∝ f(y_t|s_t) · P(s_t|Ω_{t-1}) updated by prediction then density weighting, with state likelihoods from Gaussian emissions.
    Kim smoother: ξ̂_{t|T} = ξ̂_{t|t} ⊙ [P^T(ξ̂_{t+1|T} ÷ ξ̂_{t+1|t})] combines filtered and forecast probabilities backward in time for ex post regime inference.
    2D observed path
    Synthetic sample, latent regimes, and regime means
    Observed series with latent regimesty_t0.0035.8071.60107.40143.20179.00-2.73-1.68-0.630.421.472.53Observation y_tRegime 0 meanRegime 1 mean
    2D filter vs smoother
    Hamilton forward filter and Kim backward smoother
    Hamilton filter vs Kim smoothertProbability0.0035.8071.60107.40143.20179.000.020.220.410.610.801.00Filtered P(s=1)Smoothed P(s=1)
    2D state densities
    Gaussian emission densities by regime
    State-conditional emission densitiesObservation y_tf(y_t|s_t)-7.13-4.32-1.511.304.116.931.407e-360.150.290.440.580.73Regime 0Regime 1
    2D transition matrix
    Markov persistence structure
    Transition matrix PColumns = previous state, rows = current stateto 0to 1to 0to 10.920.080.090.91
    3D regime estimation surface
    Log-likelihood over persistence parameters
    3D log-likelihood surface over p00 and p11p00p11ℓsurface grid52 × 52 cells · max |ℓ| = 250.808215
    Source equations
    Hamilton (1989), Hamilton (1994), Kim smoother
    Latent regime: s_t ∈ {0,1} follows a first-order Markov chain with transition matrix P.
    Emission density: y_t | s_t=j ~ N(μ_j, σ_j²).
    Hamilton filter: ξ̂_{t|t} = [ξ̂_{t|t-1} ⊙ f_t] / 1^T[ξ̂_{t|t-1} ⊙ f_t] updates recursively from predictive probabilities and regime-specific densities, and the log-likelihood is the sum of the per-period predictive densities. See the UCSD note’s equations (11)–(14).
    Kim smoother: ξ̂_{t|T} = ξ̂_{t|t} ⊙ [P^T(ξ̂_{t+1|T} ÷ ξ̂_{t+1|t})] recovers ex post regime probabilities using the forward filter and the backward probability recursion.
    finance methodology

    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.

    Step-by-step methodWorked exampleAssumptions explained

    How to use

    1. Pick a scenario (balanced, recession, volatile, or bull) or set the parameters directly; each scenario preloads a plausible pair of regime means and volatilities.
    2. Set the two regime means mu0 and mu1 and the two regime volatilities sigma0 and sigma1, with regime 1 typically the higher-volatility state.
    3. 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.
    4. Set the initial probability of starting in regime 1 and the horizon (number of periods) for the synthetic series.
    5. 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.
    6. 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

    ξt∣t−1=P ξt−1∣t−1,ξt∣t=ξt∣t−1⊙ηt1⊤(ξt∣t−1⊙ηt),ηt,j=12πσjexp⁡ ⁣(−(yt−μj)22σj2),log⁡L=∑t=1Tlog⁡ ⁣[1⊤(ξt∣t−1⊙ηt)],E[durationj]=11−pjj\xi_{t|t-1} = P\,\xi_{t-1|t-1}, \quad \xi_{t|t} = \frac{\xi_{t|t-1} \odot \eta_t}{\mathbf{1}^{\top}(\xi_{t|t-1} \odot \eta_t)}, \quad \eta_{t,j} = \frac{1}{\sqrt{2\pi}\sigma_j}\exp\!\left(-\frac{(y_t-\mu_j)^2}{2\sigma_j^2}\right), \quad \log L = \sum_{t=1}^{T}\log\!\left[\mathbf{1}^{\top}(\xi_{t|t-1}\odot\eta_t)\right], \quad \mathbb{E}[\text{duration}_j] = \frac{1}{1-p_{jj}}

    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

    Inputs
    • 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.
    Calculation path
    1. 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.

    Result

    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.