negLogLike: Returns the negative log likelihood calculated with the forward equations.

viterbiPath: Calculates the most likely sequence of hidden states for the Markov model given the current parameters.

1 2 | ```
negLogLike(par,fx.par,data,nstates,stFn,trFn,emFn)
viterbiPath(par,fx.par,data,nstates,stFn,trFn,emFn)
``` |

`par` |
A list of parameters, over which the likelihood will be optimized. |

`fx.par` |
A list of fixed parameters. |

`data` |
A list of data objects, which must contain a vector O, which represents the observed sequence of the HMM. |

`nstates` |
The number of states of the HMM. |

`stFn` |
A function which takes arguments par, fx.par, data, and nstates, and returns a vector of length nstates of starting probabilities. |

`trFn` |
A function which takes arguments par, fx.par, data, and nstates, and returns a matrix of dimension (nstates,nstates) of the transition probabilities. |

`emFn` |
A function which takes arguments par, fx.par, data, and nstates, and returns a matrix of dimension (nstates,length(O)) of the emission probabilities. |

negLogLike: The negative log likelihood of the HMM. The likelihood is slightly modified to account for ranges with read counts which have zero probability of originating from any of the states. In this case the likelihood is lowered and the range is skipped.

viterbiPath: The Viterbi path through the states given the parameters.

On the forward equations and the Viterbi algorithm:

Rabiner, L. R. (1989): "A tutorial on hidden Markov models and selected applications in speech recognition," Proceedings of the IEEE, 77, 257, 286, http://dx.doi.org/10.1109/5.18626.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | ```
## functions for starting, transition, and emission probabilities
stFn <- function(par,fx.par,data,nstates) rep(1/nstates,nstates)
trFn <- function(par,fx.par,data,nstates) {
A <- matrix(1/(nstates*10),ncol=nstates,nrow=nstates)
diag(A) <- 1 - rowSums(A)
A
}
emFn <- function(par,fx.par,data,nstates) {
t(sapply(1:nstates,function(j) dnorm(data$O,par$means[j],fx.par$sdev)))
}
## simulate some observations from two states
Q <- c(rep(1,100),rep(2,100),rep(1,100),rep(2,100))
T <- length(Q)
means <- c(-0.5,0.5)
sdev <- 1
O <- rnorm(T,means[Q],sdev)
## use viterbiPath() to recover the state chain using parameters
viterbi.path <- viterbiPath(par=list(means=means),
fx.par=list(sdev=sdev), data=list(O=O), nstates=2,stFn,trFn,emFn)
plot(O,pch=Q,col=c("darkgreen","orange")[viterbi.path])
``` |

Questions? Problems? Suggestions? Tweet to @rdrrHQ or email at ian@mutexlabs.com.

All documentation is copyright its authors; we didn't write any of that.