-
Notifications
You must be signed in to change notification settings - Fork 0
/
myhmm_log.py
345 lines (314 loc) · 15.2 KB
/
myhmm_log.py
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
"""
-------------------------------- (C) ---------------------------------
myhmm.py
Author: Anantharaman Narayana Iyer
Date: 7 Sep 2014
Author: Anantharaman Palacode Narayana Iyer
Distributed under the BSD license:
Copyright 2010 (c) Anantharaman Palacode Narayana Iyer, <[email protected]>
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above
copyright notice, this list of conditions and the following
disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials
provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER "AS IS" AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
"""
import json
import os
import sys
import math
class MyHmmLog(object): # base class for different HMM models
def __init__(self, model_name):
# model is (A, B, pi) where A = Transition probs, B = Emission Probs, pi = initial distribution
# a model can be initialized to random parameters using a json file that has a random params model
if model_name == None:
print "Fatal Error: You should provide the model file name"
sys.exit()
self.model = json.loads(open(model_name).read())["hmm"]
self.A = self.model["A"]
self.states = self.A.keys() # get the list of states
self.N = len(self.states) # number of states of the model
self.B = self.model["B"]
self.symbols = self.B.values()[0].keys() # get the list of symbols, assume that all symbols are listed in the B matrix
self.M = len(self.symbols) # number of states of the model
self.pi = self.model["pi"]
# let us generate log of model params: A, B, pi
self.logA = {}
self.logB = {}
self.logpi = {}
self.set_log_model()
return
def set_log_model(self):
for y in self.states:
self.logA[y] = {}
for y1 in self.A[y].keys():
self.logA[y][y1] = math.log(self.A[y][y1])
self.logB[y] = {}
for sym in self.B[y].keys():
if self.B[y][sym] == 0:
self.logB[y][sym] = sys.float_info.min # this is to handle symbols that never appear in the dataset
else:
self.logB[y][sym] = math.log(self.B[y][sym])
if self.pi[y] == 0:
self.logpi[y] = sys.float_info.min # this is to handle symbols that never appear in the dataset
else:
self.logpi[y] = math.log(self.pi[y])
def backward(self, obs):
self.bwk = [{} for t in range(len(obs))]
T = len(obs)
# Initialize base cases (t == T)
for y in self.states:
self.bwk[T-1][y] = 1 #self.A[y]["Final"] #self.pi[y] * self.B[y][obs[0]]
for t in reversed(range(T-1)):
for y in self.states:
self.bwk[t][y] = sum((self.bwk[t+1][y1] * self.A[y][y1] * self.B[y1][obs[t+1]]) for y1 in self.states)
prob = sum((self.pi[y]* self.B[y][obs[0]] * self.bwk[0][y]) for y in self.states)
return prob
def backward_log(self, obs):
self.bwk_log = [{} for t in range(len(obs))]
T = len(obs)
# Initialize base cases (t == T)
for y in self.states:
self.bwk_log[T-1][y] = math.log(1) # #self.A[y]["Final"] #self.pi[y] * self.B[y][obs[0]]
for t in reversed(range(T-1)):
for y in self.states:
ailist = [] #initialize ths as we need the max value of ai
for y1 in self.states:
ai = self.bwk_log[t+1][y1] + self.logA[y][y1] + self.logB[y1][obs[t+1]]
#print "ai = ", ai, "aimax = ", aimax
ailist.append(ai)
aimax = max(ailist)
self.bwk_log[t][y] = aimax + math.log(sum((math.exp(self.bwk_log[t+1][y1] + self.logA[y][y1] + self.logB[y1][obs[t+1]] - aimax)) for y1 in self.states))
prob = sum((self.pi[y]* self.B[y][obs[0]] * math.exp(self.bwk_log[0][y])) for y in self.states)
return prob
def forward(self, obs):
self.fwd = [{}]
# Initialize base cases (t == 0)
for y in self.states:
self.fwd[0][y] = self.pi[y] * self.B[y][obs[0]]
# Run Forward algorithm for t > 0
for t in range(1, len(obs)):
self.fwd.append({})
for y in self.states:
self.fwd[t][y] = sum((self.fwd[t-1][y0] * self.A[y0][y] * self.B[y][obs[t]]) for y0 in self.states)
prob = sum((self.fwd[len(obs) - 1][s]) for s in self.states)
return prob
def forward_log(self, obs):
self.fwd_log = [{}]
# Initialize base cases (t == 0)
for y in self.states:
self.fwd_log[0][y] = self.logpi[y] + self.logB[y][obs[0]]
# Run Forward algorithm for t > 0
for t in range(1, len(obs)):
self.fwd_log.append({})
for y in self.states:
ailist = [] #initialize ths as we need the max value of ai
for y0 in self.states:
ai = self.fwd_log[t-1][y0] + self.logA[y0][y] + self.logB[y][obs[t]]
#print "ai = ", ai, "aimax = ", aimax
ailist.append(ai)
aimax = max(ailist)
self.fwd_log[t][y] = aimax + math.log(sum((math.exp(self.fwd_log[t-1][y0] + self.logA[y0][y] + self.logB[y][obs[t]] - aimax)) for y0 in self.states))
#print aimax
prob = sum((math.exp(self.fwd_log[len(obs) - 1][s])) for s in self.states)
return prob
def viterbi(self, obs):
vit = [{}]
path = {}
# Initialize base cases (t == 0)
for y in self.states:
vit[0][y] = self.pi[y] * self.B[y][obs[0]]
path[y] = [y]
# Run Viterbi for t > 0
for t in range(1, len(obs)):
vit.append({})
newpath = {}
for y in self.states:
(prob, state) = max((vit[t-1][y0] * self.A[y0][y] * self.B[y][obs[t]], y0) for y0 in self.states)
vit[t][y] = prob
newpath[y] = path[state] + [y]
# Don't need to remember the old paths
path = newpath
n = 0 # if only one element is observed max is sought in the initialization values
if len(obs)!=1:
n = t
(prob, state) = max((vit[n][y], y) for y in self.states)
return (prob, path[state])
def viterbi_log(self, obs):
vit = [{}]
path = {}
# Initialize base cases (t == 0)
for y in self.states:
vit[0][y] = self.logpi[y] + self.logB[y][obs[0]]
path[y] = [y]
# Run Viterbi for t > 0
for t in range(1, len(obs)):
vit.append({})
newpath = {}
for y in self.states:
(prob, state) = max((vit[t-1][y0] + self.logA[y0][y] + self.logB[y][obs[t]], y0) for y0 in self.states)
vit[t][y] = prob
newpath[y] = path[state] + [y]
# Don't need to remember the old paths
path = newpath
n = 0 # if only one element is observed max is sought in the initialization values
if len(obs)!=1:
n = t
(prob, state) = max((vit[n][y], y) for y in self.states)
return (prob, path[state])
def forward_backward(self, obs): # returns model given the initial model and observations
gamma = [{} for t in range(len(obs))] # this is needed to keep track of finding a state i at a time t for all i and all t
zi = [{} for t in range(len(obs) - 1)] # this is needed to keep track of finding a state i at a time t and j at a time (t+1) for all i and all j and all t
# get alpha and beta tables computes
p_obs = self.forward(obs)
self.backward(obs)
# compute gamma values
for t in range(len(obs)):
for y in self.states:
gamma[t][y] = (self.fwd[t][y] * self.bwk[t][y]) / p_obs
if t == 0:
self.pi[y] = gamma[t][y]
#compute zi values up to T - 1
if t == len(obs) - 1:
continue
zi[t][y] = {}
for y1 in self.states:
zi[t][y][y1] = self.fwd[t][y] * self.A[y][y1] * self.B[y1][obs[t + 1]] * self.bwk[t + 1][y1] / p_obs
# now that we have gamma and zi let us re-estimate
for y in self.states:
for y1 in self.states:
# we will now compute new a_ij
val = sum([zi[t][y][y1] for t in range(len(obs) - 1)]) #
val /= sum([gamma[t][y] for t in range(len(obs) - 1)])
self.A[y][y1] = val
# re estimate gamma
for y in self.states:
for k in self.symbols: # for all symbols vk
val = 0.0
for t in range(len(obs)):
if obs[t] == k :
val += gamma[t][y]
val /= sum([gamma[t][y] for t in range(len(obs))])
self.B[y][k] = val
return
def forward_backward_multi(self, obslist): # returns model given the initial model and observations
count = 0
while (True):
temp_aij = {}
temp_bjk = {}
temp_pi = {}
K_list = []
lp0 = 0.0
#set up the transition and emission probs
for y in self.states:
temp_pi[y] = 0.0
temp_bjk[y] = {}
for sym in self.symbols:
temp_bjk[y][sym] = 0.0
temp_aij[y] = {}
for y1 in self.states:
temp_aij[y][y1] = 0.0
#set up the transition and emission probs
for obs in obslist:
zi_num = {}
zi_den = {}
gamma_num = {}
#print 'O = ', obs
p_obs = self.forward_log(obs) # this represents Pk
lp0 += math.log(p_obs)
self.backward_log(obs) # this will set up the beta table
#prob_inv = float(1) / p_obs # this is our pk
#pk_list.append(p_obs) # keep the pk values
for t in range(len(obs) - 1):
zi_num[t] = {}
zi_den[t] = {}
gamma_num[t] = {}
for y in self.states:
zi_num[t][y] = {}
zi_den[t][y] = 0.0
#set up zi values
for y1 in self.states:
xx = math.log(self.A[y][y1])
if self.B[y1][obs[t + 1]] == 0:
print "ERROR for ", obs
yy = math.log(self.B[y1][obs[t + 1]])
zi_num[t][y][y1] = math.exp(self.fwd_log[t][y] + math.log(self.A[y][y1]) + math.log(self.B[y1][obs[t + 1]]) + self.bwk_log[t + 1][y1])
zi_den[t][y] = math.exp(self.fwd_log[t][y] + self.bwk_log[t][y])
#set up gamma values
gamma_num[t][y] = {}
for sym in self.symbols: # for all symbols supported by our HMM
gamma_num[t][y][sym] = 0.0
if obs[t] == sym :
gamma_num[t][y][sym] = math.exp(self.fwd_log[t][y] + self.bwk_log[t][y])
#let us roll up the zi and gamma marginalizing for t
aij_params = {}
bjk_params = {}
for y in self.states:
aij_params[y] = {}
for y1 in self.states:
num = sum([zi_num[t][y][y1] for t in range(len(obs) - 1)]) * (float(1)/p_obs) #
den = sum([zi_den[t][y] for t in range(len(obs) - 1)]) * (float(1)/p_obs) #
aij_params[y]['prob'] = den # marginalized probability of y for kth observation
aij_params[y][y1] = num
bjk_params[y] = {}
for sym in self.symbols:
num = sum([gamma_num[t][y][sym] for t in range(len(obs) - 1)]) * (float(1)/p_obs) #
bjk_params[y]['prob'] = den
bjk_params[y][sym] = num
K_list.append({'aij': aij_params, 'bjk': bjk_params})
# now we are done with all observations and the K_list holds the values for our aij, bkj, pi
for y in self.states:
temp_pi[y] += zi_den[0][y] * (float(1)/p_obs)
for y1 in self.states:
den_sum = 0.0
for k in K_list: # go through all observations
temp_aij[y][y1] += k['aij'][y][y1]
#print 'prob = ', k['aij'][y]['prob']
den_sum += k['aij'][y]['prob']
temp_aij[y][y1] /= den_sum
for sym in self.symbols:
den_sum = 0.0
for k in K_list:
temp_bjk[y][sym] += k['bjk'][y][sym]
#print 'prob = ', k['bjk'][y]['prob']
den_sum += k['bjk'][y]['prob']
temp_bjk[y][sym] /= den_sum
#print '----------TEMP = ', temp_aij, ' obs = ', obs, ' bjk = ', temp_bjk, ' pi = ', temp_pi
#print K_list
#print '\nAIJ = ', temp_aij
#print '\nBKJ = ', temp_bjk
#print '\nPI = ', temp_pi
self.A = temp_aij
self.B = temp_bjk
self.pi = temp_pi
self.set_log_model()
p = 0.0
lp = 0.0
for obs in obslist:
p = self.forward_log(obs)
lp += math.log(p)
#print 'lp0 = ', lp0, ' lp = ', lp
if (math.fabs((lp - lp0)) < 100) or (count >= 100):
break
else:
count += 1
lp0 = 0.0
return
#if __name__ == '__main__':