Reader question: Financial returns can look calm for months and then suddenly behave as if a different process has taken over. How can an algorithm model those changing conditions when the regime itself is never directly observed?
A Hidden Markov Model, or HMM, treats the market regime as an unobserved discrete state. The observed data—returns, spreads, volatility measures or other variables—are generated from a probability distribution that depends on that hidden state. The state itself evolves through a Markov transition matrix.
This article owns the finite-state hidden-regime inference problem: observed financial time series + state-dependent emission distributions + a transition matrix → filtered state probabilities, smoothed state probabilities, most likely state paths, likelihoods, forecasts and diagnostics about whether a regime interpretation is credible.
It does not own GARCH volatility recursion, stochastic-volatility diffusion, structural-break dating or trading rules. Those are separate mathematical jobs. Here the central question is how to infer an unobserved discrete state from noisy observations without pretending that the state labels are directly visible.
This is public mathematics and computational education. It is not personalized financial advice, a market-timing system or a claim that future market regimes can be known with certainty.
1. The hidden-state idea
Let the hidden regime at time t be:
St ∈ {1,2,…,K}.
For a two-state model, we might informally call the states “low-volatility” and “high-volatility”, but those names are interpretations applied after estimation. Mathematically, they are simply state 1 and state 2.
The observed variable Yt is generated from a state-dependent distribution:
Yt | St=k ~ fk(·; θk).
For example, a simple return model may use:
Yt | St=k ~ Normal(μk, σk2).
A high-volatility state could have a larger σ. A stress state could also have a more negative mean or a heavier-tailed emission distribution.
2. The transition matrix controls regime persistence
The hidden state follows a first-order Markov chain:
P(St=j | St−1=i, St−2,…) = P(St=j | St−1=i).
Define the transition matrix:
P = [pij]
where:
pij = P(St=j | St−1=i).
Every row must sum to 1.
In a two-state model:
P = [[p11, 1−p11], [1−p22, p22]].
If p11 and p22 are close to 1, regimes are persistent.
3. Expected regime duration
For a state with self-transition probability pkk, the duration has a geometric form in the basic HMM. The expected number of periods spent in state k before leaving is:
E[Dk] = 1 / (1 − pkk).
If pkk=0.95, the expected duration is 20 periods.
This formula exposes an important assumption: the probability of leaving a state does not depend on how long the process has already been in that state. If real regimes have duration dependence, a hidden semi-Markov model may be more appropriate.
4. Prediction before seeing today’s observation
Suppose the filtered state-probability vector after observing data through time t−1 is:
αt−1(i) = P(St−1=i | Y1:t−1).
Before seeing Yt, predict today’s state probabilities:
qt(j) = Σi αt−1(i)pij.
This is simply matrix propagation through the transition probabilities.
5. Update after observing today’s data
Once Yt arrives, each state’s probability is multiplied by how likely that observation is under the state’s emission distribution:
αt(j) ∝ qt(j) fj(Yt).
Normalize so the probabilities sum to 1:
αt(j) = qt(j)fj(Yt) / Σm qt(m)fm(Yt).
This recursive calculation is the core of the forward filter.
6. Filtered probability is not the same as smoothed probability
A filtered state probability at time t uses information available only through time t:
P(St=k | Y1:t).
A smoothed probability uses the full sample, including observations after time t:
P(St=k | Y1:T).
Smoothing can make historical regimes look much cleaner because later data help reinterpret earlier ambiguous observations.
This creates a major diagnostic boundary. A chart built from smoothed probabilities may be excellent for historical explanation but invalid for a real-time decision rule because it contains future information.
7. The backward recursion
The backward algorithm computes the likelihood of the remaining future observations conditional on each possible current state. Define:
βt(i) = P(Yt+1:T | St=i).
The recursion is:
βt(i) = Σj pij fj(Yt+1) βt+1(j).
Combining forward and backward information produces smoothed state probabilities.
8. Numerical underflow is a real algorithmic failure
Likelihoods are products of many probabilities smaller than 1. Over long samples, naïvely multiplying them can underflow to zero in floating-point arithmetic.
Two common remedies are:
- scaling: normalize forward probabilities at each time step and accumulate the scale factors;
- log-domain computation: use log probabilities and the log-sum-exp identity.
A model whose mathematical likelihood is valid can still fail computationally if this numerical issue is ignored.
9. The Viterbi path answers a different question
Filtered or smoothed probabilities give a probability distribution over states at each date.
The Viterbi algorithm instead asks:
Which complete state sequence S1:T has the highest joint posterior probability?
It uses dynamic programming. At each time and state, it stores the best probability of any path ending in that state, along with a backpointer to the preceding state.
The result is one globally most likely path.
It is possible for the Viterbi state at a particular date to differ from the state with the highest marginal smoothed probability at that date. One optimizes a whole path; the other summarizes a marginal distribution.
10. Maximum likelihood through the forward algorithm
Given parameters θ, the forward recursion yields the probability of the observed sequence:
L(θ) = P(Y1:T | θ).
The log-likelihood can then be maximized numerically.
Parameters may include:
- state-specific means;
- state-specific variances;
- tail parameters;
- transition probabilities;
- autoregressive coefficients;
- covariate effects.
Direct optimization is possible, but HMMs also admit an expectation–maximization procedure.
11. Baum–Welch is an EM algorithm
The Baum–Welch algorithm alternates between two stages.
E-step: using the current parameters, calculate expected state occupancies and expected state-to-state transition counts from the forward–backward probabilities.
M-step: update emission parameters and transition probabilities to maximize the expected complete-data log-likelihood.
For Gaussian emissions, state means and variances can be updated as probability-weighted sample moments. Transition probabilities are updated from expected transition counts.
The likelihood should not decrease from one EM iteration to the next, but EM can converge to a local optimum rather than the global optimum.
12. Initialization is therefore part of the model
Two different starting parameter sets can converge to different local maxima.
A robust calibration routine should therefore use:
- multiple random or structured initializations;
- likelihood comparison across runs;
- parameter sanity checks;
- state-ordering rules for interpretation;
- out-of-sample comparison.
A single successful optimizer status is weak evidence.
13. Label switching
The likelihood is unchanged if state labels are permuted together with their parameters. A two-state solution labelled “1 = calm, 2 = stress” is mathematically equivalent to one labelled “1 = stress, 2 = calm”.
This is label switching.
For reproducible reporting, states can be ordered after estimation using an interpretable statistic such as ascending volatility. That ordering is a reporting convention, not an additional empirical discovery.
14. State count is a model-selection problem
Adding states almost always increases in-sample likelihood. That does not mean the new state is real.
Candidate model comparisons can use:
- AIC;
- BIC;
- out-of-sample log predictive density;
- state-separation diagnostics;
- minimum useful occupancy;
- parameter stability across windows.
If a three-state model creates a tiny state containing only a few unusual observations, it may be modelling outliers rather than a persistent regime.
15. Gaussian emissions can manufacture regimes to explain heavy tails
Suppose returns are actually generated by one heavy-tailed distribution. A two-state Gaussian HMM may split ordinary observations into a low-variance state and extreme observations into a high-variance state.
The algorithm can produce strong-looking regime probabilities even though the true mechanism is continuous heavy-tailed noise rather than discrete state switching.
Falsifier: compare against a one-state Student-t model or other heavy-tailed benchmark. If the discrete regimes lose predictive value once the emission distribution is improved, the original states may have been compensating for misspecified tails.
16. Regime persistence can be exaggerated by autocorrelation misspecification
If the emission process has autocorrelation but the model assumes conditionally independent observations, the HMM may use persistent states to absorb serial structure that belongs inside the emission equation.
Falsifier: inspect residual autocorrelation within each inferred state and compare with a Markov-switching autoregression.
17. A hidden semi-Markov alternative
The geometric duration distribution of a basic HMM implies a constant exit hazard from each state.
A hidden semi-Markov model specifies a more flexible state-duration distribution directly. This is useful if real regimes show duration dependence, minimum lengths or long-tail persistence inconsistent with a geometric law.
18. Time-varying transition probabilities
The basic model assumes a fixed transition matrix. In some applications, transition probabilities can depend on covariates:
pij,t = g(Zt; γ).
For example, a logistic link can map macro or market variables into transition probabilities.
This can improve flexibility, but it also risks turning the model into a highly parameterized classifier. The additional predictors need out-of-sample evidence.
19. Inputs and outputs
Inputs can include:
- time-stamped observations;
- number of states K;
- emission-family choice;
- initial state probabilities;
- transition matrix or transition model;
- initial parameter seeds;
- missing-data policy;
- optimizer or EM settings;
- scaling/log-domain numerical rules;
- training and validation windows.
Outputs can include:
- log-likelihood;
- estimated transition matrix;
- state-specific emission parameters;
- filtered probabilities;
- smoothed probabilities;
- Viterbi path;
- expected state durations;
- one-step-ahead regime forecasts;
- state-conditioned predictive distributions;
- diagnostic residuals;
- uncertainty around parameters and state probabilities.
20. Evidence polarity
Evidence for confidence includes distinct state-dependent distributions, stable transition estimates, meaningful state occupancy, consistent results across multiple starts, better out-of-sample density forecasts than simpler alternatives, residuals without obvious remaining serial structure, and regime probabilities that are not entirely rewritten when a small amount of new data arrives.
Evidence against confidence includes local-maxima sensitivity, nearly identical states, a tiny state that merely captures outliers, implausibly persistent or rapidly switching states, smoothed histories that look excellent while filtered real-time probabilities are poor, unstable labels, strong within-state residual autocorrelation or a simpler heavy-tailed model that performs equally well.
21. Counterexample: hindsight creates an artificial success story
A smoothed regime plot may identify a crisis almost exactly from its first day because observations from later in the crisis help relabel earlier dates.
If that smoothed plot is then presented as if the algorithm detected the crisis in real time, the evaluation contains look-ahead bias.
Falsifier: reconstruct the historical sequence using only filtered probabilities available on each date.
22. Counterexample: too many states
A five-state HMM can often fit a return series better than a two-state HMM in sample. But if two states have almost identical emission parameters and unstable transition probabilities, the extra complexity is not creating meaningful information.
Falsifier: remove one state and compare out-of-sample likelihood, forecast calibration and state interpretability.
23. Counterexample: nonstationary transitions
A transition matrix estimated across a long historical period can average together structurally different eras. The model then reports a precise long-run persistence estimate that never truly held in any one period.
Falsifier: estimate the transition matrix across rolling or subperiod samples and test whether changes exceed sampling uncertainty.
24. Counterexample: discrete states imposed on a continuous process
Volatility may evolve continuously rather than jumping between a few regimes. An HMM discretizes that continuum into states.
If state boundaries shift dramatically with the chosen number of states, a continuous-state stochastic-volatility model or particle filter may describe the mechanism more naturally.
25. Diagnostic ladder
- Verify timestamps, missing observations and data transformations.
- Fit a one-state benchmark.
- Fit the candidate HMM from many initializations.
- Check monotonic likelihood improvement for EM.
- Inspect transition rows and implied durations.
- Compare state emission distributions.
- Check state occupancy and entropy.
- Inspect filtered—not only smoothed—probabilities.
- Test residual autocorrelation and tail behavior within states.
- Compare Gaussian and heavy-tailed emissions.
- Compare different state counts.
- Evaluate out-of-sample log likelihood and calibration.
- Stress transition estimates across subperiods.
- Compare with continuous-state alternatives.
26. Connections to the surrounding Bukit Timah Tutor knowledge estate
The volatility side connects directly to GARCH and EWMA volatility forecasting. GARCH keeps one continuous variance recursion; an HMM instead places probability mass over discrete states.
Model validation connects to bank risk-model validation, especially benchmarking, challenger models and monitoring.
Distribution-level evaluation connects to the site’s VaR backtesting article, where hidden-state forecasts can ultimately be judged by realized outcomes rather than attractive regime charts.
The full lane is indexed at Finance & Banking Algorithms | Applied Mathematics in Real Financial Systems.
27. What would falsify confidence?
Confidence should be withdrawn if the state solution is highly sensitive to initialization; if states are not statistically or economically distinguishable; if filtered real-time probabilities fail while smoothed histories look clean; if persistence estimates are unstable; if residual dependence remains; if a heavy-tailed one-state model matches or beats the HMM; or if out-of-sample predictive density deteriorates when extra states are added.
28. Verification and update triggers
Preserve model specification, state count, emission family, transition parameterization, initialization seeds, optimizer settings, training window and every filtered/smoothed probability series. Re-run validation after material changes in data frequency, market structure, emission family, transition model or feature set.
Trigger review when state occupancy collapses, transition probabilities move materially, filtered probabilities become persistently ambiguous, residual tests worsen, out-of-sample density scores deteriorate, a new regime appears to be repeatedly misclassified, or the chosen state count loses stability across adjacent samples.
29. Primary and high-quality references
- James D. Hamilton, A New Approach to the Economic Analysis of Nonstationary Time Series and the Business Cycle, Econometrica, 1989.
- Lawrence R. Rabiner, A Tutorial on Hidden Markov Models and Selected Applications in Speech Recognition, Proceedings of the IEEE, 1989. The application is speech, but the forward, backward and Viterbi machinery is general.
- Stan documentation, Hidden Markov Models, for a modern computational statement of emission matrices, transition matrices and marginal state probabilities.
- Statsmodels documentation, MarkovRegression, for a practical econometric implementation.
- Federal Reserve Board and OCC, SR 11-7 — Guidance on Model Risk Management, for independent validation, model limitations and ongoing monitoring.
Educational boundary: Hidden-state probabilities are conditional model outputs, not observed facts. A regime label becomes useful only when the state specification survives alternative models, real-time evaluation and out-of-sample verification.

