-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhmm.h
464 lines (370 loc) · 16.8 KB
/
hmm.h
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
#pragma once
#include <unordered_map>
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
#include <array>
#include <fstream>
#include <set>
int argmax(double* arr, int size) {
auto max_element = std::max_element(arr, arr + size);
return std::distance(arr, max_element);
}
std::pair<double, std::string> evaluate(const std::string& emissions, const std::string& predicted, bool simple=true) {
if (emissions.length() != predicted.length()) {
std::cerr << "True emissions and predicted emissions differ in length!" << std::endl;
return {{},{}};
}
int correct = 0;
std::string hit_or_miss;
for (auto i = 0; i < emissions.length(); ++i) {
if (simple ? emissions[i] == predicted[i]
: (std::isupper(emissions[i]) && std::isupper(predicted[i])) || (std::islower(emissions[i]) && std::islower(predicted[i]))) {
correct++;
hit_or_miss.append("+");
} else {
hit_or_miss.append("-");
}
}
return {correct / (emissions.length() * 1.0), hit_or_miss};
}
// forward declaration
class Test;
// N = len(states), M = len(symbols)
template<int N, int M>
class hidden_markov_chain {
friend class Test;
// known from the beginning
std::vector<char> _states;
std::vector<char> _symbols;
// parameters that we need to figure out
double _transition_probabilities[N][N]; // N x N // a
double _emission_probabilities[N][M]; // N x M // b
double _states_probabilities[N]; // N // initial_distribution
// emission to emission_index_map
std::unordered_map<char, int> _emission_to_idx;
std::unordered_map<char, int> _state_to_idx;
public:
hidden_markov_chain(const std::vector<char>& states, const std::vector<char>& symbols) :
_states(states), _symbols(symbols), _state_to_idx(create_state_to_idx_map(states)),
_emission_to_idx(create_emission_to_idx_map(symbols)) {
}
void fit(const std::string& states, const std::string& emissions, int n_iter) {
estimate_initial_probabilities(states, emissions);
print_transition_probabilities_and_emission_probabilities();
baum_welch_algorithm(emissions, n_iter);
}
std::string predict(const std::string& data) {
return viterbi_algorithm(data);
}
private:
static std::unordered_map<char, int> create_emission_to_idx_map(const std::vector<char>& emissions) {
int index = 0;
std::unordered_map<char, int> emission_to_idx;
for (char c: emissions)
emission_to_idx[c] = index++;
return emission_to_idx;
}
static std::unordered_map<char, int> create_state_to_idx_map(const std::vector<char>& states) {
int index = 0;
std::unordered_map<char, int> state_to_idx;
for (char c: states)
state_to_idx[c] = index++;
return state_to_idx;
}
void estimate_initial_probabilities(const std::string& states, const std::string& emissions) {
//estimate initial probabilities for transitions, emissions and states based on frequencies, slide 4 (HMM2)
if (states.length() != emissions.length()) {
std::cerr << "Given states and corresponding emissions MUST match in length." << std::endl;
return;
}
// INITIAL STATES PROBABILITIES
// count all states occurrences
std::unordered_map<char, int> states_freqs;
for (char state: states)
states_freqs[state] += 1;
// estimate initial states probabilities as freq(state) / number_of_emissions
for (auto i = 0; i < _states.size(); ++i)
_states_probabilities[i] = (double) states_freqs[_states[i]] / (double) states.length();
// INITIAL TRANSITIONS PROBABILITIES
// count all transitions
std::unordered_map<std::string, int> transition_freqs;
for (auto i = 0; i < states.length() - 1; ++i)
transition_freqs[states.substr(i, 2)] += 1;
// put frequencies of transitions into matrix
for (auto i = 0; i < _states.size(); ++i) {
for (auto j = 0; j < _states.size(); ++j) {
auto from_state = _states[i];
auto to_state = _states[j];
std::string transition{from_state, to_state};
_transition_probabilities[i][j] = (double) transition_freqs[transition];
}
}
// make probabilities of frequencies found in matrix
for (auto i = 0; i < _states.size(); ++i) {
double col_sum = 0;
for (auto j = 0; j < _states.size(); ++j) col_sum += _transition_probabilities[j][i];
for (auto j = 0; j < _states.size(); ++j) _transition_probabilities[j][i] /= col_sum;
}
// deal with 0 probabilities
double epsilon = 0.01;
for (auto i = 0; i < N; ++i) {
for (auto j = 0; j < N; ++j) {
if ( _transition_probabilities[i][j] == 0)
_transition_probabilities[i][j] = epsilon;
// they aren't correcting for this so aren't we
}
}
// INITIAL EMISSIONS PROBABILITIES
std::unordered_map<char, std::vector<int>> emission_freqs;
std::unordered_map<char, int> state_to_idx;
for (char _symbol: _symbols)
for (auto j = 0; j < _states.size(); ++j) {
emission_freqs[_symbol].resize(_states.size());
state_to_idx[_states[j]] = j;
}
for (auto i = 0; i < emissions.length(); ++i) {
auto state = states[i];
auto emission = emissions[i];
emission_freqs[emission][state_to_idx[state]] += 1;
}
for (auto i = 0; i < _states.size(); ++i) {
for (auto j = 0; j < _symbols.size(); ++j)
_emission_probabilities[i][j] = (double) emission_freqs[_symbols[j]][i] / (double) states_freqs[_states[i]];
}
}
double** forward(const std::string& data) {
size_t data_length = data.length();
auto** alpha = new double*[data_length];
for (int i = 0; i < data_length; i++) {
alpha[i] = new double[N];
for (int j = 0; j < N; j++) {
i == 0 ? alpha[0][j] = - log(_states_probabilities[j]) - log(_emission_probabilities[j][0])
: alpha[i][j] = 0;
}
}
int V[data_length];
for (int i = 0; i < data_length; i++) V[i] = _emission_to_idx[data[i]];
for (int i = 1; i < data_length; i++) {
for (int j = 0; j < N; j++) {
double result = 0;
for (int k = 0; k < N; k++)
result += - log(alpha[i - 1][k]) - log(_transition_probabilities[k][j]) - log(_emission_probabilities[j][V[i]]);
alpha[i][j] = result;
}
}
// convert back from log space
for (int i = 0; i < data_length; i++) {
for (int j = 0; j < N; j++)
alpha[i][j] = exp(-alpha[i][j]);
}
return alpha;
}
double** backward(const std::string& data) {
size_t data_length = data.length();
auto** beta = new double* [data_length];
for (int i = 0; i < data_length; i++) {
beta[i] = new double[N];
for (int j = 0; j < N; j++)
i == data_length - 1 ? beta[data_length - 1][j] = 1
: beta[i][j] = 0;
}
int V[data_length];
for (int i = 0; i < data_length; i++) V[i] = _emission_to_idx[data[i]];
for (int i = data_length - 2; i >= 0; i--) {
for (int j = 0; j < N; j++) {
double result = 0;
for (int k = 0; k < N; k++)
result += - log(beta[i + 1][k]) - log(_transition_probabilities[j][k]) - log(_emission_probabilities[k][V[i + 1]]);
beta[i][j] = result;
}
}
// convert back from log space
for (int i = 0; i < data_length; i++) {
for (int j = 0; j < N; j++)
beta[i][j] = exp(-beta[i][j]);
}
return beta;
}
std::string viterbi_algorithm(const std::string& data) {
std::unordered_map<int, char> idx_to_state;
for (auto& it: _state_to_idx) idx_to_state[it.second] = it.first;
int data_length = data.length();
int V[data_length];
for (int i = 0; i < data_length; i++) V[i] = _emission_to_idx[data[i]];
int T = data_length;
double omega[T][N];
for (int i = 0; i < T; i++){
for (int j = 0; j < N; j++){
omega[i][j] = 0.0;
}
}
int prev[T-1][N];
for (int i = 0; i < T; i++){
for (int j = 0; j < N; j++){
prev[i][j] = 0;
}
}
for (int j = 0; j < N; j++) {
omega[0][j] = log(_states_probabilities[j] * _emission_probabilities[j][V[0]]);
}
for (int t = 1; t < T; t++){
for (int j = 0; j < N; j++){
double probability[N];
double *omega_row = omega[t-1];
for(int p = 0; p < N; p++){
probability[p] = omega_row[p] + log(_transition_probabilities[p][j]) + log(_emission_probabilities[j][V[t]]);
}
int size_of_probability = sizeof(probability) / sizeof(probability[0]);
int argMax = argmax(probability, size_of_probability);
prev[t-1][j] = argMax;
omega[t][j] = probability[argMax];
}
}
int S[T];
for (int i = 0; i < T; i++){
S[i] = 0;
}
double temp[N];
double *omega_row = omega[T-1];
for (int p = 0; p < N; p++){
temp[p] = omega_row[p];
}
int size_of_temp = sizeof(temp) / sizeof(temp[0]);
int argMax = argmax(temp, size_of_temp);
int last_state = argMax;
S[0] = last_state;
int backtrack_index = 1;
for (int i = T - 2; i >= 0; i--){
S[backtrack_index] = prev[i][last_state];
last_state = prev[i][last_state];
backtrack_index++;
}
std::string result;
for (int i = data_length - 1; i >= 0; i--){
result += idx_to_state[S[i]];
}
return result;
}
void baum_welch_algorithm(const std::string& data, int n_iter) {
int data_length = data.length(); // T
int V[data_length];
for (int i = 0; i < data_length; i++) V[i] = _emission_to_idx[data[i]];
for (int n = 0; n < n_iter; n++) {
double** alpha = forward(data);
double** beta = backward(data);
// Inicijalizacija xi
double*** xi = new double** [N];
for (int j = 0; j < N; j++) {
xi[j] = new double* [N];
for (int k = 0; k < N; k++) {
xi[j][k] = new double[data_length - 1];
for (int m = 0; m < data_length - 1; m++) xi[j][k][m] = 0;
}
}
for (int t = 0; t < data_length - 1; t++) {
double* first_dot = new double[N]; // np.dot(alpha2[t, :].T, a2)
for (int t_alpha = 0; t_alpha < N; t_alpha++) {
double first_dot_r = 0;
for (int t_transition = 0; t_transition < N; t_transition++)
first_dot_r += alpha[t][t_transition] * _transition_probabilities[t_transition][t_alpha];
first_dot[t_alpha] = first_dot_r;
}
double second_result[N]; // b2[:, V2[t + 1]].T
for (int vpom = 0; vpom < N; vpom++)
second_result[vpom] = _emission_probabilities[vpom][V[t + 1]];
double third_result[N]; // np.dot(alpha2[t, :].T, a2) * b2[:, V2[t + 1]].T
for (int vpom = 0; vpom < N; vpom++)
third_result[vpom] = first_dot[vpom] * second_result[vpom];
double fourth_result[N]; // beta2[t + 1, :]
for (int vpom = 0; vpom < N; vpom++)
fourth_result[vpom] = beta[t + 1][vpom];
double denominator = 0;
for (int vpom = 0; vpom < N; vpom++)
denominator += third_result[vpom] * fourth_result[vpom];
// if(t == 0)std::cout << "denominator: " << denominator << std::endl;
for (int i = 0; i < N; i++) {
double numerator[N]; // alpha2[t, i] * a2[i, :] * b2[:, V2[t + 1]].T * beta2[t + 1, :].T
for (int vpom = 0; vpom < N; vpom++)
numerator[vpom] = alpha[t][i] * _transition_probabilities[i][vpom] *
_emission_probabilities[vpom][V[t + 1]] * beta[t + 1][vpom];
for (int vpom = 0; vpom < N; vpom++)
xi[i][vpom][t] = numerator[vpom] / denominator;
}
}
double** gamma = new double* [N]; // np.sum(xi, axis=1)
double* gamma_sum_by_1_axis = new double[N]; // np.sum(gamma, axis=1).reshape((-1, 1))
for (int vpom1 = 0; vpom1 < N; vpom1++) {
gamma[vpom1] = new double[data_length - 1];
gamma_sum_by_1_axis[vpom1] = 0;
for (int vpom2 = 0; vpom2 < data_length - 1; vpom2++) {
gamma[vpom1][vpom2] = 0;
for (int vpom3 = 0; vpom3 < N; vpom3++)
gamma[vpom1][vpom2] += xi[vpom1][vpom3][vpom2];
gamma_sum_by_1_axis[vpom1] += gamma[vpom1][vpom2];
}
}
double** pom_var_gamma = new double* [N];// np.sum(xi, 2)
for (int vpom1 = 0; vpom1 < N; vpom1++) {
pom_var_gamma[vpom1] = new double[N];
for (int vpom2 = 0; vpom2 < N; vpom2++) {
pom_var_gamma[vpom1][vpom2] = 0;
for (int vpom3 = 0; vpom3 < data_length - 1; vpom3++)
pom_var_gamma[vpom1][vpom2] += xi[vpom1][vpom2][vpom3];
_transition_probabilities[vpom1][vpom2] = pom_var_gamma[vpom1][vpom2] / gamma_sum_by_1_axis[vpom1];
}
}
// Add additional T'th element in gamma
double xi_sum_by_axis_2[N]; // np.sum(xi[:, :, T - 2], axis=0)
for (int vpom1 = 0; vpom1 < N; vpom1++) {
xi_sum_by_axis_2[vpom1] = 0;
for (int vpom2 = 0; vpom2 < N; vpom2++)
xi_sum_by_axis_2[vpom1] += xi[vpom2][vpom1][data_length - 2];
}
double** new_gamma = new double* [N];// gamma = np.hstack((gamma, np.sum(xi[:, :, T - 2], axis=0).reshape((-1, 1))))
for (int vpom1 = 0; vpom1 < N; vpom1++) {
new_gamma[vpom1] = new double[data_length];
for (int vpom2 = 0; vpom2 < data_length - 1; vpom2++)
new_gamma[vpom1][vpom2] = gamma[vpom1][vpom2];
new_gamma[vpom1][data_length - 1] = xi_sum_by_axis_2[vpom1];
}
// M je K
double denominator_2[N];
for (int vpom1 = 0; vpom1 < N; vpom1++)
denominator_2[vpom1] = gamma_sum_by_1_axis[vpom1] + xi_sum_by_axis_2[vpom1];
for (int vpom1 = 0; vpom1 < M; vpom1++) {
for (int vpom2 = 0; vpom2 < N; vpom2++) {
double pom_new_gamma_sum = 0;
for (int vpom3 = 0; vpom3 < data_length; vpom3++) {
if (vpom1 == V[vpom3])
pom_new_gamma_sum += new_gamma[vpom2][vpom3];
}
_emission_probabilities[vpom2][vpom1] = pom_new_gamma_sum / denominator_2[vpom2];
}
}
delete[] gamma_sum_by_1_axis;
delete[] pom_var_gamma;
delete[] new_gamma;
delete[] gamma;
}
// print_transition_probabilities_and_emission_probabilities();
}
void print_transition_probabilities_and_emission_probabilities() {
std::cout << "_transition_probabilities" << std::endl;
for (int t_alpha = 0; t_alpha < N; t_alpha++) {
for (int t_transition = 0; t_transition < N; t_transition++)
std::cout << _transition_probabilities[t_alpha][t_transition] << ", ";
std::cout << std::endl;
}
std::cout << std::endl;
std::cout << "_transition_probabilities end" << std::endl << std::endl;
std::cout << "_emission_probabilities" << std::endl;
for (int vpom1 = 0; vpom1 < N; vpom1++) {
for (int vpom2 = 0; vpom2 < M; vpom2++)
std::cout << _emission_probabilities[vpom1][vpom2] << ", ";
std::cout << std::endl;
}
std::cout << "_emission_probabilities end" << std::endl << std::endl;
}
};