Learn R Programming

tileHMM (version 1.0-7)

viterbi: Calculate Most Likely State Sequence Using the Viterbi Algorithm

Description

The Viterbi algorithm computes the most likely sequence of states given an HMM and an observation sequence.

Usage

"viterbi"(hmm, obs, names=TRUE)

Arguments

hmm
Object of class hmm.
obs
A vector containing the observation sequence.
names
Logical indicating whether state names should be returned. If TRUE (the default) the returned sequence consists of state names, otherwise the state index is returned instead.

Value

A list with components
stateSeq
Most likely state sequence.
logProb
The probability of stateSeq given hmm and obs.
matrix
The dynamic programming matrix.

References

Viterbi, A. J. 1967 Error bounds for convolutional codes and an assymptotically optimal decoding algorithm. IEEE Transactions on Information Theory, 13, 2600--269.

See Also

viterbiTraining

Examples

Run this code
## create two state HMM with t distributions
state.names <- c("one","two")
transition <- c(0.1, 0.02)
location <- c(1, 2)
scale <- c(1, 1)
df <- c(4, 6)
model <- getHMM(list(a=transition, mu=location, sigma=scale, nu=df),
    state.names)

## obtain observation sequence from model
obs <- sampleSeq(model, 100, return.states=TRUE)

## compute most likely state sequence for obs
vit.res <- viterbi(model, obs$observation)

## how well did we do?
sum(vit.res$stateSeq == obs$states)/length(vit.res$stateSeq)

Run the code above in your browser using DataLab