Skip to content

simulate_hmm()

simulate_hmm() generates synthetic observed and hidden-state sequences from known HMM parameters. Use it for method checks, power studies, or teaching.

Function Usage

python
simulate_hmm(
    n_sequences,
    initial_probs,
    transition_probs,
    emission_probs,
    sequence_length,
    alphabet=None,
    state_names=None,
    random_state=None
)

seqHMM Parameter Mapping

SequenzoseqHMM simulate_hmm()
n_sequencesNumber of sequences to draw
initial_probsInitial distribution
transition_probsTransition matrix
emission_probsEmission matrix
sequence_lengthLength of each sequence
alphabetObserved state symbols
state_namesHidden state labels
random_stateRNG seed

Entry Parameters

ParameterRequiredTypeDescription
n_sequencesintHow many sequences to simulate.
initial_probsndarrayShape (n_states,), sums to 1.
transition_probsndarrayShape (n_states, n_states), rows sum to 1.
emission_probsndarrayShape (n_states, n_symbols), rows sum to 1.
sequence_lengthintNumber of time points per sequence.
alphabetList[str] / NoneObserved symbols. Default: "0", "1", …
state_namesList[str] / NoneHidden state names.
random_stateint / NoneReproducibility seed.

What It Returns

A dict:

KeyDescription
observationsList of observed state sequences
statesList of hidden state sequences
observations_dfDataFrame suitable for building SequenceData

Example

python
import numpy as np
from sequenzo.seqhmm import simulate_hmm

initial_probs = np.array([0.5, 0.5])
transition_probs = np.array([[0.7, 0.3], [0.3, 0.7]])
emission_probs = np.array([[0.9, 0.1], [0.1, 0.9]])

sim = simulate_hmm(
    n_sequences=10,
    initial_probs=initial_probs,
    transition_probs=transition_probs,
    emission_probs=emission_probs,
    sequence_length=20,
    alphabet=["A", "B"],
    random_state=42,
)

print(sim["observations"][0])

R Counterpart

  • Closest R function: seqHMM simulate_hmm()

Notes

  • Validate matrix shapes before calling; mismatched dimensions raise explicit errors.
  • To recover parameters with estimation, wrap observations in SequenceData and run build_hmm() + fit_model().

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.