Skip to content
Paul Lott edited this page Nov 12, 2013 · 13 revisions

#Comparison of Dishonest Casino Models in R-HMM, Mamot, HMMoc, and StochHMM.

##R HMM Model Adapted from R HMM Library

library(HMM)
args = commandArgs(trailingOnly = TRUE);

#Name of dice roll file (which is comma separated)
seqFile = args[1];

#Flag for Viterbi (if 1 then perform viterbi, otherwise perform posterior)
viterbi = as.numeric(args[2])

alphabet = c(1,2,3,4,5,6)
states = c("Fair", "Loaded")

#State Emissions
fair = c(1/6,1/6,1/6,1/6,1/6,1/6)
loaded = c(0.1,0.1,0.1,0.1,0.1,0.5)

#Transition matrix
transProbs = matrix(c(0.99, 0.01, 0.02, 0.98), c(2,2), byrow = TRUE)

#Emission Matrix
emissionProbs = matrix(fair, loaded), c(2, 2), byrow = TRUE)

hmm = initHMM(states, alphabet, transProbs = transProbs, emissionProbs = emissionProbs)

observations = scan(file = seqFile,sep=",")

if (viterbi == 1){
    vit = viterbi(hmm, observations)
}

if (viterbi == 0){
    f = forward(hmm, observations)
    b = backward(hmm, observations)
    i <- f[1, length(observations)]
    j <- f[2, length(observations)]
    probObservations = (i + log(1 + exp(j - i)))
    posterior = exp((f + b) - probObservations)
}

gc()

##Mamot Model

# HMM for the "dishonest casino" presented by Durbin, Eddy, Krogh, Mitchison 
# in "Biological sequence analysis" (Cambridge University Press, 1998).
# Exercise 1.1, p. 6 and p. 54, 56, 59, 61, 65

Alphabet: abcdef
################################################
State BEGIN
	E: 0  0  0  0  0  0
	T: F 1
# Fair
State F
	E: 0.1666  0.1666  0.1666  0.1666  0.1666  0.1666
	T: F 0.95  L 0.049  END 0.001
# not fair
State L 
	E: 0.1 0.1 0.1 0.1 0.1 0.5
	T: F 0.1  L 0.9
State END  
	E: 0  0  0  0  0  0
	T: END 0
################################################
NULLMODEL:  0.1666  0.1666  0.1666  0.1666  0.1666  0.1666