Skip to content

fit_model_advanced()

fit_model_advanced() runs a multi-stage optimization pipeline: optional EM warm-start, global search, local refinement, and random restarts. Use it when standard fit_model() EM stalls in a poor local optimum.

Function Usage

python
fit_model_advanced(
    model,
    em_step=True,
    global_step=False,
    local_step=False,
    n_iter=100,
    tol=1e-2,
    n_restarts=0,
    verbose=False,
    random_state=None
)

seqHMM Parameter Mapping

SequenzoseqHMM fit_model(...)
em_stepEM initialization / refinement
global_stepGlobal optimization (MLSL in R)
local_stepLocal optimization (L-BFGS in R)
n_restartsRandom restarts with new initials
n_iter, tolIteration and tolerance controls

Entry Parameters

ParameterRequiredTypeDescription
modelHMM / MHMM / NHMMUnfitted or partially fitted model.
em_stepboolRun EM first. Default True.
global_stepboolGlobal optimization after EM. Default False.
local_stepboolL-BFGS polish after EM/global. Default False.
n_iterintIteration cap for EM/local steps. Default 100.
tolfloatConvergence tolerance. Default 1e-2.
n_restartsintExtra random restarts; keeps best log-likelihood. Default 0.
verboseboolPrint progress.
random_stateint / NoneSeed for restarts.

What It Returns

The fitted model object with the best log-likelihood found across restarts and stages.

Example

python
from sequenzo.seqhmm import build_hmm, fit_model_advanced

hmm = build_hmm(seq, n_states=4, random_state=42)

hmm = fit_model_advanced(
    hmm,
    em_step=True,
    global_step=True,
    local_step=True,
    n_restarts=5,
    verbose=True,
    random_state=42,
)

print(hmm.log_likelihood, hmm.converged)

R Counterpart

  • Closest R function: seqHMM fit_model() with global_step and local_step enabled
  • Mapping note: R uses nloptr MLSL; Sequenzo uses scipy.optimize (differential evolution / L-BFGS).

Notes

  • Global and local steps are slower than EM alone; enable them when EM log-likelihood is unstable across random_state values.
  • Works with HMM, MHMM, and NHMM, but runtime grows quickly for NHMM.
  • For routine basic HMM work, fit_model() is usually sufficient.

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.