Skip to content

fit_model()

fit_model() estimates HMM parameters (initial, transition, and emission probabilities) from the observations attached at build time. It uses the Expectation–Maximization (EM) algorithm.

Function Usage

python
fit_model(
    model,
    n_iter=100,
    tol=1e-2,
    verbose=False
)

seqHMM Parameter Mapping

SequenzoseqHMM fit_model()
modelFitted object target (hmm class in R)
n_iterMaximum EM iterations (maxit-style control)
tolLog-likelihood convergence threshold
verbosePrint iteration progress

For global/local optimization steps available in R's fit_model(), use fit_model_advanced() instead.

Entry Parameters

ParameterRequiredTypeDescription
modelHMMModel created by build_hmm().
n_iterintMaximum EM iterations. Default 100.
tolfloatStop when the gain in log-likelihood falls below this value. Default 1e-2.
verboseboolPrint iteration log. Default False.

What It Returns

The same HMM object, modified in place. After fitting, inspect:

AttributeMeaning
log_likelihoodFitted log-likelihood
n_iterNumber of EM iterations run
convergedWhether the tolerance criterion was met
initial_probs, transition_probs, emission_probsEstimated parameters

Example

python
from sequenzo import SequenceData, load_dataset
from sequenzo.seqhmm import build_hmm, fit_model

df = load_dataset("mvad")
seq = SequenceData(df, time=range(15, 86), states=["EM", "FE", "HE", "JL", "SC", "TR"])

hmm = build_hmm(seq, n_states=4, random_state=42)
hmm = fit_model(hmm, n_iter=100, tol=1e-2, verbose=True)

print(hmm.log_likelihood, hmm.converged, hmm.n_iter)

R Counterpart

  • Closest R function: seqHMM fit_model() with EM step only
  • Mapping note: R also supports global_step and local_step; in Sequenzo use fit_model_advanced().

Notes

  • The model must be built with build_hmm() before fitting.
  • If EM stalls below tolerance, increase n_iter or try fit_model_advanced() with random restarts.
  • Multichannel fitting uses a pure-Python EM implementation and may be slower on large samples.

Authors

Code: Yuqi Liang

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.