This example is taken from Durbin et. al. (1999): A dishonest casino uses two dice, one of them is fair the other is loaded.
The observer doesn’t know which die is actually taken (the state is hidden), but the sequence of throws (observations) can be used to infer which die (state) was used.
library(HMM)
dishonestCasino()
## Plot simulated throws:
## Simulated, which die was used:
##
## Most probable path (viterbi):
##
## Differences:
##
## Posterior-probability:
##
## Difference with classification by posterior-probability:
##
## Difference with classification by posterior-probability > .95:
Specify the following HMM.
A=matrix(c(0.95,0.05,0.05,0.95),2,2);
B=matrix(c(1/3, 0.1, 1/3, 0.2, 1/3,0.7),2,3);
hmm=initHMM(c("A", "B"), c(1, 2, 3),
startProbs=c(1/2, 1/2),
transProbs=A, emissionProbs=B)
print(hmm)
## $States
## [1] "A" "B"
##
## $Symbols
## [1] 1 2 3
##
## $startProbs
## A B
## 0.5 0.5
##
## $transProbs
## to
## from A B
## A 0.95 0.05
## B 0.05 0.95
##
## $emissionProbs
## symbols
## states 1 2 3
## A 0.3333 0.3333 0.3333
## B 0.1000 0.2000 0.7000
Generate n=500 obs from this HMM.
n=500;
data=simHMM(hmm, n)
names(data)
## [1] "states" "observation"
cbind(data$states[1:5], data$observation[1:5])
## [,1] [,2]
## [1,] "A" "2"
## [2,] "A" "2"
## [3,] "A" "2"
## [4,] "A" "3"
## [5,] "A" "3"