Forward algorithm with homogeneous transition probability matrix
forward.RdCalculates the log-likelihood of a sequence of observations under a homogeneous hidden Markov model using the forward algorithm.
Usage
forward(
delta,
Gamma,
allprobs,
trackID = NULL,
logspace = FALSE,
ad = NULL,
bw = NULL,
report = TRUE
)Arguments
- delta
initial or stationary distribution of length
N, or matrix of dimensionc(k,N)forkindependent tracks, iftrackIDis provided- Gamma
transition probability matrix of dimension
c(N,N), or array ofktransition probability matrices of dimensionc(N,N,k), iftrackIDis provided- allprobs
matrix of state-dependent probabilities/ density values of dimension
c(n, N)- trackID
optional vector of length
ncontaining IDs that separate tracks.If provided, the total log-likelihood will be the sum of each track's likelihood contribution. In this case,
Gammacan be a matrix, leading to the same transition probabilities for each track, or an array of dimensionc(N,N,k), with one (homogeneous) transition probability matrix for each track. Furthermore, instead of a single vectordeltacorresponding to the initial distribution, adeltamatrix of initial distributions, of dimensionc(k,N), can be provided, such that each track starts with it's own initial distribution.- logspace
logical, indicating whether the probabilities/ densities in the
allprobsmatrix are on log-scale. If so, internal computations are also done on log-scale which is numerically more robust when the entries are very small. Note that this is only supported when used in AD mode withRTMB.- ad
optional logical, indicating whether automatic differentiation with
RTMBshould be used. By default, the function determines this itself.- bw
optional integer, indicating the bandwidth for a banded approximation of the forward algorithm. This is for expert users only, if sparsity in the Hessian matrix w.r.t. observations is required.
- report
logical, indicating whether
delta,Gamma,allprobs, and potentiallytrackIDshould be reported from the fitted model. Defaults toTRUE, but only works ifad = TRUE, as it uses theRTMBpackage.When there are multiple tracks, for compatibility with downstream functions like
viterbi,stateprobsorpseudo_res,forwardshould only be called once with atrackIDargument.
See also
Other forward algorithms:
forward2(),
forward_g(),
forward_g2(),
forward_hsmm(),
forward_ihsmm(),
forward_p(),
forward_phsmm()
Examples
## negative log likelihood function
nll = function(par, step) {
# parameter transformations for unconstrained optimisation
Gamma = tpm(par[1:2]) # multinomial logit link
delta = stationary(Gamma) # stationary HMM
mu = exp(par[3:4])
sigma = exp(par[5:6])
# calculate all state-dependent probabilities
allprobs = matrix(1, length(step), 2)
ind = which(!is.na(step))
for(j in 1:2) allprobs[ind,j] = dgamma2(step[ind], mu[j], sigma[j])
# simple forward algorithm to calculate log-likelihood
-forward(delta, Gamma, allprobs)
}
## fitting an HMM to the trex data
par = c(-2,-2, # initial tpm params (logit-scale)
log(c(0.3, 2.5)), # initial means for step length (log-transformed)
log(c(0.2, 1.5))) # initial sds for step length (log-transformed)
mod = nlm(nll, par, step = trex$step[1:1000])