Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Previous post presented the problem of dishonest casino that ocassionally uses loaded die. Sequence of the real states is hidden, and we are trying to figure it out just by looking at the observations (symbols).

```# backtracking algorithm
for (i in 2:length(symbol.sequence)) {
# probability vector stores the current emission with respect to (i-1) observation of selected state and transition probability
# state vector (pointer) on the other hand is only storing the most probable state in (i-1), which we will later use for backtracking

tmp.path.probability <- lapply(states, function(l) {
max.k <- unlist(lapply(states, function(k) {
prob.history[i-1, k] + transition.matrix[k, l]
}))

return(c(states[which(max.k == max(max.k))], max(max.k) + emission.matrix[symbol.sequence[i], l]))
})

prob.history <- rbind(prob.history, data.frame(F = as.numeric(tmp.path.probability[]), L = as.numeric(tmp.path.probability[])))

state.history <- data.frame(F = c(as.character(state.history[,tmp.path.probability[]]), "F"), L = c(as.character(state.history[,tmp.path.probability[]]), "L"))
}

# selecting the most probable path
viterbi.path <- as.character(state.history[,c("F", "L")[which(max(prob.history[length(symbol.sequence), ]) == prob.history[length(symbol.sequence), ])]])
```

If we apply our implementation to the data in the previous post, we can get the idea how well can HMM reconstruct the real history.

```viterbi.table <- table(viterbi.path == real.path)
cat(paste(round(viterbi.table["TRUE"] / sum(viterbi.table) * 100, 2), "% accuracy\n", sep = ""))
# 71.33% accuracy
```

Cheers, mintgene.  