Skip to content

build_mnhmm()

build_mnhmm() creates an unfitted Mixture Non-homogeneous Hidden Markov Model (MNHMM). An MNHMM combines two ideas: mixture HMMs, where each sequence belongs probabilistically to a latent cluster, and non-homogeneous HMMs, where initial, transition, emission, or cluster probabilities can depend on covariates.

Use this builder when you want to define an MNHMM object first, inspect its structure, or pass it into lower-level methods. If you want to build and fit the model in one step, use estimate_mnhmm().

Function Usage

python
build_mnhmm(
    observations,
    n_states,
    n_clusters,
    X=None,
    X_pi=None,
    X_A=None,
    X_B=None,
    X_cluster=None,
    emission_formula=None,
    initial_formula=None,
    transition_formula=None,
    cluster_formula=None,
    data=None,
    id_var=None,
    time_var=None,
    eta_pi=None,
    eta_A=None,
    eta_B=None,
    eta_omega=None,
    eta_pi_reduced=None,
    eta_A_reduced=None,
    eta_B_reduced=None,
    eta_omega_reduced=None,
    initial_probs=None,
    transition_probs=None,
    emission_probs=None,
    cluster_probs=None,
    cluster_names=None,
    state_names=None,
    random_state=None,
)

Entry Parameters

ParameterRequiredTypeDescription
observationsYesSequenceData / sequence of SequenceDataObserved sequences. Pass a list or tuple for multichannel models.
n_statesYesint / sequence of intHidden-state count per mixture cluster. A single integer applies to all clusters.
n_clustersYesintNumber of mixture clusters. Must be at least 2.
XNondarray / NoneShared covariate array used by the model when probability-family-specific arrays are not supplied.
X_piNondarray / NoneCovariates for initial-state probabilities.
X_ANondarray / NoneCovariates for transition probabilities.
X_BNondarray / NoneCovariates for emission probabilities.
X_clusterNondarray / NoneTime-constant covariates for mixture-cluster probabilities.
emission_formulaNostr / Formula / NoneFormula used to build X_B from data.
initial_formulaNostr / Formula / NoneFormula used to build X_pi from data.
transition_formulaNostr / Formula / NoneFormula used to build X_A from data.
cluster_formulaNostr / Formula / NoneFormula used to build X_cluster; covariates must be time-constant.
dataNopandas.DataFrame / NoneCovariate data used with formula inputs. Initial, transition, and emission formulas require one row per sequence-time combination; cluster_formula may use ID-level data when covariates are time-constant.
id_varNostr / NoneID column in data. Required for formula-based construction.
time_varNostr / NoneTime column in data. Required for time-varying formulas.
eta_piNosequence of arrays / NoneFull coefficient arrays for initial-state probabilities, one per cluster.
eta_ANosequence of arrays / NoneFull coefficient arrays for transition probabilities, one per cluster.
eta_BNosequence of arrays / NoneFull coefficient arrays for emission probabilities. For multichannel data, use one coefficient array per channel within each cluster.
eta_omegaNondarray / NoneFull coefficient array for mixture-cluster probabilities.
eta_pi_reducedNosequence of arrays / NoneReduced coefficient arrays for initial-state probabilities, excluding the reference category.
eta_A_reducedNosequence of arrays / NoneReduced coefficient arrays for transition probabilities, excluding the reference category in each transition row.
eta_B_reducedNosequence of arrays / NoneReduced coefficient arrays for emission probabilities, excluding the reference observed category.
eta_omega_reducedNondarray / NoneReduced coefficient array for mixture-cluster probabilities, excluding the reference cluster.
initial_probsNosequence of arrays / NoneFixed initial probabilities per cluster.
transition_probsNosequence of arrays / NoneFixed transition matrices per cluster.
emission_probsNonested arrays / NoneFixed emission probabilities. For multichannel data, use emission_probs[cluster][channel].
cluster_probsNondarray / NoneFixed mixture probabilities.
cluster_namesNosequence of str / NoneLabels for mixture clusters.
state_namesNosequence / NoneHidden-state labels, usually one list per cluster.
random_stateNoint / NoneSeed for randomized starts.

Returns

An unfitted MNHMM object. The object stores the observations, cluster structure, covariate design matrices or formulas, fixed probability arrays, and coefficient starting values. It does not estimate parameters until you call a fitting method or use estimate_mnhmm().

Example

python
import pandas as pd
from sequenzo import SequenceData, build_mnhmm, load_dataset

df = load_dataset("mvad")
time_cols = list(df.columns[14:])
states = ["employment", "FE", "HE", "joblessness", "school", "training"]
seq = SequenceData(df, time=time_cols, states=states)

rows = []
for row_index, sequence_id in enumerate(seq.ids):
    for time_index, time_label in enumerate(seq.time):
        rows.append({
            "id": sequence_id,
            "time": time_label,
            "time_index": time_index,
            "cohort_proxy": row_index % 2,
        })

covariates = pd.DataFrame(rows)

model = build_mnhmm(
    observations=seq,
    n_clusters=3,
    n_states=4,
    transition_formula="~ time_index",
    cluster_formula="~ cohort_proxy",
    data=covariates,
    id_var="id",
    time_var="time",
    random_state=42,
)

For multichannel data, pass aligned SequenceData objects. The following block is schematic: replace employment_seq and family_seq with prepared SequenceData objects that share the same IDs and time grid.

python
model = build_mnhmm(
    observations=[employment_seq, family_seq],
    n_clusters=2,
    n_states=[3, 4],
    cluster_formula="~ cohort",
    data=covariates,
    id_var="id",
    time_var="time",
)

Notes

  • cluster_formula covariates must be time-constant. If the data contain repeated rows per ID, Sequenzo checks that the resulting values do not vary over time.
  • initial_formula, transition_formula, and emission_formula require data, id_var, and time_var arranged on the sequence-time grid.
  • cluster_formula can use ID-level data with one row per sequence ID, or long data if the resulting covariates are constant within each ID.
  • MNHMM does not support missing sequence observations because the model preserves alignment across sequences, channels, and covariate arrays.
  • Use either full eta_* arrays or reduced eta_*_reduced arrays for a probability family, not both.
  • Supplied probability arrays are treated as fixed probabilities by build_mnhmm(). In estimate_mnhmm(), set probability_parameters_as_starts=True when you want supplied probabilities to initialize covariate parameters instead.

See Also

Authors

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

Sequenzo is released under the BSD-3-Clause License; this documentation site source is licensed under MIT.