Markov Chain Models
Hidden Markov models (HMMs) treat observed life-course sequences as emissions from unobserved latent states. Instead of comparing whole trajectories with a distance matrix, you specify a generative model: how likely each hidden state is at the start, how likely transitions between hidden states are, and how each hidden state produces observed states.
These pages document sequenzo.seqhmm, Sequenzo's Python implementation inspired by the R seqHMM package (Helske & Helske, 2019). The API follows seqHMM's workflow of building, fitting, predicting, and visualizing models while using Python conventions and SequenceData as the main input type.
What You Need Before You Start
Most pages assume that you already have:
A
SequenceDataobject with one row per case and one column per time point (or a list ofSequenceDataobjects for multichannel HMM).A clear research question about latent dynamics: recurring hidden regimes, mixture clusters with different dynamics, or covariate-dependent transition/emission probabilities.
If you are new to HMMs, start with the Conceptual Guides:
Model Types in This Module
| Model | Main build function | Typical question |
|---|---|---|
| Basic HMM | build_hmm() | What latent regimes generate the observed sequences? |
| Mixture HMM (MHMM) | build_mhmm() | Are there distinct subgroups, each with its own HMM? |
| Non-homogeneous HMM (NHMM) | build_nhmm() | Do transition or emission probabilities depend on covariates or time? |
| Mixture non-homogeneous HMM (MNHMM) | build_mnhmm(), estimate_mnhmm() | Are there latent subgroups whose HMM probabilities also depend on covariates? |
All fitted models share the same high-level workflow:
- Build the model structure (
build_hmm,build_mhmm,build_nhmm, orbuild_mnhmm). - Fit parameters with EM or numerical optimization (
fit_model,fit_mhmm,fit_nhmm,estimate_mnhmm, orfit_model_advanced). - Predict latent states or cluster membership (
predict,predict_mhmm,posterior_probs,posterior_probs_mhmm, orhidden_paths). - Evaluate with AIC/BIC (
aic,bic,compare_models). For HMM, MHMM, and NHMM, optional bootstrap summaries are available throughbootstrap_model(). - Visualize estimated parameters (
plot_hmm,plot_mhmm).
Basic HMM Workflow
- Prepare sequence data. Build
SequenceDatawith the correct state alphabet and time columns. - Choose the number of hidden states. Start with a small range (for example 3–6) and compare models with BIC.
- Build and fit. Call
build_hmm()thenfit_model(). Setrandom_statefor reproducible initialization. - Inspect fit quality. Check
model.log_likelihood,model.converged, andmodel.n_iter. - Decode latent paths. Use
predict()for the Viterbi path orposterior_probs()for state probabilities at each time point. - Plot parameters. Use
plot_hmm(model, which='network')for a seqHMM-style graph, orwhich='all'for matrix views.
How This Differs from Distance-Based Analysis
Distance-based tools (clustering, discrepancy analysis, group comparison with LRT/BIC on distances) summarize how different observed sequences are. HMMs instead estimate a generative mechanism: latent states, transitions, and emissions.
Use HMMs when you want interpretable latent dynamics or mixture clusters defined by Markov structure. Use distance-based methods when your substantive question is about overall trajectory dissimilarity without a latent-state story.
Included Pages
- Conceptual Guides: Markov chain, HMM, and MHMM in plain language
- Sequenzo–seqHMM Mapping: correspondence with the R seqHMM package
- Basic HMM:
build_hmm(),fit_model(),predict(),posterior_probs(),plot_hmm() - Mixture HMM:
build_mhmm(),fit_mhmm(),predict_mhmm(),posterior_probs_mhmm(),plot_mhmm() - Non-homogeneous HMM:
build_nhmm(),fit_nhmm() - Mixture non-homogeneous HMM:
build_mnhmm(),estimate_mnhmm() - Model comparison:
aic(),bic(),compare_models() - Simulation:
simulate_hmm(),simulate_mhmm(),simulate_nhmm(),simulate_mnhmm() - Advanced tools:
bootstrap_model()for HMM, MHMM, and NHMM;fit_model_advanced()
Implementation Notes and Scope
- Covariate-dependent mixture weights are handled by MNHMM through
cluster_formula/X_cluster; usebuild_mhmm()/fit_mhmm()for mixture HMMs without mixture-weight covariates. - NHMM and MNHMM builder formula strings are passed through
patsy, so interactions and inline transforms such asnp.log(...)are supported in design-matrix construction.lag()is available for time-varying formula matrices, with restrictions such as no lag terms ininitial_formula;cluster_formulamust be time-constant. Simulation helpers such assimulate_nhmm()use simpler formula parsing and should use plain column names. - Multichannel MHMM and MNHMM estimation is computationally demanding on large samples because parts of the EM workflow are implemented in Python.
- MNHMM expects complete sequence observations so that time, channel, and covariate arrays remain aligned.
See Also
- Markov Chain, HMM, and MHMM are the conceptual guides.
- Sequence Analysis vs. LCA vs. HMM helps decide whether an HMM fits the research question.
- Model Comparison compares fitted models with AIC and BIC.
Authors
Code: Yuqi Liang, Yapeng Wei
Documentation: Yuqi Liang
References
Helske, S., & Helske, J. (2019). Mixture hidden Markov models for sequence data: The seqHMM package in R. Journal of Statistical Software, 88(3), 1–32. https://doi.org/10.18637/jss.v088.i03