This repository has been archived by the owner on Jan 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
tdigest_test.go
454 lines (412 loc) · 11.5 KB
/
tdigest_test.go
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
package tdigest
import (
"fmt"
"math"
"math/rand"
"reflect"
"testing"
)
func TestFindNearest(t *testing.T) {
type testcase struct {
centroids []*centroid
val float64
want []int
}
testcases := []testcase{
{[]*centroid{{0, 1}, {1, 1}, {2, 1}}, -1, []int{0}},
{[]*centroid{{0, 1}, {1, 1}, {2, 1}}, 0, []int{0}},
{[]*centroid{{0, 1}, {1, 1}, {2, 1}}, 1, []int{1}},
{[]*centroid{{0, 1}, {1, 1}, {2, 1}}, 2, []int{2}},
{[]*centroid{{0, 1}, {1, 1}, {2, 1}}, 3, []int{2}},
{[]*centroid{{0, 1}, {2, 1}}, 1, []int{0, 1}},
{[]*centroid{}, 1, []int{}},
}
for i, tc := range testcases {
d := TDigest{centroids: tc.centroids}
have := d.nearest(tc.val)
if len(tc.want) == 0 {
if len(have) != 0 {
t.Errorf("TDigest.nearest wrong test=%d, have=%v, want=%v", i, have, tc.want)
}
} else {
if !reflect.DeepEqual(tc.want, have) {
t.Errorf("TDigest.nearest wrong test=%d, have=%v, want=%v", i, have, tc.want)
}
}
}
}
func BenchmarkFindNearest(b *testing.B) {
n := 500
d := simpleTDigest(n)
b.ResetTimer()
var val float64
for i := int64(0); i < int64(b.N); i++ {
val = float64(i % d.countTotal)
_ = d.nearest(val)
}
}
func TestFindAddTarget(t *testing.T) {
testcase := func(in []*centroid, val float64, want int) func(*testing.T) {
return func(t *testing.T) {
d := TDigest{centroids: in, compression: 1}
for _, c := range in {
d.countTotal += c.count
}
have := d.findAddTarget(val)
if have != want {
t.Errorf("TDigest.findAddTarget wrong have=%v, want=%v", have, want)
}
}
}
t.Run("empty digest", testcase(nil, 1, -1))
t.Run("exactly one with room", testcase(
[]*centroid{{0.0, 1}, {1.0, 1}, {2.0, 1}},
1, 1))
t.Run("exactly one without room", testcase(
[]*centroid{{0.0, 1}, {1.0, 3}, {2.0, 1}},
1, -1))
t.Run("multiple candidates", func(t *testing.T) {
t.Run("all lesser", func(t *testing.T) {
t.Run("with room", testcase(
[]*centroid{{0.0, 1}, {1.0, 1}, {1.0, 3}, {2.0, 1}},
1.1, 2))
t.Run("without room", testcase(
[]*centroid{{0.0, 1}, {1.0, 1}, {1.0, 4}, {2.0, 1}},
1.1, -1))
})
t.Run("all greater", func(t *testing.T) {
t.Run("with room", testcase(
[]*centroid{{0.0, 1}, {1.0, 1}, {1.0, 3}, {2.0, 1}},
0.9, 1))
t.Run("without room", testcase(
[]*centroid{{0.0, 1}, {1.0, 3}, {1.0, 4}, {2.0, 1}},
0.9, -1))
})
t.Run("all equal", func(t *testing.T) {
t.Run("with room in none", testcase(
[]*centroid{{0.0, 1}, {1.0, 3}, {1.0, 3}, {2.0, 1}},
1.0, -1))
t.Run("with room in one", testcase(
[]*centroid{{0.0, 1}, {1.0, 2}, {1.0, 3}, {2.0, 1}},
1.0, 1))
t.Run("with room in multiple", func(t *testing.T) {
d := TDigest{
centroids: []*centroid{{0.0, 1}, {1.0, 1}, {1.0, 2}, {2.0, 1}},
compression: 1,
}
for _, c := range d.centroids {
d.countTotal += c.count
}
have := d.findAddTarget(1.0)
if have != 1 && have != 2 {
t.Errorf("TDigest.findAddTarget wrong have=%v, want=1 or 2", have)
}
})
})
t.Run("both greater and lesser", func(t *testing.T) {
t.Run("with room below", testcase(
[]*centroid{{0.0, 1}, {0.8, 1}, {0.8, 1}, {1.0, 6}, {1.0, 1}, {2.0, 1}},
0.9, 2))
t.Run("with room above", testcase(
[]*centroid{{0.0, 1}, {0.8, 1}, {0.8, 6}, {1.0, 1}, {1.0, 1}, {2.0, 1}},
0.9, 3))
t.Run("with no room", testcase(
[]*centroid{{0.0, 1}, {0.8, 1}, {0.8, 6}, {1.0, 6}, {1.0, 1}, {2.0, 1}},
0.9, -1))
t.Run("with room above and below", func(t *testing.T) {
d := TDigest{
centroids: []*centroid{
{0.0, 1}, {0.8, 1}, {0.8, 1},
{1.0, 1}, {1.0, 1}, {2.0, 1}},
compression: 1,
}
for _, c := range d.centroids {
d.countTotal += c.count
}
have := d.findAddTarget(0.9)
if have != 2 && have != 3 {
t.Errorf("TDigest.findAddTarget wrong have=%v, want=2 or 3", have)
}
})
})
})
}
// adding a new centroid should maintain sorted order
func TestAddNewCentroid(t *testing.T) {
type testcase struct {
centroidVals []float64
add float64
want []float64
}
testcases := []testcase{
{[]float64{}, 1, []float64{1}},
{[]float64{1}, 2, []float64{1, 2}},
{[]float64{1, 2}, 1.5, []float64{1, 1.5, 2}},
{[]float64{1, 1.5, 2}, -1, []float64{-1, 1, 1.5, 2}},
{[]float64{1, 1.5, 2}, 3, []float64{1, 1.5, 2, 3}},
{[]float64{1, 1.5, 2}, 1.6, []float64{1, 1.5, 1.6, 2}},
}
for i, tc := range testcases {
d := tdFromMeans(tc.centroidVals)
d.addNewCentroid(tc.add, 1)
have := make([]float64, len(d.centroids))
for i, c := range d.centroids {
have[i] = c.mean
}
if !reflect.DeepEqual(tc.want, have) {
t.Errorf("TDigest.addNewCentroid wrong test=%d, have=%v, want=%v", i, have, tc.want)
}
}
}
func verifyCentroidOrder(t *testing.T, cs *TDigest) {
if len(cs.centroids) < 2 {
return
}
last := cs.centroids[0]
for i, c := range cs.centroids[1:] {
if c.mean < last.mean {
t.Errorf("centroid %d lt %d: %v < %v", i+1, i, c.mean, last.mean)
}
last = c
}
}
func TestQuantileOrder(t *testing.T) {
// stumbled upon in real world application: adding a 1 to this
// resulted in the 6th centroid getting incremented instead of the
// 7th.
d := &TDigest{
countTotal: 14182,
compression: 100,
centroids: []*centroid{
¢roid{0.000000, 1},
¢roid{0.000000, 564},
¢roid{0.000000, 1140},
¢roid{0.000000, 1713},
¢roid{0.000000, 2380},
¢roid{0.000000, 2688},
¢roid{0.000000, 1262},
¢roid{2.005758, 1563},
¢roid{30.499251, 1336},
¢roid{381.533509, 761},
¢roid{529.600000, 5},
¢roid{1065.294118, 17},
¢roid{2266.444444, 36},
¢roid{4268.809783, 368},
¢roid{14964.148148, 27},
¢roid{41024.579618, 157},
¢roid{124311.192308, 52},
¢roid{219674.636364, 22},
¢roid{310172.775000, 40},
¢roid{412388.642857, 14},
¢roid{582867.000000, 16},
¢roid{701434.777778, 9},
¢roid{869363.800000, 5},
¢roid{968264.000000, 1},
¢roid{987100.666667, 3},
¢roid{1029895.000000, 1},
¢roid{1034640.000000, 1},
},
}
d.Add(1.0, 1)
verifyCentroidOrder(t, d)
}
func TestQuantile(t *testing.T) {
type testcase struct {
weights []int64
idx int
want float64
}
testcases := []testcase{
{[]int64{1, 1, 1, 1}, 0, 0.0},
{[]int64{1, 1, 1, 1}, 1, 0.25},
{[]int64{1, 1, 1, 1}, 2, 0.5},
{[]int64{1, 1, 1, 1}, 3, 0.75},
{[]int64{5, 1, 1, 1}, 0, 0.250},
{[]int64{5, 1, 1, 1}, 1, 0.625},
{[]int64{5, 1, 1, 1}, 2, 0.750},
{[]int64{5, 1, 1, 1}, 3, 0.875},
{[]int64{1, 1, 1, 5}, 0, 0.0},
{[]int64{1, 1, 1, 5}, 1, 0.125},
{[]int64{1, 1, 1, 5}, 2, 0.250},
{[]int64{1, 1, 1, 5}, 3, 0.625},
}
for i, tc := range testcases {
d := tdFromWeights(tc.weights)
have := d.quantileOf(tc.idx)
if have != tc.want {
t.Errorf("TDigest.quantile wrong test=%d, have=%.3f, want=%.3f", i, have, tc.want)
}
}
}
func TestAddValue(t *testing.T) {
type testcase struct {
value float64
weight int
want []*centroid
}
testcases := []testcase{
{1.0, 1, []*centroid{{1, 1}}},
{0.0, 1, []*centroid{{0, 1}, {1, 1}}},
{2.0, 1, []*centroid{{0, 1}, {1, 1}, {2, 1}}},
{3.0, 1, []*centroid{{0, 1}, {1, 1}, {2.5, 2}}},
{4.0, 1, []*centroid{{0, 1}, {1, 1}, {2.5, 2}, {4, 1}}},
{math.NaN(), 1, []*centroid{{0, 1}, {1, 1}, {2.5, 2}, {4, 1}}},
{math.Inf(-1), 1, []*centroid{{0, 1}, {1, 1}, {2.5, 2}, {4, 1}}},
{math.Inf(+1), 1, []*centroid{{0, 1}, {1, 1}, {2.5, 2}, {4, 1}}},
}
d := NewWithCompression(1)
for i, tc := range testcases {
d.Add(tc.value, tc.weight)
if !reflect.DeepEqual(d.centroids, tc.want) {
t.Fatalf("TDigest.addValue unexpected state step=%d, have=%v, want=%v", i, d.centroids, tc.want)
}
}
}
func TestQuantileValue(t *testing.T) {
d := NewWithCompression(1)
d.countTotal = 8
d.centroids = []*centroid{{0.5, 3}, {1, 1}, {2, 2}, {3, 1}, {8, 1}}
type testcase struct {
q float64
want float64
}
// correct values, determined by hand with pen and paper for this set of centroids
testcases := []testcase{
{0.0, 5.0 / 40.0},
{0.1, 13.0 / 40.0},
{0.2, 21.0 / 40.0},
{0.3, 29.0 / 40.0},
{0.4, 37.0 / 40.0},
{0.5, 20.0 / 15.0},
{0.6, 28.0 / 15.0},
{0.7, 36.0 / 15.0},
{0.8, 44.0 / 15.0},
{0.9, 13.0 / 2.0},
{1.0, 21.0 / 2.0},
}
var epsilon = 1e-8
for i, tc := range testcases {
have := d.Quantile(tc.q)
if math.Abs(have-tc.want) > epsilon {
t.Errorf("TDigest.Quantile wrong step=%d, have=%v, want=%v",
i, have, tc.want)
}
}
}
func BenchmarkFindAddTarget(b *testing.B) {
n := 500
d := simpleTDigest(n)
b.ResetTimer()
var val float64
for i := int64(0); i < int64(b.N); i++ {
val = float64(i % d.countTotal)
_ = d.findAddTarget(val)
}
}
// add the values [0,n) to a centroid set, equal weights
func simpleTDigest(n int) *TDigest {
d := NewWithCompression(1.0)
for i := 0; i < n; i++ {
d.Add(float64(i), 1)
}
return d
}
func tdFromMeans(means []float64) *TDigest {
centroids := make([]*centroid, len(means))
for i, m := range means {
centroids[i] = ¢roid{m, 1}
}
d := NewWithCompression(1.0)
d.centroids = centroids
d.countTotal = int64(len(centroids))
return d
}
func tdFromWeights(weights []int64) *TDigest {
centroids := make([]*centroid, len(weights))
countTotal := int64(0)
for i, w := range weights {
centroids[i] = ¢roid{float64(i), w}
countTotal += w
}
d := NewWithCompression(1.0)
d.centroids = centroids
d.countTotal = countTotal
return d
}
func ExampleTDigest() {
rand.Seed(5678)
values := make(chan float64)
// Generate 100k uniform random data between 0 and 100
var (
n int = 100000
min, max float64 = 0, 100
)
go func() {
for i := 0; i < n; i++ {
values <- min + rand.Float64()*(max-min)
}
close(values)
}()
// Pass the values through a TDigest, compression parameter 100
td := New()
for val := range values {
// Add the value with weight 1
td.Add(val, 1)
}
// Print the 50th, 90th, 99th, 99.9th, and 99.99th percentiles
fmt.Printf("50th: %.5f\n", td.Quantile(0.5))
fmt.Printf("90th: %.5f\n", td.Quantile(0.9))
fmt.Printf("99th: %.5f\n", td.Quantile(0.99))
fmt.Printf("99.9th: %.5f\n", td.Quantile(0.999))
fmt.Printf("99.99th: %.5f\n", td.Quantile(0.9999))
}
func TestMerge(t *testing.T) {
values := make(chan float64)
// Generate 100k uniform random data between 0 and 100
var (
n int = 100000
min, max float64 = 0, 100
)
go func() {
for i := 0; i < n; i++ {
values <- min + rand.Float64()*(max-min)
}
close(values)
}()
// Pass the values through two TDigests
td1 := New()
td2 := New()
i := 0
for val := range values {
// Add the value with weight 1. Alternate between the digests.
if i%2 == 0 {
td1.Add(val, 1)
} else {
td2.Add(val, 1)
}
i += 1
}
rand.Seed(2)
// merge both into a third tdigest.
td := New()
td1.MergeInto(td)
td2.MergeInto(td)
t.Logf("10th: %.5f\n", td1.Quantile(0.1))
t.Logf("50th: %.5f\n", td1.Quantile(0.5))
t.Logf("90th: %.5f\n", td1.Quantile(0.9))
t.Logf("99th: %.5f\n", td1.Quantile(0.99))
t.Logf("99.9th: %.5f\n", td1.Quantile(0.999))
t.Logf("99.99th: %.5f\n", td1.Quantile(0.9999))
t.Logf("10th: %.5f\n", td2.Quantile(0.1))
t.Logf("50th: %.5f\n", td2.Quantile(0.5))
t.Logf("90th: %.5f\n", td2.Quantile(0.9))
t.Logf("99th: %.5f\n", td2.Quantile(0.99))
t.Logf("99.9th: %.5f\n", td2.Quantile(0.999))
t.Logf("99.99th: %.5f\n", td2.Quantile(0.9999))
t.Logf("10th: %.5f\n", td.Quantile(0.1))
t.Logf("50th: %.5f\n", td.Quantile(0.5))
t.Logf("90th: %.5f\n", td.Quantile(0.9))
t.Logf("99th: %.5f\n", td.Quantile(0.99))
t.Logf("99.9th: %.5f\n", td.Quantile(0.999))
t.Logf("99.99th: %.5f\n", td.Quantile(0.9999))
}