-
Notifications
You must be signed in to change notification settings - Fork 0
/
HMM.py
161 lines (127 loc) · 4.13 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
import numpy as np
import math
# init variables
a = np.matrix([[1, 0, 0, 0], [0.2, 0.3, 0.1, 0.4],
[0.2, 0.5, 0.2, 0.1], [0.7, 0.1, 0.1, 0.1]]) # Transition proba (i=nb of state j=nb of sample)
b = np.matrix([[1, 0, 0, 0, 0], [0, 0.3, 0.4, 0.1, 0.2],
[0, 0.1, 0.1, 0.7, 0.1], [0, 0.5, 0.2, 0.1, 0.2]]) # Emission proba -j=nb of state k=nb of output)
T = 5
N = 4
z_init = 1 # Initial state (Range between 0 and N-1)
x = [1, 3, 2, 0] # outputs of the system
# HMM Forward algorithm
def forward(a, b, x):
print("Forward")
alpha = np.zeros((N, T))
for t in range(T):
for j in range(N):
if t == 0:
if j == z_init:
alpha[j, t] = 1
else:
alpha[j, t] = 0
else:
sum = 0
for i in range(N):
alpha[j, t] += a[i, j] * alpha[i, t-1]
print(alpha[i, t-1])
alpha[j, t] *= b[j, x[t - 1]]
#alpha[j, t] = b[j, x[t-1]]*np.sum(np.multiply(a[:, j], alpha[:, t-1]))
print("alpha =")
print(alpha)
probmodel = alpha[0, T - 1]
print("Probamodel forward " + str(probmodel))
return alpha
# HMM Backward algorithm
def backward(a, b, x):
print("Backward")
beta = np.zeros((N, T))
beta[0, T - 1] = 1
for t in range(T-2, -1, -1):
for i in range(N):
# if t == T - 1:
# if i == 0:
# beta[i, t] = 1
# else:
# beta[i, t] = 0
# else:
for j in range(N):
beta[i, t] += beta[j, t + 1] * a[i, j] * b[j, x[t]]
probmodel = beta[N - 1, 0]
print(beta)
print("Probamodel backward " + str(probmodel))
return beta
def estimate_gamma(alpha, beta, a, b, x):
print("Estimate gamma")
gamma = np.zeros((N, N, T))
for t in range(1, T):
for i in range(N):
for j in range(N):
gamma[i, j, t] = alpha[i, t - 1] * a[i, j] * b[j, x[t - 1]] * beta[j, t]
# print("gamma=")
#print(gamma)
return gamma
def update_a(a, gamma, x):
print("Update a")
for i in range(N):
for j in range(N):
den = 0
nom = 0
for t in range(T):
nom += gamma[i, j, t]
#print("gamma " + str(gamma[i, j, t]))
for l in range(N):
den += gamma[i, l, t]
if den == 0:
den += 1
#print("nom" + str(nom))
#print("den" + str(den))
a[i, j] = nom / den
a[0, 0] = 1
return a
def update_b(b, gamma, x):
print("Update b")
for j in range(N):
for k in range(T-1):
den = 0
nom = 0
for t in range(1, T):
if x[t - 1] == k:
b[j, k + 1] += np.sum(gamma[j, :, t])
#if t == 3 and k == 2:
#print("allo")
#print(np.sum(gamma[j, :, t]))
#print(b[j, k + 1])
for t in range(1, T):
den += np.sum(gamma[j, :, t])
if den == 0:
den += 1
b[j, k + 1] = b[j, k + 1] / den
b[0, 0] = 1
return b
def baum_welch(x):
print("Baum Welch")
a = np.matrix([[1, 0, 0, 0], [0.2, 0.3, 0.1, 0.4], [0.2, 0.5, 0.2, 0.1],
[0.7, 0.1, 0.1, 0.1]]) # Transition proba (i=nb of state j=nb of sample)
b = np.matrix([[1, 0, 0, 0, 0], [0, 0.3, 0.4, 0.1, 0.2], [0, 0.1, 0.1, 0.7, 0.1],
[0, 0.5, 0.2, 0.1, 0.2]]) # Emission proba -j=nb of state k=nb of output)
print(a)
print(b)
iter = 0
while iter in range(10):
print("Iteration: " + str(iter))
# Estimate alpha and beta
alpha = forward(a, b, x)
beta = backward(a, b, x)
# Estimate gamma
gamma = estimate_gamma(alpha, beta, a, b, x)
# Update a and beta
a = update_a(a, gamma, x)
b = update_b(b, gamma, x)
print("a=")
print(a)
print("b=")
print(b)
iter += 1
return a, b
baum_welch(x)