forked from edrosten/threeB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_forward_backward_deriv.cc
170 lines (131 loc) · 3.45 KB
/
test_forward_backward_deriv.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
#include <tr1/array>
#include <tr1/tuple>
#include <TooN/TooN.h>
#include <TooN/functions/derivatives.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 <cvd/random.h>
#include "forward_algorithm.h"
#include "utility.h"
#undef make_tuple
using namespace std;
using namespace CVD;
using namespace tag;
using namespace std::tr1;
using namespace TooN;
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;
}
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> vector<double> generate_gaussian_observations(const vector<int>& states, const Vector<States*2>& params)
{
vector<double> obs;
for(unsigned int i=0; i < states.size(); i++)
obs.push_back(rand_g() * params[states[i]*2+1] + params[states[i]*2]);
return obs;
}
template<int States> double log_prob(double obs, int state, const Vector<States*2>& params)
{
double mu = params[state*2];
double sigma = params[state*2+1];
return -sq(obs - mu) / (2*sq(sigma)) - .5*log(2*M_PI) - log(sigma);
}
template<int States> Vector<States*2> prob_diff(double obs, int state, const Vector<States*2>& params)
{
double mu = params[state*2];
double sigma = params[state*2+1];
Vector<States*2> ret = Zeros;
//d foo / d mu
ret[state*2+0] = (obs - mu) / sq(sigma);
ret[state*2+1] = sq(obs - mu) / (sigma*sigma*sigma) - 1/sigma;
return ret;
}
template<int States> struct BjOt
{
static const int NumParameters = States*2;
Vector<States*2> p;
BjOt(const Vector<States*2>& v)
:p(v)
{}
double log(int state, double obs) const
{
return log_prob<States>(obs, state, p);
}
Vector<States*2> diff_log(int state, double obs) const
{
return prob_diff<States>(obs, state, p);
}
Matrix<States*2> hess_log(int, double) const
{
return Zeros;
}
};
struct EvalHmm{
vector<double> obs;
Matrix<2> A;
Vector<2> pi;
double operator()(const Vector<4>& p) const
{
return forward_algorithm(A, pi, BjOt<2>(p), obs);
}
};
int main()
{
enableFPE();
cout << setprecision(10);
Matrix<2> A = Data(.5, .5, .5, .5);
Vector<2> pi = makeVector(.5,.5);
Vector<4> params = makeVector(0, 10, 0, 1);
vector<int> states = run_hmm(A, pi, 10);
vector<double> O = generate_gaussian_observations<2>(states, params);
BjOt<2> B(params);
cout << forward_algorithm(A, pi, B, O) << endl;
cout << "Exact with forward algo = " << forward_algorithm_deriv(A, pi, B, O).second << endl;
EvalHmm e;
e.A = A;
e.pi = pi;
e.obs = O;
cout << "Approx with finite diff = " << numerical_gradient(e, params) << endl;
Vector<4> sum_diff = Zeros;
vector<array<double, 2> > delta, epsilon;
tie(delta, epsilon) = forward_backward_algorithm(A, pi, B, O);
int N = 100000;
for(int i=0; i < N; i++)
{
vector<int> s = backward_sampling<2>(A, delta);
Vector<4> E = Zeros;
for(unsigned int j=0; j < s.size(); j++)
{
E += prob_diff<2>(O[j], s[j], params);
}
//cout << E << endl;
sum_diff+=E;
}
cout << "Approx with ffbs sampling " << sum_diff / N << endl;
}