optimalPath: optimalPath

Description Usage Arguments Value Examples

View source: R/optimalPath.R

Description

optimalPath performs Viterbi Algorithm on a single observed trajectures x to predict the optimal path according to the rule, Maximum A Posteriori (MAP).

Usage

1
optimalPath(x, u, sig, A, pi0)

Arguments

x

a single observed trajectures

u

(All parameters used are from the result of estimateHMM) a vector of estimated means of Normal distribution for each state (emission probability)

sig

a vector of estimated standard deviations of Normal distribution for each state (emission probability)

A

a matrix of estimated transition probability

pi0

a vector of estimated initial probability.

Value

A list containing:

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
set.seed(1221)
num = 10; n = 250
df <- generateHMM(num=num,n=n)

### using Constrain 1
temp <- estimateHMM(df$X, M=3, constr=matrix(1,3,3), tol=0.001)
u <- temp$u[,temp$iter+1]; sig <- temp$sig[,temp$iter+1]
A <- temp$trans[[temp$iter+1]]; pi0 <- temp$pi0
res <- matrix(0, num ,3)
colnames(res) <- c("rate", "TRUE", "FASLE")
for (i in 1:num){
  res[i,2] <- sum(optimalPath(df$X[,i], u, sig, A, pi0)$opt.path == df$Z[,i])
  res[i,3] <- n-res[i,2]
  res[i,1] <- res[i,2]/n
}
print(res)


### using Constrain 2
constr2 <- matrix(1,3,3)
constr2[1,1] <- 0; constr2[3,3] <- 0;
temp <- estimateHMM(df$X, M=3, constr=constr2, tol=0.001)
u <- temp$u[,temp$iter+1]; sig <- temp$sig[,temp$iter+1]
A <- temp$trans[[temp$iter+1]]; pi0 <- temp$pi0
res <- matrix(0, num ,3)
colnames(res) <- c("rate", "TRUE", "FASLE")
for (i in 1:num){
  res[i,2] <- sum(optimalPath(df$X[,i], u, sig, A, pi0)$opt.path == df$Z[,i])
  res[i,3] <- n-res[i,2]
  res[i,1] <- res[i,2]/n
}
print(res)

jiangrongo/HMM documentation built on May 19, 2019, 9:38 p.m.