hmmlearn: Hidden Markov Models in Python
Citation. hmmlearn developers. hmmlearn: Hidden Markov Models in Python, with scikit-learn like API. hmmlearn/hmmlearn, version 0.3.3 of 30 October 2024, under the BSD 3-Clause licence. Documentation. There is no paper. The README says: "This package is under limited-maintenance mode." Its last commit was on 31 October 2024. Compared with statsmodels' Markov switching models (statsmodels.tsa.regime_switching, BSD).
Status. Not applicable. Every public method that returns state probabilities or a state path uses the whole sequence it is given, so it looks ahead. The forward pass that a trading signal needs is only in a private C extension. With limited maintenance as well, the project shouldn't depend on it. For #233–#235, use statsmodels' filtered probabilities, or a short in-house forward filter, refitted on an expanding window.
Read. Read on 2026-11-28 through the GitHub API, at tag 0.3.3: the README, src/hmmlearn/base.py in full, and the file trees of tags 0.2.8, 0.3.0 and 0.3.3. I read the stock example from the history, at commit da0fdf7f (January 2016), and the commit that removed it. I read the names of statsmodels' filtered, smoothed and predicted probabilities in markov_switching.py. I didn't read Rabiner (1989), which is paywalled, and I didn't run either library.
Summary
Claim. "Unsupervised learning and inference of hidden Markov models", with a scikit-learn style API.
What the code does.
- Models. Gaussian, Gaussian-mixture, categorical, multinomial and Poisson emissions, with variational versions in
vhmm. Training is by EM, withn_iter,toland a convergence monitor. score_samples(X)returns the log likelihood and "State-membership probabilities for each sample inX". In the code, it runs_hmmc.forward_logand then_hmmc.backward_logover each sequence. So the probabilities are smoothed: the state at t depends on data after t.predict_proba(X)returns the same posteriors, fromscore_samples.predict(X)anddecode(X)use "viterbi", which "finds the most likely sequence of states, given all emissions". The other option is "map", which "finds the sequence of the individual most-likely states, given all emissions".score(X)returns the log likelihood alone, from the forward pass. It is the one public method that doesn't look ahead.- The forward lattice.
fwdlattice, the filtered quantity, exists only inside the private_score_logand_score_scaling, from the C extension_hmmc. - Model choice.
aic(X)andbic(X).
The stock example. Until 2018 the gallery had "Gaussian HMM of stock data". It took Intel's daily closes from 1995 to 2012. It fitted GaussianHMM(n_components=4, covariance_type="diag", n_iter=1000) to the whole sample, then ran model.predict(X) on the same data. A commit of 1 October 2018 removed it, because "Both the yahoo and google APIs are dead". Its pattern lives on in blog posts, such as the one #233 asks about.
Where the claims go beyond the evidence
"Posterior" doesn't say which. The docstrings call the output of predict_proba "the posterior probability for each state". They don't say that it is the smoothed posterior, given the whole input. A user who passes a full price history gets each day's state given the future.
The example taught the mistake. It fitted the parameters and decoded the states on one full sample, and then plotted the states against prices. Every state in it used data from up to 17 years later. The example was removed because its data source died, not because of that.
EM finds local optima. The fit depends on random_state and on the initial parameters. The code gives one fit per call, with no restarts, so one run shows one optimum.
Limited maintenance. The latest release is 0.3.3, of October 2024, and the README states limited maintenance. A compiled extension with no maintainer can break on a new NumPy or Python, which the Modal image would find first.
Credit. The code is short, and it states its two decoding algorithms plainly, as "given all emissions". score is causal, and bic makes model choice by likelihood easy.
Use in this repo
Filtered probabilities only. A regime signal at t must be P(state at t | data up to t). The forward recursion gives it. Start from α₁ ∝ π · b(x₁). Then, for each t, set αₜ ∝ (αₜ₋₁ A) · b(xₜ). Here π is the start distribution, A the transition matrix and b the emission densities. Normalising αₜ to sum to 1 at each step gives the filtered probabilities. It is about 15 lines in plain Python. statsmodels gives the same quantity as filtered_marginal_probabilities, beside smoothed_marginal_probabilities, so its name is explicit. Calling hmmlearn's predict_proba on each prefix is also correct, because the last row of a prefix is filtered. But it costs O(T²).
Parameters must be causal too. Filtered probabilities from parameters fitted on the whole sample still look ahead. Refit on an expanding window, at most once a month for daily data (splits.py, #10), and filter each day with the latest parameters. That is the same rule as for the regime labels of #328.
Keep labels stable across refits. EM numbers its states arbitrarily, and the numbers can swap at a refit. Sort the states after each fit by a key chosen in advance, such as the emission variance. Then "state 1" always means the calm one.
Test it with the oracle's idea. The probabilities at t must not change when data after t is added. The test computes the signal on the first t days, and on the whole series. It then checks that the first t values are equal, at several t. The look-ahead oracle in open_familyoffice/oracle.py applies the same idea to whole strategies (#87). Smoothed probabilities fail this test, and filtered ones pass.
Every fit is a trial. The number of states, the covariance type, the emission family and each seed are choices (#7). Choose the number of states by BIC or by out-of-sample likelihood (score on the next window), not by how the regimes look on a chart. Report the spread across seeds.
The dependency decision. statsmodels, behind the optional extra that #315 proposes, has the Hamilton filter for Markov switching regressions. An in-house forward filter needs no dependency at all, and is easy to test. PyMC (pymc.md) adds uncertainty about the parameters, at much more cost. hmmlearn's EM is useful only for a quick look, with score and bic, never for the signal.