-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhmm.py
204 lines (150 loc) · 5.73 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
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
# -*- coding: utf-8 -*-
import numpy as np
class HiddenMarkovModel:
"""A Hidden Markov Model (HMM).
Attributes
----------
states : array_like or numpy ndarray
List of states.
observations : array_like or numpy ndarray
Observations space array.
tp : array_like or numpy ndarray
Transition probability matrix which stores probability of
moving from state i (row) to state j (col).
ep : array_like or numpy ndarray
Emission probability matrix which stores probability of
seeing observation o (col) from state s (row).
pi : array_like or numpy ndarray
Initial state probabilities array.
"""
def __init__(self, states, observations, tp, ep, pi):
self.states = np.array(states)
self.observations = np.array(observations)
self.num_states = self.states.shape[0]
self.num_observations = self.observations.shape[0]
self.tp = np.array(tp)
self.ep = np.array(ep)
self.pi = np.array(pi)
def likelihood(self, obs):
"""Compute the likelihood of an observation sequence.
Parameters
----------
obs : array_like or numpy ndarray
Sequence of observations.
Returns
-------
prob : float
Probability likelihood for observation sequence.
"""
prob, _ = self.likelihood_forward(obs)
return prob
def likelihood_forward(self, obs):
"""Compute observation likelihood using the forward algorithm.
Parameters
----------
obs : array_like or numpy ndarray
Sequence of observations of size T.
Returns
-------
prob : float
Probability likelihood for observation sequence.
alpha : numpy ndarray
Forward probability matrix of shape (num_states x T).
"""
T = len(obs)
alpha = np.zeros((self.num_states, T))
# initialization
o_0 = self._get_observation_idx(obs[0])
alpha[:, 0] = self.pi * self.ep[:, o_0]
# recursion
for t in range(1, T):
o_t = self._get_observation_idx(obs[t])
alpha[:, t] = alpha[:, t-1].dot(self.tp) * self.ep[:, o_t]
# termination
prob = alpha[:, T-1].sum()
return prob, alpha
def likelihood_backward(self, obs):
"""Compute observation likelihood using the backward algorithm.
Parameters
----------
obs : array_like or numpy ndarray
Sequence of observations of size T.
Returns
-------
prob : float
Probability likelihood for observation sequence.
beta : numpy ndarray
Backward probability matrix of shape (num_states x T).
"""
T = len(obs)
beta = np.zeros((self.num_states, T))
# initialization
beta[:, T-1] = 1
# recursion
for t in range(T-2, -1, -1):
o_t1 = self._get_observation_idx(obs[t+1])
beta[:, t] = self.tp.dot(self.ep[:, o_t1] * beta[:, t+1])
# termination
o_0 = self._get_observation_idx(obs[0])
prob = self.pi.dot(self.ep[:, o_0] * beta[:, 0])
return prob, beta
def decode(self, obs):
"""Determine the best hidden sequence using the Viterbi algorithm.
Parameters
----------
obs : array_like or numpy ndarray
Sequence of observations of size T.
Returns
-------
path : numpy ndarray
Sequence of states of size T.
prob : float
Probability likelihood for observation sequence along path.
"""
T = len(obs)
delta = np.zeros((self.num_states, T))
# initialization
o_0 = self._get_observation_idx(obs[0])
delta[:, 0] = self.pi * self.ep[:, o_0]
# recursion
for t in range(1, T):
o_t = self._get_observation_idx(obs[t])
delta_prev = delta[:, t-1].reshape(-1, 1)
delta[:, t] = (delta_prev * self.tp).max(axis=0) * self.ep[:, o_t]
# termination
path = self.states[delta.argmax(axis=0)]
prob = delta[:, T-1].max()
return path, prob
def learn(self, obs, iterations=1):
"""Learn parameters from an observation sequence using Baum-Welch.
Parameters
----------
obs : array_like or numpy ndarray
Sequence of observations of size T.
iterations : int, optional
Number of Expectation-Maximization (EM) iterations.
Defaults to 1.
"""
for _ in range(iterations):
T = len(obs)
# expectation step
likelihood, alpha = self.likelihood_forward(obs)
_, beta = self.likelihood_backward(obs)
gamma = alpha * beta / (alpha * beta).sum(axis=0)
xi = np.zeros((self.num_states, self.num_states, T-1))
for t in range(T-1):
o_t1 = self._get_observation_idx(obs[t+1])
for i in range(self.num_states):
xi[i, :, t] = alpha[i, t] * self.tp[i, :] \
* self.ep[:, o_t1] * beta[:, t+1]
xi /= xi.sum(axis=(0, 1))
# maximization step
self.pi = gamma[:, 0]
self.tp = xi.sum(axis=2) / gamma[:, :-1].sum(axis=1).reshape(-1, 1)
for idx, o in enumerate(self.observations):
indices = np.argwhere(obs == o).flatten()
self.ep[:, idx] = gamma[:, indices].sum(axis=1) \
/ gamma.sum(axis=1)
def _get_observation_idx(self, o):
"""Get the vocabulary index value of an observation."""
return np.argwhere(self.observations == o).flatten().item()