forked from edrosten/threeB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_forward_backward.cc
176 lines (133 loc) · 3.82 KB
/
test_forward_backward.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
#include <tr1/array>
#include <tr1/tuple>
#include <TooN/TooN.h>
#include <algorithm>
#include <numeric>
#include <functional>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdlib>
#include <cassert>
#include <tag/stdpp.h>
#include <cvd/cpu_hacks.h>
#include "forward_algorithm.h"
#undef make_tuple
using namespace std;
using namespace CVD;
using namespace tag;
using namespace std::tr1;
using namespace TooN;
template<class C> double sumarray(const C& in)
{
return accumulate(in.begin(), in.end(), 0., plus<double>());
}
template<int I, class Base> int select_random_element(const Vector<I, double, Base>& v)
{
double total=0, choice = drand48();
for(int i=0; i < v.size(); i++)
{
total += v[i];
if(choice <= total)
return i;
}
return v.size()-1;
}
///Observer which is the same as in hmm_test.cc
struct HmmTestObservations
{
//Pobability of emmiting symbols in a given state.
Vector<6> parameters, real_parameters;
Matrix<2, 3, double, Reference::RowMajor> B;
Vector<2> pi;
Matrix<2> A;
vector<int> O, Q;
HmmTestObservations()
:real_parameters(makeVector(.6,.2,.2,.2,.1,.7)),B(¶meters[0])
{
//B[0] = makeVector(.6,.2,.2); //Probabilities of symbols being emitted in state 0
//B[1] = makeVector(.2,.1,.7); //Probabilities of symbols being emitted in state 1
parameters=real_parameters;
srand48(0);
//Transition probabilities.
//Row = from col = to
A[0] = makeVector(.9, .1);
A[1] = makeVector(.2, .8);
//Initial state probabilities
//Note numerical derivatives for an intermadiate result
//will appear incorrect if any of these are exactly 0
pi = makeVector(.9, .1);
Q = run_hmm(A, pi, 100);
O = make_observations(B, Q);
}
double log(int state, int observation) const
{
assert(state == 0 || state == 1);
assert(observation >=0 && observation < 3);
return ::log(B[state][observation]);
}
template<int States> vector<int> run_hmm(Matrix<States> A, Vector<States> pi, int n)
{
int state = select_random_element(pi);
vector<int> states;
for(int i=0 ;i<n; i++)
{
states.push_back(state);
state = select_random_element(A[state]);
}
return states;
}
template<int States, int Outputs, class Base> vector<int> make_observations(const Matrix<States, Outputs, double, Base>& B, const vector<int>& Q)
{
vector<int> O;
for(unsigned int i=0; i < Q.size(); i++)
O.push_back(select_random_element(B[Q[i]]));
return O;
}
};
int main()
{
enableFPE();
cout << setprecision(10);
HmmTestObservations Obs;
double log;
log = forward_algorithm(Obs.A, Obs.pi, Obs, Obs.O);
cout << "From forward_algorithm: " << log << endl;
vector<array<double, 2> > delta, epsilon;
tie(delta, epsilon) = forward_backward_algorithm(Obs.A, Obs.pi, Obs, Obs.O);
for(unsigned int i=0; i < delta.size(); i++)
{
double sum=0;
for(unsigned int j=0; j < delta[j].size(); j++)
sum += exp(delta[i][j] + epsilon[i][j]);
cout << ln(sum) << endl;
}
array<double, 2> zero;
zero.assign(0);
vector<array<double, 2> > samples(delta.size(), zero);
RngDrand48 rng;
for(int i=0; i < 100000; i++)
{
vector<int> s = backward_sampling<2, int>(Obs.A, delta, rng, 1e10);
for(unsigned int j=0; j < s.size(); j++)
samples[j][s[j]]++;
}
cout << setprecision(5);
for(unsigned int i=0; i < delta.size(); i++)
{
double hi=0;
for(unsigned int j=0; j < delta[j].size(); j++)
hi = max(delta[i][j] + epsilon[i][j], hi);
double sum=0;
for(unsigned int j=0; j < delta[j].size(); j++)
sum += exp(delta[i][j] + epsilon[i][j] - hi);
double lnp = ln(sum) + hi;
for(unsigned int j=0; j < delta[j].size(); j++)
cout << setw(10) << exp(delta[i][j] + epsilon[i][j] - lnp) << " ";
cout << " ";
double s = sumarray(samples[i]);
for(unsigned int j=0; j < delta[j].size(); j++)
cout << setw(10) << samples[i][j]/s << " ";
cout << " " << lnp << endl;
}
}