Skip to content

posterior_probs()

posterior_probs() computes the probability of each hidden state at each time point, given the observed sequence. It uses the forward–backward algorithm and is useful when you need uncertainty about latent states, not just a single Viterbi path.

Function Usage

python
posterior_probs(
    model,
    newdata=None
)

seqHMM Parameter Mapping

SequenzoseqHMM posterior_probs()
modelFitted hmm object
newdataOptional new stslist
Return valueLong-format table of state probabilities

Entry Parameters

ParameterRequiredTypeDescription
modelHMMFitted model from build_hmm() + fit_model().
newdataSequenceData / NoneSequences to score. If None, uses training data on the model.

What It Returns

A pandas DataFrame in long format with columns:

ColumnDescription
idSequence index (0-based)
timeTime index within the sequence (1-based, matching R convention)
stateHidden state index (0-based)
probabilityPosterior probability of that state at that time point

For each (id, time) pair, probabilities over state sum to 1.

Example

python
from sequenzo.seqhmm import build_hmm, fit_model, posterior_probs

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

probs = posterior_probs(hmm)
print(probs.head())

# Most probable state at each time point
idx = probs.groupby(["id", "time"])["probability"].idxmax()
most_probable = probs.loc[idx]

R Counterpart

  • Closest R function: seqHMM posterior_probs()
  • Mapping note: Same long-format logic; time indexing is 1-based in both interfaces.

Notes

  • Requires a fitted model.
  • Compare with predict(): Viterbi gives one best path; posterior probabilities can split mass across states when the model is uncertain.
  • For mixture models, use posterior_probs_mhmm() for cluster membership.

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.