-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlockCorr.c
More file actions
551 lines (497 loc) · 18.4 KB
/
BlockCorr.c
File metadata and controls
551 lines (497 loc) · 18.4 KB
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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
#include <math.h>
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>
#include "list.h"
#include "BlockCorr.h"
// compute pearson correlation coefficient between time series at positions i1 and i2 in d (of length l)
// NOTE: result may be nan, if the variance of any of the time series is zero, or if
// any of the time series contains nans
double pearson2(const double *d, const long int i, const long int j, const long int l) {
long int k;
double sum_i = 0.0, sum_j = 0.0, sum_ii = 0.0, sum_jj = 0.0, sum_ij = 0.0;
#pragma omp simd
for (k = 0; k < l; k++) {
sum_i += d[i*l+k];
sum_j += d[j*l+k];
sum_ii += d[i*l+k]*d[i*l+k];
sum_jj += d[j*l+k]*d[j*l+k];
sum_ij += d[i*l+k]*d[j*l+k];
}
return (l*sum_ij-sum_i*sum_j)/sqrt((l*sum_ii-sum_i*sum_i)*(l*sum_jj-sum_j*sum_j));
}
// compute n-by-n correlation matrix for complete data set d with n rows and l columns
double *pearson(const double *d, long int n, long int l) {
double *sums = (double *) calloc(n, sizeof (double));
double *sumsqs = (double *) calloc(n, sizeof (double));
double *sqsums = (double *) calloc(n, sizeof (double));
double *coef = (double *) calloc(n*n, sizeof (double));
if (!coef || !sums || !sumsqs || !sqsums) return NULL;
#pragma omp parallel for
for (long int i = 0; i < n; i++) {
#pragma omp simd
for (long k = 0; k < l; k++) {
sums[i] += d[i*l+k];
sumsqs[i] += d[i*l+k]*d[i*l+k];
}
}
#pragma omp simd
for (long int i = 0; i < n; i++) {
sqsums[i] = sums[i]*sums[i];
}
#pragma omp parallel for
for (long int ij = 0; ij < n*(n-1)/2; ij++) {
double sum_ij = 0.0;
long int i = ij / n;
long int j = ij % n;
if (j <= i) {
i = n - i - 2;
j = n - j - 1;
}
#pragma omp simd
for (long int k = 0; k < l; k++) {
sum_ij += d[i*l+k]*d[j*l+k];
}
coef[i*n+j] = (l*sum_ij-sums[i]*sums[j])/sqrt((l*sumsqs[i]-sqsums[i])*(l*sumsqs[j]-sqsums[j]));
coef[j*n+i] = coef[i*n+j];
}
#pragma omp simd
for (long int i = 0; i < n; i++) {
coef[i*n+i] = 1.0;
}
free(sums);
free(sumsqs);
free(sqsums);
return coef;
}
// compute upper triangular part of the correlation matrix
// and store as a vector of length n*(n+1)/2
double *
pearson_triu(const double *d, long int n, long int l) {
long int i, j, k;
double *sums = (double *) calloc(n, sizeof (double));
double *sumsqs = (double *) calloc(n, sizeof (double));
double *coef = (double *) calloc(n*(n+1)/2, sizeof (double));
double sum_ij;
if (!coef) return NULL;
#pragma omp parallel for
for (i = 0; i < n; i++) {
#pragma omp simd
for (k = 0; k < l; k++) {
sums[i] += d[i*l+k];
sumsqs[i] += d[i*l+k]*d[i*l+k];
}
}
#pragma omp parallel for collapse(2) private (sum_ij) schedule(dynamic)
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
if (i > j) continue;
sum_ij = 0.0;
#pragma omp simd
for (k = 0; k < l; k++) {
sum_ij += d[i*l+k]*d[j*l+k];
}
coef[i*n-i*(i+1)/2+j] = (l*sum_ij-sums[i]*sums[j])/sqrt((l*sumsqs[i]-sums[i]*sums[i])*(l*sumsqs[j]-sums[j]*sums[j]));
}
}
free(sums);
free(sumsqs);
return coef;
}
// find equivalence classes in a time series data set
//
// d: data set with n rows (time series) and l columns (time steps)
// alpha: transitivity threshold
// kappa: minimum cluster size
// max_nan: maximum number of nans within a pivot time series
long int *cluster(const double *d, long int n, long int l, double alpha, long int kappa, long int max_nan)
{
long int corr_count, pivot, i, nan_count;
long int *membs;
double rho;
llist_li timeseries_l;
llist_li *clustermemb_pos_l;
llist_li *clustermemb_neg_l;
llist_li *noise_l;
llist_ptr cluster_l;
llist_item_li *iter_li, *iter_li_next;
llist_item_ptr *iter_ptr;
membs = (long int *) calloc(n, sizeof (long int));
if (!membs) {
return NULL;
}
// initialize time series index list
llist_li_init(×eries_l);
for (i = 0; i < n; i++) {
llist_li_push_back(×eries_l, i);
}
// initialize cluster list
llist_ptr_init(&cluster_l);
// initialize noise cluster and add to cluster list (always at position 1)
noise_l = (llist_li *) malloc(sizeof(llist_li));
if (!noise_l) return NULL;
llist_li_init(noise_l);
llist_ptr_push_back(&cluster_l, noise_l);
// iterate over all time series until none is left
corr_count = 0;
while (llist_li_size(×eries_l) > 0) {
printf("\r% 9ld left...", llist_li_size(×eries_l));
pivot = llist_li_front(×eries_l);
// check if pivot contains too many nans to be considered a pivot
nan_count = 0;
for (i = 0; i < l; i++)
if (isnan(d[pivot*l+i]))
nan_count++;
if (nan_count > max_nan) {
// add pivot to noise cluster
llist_li_relink(timeseries_l.last, ×eries_l, noise_l);
continue;
}
// initialize positive and negative clusters
clustermemb_pos_l = (llist_li *) malloc(sizeof(llist_li));
if (!clustermemb_pos_l) return NULL;
llist_li_init(clustermemb_pos_l);
clustermemb_neg_l = (llist_li *) malloc(sizeof(llist_li));
if (!clustermemb_neg_l) return NULL;
llist_li_init(clustermemb_neg_l);
// compute all correlations between pivot and remaining time series
// and create positive and negative protoclusters
iter_li = timeseries_l.first;
while (iter_li != NULL) {
iter_li_next = iter_li->next; // store successor before relinking
rho = pearson2(d, pivot, iter_li->data, l);
corr_count++;
if (isnan(rho)) {
// NOTE: we add the tested time series to the noise cluster, this might not be
// a good idea if nan value occurs because there are no overlapping valid time steps
// in pivot and tested time series
llist_li_relink(iter_li, ×eries_l, noise_l);
} else {
if (rho >= alpha) llist_li_relink(iter_li, ×eries_l, clustermemb_pos_l);
if (rho <= -alpha) llist_li_relink(iter_li, ×eries_l, clustermemb_neg_l);
}
iter_li = iter_li_next;
}
// check whether protoclusters fulfill the minimium size constraints
if (llist_li_size(clustermemb_pos_l) >= kappa) {
// add to final clustering
llist_ptr_push_back(&cluster_l, clustermemb_pos_l);
} else {
// relink all time series to noise cluster
llist_li_relink_all(clustermemb_pos_l, noise_l);
free(clustermemb_pos_l);
}
if (llist_li_size(clustermemb_neg_l) >= kappa) {
// add to final clustering
llist_ptr_push_back(&cluster_l, clustermemb_neg_l);
} else {
// relink all time series to noise cluster
llist_li_relink_all(clustermemb_neg_l, noise_l);
free(clustermemb_neg_l);
}
}
printf("\rclustering finished with %ld correlation computations.\n", corr_count);
// prepare output array with cluster assignments
// skip noise cluster (membs id=0 during initialization)
i = 1;
iter_ptr = cluster_l.first->next;
while (iter_ptr != NULL) {
iter_li = ((llist_li *) iter_ptr->data)->first;
while (iter_li != NULL) {
membs[iter_li->data] = i;
iter_li = iter_li->next;
}
llist_li_destroy((llist_li *) iter_ptr->data);
free(iter_ptr->data);
iter_ptr = iter_ptr->next;
i++;
}
llist_ptr_destroy(&cluster_l);
llist_li_destroy(×eries_l);
return membs;
}
// assumes that the key really is somewhere in the array
long int binary_search_li(long int key, long int *arr, long int len) {
long int liml, limr, rpos;
liml = 0;
limr = len-1;
do {
rpos = (liml+limr)/2;
if (arr[rpos] < key) {
liml = rpos + 1;
} else if (arr[rpos] > key) {
limr = rpos - 1;
} else {
break; // found it
}
} while (liml <= limr);
return rpos;
}
// COREQ
// find equivalence classes in a time series data set and estimate correlations
// NOTE: no kappa, no noise cluster estimation, no negative clusters, no NaN handling
//
// INPUT
// d: data set with n rows (time series) and l columns (time steps)
// n, l: see above
// alpha: transitivity threshold
// est_strat: estimation strategy (pivot-based, average-based)
//
// OUTPUT
// membs: uninitialized pointer to array for class memberships
// pivots: uninitialized pointer to array for pivot indices
// cluster_corrs: uninitialized pointer to array for class correlations (upper triangular indexing)
// n_clus: total number of resulting clusters
// corr_comps: total number of correlation computations required for clustering/estimation
long int *
coreq(const double *d, long int n, long int l, double alpha, coreq_estimation_strategy_t est_strat,
long int **membs, long int **pivots, double **cluster_corrs,
long int *n_clus, long int *corr_comps)
{
long int pivot, remaining, i, j, k, rpos, sample_size;
double rho, sum_ij;
llist_li timeseries_l; // holds all unprocessed time series
llist_li pivot_l; // holds all pivot objects selected so far
llist_li *clustermemb_l; // holds all time series assigned to a cluster
llist_ptr cluster_l; // holds all clusters
llist_ptr correlations_idx_l; // holds corrs between all pivots and time series (indices)
llist_ptr correlations_val_l; // holds corrs between all pivots and time series (correlations)
llist_li correlations_cnt_l; // holds the number of entries in the previous lists
llist_item_li *iter_li, *iter_li_next;
llist_item_ptr *iter_ptr, *iter_idx, *iter_val;
long int **cluster_arr; // hold all clusters with their members
long int *cluster_size_arr; // hold all cluster sizes
// precompute some data statistics for fast correlation computation
double *sums = (double *) calloc(n, sizeof (double));
double *sumsqs = (double *) calloc(n, sizeof (double));
if ((!sums) || (!sumsqs)) return NULL;
#pragma omp parallel for
for (i = 0; i < n; i++) {
#pragma omp simd
for (k = 0; k < l; k++) {
sums[i] += d[i*l+k];
sumsqs[i] += d[i*l+k]*d[i*l+k];
}
}
*membs = (long int *) calloc(n, sizeof(long int));
if (!*membs) return NULL;
// initialize time series index list
llist_li_init(×eries_l);
for (i = 0; i < n; i++) {
llist_li_push_back(×eries_l, i);
}
// initialize lists
llist_ptr_init(&cluster_l);
llist_ptr_init(&correlations_idx_l);
llist_ptr_init(&correlations_val_l);
llist_li_init(&correlations_cnt_l);
llist_li_init(&pivot_l);
// iterate over all time series until none is left
*corr_comps = 0;
while (llist_li_size(×eries_l) > 0) {
remaining = llist_li_size(×eries_l);
printf("\r% 9ld left...", remaining);
// select pivot time series and create its correlation container
pivot = llist_li_front(×eries_l);
llist_li_push_back(&pivot_l, pivot);
llist_ptr_push_back(&correlations_idx_l, (long int *) calloc(remaining, sizeof (long int)));
llist_ptr_push_back(&correlations_val_l, (double *) calloc(remaining, sizeof (double)));
llist_li_push_back(&correlations_cnt_l, remaining);
// initialize cluster container
clustermemb_l = (llist_li *) malloc(sizeof (llist_li));
if (!clustermemb_l) return NULL;
llist_li_init(clustermemb_l);
// compute all correlations between pivot and remaining time series
iter_li = timeseries_l.first;
i = 0;
while (iter_li != NULL) {
iter_li_next = iter_li->next; // store successor before relinking
// compute correlation
sum_ij = 0.0;
#pragma omp simd
for (k = 0; k < l; k++) {
sum_ij += d[pivot*l+k]*d[(iter_li->data)*l+k];
}
rho = (l*sum_ij-sums[pivot]*sums[iter_li->data])/sqrt((l*sumsqs[pivot]-sums[pivot]*sums[pivot])
*(l*sumsqs[iter_li->data]-sums[iter_li->data]*sums[iter_li->data]));
(*corr_comps)++;
((long int *) llist_ptr_back(&correlations_idx_l))[i] = iter_li->data;
((double *) llist_ptr_back(&correlations_val_l))[i] = rho;
// add time series to cluster
if (rho >= alpha) {
llist_li_relink(iter_li, ×eries_l, clustermemb_l);
}
iter_li = iter_li_next;
i++;
}
// add to final clustering
llist_ptr_push_back(&cluster_l, clustermemb_l);
}
*n_clus = llist_li_size(&pivot_l);
printf("\rclustering finished with %ld correlation computations --- %ld clusters detected\n", *corr_comps, *n_clus);
// prepare output array with cluster assignments
// and buffer all clusters in cluster_arr for O(1) access to members
cluster_arr = (long int **) calloc(*n_clus, sizeof (long int *));
cluster_size_arr = (long int *) calloc(*n_clus, sizeof (long int));
i = 0;
iter_ptr = cluster_l.first;
while (iter_ptr != NULL) {
cluster_arr[i] = (long int *) calloc(llist_li_size((llist_li *) iter_ptr->data), sizeof (long int));
cluster_size_arr[i] = llist_li_size((llist_li *) iter_ptr->data);
j = 0;
iter_li = ((llist_li *) iter_ptr->data)->first;
while (iter_li != NULL) {
cluster_arr[i][j] = iter_li->data;
(*membs)[iter_li->data] = i;
iter_li = iter_li->next;
j++;
}
llist_li_destroy((llist_li *) iter_ptr->data);
free(iter_ptr->data);
iter_ptr = iter_ptr->next;
i++;
}
// prepare output array with pivots
*pivots = (long int *) calloc(*n_clus, sizeof (long int));
i = 0;
iter_li = pivot_l.first;
while (iter_li != NULL) {
(*pivots)[i] = iter_li->data;
iter_li = iter_li->next;
i++;
}
// prepare output array with correlation estimates in O(K*(K+1)/2 * log2(N) * log2(N))
// NOTE: we use binary search to look up the precomputed pivot-time series correlations;
// no additional correlation computations are necessary, which would require O(K*(K+1)/2 * T * log2(N))
*cluster_corrs = (double *) calloc((*n_clus)*(*n_clus+1)/2, sizeof (double));
iter_idx = correlations_idx_l.first;
iter_val = correlations_val_l.first;
iter_li = correlations_cnt_l.first;
for (i = 0; i < (*n_clus); i++) { // loop over all pairs of pivots
for (j = i; j < (*n_clus); j++) {
switch (est_strat) {
case COREQ_PIVOT:
// search for pivot j in the correlation index list of pivot i
rpos = binary_search_li((*pivots)[j], ((long int *) iter_idx->data), iter_li->data);
// retrieve pivot i-j correlation from position correlation value list at position rpos
(*cluster_corrs)[i*(*n_clus)-i*(i+1)/2+j] = ((double *) iter_val->data)[rpos];
break;
case COREQ_PIVOT_GUARANTEE:
// same as above, but with scaling alpha-dependent scaling factor
rpos = binary_search_li((*pivots)[j], ((long int *) iter_idx->data), iter_li->data);
(*cluster_corrs)[i*(*n_clus)-i*(i+1)/2+j] = 0.5*(1.0+alpha*alpha) * ((double *) iter_val->data)[rpos];
break;
case COREQ_AVERAGE:
default:
// sample log2(N_k) precomputed correlations and use average as estimate
sample_size = fmax(1,ceil(log2(cluster_size_arr[j])));
(*cluster_corrs)[i*(*n_clus)-i*(i+1)/2+j] = 0;
for (k = 0; k < sample_size; k++) {
// sample random member of cluster j
long int sample = rand() % cluster_size_arr[j];
rpos = binary_search_li(cluster_arr[j][sample], ((long int *) iter_idx->data), iter_li->data);
(*cluster_corrs)[i*(*n_clus)-i*(i+1)/2+j] += ((double *) iter_val->data)[rpos];
}
(*cluster_corrs)[i*(*n_clus)-i*(i+1)/2+j] /= sample_size;
}
}
iter_idx = iter_idx->next;
iter_val = iter_val->next;
iter_li = iter_li->next;
}
// destroy correlation containers
iter_ptr = correlations_idx_l.first;
while (iter_ptr != NULL) {
free(iter_ptr->data);
iter_ptr = iter_ptr->next;
}
iter_ptr = correlations_val_l.first;
while (iter_ptr != NULL) {
free(iter_ptr->data);
iter_ptr = iter_ptr->next;
}
// destroy cluster buffer array
for (i = 0; i < *n_clus; i++) {
free(cluster_arr[i]);
}
free(cluster_arr);
free(cluster_size_arr);
llist_ptr_destroy(&cluster_l);
llist_ptr_destroy(&correlations_idx_l);
llist_ptr_destroy(&correlations_val_l);
llist_li_destroy(&correlations_cnt_l);
llist_li_destroy(×eries_l);
llist_li_destroy(&pivot_l);
free(sums);
free(sumsqs);
return *membs;
}
// compute aggregated losses for evaluation:
// absolute deviation, squared deviation, maximum deviation
//
// d: data set with n rows and l columns (may be NULL if corr_triu specified)
// corr_triu: precomputed true correlations in triu array (may be NULL if d specified)
// corr_clus_triu: precomputed cluster correlations in triu array
// membs: cluster membership vector of length n
// n: number of time series (rows in d)
// l: number of time steps (columns in d)
// k: number of clusters
int
compute_loss(const double *d, const double *corr_triu, const double *corr_clus_triu, const long int *membs,
long int n, long int l, long int k,
double *loss_abs, double *loss_sq, double *loss_max, long int *elements) {
long int i, j, ii, jj;
double corr_est, corr_tru;
double loss_abs0 = 0., loss_sq0 = 0., loss_max0 = 0.;
long int elements0 = 0;
int abort = 0;
if ((d == NULL) && (corr_triu == NULL)) {
return -1;
}
#pragma omp parallel for private(i, j, corr_tru, corr_est, ii, jj) \
reduction(+:loss_abs0,loss_sq0,elements0) \
reduction(max:loss_max0) \
schedule(dynamic)
for (i = 0; i < n; i++) {
if ((membs[i] < 0) || (membs[i] >= k)) {
// Invalid cluster index (must have range 0, ..., k-1). Noise cluster 0 missing?
abort = 1;
#pragma omp flush (abort)
}
for (j = i; j < n; j++) {
// for error handling
#pragma omp flush (abort)
if (abort)
continue;
if ((membs[j] < 0) || (membs[j] >= k)) {
// Invalid cluster index (must have range 0, ..., k-1). Noise cluster 0 missing?
abort = 1;
#pragma omp flush (abort)
}
if (corr_triu != NULL) { // use precomputed correlation
corr_tru = corr_triu[i*n-(i*(i+1))/2+j]; // triu indexing (with diagonal)
} else {
corr_tru = pearson2(d, i, j, l);
}
ii = fminl(membs[i], membs[j]); // triu index formula below for ii<jj
jj = fmaxl(membs[i], membs[j]);
corr_est = corr_clus_triu[ii*k-ii*(ii+1)/2+jj]; // triu indexing (with diagonal)
if (!(isnan(corr_tru) || isnan(corr_est))) {
elements0 += 1;
loss_abs0 += fabs(corr_tru-corr_est);
loss_sq0 += (corr_tru-corr_est)*(corr_tru-corr_est);
if (loss_max0 < fabs(corr_tru-corr_est)) {
loss_max0 = fabs(corr_tru-corr_est);
}
}
}
}
if (abort) {
return -2;
}
*loss_abs = loss_abs0;
*loss_sq = loss_sq0;
*loss_max = loss_max0;
*elements = elements0;
return 0;
}