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.
- I trained the HSMM on my entire dataset and obtained the transition matrix from the model:
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
- State Sequence Prediction:
Using the Viterbi algorithm, I predicted the most likely state sequences for my data:
viterbi_combined <- testrun$yhat
- Transition Matrix Calculation from Predicted States:
I calculated the transition matrix based on the predicted state sequences:
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?