-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathHMM.R
35 lines (33 loc) · 1.14 KB
/
HMM.R
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
33
34
35
library(HMM)
# # Initialise HMM
# hmm = initHMM(c("A","B"), c("L","R"), transProbs=matrix(c(.8,.2,.2,.8),2),
# emissionProbs=matrix(c(.6,.4,.4,.6),2))
# print(hmm)
# # Sequence of observations
# observations = c("L","L","R","R")
# # Calculate backward probablities
# logBackwardProbabilities = backward(hmm,observations)
# print(exp(logBackwardProbabilities))
# Initial HMM
# hmm = initHMM(c("A","B"),c("L","R"),
# transProbs=matrix(c(.9,.1,.1,.9),2),
# emissionProbs=matrix(c(.5,.51,.5,.49),2))
# print(hmm)
# # Sequence of observation
# a = sample(c(rep("L",100),rep("R",300)))
# print(a)
# b = sample(c(rep("L",300),rep("R",100)))
# observation = c(a,b)
# print(observation)
# # Baum-Welch
# bw = baumWelch(hmm,observation,10)
# print(bw$hmm)
# Initialise HMM
hmm = initHMM(c("A","B"), c("L","R"), transProbs=matrix(c(.8,.2,.2,.8),2),
emissionProbs=matrix(c(.6,.4,.4,.6),2))
print(hmm)
# Sequence of observations
observations = c("L","L","R","R")
# Calculate forward probablities
logForwardProbabilities = forward(hmm,observations)
print(exp(logForwardProbabilities))