How to get the best state sequence using Viterbi algorithm

I am writing this up because it seems like there should be a standard, easy way to do this and I just haven’t found it.

I wrote up my Viterbi algorithm in Stan as shown here in the Stan User’s Guide, which gives me the most likely state sequence for my HMM at each iteration of the Markov chain. If I want a single most likely state sequence that doesn’t change with the iteration, I have a two ideas:

  1. Take the mode of the sequences across all iterations. So I will end up with the state sequence that appears most often. Note that it is not very fast in R to find the counts of unique rows to find the most common rows or to generally find the “mode” row, especially if the chain is very long.
  2. First find the posterior mean for model parameters and use that to calculate the Viterbi algorithm. I don’t know if this is commonly done. I’m also not completely sure how to do this in Stan and whether it should be done in the generated quantities block or elsewhere. I’d be fine with doing it in R after running the model in Stan if this is most appropriate.

Which of these would make more sense?

Thanks in advance!


It’s the usual mean vs mode question really. If the Q is really ‘what is the most common sequence out of all possible sequences, with no consideration of the similarity / closeness of the sequences’, you need the computationally more intense first option. In some cases this could be a seemingly unusual sequence though, in which case you might just want the sequence corresponding to the central tendency of the higher level parameters. The nice thing about nonlinearity though is you can dream up scenarios where both cases give unintuitive results 😆

1 Like