-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhmm.cc
458 lines (371 loc) · 16.7 KB
/
hmm.cc
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
#include <algorithm>
#include <vector>
#include <stdexcept>
#include <iostream>
#include <cstddef>
#include "hmm.h"
using std::vector;
using std::string;
using std::pair;
using HMM::Data::Model;
using HMM::Data::ExperimentData;
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Data namespace definitions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
/**
* \note
* Auxiliary functions, for internal usage only.
*/
namespace
{
/**
* \brief Converts first string element to char emission symbol for HMM
*
* \note
* String assumed to be non-empty and symbol[0] is supposed to be in a..z ascii.
*/
char symbolToInd(const string& symbol)
{
return symbol[0] - 'a';
}
};
void Model::ReadModel(std::istream& modelSource)
{
// section: states reading
size_t nstates;
string stateName;
modelSource >> nstates;
if (nstates < 2) {
throw std::domain_error("There must be at least two states: begin and end");
}
for (size_t i = 0; i < nstates; ++i) {
modelSource >> stateName;
stateNameToIndex[stateName] = i;
stateIndexToName.push_back(stateName);
}
// section: alphabet reading
modelSource >> alphabetSize;
// section: transitions reading
size_t ntransitions;
string targetStateName;
transitionProb.assign(nstates, vector<double> (nstates, 0));
modelSource >> ntransitions;
for (size_t i = 0; i < ntransitions; ++i) {
double prob;
modelSource >> stateName >> targetStateName >> prob;
size_t fromInd = stateNameToIndex[stateName];
size_t toInd = stateNameToIndex[targetStateName];
if (fromInd + 1 == nstates) {
throw std::domain_error("Transition from the ending state is forbidden");
}
if (toInd == 0) {
throw std::domain_error("Transition to the starting state is forbidden");
}
transitionProb[fromInd][toInd] = prob;
}
// section: state-symbol emission probabilities reading
size_t nemissions;
string symbol; // supposed to be single character, string is used for simpler reading code
stateSymbolProb.assign(nstates, vector<double> (alphabetSize, 0));
modelSource >> nemissions;
for (size_t i = 0; i < nemissions; ++i) {
double prob;
modelSource >> stateName >> symbol >> prob;
size_t stateInd = stateNameToIndex[stateName];
size_t symbolInd = symbolToInd(symbol);
if (stateInd == 0 || stateInd + 1 == nstates) {
throw std::domain_error("Symbol emission from the beginning or the ending states is forbidden");
}
stateSymbolProb[stateInd][symbolInd] = prob;
}
}
void ExperimentData::ReadExperimentData(const Model& model, std::istream& dataSource)
{
size_t nsteps;
size_t stepNumber;
string stateName;
string symbol;// supposed to be single character, string is used for simpler reading code
dataSource >> nsteps;
if (nsteps == 0) {
throw std::domain_error("Empty experiment data");
}
for (size_t i = 0; i < nsteps; ++i) {
dataSource >> stepNumber >> stateName >> symbol;
size_t stateInd = model.stateNameToIndex.at(stateName);
size_t symbolInd = symbolToInd(symbol);
timeStateSymbol.emplace_back(stepNumber, stateInd, symbolInd);
}
}
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< end of Data namespace definitions <<<<<<<<<<<<<<<<<<<<<<<<<<
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Algorithms namespace definitions >>>>>>>>>>>>>>>>>>>>>>>>>>
const size_t HMM_UNDEFINED_STATE = -1;
/**
* \note
* Auxiliary functions, for internal usage only.
*/
namespace
{
/**
* \brief Aux. function to calculate new state probability for the Viterbi algorithm step
*/
double CalcNewStateProbability(size_t stepNumber, size_t prevState,
size_t curState, size_t curSymbol, const Model& model,
const vector<vector<double> >& sequenceProbability)
{
double prevProbability = 1.;
if (stepNumber == 0 && prevState == 0) {
prevProbability = 1.;
} else if (stepNumber == 0 && prevState != 0) {
prevProbability = 0.;
} else {
prevProbability = sequenceProbability[stepNumber - 1][prevState];
}
return (prevProbability *
model.transitionProb[prevState][curState] *
model.stateSymbolProb[curState][curSymbol]);
}
/**
* \brief Aux. function to find the best previous state during the Viterbi algorithm step
*/
size_t FindBestTransitionSource(size_t stepNumber, size_t curState,
size_t curSymbol, const Model& model,
const vector<vector<double> >& sequenceProbability)
{
if (stepNumber == 0) {
return 0;
}
size_t nstates = model.transitionProb.size();
double bestProbValue = -1;
size_t bestPrevState = HMM_UNDEFINED_STATE;
for (size_t prevState = 0; prevState < nstates; ++prevState) {
double curProb = CalcNewStateProbability(stepNumber, prevState,
curState, curSymbol,
model, sequenceProbability);
if (curProb > bestProbValue) {
bestProbValue = curProb;
bestPrevState = prevState;
}
}
// there must be at least two states => the result won't be undefined
return bestPrevState;
}
/**
* \brief Aux. function to get the cumulative forward step transition probability
*
* \details
* This is used inside forward-backward algorithm at forward probabilities calculation.
*/
double CalcForwardStepProbability(size_t stepNumber, size_t curState,
const Model& model, const ExperimentData& data,
const vector<vector<double> >& forwardStateProbability)
{
size_t nstates = model.transitionProb.size();
size_t curSymbol = std::get<2> (data.timeStateSymbol[stepNumber]);
if (stepNumber == 0) {
return model.transitionProb[0][curState] * model.stateSymbolProb[curState][curSymbol];
} else {
double prevCumulativeProb = 0;
for (size_t prevState = 0; prevState < nstates; ++prevState) {
prevCumulativeProb += (forwardStateProbability[stepNumber - 1][prevState] *
model.transitionProb[prevState][curState]);
}
return prevCumulativeProb * model.stateSymbolProb[curState][curSymbol];
}
}
/**
* \brief Aux. function to get the cumulative probability for the backward step
*
* \details
* This is used inside forward-backward algorithm at backward probabilities calculation.
*/
double CalcBackwardStepProbability(size_t stepNumber, size_t curState,
const Model& model, const ExperimentData& data,
const vector<vector<double> >& backwardStateProbability)
{
size_t nstates = model.transitionProb.size();
size_t maxtime = data.timeStateSymbol.size();
if (stepNumber + 1 == maxtime) {
return 1.; // probability to describe empty sequence is 1.
} else {
size_t nextSymbol = std::get<2> (data.timeStateSymbol[stepNumber + 1]);
double nextCumulativeProb = 0.;
for (size_t nextState = 0; nextState < nstates; ++nextState) {
nextCumulativeProb += (model.transitionProb[curState][nextState] *
model.stateSymbolProb[nextState][nextSymbol] *
backwardStateProbability[stepNumber + 1][nextState]);
}
return nextCumulativeProb;
}
}
};
vector<size_t>
HMM::Algorithms::FindMostProbableStateSequence(const Model& model, const ExperimentData& data)
{
// section: prepare and initialize data structures for calculations
size_t nstates = model.transitionProb.size();
size_t maxtime = data.timeStateSymbol.size();
/**
* \note
* sequenceProbability[i][j] is the probability of the most probable sequence of states
* for 1..i observations for which the last state is j-th
*/
vector<vector<double> > sequenceProbability(maxtime,
vector<double> (nstates, 0));
/**
* \note
* prevSeqState[i][j] is the previous state from which the most probable
* sequence (with probability sequenceProbability[i][j]) for 1..i observations with the last
* state at j has been formed.
* This information will help to recover the whole sequence.
*/
vector<vector<size_t> > prevSeqState(maxtime,
vector<size_t> (nstates, HMM_UNDEFINED_STATE));
// section: calculate probabilities for Viterbi algorithm using dynamic programming approach
for (size_t t = 0; t < maxtime; ++t) {
for (size_t curState = 0; curState < nstates; ++curState) {
size_t curSymbol = std::get<2> (data.timeStateSymbol[t]);
size_t bestPrevState = FindBestTransitionSource(t, curState,
curSymbol, model,
sequenceProbability);
double bestProbValue = CalcNewStateProbability(t, bestPrevState,
curState, curSymbol,
model, sequenceProbability);
sequenceProbability[t][curState] = bestProbValue;
prevSeqState[t][curState] = bestPrevState;
}
}
// section: collect most probable sequence in the reverse order
vector<size_t> mostProbableSeq;
ptrdiff_t curStep = maxtime - 1;
// find the last state of the most probable sequence to start recovery from it
size_t curState = std::distance(std::begin(sequenceProbability[curStep]),
std::max_element(std::begin(sequenceProbability[curStep]),
std::end(sequenceProbability[curStep])));
for (; curStep > 0; --curStep) {
curState = prevSeqState[curStep][curState];
mostProbableSeq.push_back(curState);
}
mostProbableSeq.push_back(curState);
// section: restore correct order and return results
std::reverse(std::begin(mostProbableSeq), std::end(mostProbableSeq));
return std::move(mostProbableSeq);
}
vector<vector<pair<double, double> > >
HMM::Algorithms::CalcForwardBackwardProbabiliies(const Model& model, const ExperimentData& data)
{
size_t nstates = model.transitionProb.size();
size_t maxtime = data.timeStateSymbol.size();
/**
* \note
* forwardStateProbability[i][j] is the probability that any hidden sequence (with
* the hidden state at i-th step equal to j) describes first 1..i observations.
*/
vector<vector<double> > forwardStateProbability(maxtime, vector<double> (nstates, 0));
// section: calculate forward probabilities of the forward-backward algorithm
for (size_t t = 0; t < maxtime; ++t) {
for (size_t curState = 0; curState < nstates; ++curState) {
double cumulativePrevProbability =
CalcForwardStepProbability(t, curState, model, data, forwardStateProbability);
forwardStateProbability[t][curState] = cumulativePrevProbability;
}
}
/**
* \note
* backwardStateProbability[i][j] is the probability that any hidden sequence (with
* the hidden state at i+1 step equal to j) describes last i+1..T observations.
*/
vector<vector<double> > backwardStateProbability(maxtime, vector<double> (nstates, 0.));
// section: calculate backward probabilities of the forward-backward algorithm
for (ptrdiff_t t = maxtime - 1; t >= 0; --t) {
for (size_t curState = 0; curState < nstates; ++curState) {
double cumulativeNextProbability =
CalcBackwardStepProbability(t, curState, model, data, backwardStateProbability);
backwardStateProbability[t][curState] = cumulativeNextProbability;
}
}
// section: return joined results
vector<vector<pair<double, double> > > forwardBackwardProbability
(maxtime, vector<pair<double, double> > (nstates, pair<double, double>()));
for (size_t t = 0; t < maxtime; ++t) {
for (size_t curState = 0; curState < nstates; ++curState) {
forwardBackwardProbability[t][curState] =
pair<double, double>(forwardStateProbability[t][curState],
backwardStateProbability[t][curState]);
}
}
return std::move(forwardBackwardProbability);
}
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< end of Algorithms namespace definitions <<<<<<<<<<<<<<<<<<<<
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Estimation namespace definitions >>>>>>>>>>>>>>>>>>>>>>>>>>>
vector<size_t> HMM::Estimation::GetMostProbableStates(
const vector<vector<pair<double, double> > >& forwardBackwardProb)
{
size_t maxtime = forwardBackwardProb.size();
vector<size_t> mostProbableStates;
for (size_t t = 0; t < maxtime; ++t) {
size_t mostProbableState =
std::distance(std::begin(forwardBackwardProb[t]),
std::max_element(std::begin(forwardBackwardProb[t]),
std::end(forwardBackwardProb[t]),
[](const pair<double, double>& prev,
const pair<double, double>& next)
{return (prev.first * prev.second <
next.first * next.second);}));
mostProbableStates.push_back(mostProbableState);
}
return std::move(mostProbableStates);
}
vector<vector<size_t> >
HMM::Estimation::CombineConfusionMatrix(const ExperimentData& realData,
const vector<size_t>& predictedStates,
const Model& model)
{
size_t maxtime = predictedStates.size();
size_t nstates = model.transitionProb.size();
vector<vector<size_t> > confusionMatrix(nstates, vector<size_t> (nstates, 0));
for (size_t t = 0; t < maxtime; ++t) {
size_t predictedInd = predictedStates[t];
size_t realInd = std::get<1>(realData.timeStateSymbol[t]);
++confusionMatrix[predictedInd][realInd];
}
return std::move(confusionMatrix);
}
vector<HMM::Data::PredictionEstimation>
HMM::Estimation::GetStatePredictionEstimations(const vector<vector<size_t> >& confusionMatrix)
{
size_t nstates = confusionMatrix.size();
vector<PredictionEstimation> estimations(nstates);
vector<size_t> colSums(nstates, 0);
vector<size_t> rowSums(nstates, 0);
// section: prepare auxiliary columns sums and row sums for further usage
for (size_t i = 0; i < nstates; ++i) {
for (size_t j = 0; j < nstates; ++j) {
rowSums[i] += confusionMatrix[i][j];
colSums[i] += confusionMatrix[j][i];
}
}
size_t totalObservations = std::accumulate(std::begin(rowSums),
std::end(rowSums), 0UL);
// section: calculate prediction estimations for each state
for (size_t state = 0; state < nstates; ++state) {
estimations[state].truePositives = confusionMatrix[state][state];
estimations[state].falsePositives = rowSums[state] - confusionMatrix[state][state];
// neither predicted to be current state nor its real state is the current one
estimations[state].trueNegatives = totalObservations - rowSums[state] - colSums[state] + confusionMatrix[state][state];
estimations[state].falseNegatives = colSums[state] - confusionMatrix[state][state];
// calculate f-measure
double precision = 0;
double recall = 0;
if (rowSums[state] != 0) {
precision = static_cast<double> (confusionMatrix[state][state]) / static_cast<double> (rowSums[state]);
}
if (colSums[state] != 0) {
recall = static_cast<double> (confusionMatrix[state][state]) / static_cast<double> (colSums[state]);
}
if (rowSums[state] == 0 && colSums[state] == 0) {
estimations[state].fMeasure = 0;
} else {
estimations[state].fMeasure = 2. * (precision * recall) / (precision + recall);
}
}
return std::move(estimations);
}
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< end of Estimation namespace definitions <<<<<<<<<<<<<<<<<<<<