-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhmm.py
executable file
·174 lines (136 loc) · 4.49 KB
/
hmm.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
from __future__ import print_function
import json
import numpy as np
import sys
def forward(pi, A, B, O):
"""
Forward algorithm
Inputs:
- pi: A numpy array of initial probailities. pi[i] = P(Z_1 = s_i)
- A: A numpy array of transition probailities. A[i, j] = P(Z_t = s_j|Z_t-1 = s_i)
- B: A numpy array of observation probabilities. B[i, k] = P(X_t = o_k| Z_t = s_i)
- O: A list of observation sequence (in terms of index, not the actual symbol)
Returns:
- alpha: A numpy array alpha[j, t] = P(Z_t = s_j, x_1:x_t)
"""
S = len(pi)
N = len(O)
alpha = np.zeros([S, N])
for j in range(S):
alpha[j,0]=pi[j]*B[j,O[0]]
for t in range(1,N):
for j in range(S):
alpha[j,t]=0
for i in range(S):
alpha[j,t]= alpha[j,t] + (alpha[i,t-1]*A[i,j]*B[j,O[t]])
return alpha
def backward(pi, A, B, O):
"""
Backward algorithm
Inputs:
- pi: A numpy array of initial probailities. pi[i] = P(Z_1 = s_i)
- A: A numpy array of transition probailities. A[i, j] = P(Z_t = s_j|Z_t-1 = s_i)
- B: A numpy array of observation probabilities. B[i, k] = P(X_t = o_k| Z_t = s_i)
- O: A list of observation sequence (in terms of index, not the actual symbol)
Returns:
- beta: A numpy array beta[j, t] = P(Z_t = s_j, x_t+1:x_T)
"""
S = len(pi)
N = len(O)
beta = np.zeros([S, N])
for j in range(S):
beta[j,N-1]=1
for t in range(N-2, -1, -1):
for i in range(S):
beta[i,t]=0
for j in range(S):
beta[i,t]= beta[i,t] + (beta[j,t+1]*A[i,j]*B[j,O[t+1]])
return beta
def seqprob_forward(alpha):
"""
Total probability of observing the whole sequence using the forward algorithm
Inputs:
- alpha: A numpy array alpha[j, t] = P(Z_t = s_j, x_1:x_t)
Returns:
- prob: A float number of P(x_1:x_T)
"""
prob = 0
T=alpha.shape[1]
prob=np.sum(alpha[:,T-1])
return prob
def seqprob_backward(beta, pi, B, O):
"""
Total probability of observing the whole sequence using the backward algorithm
Inputs:
- beta: A numpy array beta: A numpy array beta[j, t] = P(Z_t = s_j, x_t+1:x_T)
- pi: A numpy array of initial probailities. pi[i] = P(Z_1 = s_i)
- B: A numpy array of observation probabilities. B[i, k] = P(X_t = o_k| Z_t = s_i)
- O: A list of observation sequence
(in terms of the observation index, not the actual symbol)
Returns:
- prob: A float number of P(x_1:x_T)
"""
prob = 0
S=beta.shape[0]
for i in range(S):
prob=prob+ (beta[i,0]*pi[i]*B[i,O[0]])
return prob
def viterbi(pi, A, B, O):
"""
Viterbi algorithm
Inputs:
- pi: A numpy array of initial probailities. pi[i] = P(Z_1 = s_i)
- A: A numpy array of transition probailities. A[i, j] = P(Z_t = s_j|Z_t-1 = s_i)
- B: A numpy array of observation probabilities. B[i, k] = P(X_t = o_k| Z_t = s_i)
- O: A list of observation sequence (in terms of index, not the actual symbol)
Returns:
- path: A list of the most likely hidden state path k* (in terms of the state index)
argmax_k P(s_k1:s_kT | x_1:x_T)
"""
path = []
S = len(pi)
N = len(O)
delta = np.zeros([S, N])
temppath = np.zeros([S, N], int)
for j in range(S):
delta[j][0]=pi[j]*B[j,O[0]]
for t in range(1,N):
for j in range(S):
temp=[]
for i in range(S):
temp.append(delta[i][t-1]*A[i,j]*B[j,O[t]])
delta[j,t]=np.max(temp)
temppath[j,t]=np.argmax(temp)
index=(int)(np.argmax(delta[:,N-1]))
path.append(index)
for t in range(N-1,0,-1):
index=temppath[index,t]
path.append(index)
path.reverse()
return path
def main():
model_file = sys.argv[1]
Osymbols = sys.argv[2]
#### load data ####
with open(model_file, 'r') as f:
data = json.load(f)
A = np.array(data['A'])
B = np.array(data['B'])
pi = np.array(data['pi'])
#### observation symbols #####
obs_symbols = data['observations']
#### state symbols #####
states_symbols = data['states']
N = len(Osymbols)
O = [obs_symbols[j] for j in Osymbols]
alpha = forward(pi, A, B, O)
beta = backward(pi, A, B, O)
prob1 = seqprob_forward(alpha)
prob2 = seqprob_backward(beta, pi, B, O)
print('Total log probability of observing the sequence %s is %g, %g.' % (Osymbols, np.log(prob1), np.log(prob2)))
viterbi_path = viterbi(pi, A, B, O)
print('Viterbi best path is ')
for j in viterbi_path:
print(states_symbols[j], end=' ')
if __name__ == "__main__":
main()