I am working on a Hidden Semi-Markov Model (HSMM) to analyze state transitions in a time series dataset. I have successfully trained the HSMM using the mhsmm
package in R and have obtained the model's transition matrix. Additionally, I have used the Viterbi algorithm to predict the most likely state sequences for my data.
model <- hsmmspec(init = initial, trans = TransMatrix, parms.emis = b, dens.emis = dmvnorm.hsmm, sojourn = d)
testrun <- hsmmfit(Datanew, model, mstep = mstep.mvnorm, lock.transition = FALSE, maxit = 1000, lock.d = FALSE)
model_transition_matrix <- testrun$model$transition
viterbi_combined <- testrun$yhat
calculate_transition_matrix <- function(state_sequence, number_states) {
transition_matrix <- matrix(0, nrow = number_states, ncol = number_states)
for (i in 1:(length(state_sequence) - 1)) {
transition_matrix[state_sequence[i], state_sequence[i + 1]] <- transition_matrix[state_sequence[i], state_sequence[i + 1]] + 1
}
row_sums <- rowSums(transition_matrix)
transition_matrix <- sweep(transition_matrix, 1, row_sums, FUN = "/")
transition_matrix[is.na(transition_matrix)] <- 0
return(transition_matrix)
}
predicted_transition_matrix <- calculate_transition_matrix(viterbi_combined, number_states)
The transition matrix obtained from the HSMM model (testrun$model$transition) and the transition matrix calculated from the predicted state sequences (predicted_transition_matrix) differ significantly. Specifically, the mean relative difference between the two matrices is around 0.2.
1)Why do these two transition matrices differ significantly?
2)Is it expected for the transition matrix from the HSMM model and the transition matrix from the predicted state sequences to differ?
The transition probabilities of a semi-Markov model have a different interpretation to those of a regular Markov chain: they are conditional on there being a transition. This is required to allow for the sojourn time distribution to be modelled separately. (In a regular Markov chain, sojourn times have a geometric distribution.) So, we would not expect the conditional transition probabilities to match the probabilities that you computed from the Viterbi sequence. See for example Section 2 of the following reference: Guédon (2003). Estimating hidden semi-Markov chains from discrete sequences. Journal of computational and graphical statistics, 12(3), 604-639.
It should be possible to modify your calculate_transition_matrix()
function to estimate the conditional transition probabilities. You could add the following line just after the for
loop in the function:
diag(transition_matrix) <- 0
This should remove all transitions from a state to itself, and so you should get estimated transition probabilities conditional on a transition.