Hi,
I wrote a forward algorithm for a continuous-time HMM with a potentially large number of states but a sparse generator matrix Q.
It looks like
forward_cont <- function(pi, # initial distribution (vector)
Q, # sparse generator matrix
allprobs, # matrix of state-dependent probabilities/ densities
timediff # time differences between observations (vector)
){
if(is.null(dim(pi))){ # if vector, reshape to matrix -> elementwise multiplication below
pi <- matrix(pi, nrow = 1)
}
n <- nrow(allprobs) # number of observations
# initialising forward algorithm
foo <- pi * allprobs[1, , drop = FALSE]
sumfoo <- sum(foo)
l <- log(sumfoo)
foo <- foo / sumfoo
# loop over remaining time points
for(t in 2:n){
# foo <- foo %*% expm(Q * timediff[t-1]) # slow naive version
foo <- t(expAv(Q * timediff[t-1], t(foo), transpose = TRUE)) # fast version
foo <- foo * allprobs[t, , drop = FALSE]
sumfoo <- sum(foo)
l <- l + log(sumfoo)
foo <- foo / sumfoo
}
l
}
What I commented out is the naive version, doing v %*% expm(A), which is not efficient due to expm() not exploiting sparsity. Hence, I replaced it with t(expAv(A, t(v), transpose = TRUE)), which yields the same result but is much faster.
When including this in a likelihood function, optimisation seems to work, but in between prints of "outer mgc: ..." I get loads of prints like "Retaping: Nold=53 Nnew=52" which does not happen when using expm().
So my question is, what does that mean, and is it something to worry about?
Cheers