forked from jvkersch/hsmmlearn
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFBImpl_Luis
321 lines (299 loc) · 9.5 KB
/
FBImpl_Luis
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
#include "FBImpl.h"
#include "dCalc.h"
#include "Mathe.h"
#include "InitData.h"
#include "error.h"
#include "matrix.h"
#include "cube.h"
#include "consts.h"
#include <stdio.h>
#include <math.h>
#include <time.h>
#include <stdlib.h>
#include <float.h>
#include <string.h>
#include <limits.h>
#include <iostream>
#include <fstream>
using namespace std;
double** StateIn = NULL; // transition probabilities
double** F = NULL; // forward quantities
double** L = NULL;
double** G = NULL;
double*** H = NULL;
double** L1 = NULL;
double* N = NULL;
double** Norm = NULL;
double** d = NULL;
double** D = NULL; // duration distribution
double* mean_d = NULL; // mena durations
double** p = NULL;
double* pi = NULL; // initial state distribution
double** eta = NULL;
double** xi = NULL;
double** alpha = NULL;
int** maxI = NULL;
int** maxU = NULL;
double** pdf = NULL; // emission duration
int* hiddenStates = NULL;
int J, tau, M;
int Censoring, Output;
bool LeftCensoring, RightCensoring;
void FBImpl(int CensoringPara, int tauPara, int JPara, int MPara,
double dPara[], double pPara[], double piPara[], double pdfPara[],
double FPara[], double LPara[], double GPara[], double L1Para[],
double NPara[], double NormPara[],
double etaPara[], double xiPara[], int *err)
{
int i, j, k, t, u, v;
double Observ, r, s, w;
ofstream output_file;
output_file.open("debug.txt");
output_file << "Running FB" << endl;
try {
InitParaAndVar(CensoringPara, tauPara, JPara, MPara, dPara, pPara, piPara, pdfPara);
CalcStoreD();
// forward recursion
for (t = 0; t <= tau - 1; t++)
{
N[t] = 0;
for (j = 0; j <= J-1; j++)
{
F[j][t] = 0; //
Observ = pdf[j][t]; // get emission likelihoods
if (t < tau - 1)
{
for (u = 1; u <= min(t+1, M); u++) // TODO Fix upper limit !?
{
if (u < t+1)
{
F[j][t] += Observ * d[j][u] * StateIn[j][t-u+1];
N[t] += Observ * D[j][u] * StateIn[j][t-u+1];
Observ *= pdf[j][t-u] / N[t-u];
}
else // (u = t+1)
{
F[j][t] += Observ * d[j][t+1] * pi[j];
N[t] += Observ * D[j][t+1] * pi[j];
}
}
}
else // (t = tau - 1)
{
for (u = 1; u <= min(tau, M); u++) // TODO Fix upper limit !?
{
if (u < tau)
{
F[j][tau-1] += Observ * D[j][u] * StateIn[j][tau-u];
Observ *= pdf[j][tau-1-u] / N[tau-1-u];
}
else // (u = tau)
{
F[j][tau-1] += Observ * D[j][tau] * pi[j];
}
}
N[tau-1] += F[j][tau];
}
if (F[j][t] <= 0)
{
output_file << "F[j][t] <= 0, namely " << F[j][t] << "for j = " << j << ", t = " << t << endl;
throw var_nonpositive_exception();
}
}
for (j = 0; j <= J-1; j++)
{
if (N[t] <= 0)
{
output_file << "N[t] <= 0, namely " << N[t] << "for t = " << t << endl;
throw var_nonpositive_exception();
}
F[j][t] /= N[t];
}
if (t < tau - 1)
{
for (j = 0; j <= J-1; j++)
{
StateIn[j][t+1] = 0;
for (i = 0; i <= J-1; i++)
{
StateIn[j][t+1] += p[i][j] * F[i][t];
}
}
}
}
output_file << "Now running backward recursion" << endl;
// Backward recursion
for (j = 0; j <= J - 1; j++)
{
L[j][tau - 1] = F[j][tau - 1];
}
for (t = tau - 2; t >= 0; t--)
{
for (j = 0; j <= J - 1; j++)
{
G[j][t + 1] = 0;
Observ = 1;
for (u = 1; u <= min(tau - 1 - t, M); u++)
{
Observ *= pdf[j][t + u] / N[t + u];
if (u < tau - 1 - t)
{
H[j][t + 1][u] = L1[j][t + u] * Observ * d[j][u] / F[j][t + u];
}
else
{
H[j][t + 1][u] = Observ * D[j][tau - 1 - t];
}
G[j][t + 1] += H[j][t + 1][u];
}
}
for (j = 0; j <= J - 1; j++)
{
L1[j][t] = 0;
for (k = 0; k <= J - 1; k++)
{
L1[j][t] += G[k][t + 1] * p[j][k];
}
L1[j][t] *= F[j][t];
L[j][t] = L1[j][t] + L[j][t + 1] - G[j][t + 1] * StateIn[j][t + 1];
}
}
// Calculation of eta and xi
output_file << "Calculating xi and eta" << endl;
if (LeftCensoring)
{
// Calculation eta
output_file << "Left censoring" << endl;
output_file << "Calculating eta" << endl;
for (j = 0; j <= J - 1; j++)
{
for (u = 1; u <= M; u++)
{
r = 1;
w = 0;
for (t = 1; t <= min(u, tau - 1); t++)
{
r = 1;
for (v = 1; v <= t; v++)
r *= pdf[j][t - v] / N[t - v];
w += L1[j][t - 1] / F[j][t - 1] * r;
}
eta[j][u] = w * d[j][u] / mean_d[j] * pi[j];
if (u >= tau)
{
r = 1;
for (v = 1; v <= tau; v++)
r *= pdf[j][tau - v] / N[tau - v];
eta[j][u] += r * (u + 1 - tau) * d[j][u] / mean_d[j] * pi[j];
}
}
}
// Calculation xi
output_file << "Calculating xi" << endl;
for (j = 0; j <= J - 1; j++)
{
for (u = 1; u <= M; u++)
{
w = 0;
for (t = 0; t <= tau - 2; t++)
{
r = 0;
for (i = 0; i <= J - 1; i++)
if (i != j) r += p[i][j] * F[i][t];
if (u <= tau - 2 - t )
r *= H[j][t + 1][u];
else
{
r *= d[j][u];
for (v = 0; v <= tau - 2 - t; v++)
r *= pdf[j][tau - 1 - v] / N[tau - 1 - v];
}
w += r;
}
xi[j][u] = w;
}
}
}
else
{
// Calculation eta
output_file << "No left censoring" << endl;
output_file << "Calculating eta" << endl;
for (j = 0; j <= J - 1; j++)
{
for (u = 1; u <= M; u++)
{
w = 0;
s = 1;
for (t = tau - 2 - ind(!(RightCensoring)) * u; t >= 0; t--)
{
r = 0;
for (i = 0; i <= J - 1; i++)
if (i != j) r += p[i][j] * F[i][t];
if (u <= tau - 2 - t )
{
r *= H[j][t + 1][u];
}
else
{
s *= pdf[j][t + 1] / N[t + 1];
r *= s * d[j][u];
}
w += r;
}
if ((RightCensoring) || (u <= tau - 1))
{
r = d[j][u] * pi[j];
if (u <= tau - 1)
{
for (v = 1; v <= u; v++)
r *= pdf[j][u - v] / N[u - v];
r *= L1[j][u - 1] / F[j][u - 1];
}
else
for (v = 1; v <= tau; v++)
r *= pdf[j][tau - v] / N[tau - v];
w += r;
}
eta[j][u] = w;
}
}
}
output_file << "Saving parameters" << endl;
// Save parameters
for (j = 0; j <= J - 1; j++) {
for (t = 0; t < tau; t++) {
FPara[j * tau + t] = F[j][t];
LPara[j * tau + t] = L[j][t];
NormPara[j * tau + t] = Norm[j][t];
}
for (t = 1; t < tau; t++)
GPara[j * tau + t] = G[j][t];
for (t = 0; t < tau - 1; t++)
L1Para[j * tau + t] = L1[j][t];
for (t = 0; t < M; t++)
etaPara[j * M + t] = eta[j][t + 1];
if (LeftCensoring)
for (t = 0; t < M; t++)
xiPara[j * M + t] = xi[j][t + 1];
}
for (t = 0; t < tau; t++) {
NPara[t] = N[t];
}
}
catch (var_nonpositive_exception e)
{
*err = 1;
}
catch (memory_exception e)
{
*err = 2;
}
catch (file_exception e)
{
*err = 3;
}
output_file << "Terminated with error code: " << *err << endl;
output_file.close();
freeMemory();
}