Learn R Programming

LaMa (version 3.1.0)

viterbi: Viterbi algorithm for state decoding in HMMs

Description

The Viterbi algorithm decodes the most probable state sequence of an HMM.

Usage

viterbi(delta, Gamma, allprobs, trackID = NULL, mod = NULL, tod = NULL)

Value

vector of decoded states of length nObs

Arguments

delta

initial distribution; either

  • a vector of length nStates,

  • a matrix of dimension c(nTracks, nStates) if trackID is provided, or

  • a model object as for mod, so that mod can be passed as the first argument

Gamma

transition probability matrix; either

  • a matrix of dimension c(nStates, nStates),

  • an array of dimension c(nStates, nStates, nTracks) if trackID is provided,

  • an array of dimension c(nStates, nStates, nObs) for time-varying transition probabilities, or

  • an array of dimension c(nStates, nStates, L) for periodic transition probabilities, if tod is provided

allprobs

matrix of state-dependent probabilities or density values of dimension c(nObs, nStates)

trackID

optional vector of length nObs containing nTracks unique IDs that separate tracks. The observations of each track need to be consecutive.

mod

optional model object containing delta, Gamma, allprobs, and optionally trackID. When using RTMB::MakeADFun or qreml with forward in the likelihood, these are reported automatically after model fitting and the object returned by obj$report(), lama or qreml can be passed directly.

tod

optional integer vector of length nObs with values in 1, ..., L, indexing the slices of Gamma for periodic models

See Also

Other decoding functions: stateprobs(), stateprobs_g(), stateprobs_p(), viterbi_g(), viterbi_p()

Examples

Run this code
delta = c(0.5, 0.5)
Gamma = matrix(c(0.9, 0.1, 0.2, 0.8), nrow = 2, byrow = TRUE)
allprobs = matrix(runif(200), nrow = 100, ncol = 2)
states = viterbi(delta, Gamma, allprobs)

Run the code above in your browser using DataLab