-
Notifications
You must be signed in to change notification settings - Fork 2
/
cf.c
386 lines (286 loc) · 8.96 KB
/
cf.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
#include "bolo.h"
#include <math.h>
struct cf *
cf_new(int type, size_t n)
{
struct cf *cf;
switch (type) {
case CF_MIN:
case CF_MAX:
case CF_SUM:
n = 1; /* running value */
break;
case CF_DELTA:
n = 2; /* first and last */
break;
case CF_MEAN:
case CF_STDEV:
case CF_VAR:
n = 4; /* mean, m2, d1, and d2 */
break;
}
cf = xalloc(1, sizeof(*cf) + n * sizeof(double));
cf->type = type;
cf->slots = n;
return cf;
}
void
cf_free(struct cf *cf)
{
free(cf);
}
void
cf_reset(struct cf *cf)
{
CHECK(cf != NULL, "cf_reset() given a NULL reservoir to reset");
cf->active = 1;
cf->carry = cf->rsv[1];
cf->i = cf->used = cf->n = 0;
memset(cf->rsv, 0, sizeof(double) * cf->slots);
}
void
cf_sample(struct cf *cf, double v)
{
uint32_t i;
CHECK(cf != NULL, "cf_sample() given a NULL cf context to sample into");
switch (cf->type) {
case CF_MIN: if (cf->n == 0 || v < cf->rsv[0]) cf->rsv[0] = v; break;
case CF_MAX: if (cf->n == 0 || v > cf->rsv[0]) cf->rsv[0] = v; break;
case CF_SUM: cf->rsv[0] += v; break;
case CF_DELTA:
if (cf->n == 0) cf->rsv[0] = (cf->active ? cf->carry : v);
cf->rsv[1] = v;
break;
case CF_MEDIAN:
if (cf->used < cf->slots) {
cf->rsv[cf->i++] = v;
cf->used++;
} else {
i = urandn(cf->n);
if (i < cf->slots) cf->rsv[i] = v;
}
break;
case CF_MEAN:
case CF_STDEV:
case CF_VAR:
/* for calculating mean, variance and standard deviation
losslessly, we use Welford's algorithm (see TAOCPv2 p232)
we cleverly (ab)use the cf->rsv array and cf->n to hold
all of our working variables. we definitely abuse the C
preprocessor to make this clearer.
*/
#define count (cf->n)
#define mean (cf->rsv[0])
#define m2 (cf->rsv[1])
#define delta1 (cf->rsv[2])
#define delta2 (cf->rsv[3])
/* minor tweak: we don't increment count (cf->n) here,
because we're going to increment it uniformly after
the switch block. */
delta1 = v - mean;
mean = mean + delta1 / (count + 1);
delta2 = v - mean;
m2 = m2 + delta1 * delta2;
#undef count
#undef mean
#undef m2
#undef delta1
#undef delta2
}
cf->n++;
}
static int
cmp_sorted(const void *_a, const void *_b)
{
double a, b;
a = *(double*)_a;
b = *(double*)_b;
return a == b ? 0
: a > b ? 1 : -1;
}
double
cf_value(struct cf *cf)
{
int mid;
switch (cf->type) {
case CF_MIN:
case CF_MAX:
return cf->n ? cf->rsv[0] : NAN;
case CF_SUM:
return cf->n ? cf->rsv[0] : 0.0;
case CF_DELTA:
return cf->n ? cf->rsv[1] - cf->rsv[0] : 0.0;
case CF_MEDIAN:
if (cf->n == 0) return NAN;
qsort(cf->rsv, cf->used, sizeof(double), cmp_sorted);
mid = cf->used / 2;
if (cf->used % 2 == 0)
return cf->rsv[mid - 1] / 2.0
+ cf->rsv[mid] / 2.0;
return cf->rsv[mid];
#define count (cf->n)
#define mean (cf->rsv[0])
#define m2 (cf->rsv[1])
case CF_MEAN: return count ? mean : NAN;
case CF_STDEV: return count > 1 ? sqrt(m2 / (count - 1)) : NAN;
case CF_VAR: return count > 1 ? m2 / (count - 1) : NAN;
#undef mean
#undef m2
}
return NAN;
}
#ifdef TEST
/* LCOV_EXCL_START */
TESTS {
subtest {
struct cf *cf;
cf = cf_new(CF_MIN, 5);
isnt_null(cf, "cf_new() should return a new reservoir");
ok(isnan(cf_value(cf)), "min cf(<empty>) is NaN");
cf_sample(cf, 2.0);
is_within(cf_value(cf), 2.0, 0.0001, "min cf(2) is 2");
cf_sample(cf, 1.0);
is_within(cf_value(cf), 1.0, 0.0001, "min cf(2,1) is 1");
cf_sample(cf, 3.0);
is_within(cf_value(cf), 1.0, 0.0001, "min cf(2,1,3) is 1");
cf_free(cf);
}
subtest {
struct cf *cf;
cf = cf_new(CF_MAX, 5);
isnt_null(cf, "cf_new() should return a new reservoir");
ok(isnan(cf_value(cf)), "max cf(<empty>) is NaN");
cf_sample(cf, 2.0);
is_within(cf_value(cf), 2.0, 0.0001, "max cf(2) is 2");
cf_sample(cf, 1.0);
is_within(cf_value(cf), 2.0, 0.0001, "max cf(2,1) is 2");
cf_sample(cf, 3.0);
is_within(cf_value(cf), 3.0, 0.0001, "max cf(2,1,3) is 3");
cf_free(cf);
}
subtest {
struct cf *cf;
cf = cf_new(CF_SUM, 5);
isnt_null(cf, "cf_new() should return a new reservoir");
is_within(cf_value(cf), 0.0, 0.0001, "sum cf(<empty>) is 0");
cf_sample(cf, 2.0);
is_within(cf_value(cf), 2.0, 0.0001, "sum cf(2) is 2");
cf_sample(cf, 1.0);
is_within(cf_value(cf), 3.0, 0.0001, "sum cf(2,1) is 3");
cf_sample(cf, 3.0);
is_within(cf_value(cf), 6.0, 0.0001, "sum cf(2,1,3) is 6");
cf_free(cf);
}
subtest {
struct cf *cf;
cf = cf_new(CF_DELTA, 5);
isnt_null(cf, "cf_new() should return a new reservoir");
is_within(cf_value(cf), 0.0, 0.0001, "delta cf(<empty>) is 0");
cf_sample(cf, 2.0);
is_within(cf_value(cf), 0.0, 0.0001, "delta cf(2) is 0");
cf_sample(cf, 1.0);
is_within(cf_value(cf), -1.0, 0.0001, "delta cf(2,1) is -1");
cf_sample(cf, 3.0);
is_within(cf_value(cf), 1.0, 0.0001, "sum cf(2,1,3) is 1");
cf_free(cf);
}
subtest {
struct cf *cf;
cf = cf_new(CF_MEAN, 5);
isnt_null(cf, "cf_new() should return a new reservoir");
ok(isnan(cf_value(cf)), "mean cf(<empty>) is NaN");
cf_sample(cf, 0.0);
is_within(cf_value(cf), 0.0, 0.0001, "mean cf(0) is 0");
cf_sample(cf, 1.0);
is_within(cf_value(cf), 0.5, 0.0001, "mean cf(0,1) is 0.5");
cf_sample(cf, 2.0);
is_within(cf_value(cf), 1.0, 0.0001, "mean cf(0,1,2) is 1.0");
cf_sample(cf, 3.0);
is_within(cf_value(cf), 1.5, 0.001, "mean cf(0,1,2,3) is 1.5");
cf_sample(cf, 15.0);
is_within(cf_value(cf), 4.2, 0.001, "cf_median(0,1,2,3,15) is 4.2");
cf_sample(cf, 5.0);
is_within(cf_value(cf), 4.3333, 0.001, "cf_median(0,1,2,3,15,5) is 4.3");
cf_free(cf);
}
subtest {
struct cf *cf;
cf = cf_new(CF_MEDIAN, 5);
isnt_null(cf, "cf_new() should return a new reservoir");
is_unsigned(cf->slots, 5, "cf_new(t,5) makes a reservoir with capacity 5");
is_unsigned(cf->used, 0, "cf_new() makes an empty reservoir");
/* make sure our calculations all return (quiet) NaN */
ok(isnan(cf_value(cf)), "cf_value(<empty>) is NaN");
cf_sample(cf, 0.0);
is_unsigned(cf->slots, 5, "cf_sample(...) doesn't affect reservoir capacity");
is_unsigned(cf->used, 1, "cf_sample(...) bumps used-count to 1");
cf_sample(cf, 1.0);
is_unsigned(cf->slots, 5, "cf_sample(...) doesn't affect reservoir capacity");
is_unsigned(cf->used, 2, "cf_sample(...) bumps used-count to 2");
cf_sample(cf, 2.0);
cf_sample(cf, 3.0);
is_within(cf_value(cf), 1.5, 0.001, "median(0,1,2,3) is 1.5 (almost full)");
cf_sample(cf, 4.0);
is_unsigned(cf->slots, 5, "cf_sample(...) doesn't affect reservoir capacity");
is_unsigned(cf->used, 5, "cf_sample(...) bumps used-count to 5");
is_within(cf_value(cf), 2.0, 0.001, "cf_median(0,1,2,3,4) is 2.0 (full)");
cf_sample(cf, 5.0);
is_unsigned(cf->slots, 5, "cf_sample(...) doesn't affect reservoir capacity");
is_unsigned(cf->used, 5, "cf_sample(...) doesn't exceed reservoir capacity");
cf_sample(cf, 6.0);
is_unsigned(cf->slots, 5, "cf_sample(...) doesn't affect reservoir capacity");
is_unsigned(cf->used, 5, "cf_sample(...) doesn't exceed reservoir capacity");
cf_sample(cf, 7.0);
cf_sample(cf, 8.0);
cf_sample(cf, 9.0);
ok(cf->rsv[0] + cf->rsv[1] + cf->rsv[2] + cf->rsv[3] + cf->rsv[4] > 10.0,
"cf_sample should replace some elements in [0..4]: "
"[%1.0lf, %1.0lf, %1.0lf, %1.0lf, %1.0lf]",
cf->rsv[0], cf->rsv[1], cf->rsv[2], cf->rsv[3], cf->rsv[4]);
cf_free(cf);
}
subtest {
struct cf *cf;
cf = cf_new(CF_STDEV, 5);
isnt_null(cf, "cf_new() should return a new reservoir");
ok(isnan(cf_value(cf)), "stdev cf(<empty>) is NaN");
cf_sample(cf, 10.0);
ok(isnan(cf_value(cf)), "stdev cf(10) is NaN");
cf_sample(cf, 2.0);
is_within(cf_value(cf), 5.6568, 0.0001, "stdev cf(10,2) is 5.65");
cf_sample(cf, 38.0);
is_within(cf_value(cf), 18.9033, 0.0001, "stdev cf(10,2,38) is 18.9");
cf_sample(cf, 23.0);
is_within(cf_value(cf), 15.75595, 0.0001, "stdev cf(10,2,38,23) is 15.76");
cf_sample(cf, 38.0);
is_within(cf_value(cf), 16.2542, 0.0001, "stdev cf(10,2,38,23,38) is 16.25");
cf_sample(cf, 23.0);
is_within(cf_value(cf), 14.54189, 0.0001, "stdev cf(10,2,38,23,38,23) is 14.54");
cf_sample(cf, 21.0);
is_within(cf_value(cf), 13.2844, 0.001, "stdev cf(10,2,38,23,38,23,21) is 13.28");
cf_free(cf);
}
subtest {
struct cf *cf;
cf = cf_new(CF_VAR, 5);
isnt_null(cf, "cf_new() should return a new reservoir");
ok(isnan(cf_value(cf)), "var cf(<empty>) is NaN");
cf_sample(cf, 10.0);
ok(isnan(cf_value(cf)), "var cf(10) is NaN");
cf_sample(cf, 2.0);
is_within(cf_value(cf), 32.0, 0.0001, "var cf(10,2) is 32");
cf_sample(cf, 38.0);
is_within(cf_value(cf), 357.3333, 0.0001, "var cf(10,2,38) is 357.3");
cf_sample(cf, 23.0);
is_within(cf_value(cf), 248.25, 0.0001, "var cf(10,2,38,23) is 248.25");
cf_sample(cf, 38.0);
is_within(cf_value(cf), 264.2, 0.0001, "var cf(10,2,38,23,38) is 264.2");
cf_sample(cf, 23.0);
is_within(cf_value(cf), 211.4666, 0.0001, "var cf(10,2,38,23,38,23) is 211.5");
cf_sample(cf, 21.0);
is_within(cf_value(cf), 176.47619, 0.001, "var cf(10,2,38,23,38,23,21) is 176.5");
cf_free(cf);
}
}
/* LCOV_EXCL_STOP */
#endif