-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.c
517 lines (424 loc) · 13.5 KB
/
utils.c
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#define MAX(a,b) (((a) > (b)) ? (a) : (b))
#define MIN(a,b) (((a) < (b)) ? (a) : (b))
//#define ABS(a) (((a) >= 0) ? (a) : (-(a)))
#define TRUE 1
#define FALSE 0
#define E_UNDERFLOW 1
#define SUCCESS 0
typedef double ldouble;
//typedef long double ldouble;
#define INF HUGE_VAL
//typedef long double ldouble;
//#define INF HUGE_VALL
typedef int BOOL;
#define CACHE_NOT_FOUND INF
#define LEFT_BOUNDARY 0
#define RIGHT_BOUNDARY 1
#define FORWARD_DIRECTION 0
#define BACKWARD_DIRECTION 1
inline int ABS(int x) {
return x >= 0 ? x : -x;
}
inline ldouble FABS(ldouble x) {
return x >= 0 ? x : -x;
}
inline int sign(int x){
return x / ABS(x);
}
ldouble nb_model(int signal, ldouble * covariates, ldouble delta, ldouble * betas, int n_covariates) {
ldouble nb_mean = 0;
for (int i = 0; i < n_covariates; i ++ ) {
nb_mean += covariates[i] * betas[i];
}
nb_mean = exp(nb_mean);
return (lgamma(signal + delta) - lgamma(signal + 1) - lgamma(delta) +
delta * (log(delta / (nb_mean + delta))) +
signal * (log(nb_mean / (nb_mean + delta)))
);
}
ldouble poisson_pmf(int k, ldouble Lambda, BOOL return_log) {
if (return_log) {
return k * log(Lambda) - lgamma(k + 1.0) - Lambda;
} else {
return exp(k * log(Lambda) - lgamma(k + 1.0) - Lambda);
}
}
ldouble normal_pdf(ldouble x, ldouble mu, ldouble stddev, BOOL return_log) {
ldouble u = (x - mu) / stddev;
if (return_log) {
return -u * u / 2 - log(sqrt(2 * M_PI) * stddev);
} else {
return exp(-u * u / 2) / (sqrt(2 * M_PI) * stddev);
}
}
ldouble LogNormal_pdf(ldouble x, ldouble mu, ldouble stddev, BOOL return_log) {
ldouble u = (log(x) - mu) / stddev;
if (return_log) {
return -u * u / 2 - log(sqrt(2 * M_PI) * stddev * x);
} else {
return exp(-u * u / 2) / (sqrt(2 * M_PI) * stddev * x);
}
}
ldouble LogNormal_pdf_corrected(ldouble x, ldouble mu, ldouble stddev, BOOL return_log) {
return (erf((log(x + 1) - mu) / (sqrt(2) * stddev)) - erf((log(x) - mu) / (sqrt(2) * stddev))) / 2;
}
ldouble laplace_pdf(ldouble x, ldouble mu, ldouble b, BOOL return_log) {
// ldouble var = stddev * stddev;
ldouble u = FABS(x - mu) / b;
if (return_log) {
return -u - log(2 * b);
} else {
return exp(-u) / (2 * b);
}
}
ldouble studentsT_pdf(ldouble x, ldouble df, ldouble stddev, BOOL return_log) {
ldouble u = x / stddev;
if (return_log) {
return lgamma((df + 1) / 2) - log(1 + u * u / df) * (df + 1) / 2 - log(df * M_PI) / 2 - log(stddev) - lgamma(df / 2);
} else {
return tgamma((df + 1) / 2) * pow(1 + u * u / df, -(df + 1) / 2) / (sqrt(df * M_PI) * stddev * tgamma(df / 2));
}
}
int normalize_posteriors(ldouble *log_posteriors, int n_clusters) {
// first, find the maximum posterior
ldouble max_log_posterior = log_posteriors[0];
int i;
for (i = 1; i < n_clusters; i++) {
if (log_posteriors[i] > max_log_posterior) {
max_log_posterior = log_posteriors[i];
}
}
if (max_log_posterior == -INF) {
return E_UNDERFLOW;
}
// calculate the ratios between all other posteriors and the maximum posterior
ldouble total_posterior_ratios = 0;
for (i = 0; i < n_clusters; i++) {
total_posterior_ratios += exp(log_posteriors[i] - max_log_posterior);
}
// in the case of underflow, return
if (total_posterior_ratios == 0.0 || isinf(1. / total_posterior_ratios)) {
return E_UNDERFLOW;
}
// calculate the maximum posterior
ldouble max_posterior = 1. / total_posterior_ratios;
// rescale cluster posteriors to sum to one
for (i = 0; i < n_clusters; i++) {
log_posteriors[i] = max_posterior * exp(log_posteriors[i] - max_log_posterior);
}
return SUCCESS;
}
int convert_and_normalize_log_matrix(ldouble ** log_matrix, int n_rows, int n_columns) {
int i, j;
ldouble max_log_value = -INF;
// find the maximum log value
for (i = 0; i < n_rows; i ++ ) {
for (j = 0; j < n_columns; j ++ ) {
if (log_matrix[i][j] > max_log_value) {
max_log_value = log_matrix[i][j];
}
}
}
// compute the sum of x / max_value for all x in the matrix
ldouble total_ratio = 0;
for (i = 0; i < n_rows; i ++ ) {
for (j = 0; j < n_columns; j ++ ) {
if (log_matrix[i][j] > -INF) {
total_ratio += exp(log_matrix[i][j] - max_log_value);
}
}
}
if (total_ratio == 0 || isinf(1. / total_ratio)) {
return E_UNDERFLOW;
}
// convert all matrix values to probabilities
ldouble max_probability = 1. / total_ratio;
for (i = 0; i < n_rows; i ++ ) {
for (j = 0; j < n_columns; j ++ ) {
if (log_matrix[i][j] > -INF) {
log_matrix[i][j] = max_probability * exp(log_matrix[i][j] - max_log_value);
} else {
log_matrix[i][j] = 0;
}
}
}
return SUCCESS;
}
int normalize_log_cube(ldouble ***log_cube, int Z, int n_rows, int n_columns) {
int i, j, k;
ldouble max_log_value = -INF;
// find the maximum log value
for (k = 0; k < Z; k ++ ) {
for (i = 0; i < n_rows; i ++ ) {
for (j = 0; j < n_columns; j ++ ) {
if (log_cube[k][i][j] > max_log_value) {
max_log_value = log_cube[k][i][j];
}
}
}
}
// compute the sum of x / max_value for all x in the matrix
ldouble total_ratio = 0;
for (k = 0; k < Z; k ++ ) {
for (i = 0; i < n_rows; i ++ ) {
for (j = 0; j < n_columns; j ++ ) {
if (log_cube[k][i][j] > -INF) {
total_ratio += exp(log_cube[k][i][j] - max_log_value);
}
}
}
}
if (total_ratio == 0 || isinf(1. / total_ratio)) {
return E_UNDERFLOW;
}
// convert all matrix values to probabilities
ldouble max_probability = 1. / total_ratio;
for (k = 0; k < Z; k ++ ) {
for (i = 0; i < n_rows; i ++ ) {
for (j = 0; j < n_columns; j ++ ) {
if (log_cube[k][i][j] > -INF) {
log_cube[k][i][j] = max_probability * exp(log_cube[k][i][j] - max_log_value);
} else {
log_cube[k][i][j] = 0;
}
}
}
}
return SUCCESS;
}
#define N_NOISE_STDDEVS 1000
int min_with_noise(int * array, ldouble * noise_means, ldouble * noise_stddevs, int start, int end) {
int m = array[start] + noise_means[start] - N_NOISE_STDDEVS * noise_stddevs[start];
for (int i = start; i < end; i ++) {
int value = array[i] + noise_means[i] - N_NOISE_STDDEVS * noise_stddevs[i];
if (value < m) {
m = value;
}
}
return m;
}
int max_with_noise(int * array, ldouble * noise_means, ldouble * noise_stddevs, int start, int end) {
int m = array[start] + noise_means[start] + N_NOISE_STDDEVS * noise_stddevs[start];
for (int i = start; i < end; i ++) {
int value = array[i] + noise_means[i] + N_NOISE_STDDEVS * noise_stddevs[i];
if (value > m) {
m = value;
}
}
return m;
}
ldouble ** matrix(int n_rows, int n_columns) {
ldouble **M = (ldouble **) calloc(n_rows, sizeof (ldouble *));
if (M == NULL) { fprintf(stderr, "Cannot allocate memory for matrix M!"); exit(1); }
for (int i = 0; i < n_rows; i ++) {
M[i] = (ldouble *) calloc(n_columns, sizeof(ldouble));
if (M[i] == NULL) { fprintf(stderr, "Cannot allocate memory for matrix M!"); exit(1); }
}
return M;
}
ldouble * new_array(int n) {
ldouble *a = (ldouble *) calloc(n, sizeof (ldouble));
if (a == NULL) { fprintf(stderr, "Cannot allocate memory for array a!"); exit(1); }
return a;
}
int ** integer_matrix(int n_rows, int n_columns) {
int **M = (int **) calloc(n_rows, sizeof (int *));
if (M == NULL) { fprintf(stderr, "Cannot allocate memory for matrix M!"); exit(1); }
for (int i = 0; i < n_rows; i ++) {
M[i] = (int *) calloc(n_columns, sizeof(int));
if (M[i] == NULL) { fprintf(stderr, "Cannot allocate memory for matrix M!"); exit(1); }
}
return M;
}
void free_matrix(ldouble **M, int n) {
for (int i = 0; i < n; i ++) {
free(M[i]);
}
free(M);
}
void free_integer_matrix(int **M, int n) {
for (int i = 0; i < n; i ++) {
free(M[i]);
}
free(M);
}
void set_array(ldouble *array, int n, ldouble value) {
for (int i = 0; i < n; i ++) {
array[i] = value;
}
}
void set_matrix(ldouble **M, int n_rows, int n_cols, ldouble value) {
for (int i = 0; i < n_rows; i ++) {
for (int j = 0; j < n_cols; j ++) {
M[i][j] = value;
}
}
}
void set_integer_matrix(int **M, int n_rows, int n_cols, int value) {
for (int i = 0; i < n_rows; i ++) {
for (int j = 0; j < n_cols; j ++) {
M[i][j] = value;
}
}
}
void print_array(ldouble *array, int n){
printf("[ ");
for (int i = 0; i < n; i ++) {
printf("%.16le", array[i]);
if (i < n - 1) {
printf(", ");
}
}
printf(" ]\n");
}
void print_matrix(ldouble **M, int n_rows, int n_cols) {
printf("[");
for (int i = 0; i < n_rows; i ++) {
printf("[");
for (int j = 0; j < n_cols; j ++) {
printf("%.16le", M[i][j]);
if (j < n_cols - 1) { printf(", "); }
}
printf("]");
if (i < n_rows - 1) { printf(",\n"); }
}
printf("]\n");
}
void print_matrix_int(int **M, int n_rows, int n_cols) {
for (int i = 0; i < n_rows; i ++) {
for (int j = 0; j < n_cols; j ++) {
printf("%d\t", M[i][j]);
}
printf("\n");
}
}
ldouble *** cube(int Z, int n_rows, int n_columns) {
ldouble ***C = (ldouble ***) calloc(Z, sizeof (ldouble **));
if (C == NULL) { fprintf(stderr, "Cannot allocate memory for cube C!"); exit(1); }
for (int z = 0; z < Z; z ++) {
C[z] = matrix(n_rows, n_columns);
}
return C;
}
int *** integer_cube(int Z, int n_rows, int n_columns) {
int ***C = (int ***) calloc(Z, sizeof (int **));
if (C == NULL) { fprintf(stderr, "Cannot allocate memory for cube C!"); exit(1); }
for (int z = 0; z < Z; z ++) {
C[z] = integer_matrix(n_rows, n_columns);
}
return C;
}
void free_cube(ldouble ***C, int Z, int n) {
for (int z = 0; z < Z; z ++){
for (int i = 0; i < n; i ++) {
free(C[z][i]);
}
free(C[z]);
}
free(C);
}
void free_integer_cube(int ***C, int Z, int n) {
for (int z = 0; z < Z; z ++){
for (int i = 0; i < n; i ++) {
free(C[z][i]);
}
free(C[z]);
}
free(C);
}
void set_cube(ldouble ***C, int Z, int n_rows, int n_cols, ldouble value) {
for (int z = 0; z < Z; z ++) {
for (int i = 0; i < n_rows; i ++) {
for (int j = 0; j < n_cols; j ++) {
C[z][i][j] = value;
}
}
}
}
void set_integer_cube(int ***C, int Z, int n_rows, int n_cols, int value) {
for (int z = 0; z < Z; z ++) {
for (int i = 0; i < n_rows; i ++) {
for (int j = 0; j < n_cols; j ++) {
C[z][i][j] = value;
}
}
}
}
void print_cube(ldouble ***C, int Z, int n_rows, int n_cols){
for (int z = 0; z < Z; z ++) {
printf("z = %d\n", z);
printf("[");
for (int i = 0; i < n_rows; i ++) {
printf("[");
for (int j = 0; j < n_cols; j ++) {
printf("%.16le", C[z][i][j]);
if (j < n_cols - 1) {printf(", ");}
}
printf("]");
if (i < n_rows - 1) {printf(",\n");}
}
printf("]\n\n");
}
}
void print_cube_int(int ***C, int Z, int n_rows, int n_cols){
for (int z = 0; z < Z; z ++) {
printf("z = %d\n", z);
for (int i = 0; i < n_rows; i ++) {
for (int j = 0; j < n_cols; j ++) {
printf("%d\t", C[z][i][j]);
}
printf("\n");
}
}
}
ldouble * ldouble_array(int length) {
ldouble * array = (ldouble *) calloc (length, sizeof (ldouble));
if (array == NULL) { fprintf(stderr, "Cannot allocate memory for ldouble array with length %d", length); exit(1); }
for (int i = 0; i < length; i++) {
array[i] = 0;
}
return array;
}
void matcopy(ldouble **from_matrix, ldouble **to_matrix, int n_rows, int n_columns) {
for (int i = 0; i < n_rows; i ++) {
for (int j = 0; j < n_columns; j ++) {
to_matrix[i][j] = from_matrix[i][j];
}
}
}
void array_copy(ldouble *from, ldouble *to, int n) {
for (int i = 0; i < n; i ++) {
to[i] = from[i];
}
}
ldouble add_log_probs(ldouble log_X, ldouble log_Y) {
if (log_X == -INF) {
return log_Y;
} else if (log_Y == -INF) {
return log_X;
}
// swap them if log_Y is the bigger number
if (log_X < log_Y) {
ldouble _tmp = log_X;
log_X = log_Y;
log_Y = _tmp;
}
ldouble to_add = log(1 + exp(log_Y - log_X));
if (to_add == -INF || to_add == INF) {
return log_X;
} else {
return log_X + to_add;
}
}
//
//int main() {
// for (long int x = 0; x < 2091259980; x++) {
// //printf("%d %le\n", x, poisson_pmf(x, 5));
// ldouble z = poisson_pmf(x % 1000, 50.);
// }
// return 0;
//}