Add TestSingletonInACrowd 💬 by Caio 8 years ago (log)
Notice that this test has its exterme quantile (0.999) skipped because the java reference implementation behaves the same. Reasoning and more details on issue #12
Notice that this test has its exterme quantile (0.999) skipped because the java reference implementation behaves the same. Reasoning and more details on issue #12
|
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 |
package tdigest
import (
"math"
"math/rand"
"sort"
"testing"
)
// Test of tdigest internals and accuracy. Note no t.Parallel():
// during tests the default random seed is consistent, but varying
// concurrency scheduling mixes up the random values used in each test.
// Since there's a random number call inside tdigest this breaks repeatability
// for all tests. So, no test concurrency here.
func TestTInternals(t *testing.T) {
tdigest := New(100)
if !math.IsNaN(tdigest.Quantile(0.1)) {
t.Errorf("Quantile() on an empty digest should return NaN. Got: %.4f", tdigest.Quantile(0.1))
}
_ = tdigest.Add(0.4, 1)
if tdigest.Quantile(0.1) != 0.4 {
t.Errorf("Quantile() on a single-sample digest should return the samples's mean. Got %.4f", tdigest.Quantile(0.1))
}
_ = tdigest.Add(0.5, 1)
if tdigest.summary.Len() != 2 {
t.Errorf("Expected size 2, got %d", tdigest.summary.Len())
}
if tdigest.summary.Min().mean != 0.4 {
t.Errorf("Min() returned an unexpected centroid: %v", tdigest.summary.Min())
}
if tdigest.summary.Max().mean != 0.5 {
t.Errorf("Min() returned an unexpected centroid: %v", tdigest.summary.Min())
}
_ = tdigest.Add(0.4, 2)
_ = tdigest.Add(0.4, 3)
if tdigest.summary.Len() != 2 {
t.Errorf("Adding centroids of same mean shouldn't change size")
}
y := tdigest.summary.Find(0.4)
if y.count != 6 || y.mean != 0.4 {
t.Errorf("Adding centroids with same mean should increment the count only. Got %v", y)
}
err := tdigest.Add(0, 0)
if err == nil {
t.Errorf("Expected Add() to error out with input (0,0)")
}
if tdigest.Quantile(0.9999999) != tdigest.summary.Max().mean {
t.Errorf("High quantiles with little data should give out the MAX recorded mean")
}
if tdigest.Quantile(0.0000001) != tdigest.summary.Min().mean {
t.Errorf("Low quantiles with little data should give out the MIN recorded mean")
}
}
func assertDifferenceSmallerThan(tdigest *TDigest, p float64, m float64, t *testing.T) {
tp := tdigest.Quantile(p)
if math.Abs(tp-p) >= m {
t.Errorf("T-Digest.Quantile(%.4f) = %.4f. Diff (%.4f) >= %.4f", p, tp, math.Abs(tp-p), m)
}
}
func TestUniformDistribution(t *testing.T) {
tdigest := New(100)
for i := 0; i < 10000; i++ {
_ = tdigest.Add(rand.Float64(), 1)
}
assertDifferenceSmallerThan(tdigest, 0.5, 0.02, t)
assertDifferenceSmallerThan(tdigest, 0.1, 0.01, t)
assertDifferenceSmallerThan(tdigest, 0.9, 0.01, t)
assertDifferenceSmallerThan(tdigest, 0.01, 0.005, t)
assertDifferenceSmallerThan(tdigest, 0.99, 0.005, t)
assertDifferenceSmallerThan(tdigest, 0.001, 0.001, t)
assertDifferenceSmallerThan(tdigest, 0.999, 0.001, t)
}
// Asserts quantile p is no greater than absolute m off from "true"
// fractional quantile for supplied data. So m must be scaled
// appropriately for source data range.
func assertDifferenceFromQuantile(data []float64, tdigest *TDigest, p float64, m float64, t *testing.T) {
q := quantile(p, data)
tp := tdigest.Quantile(p)
if math.Abs(tp-q) >= m {
t.Fatalf("T-Digest.Quantile(%.4f) = %.4f vs actual %.4f. Diff (%.4f) >= %.4f", p, tp, q, math.Abs(tp-q), m)
}
}
func TestSequentialInsertion(t *testing.T) {
tdigest := New(10)
rand.Seed(0xDEADBEEF)
data := make([]float64, 10000)
for i := 0; i < len(data); i++ {
data[i] = float64(i)
}
for i := 0; i < len(data); i++ {
_ = tdigest.Add(data[i], 1)
assertDifferenceFromQuantile(data[:i+1], tdigest, 0.001, 1.0+0.001*float64(i), t)
assertDifferenceFromQuantile(data[:i+1], tdigest, 0.01, 1.0+0.005*float64(i), t)
assertDifferenceFromQuantile(data[:i+1], tdigest, 0.05, 1.0+0.01*float64(i), t)
assertDifferenceFromQuantile(data[:i+1], tdigest, 0.25, 1.0+0.03*float64(i), t)
assertDifferenceFromQuantile(data[:i+1], tdigest, 0.5, 1.0+0.03*float64(i), t)
assertDifferenceFromQuantile(data[:i+1], tdigest, 0.75, 1.0+0.03*float64(i), t)
assertDifferenceFromQuantile(data[:i+1], tdigest, 0.95, 1.0+0.01*float64(i), t)
assertDifferenceFromQuantile(data[:i+1], tdigest, 0.99, 1.0+0.005*float64(i), t)
assertDifferenceFromQuantile(data[:i+1], tdigest, 0.999, 1.0+0.001*float64(i), t)
}
}
func TestNonSequentialInsertion(t *testing.T) {
tdigest := New(10)
// Not quite a uniform distribution, but close.
data := make([]float64, 1000)
for i := 0; i < len(data); i++ {
tmp := (i * 1627) % len(data)
data[i] = float64(tmp)
}
sorted := make([]float64, 0, len(data))
for i := 0; i < len(data); i++ {
_ = tdigest.Add(data[i], 1)
sorted = append(sorted, data[i])
// Estimated quantiles are all over the place for low counts, which is
// OK given that something like P99 is not very meaningful when there are
// 25 samples. To account for this, increase the error tolerance for
// smaller counts.
if i == 0 {
continue
}
max := float64(len(data))
fac := 1.0 + max/float64(i)
sort.Float64s(sorted)
assertDifferenceFromQuantile(sorted, tdigest, 0.001, fac+0.001*max, t)
assertDifferenceFromQuantile(sorted, tdigest, 0.01, fac+0.005*max, t)
assertDifferenceFromQuantile(sorted, tdigest, 0.05, fac+0.01*max, t)
assertDifferenceFromQuantile(sorted, tdigest, 0.25, fac+0.01*max, t)
assertDifferenceFromQuantile(sorted, tdigest, 0.5, fac+0.02*max, t)
assertDifferenceFromQuantile(sorted, tdigest, 0.75, fac+0.01*max, t)
assertDifferenceFromQuantile(sorted, tdigest, 0.95, fac+0.01*max, t)
assertDifferenceFromQuantile(sorted, tdigest, 0.99, fac+0.005*max, t)
assertDifferenceFromQuantile(sorted, tdigest, 0.999, fac+0.001*max, t)
}
}
func TestSingletonInACrowd(t *testing.T) {
tdigest := New(100)
for i := 0; i < 10000; i++ {
tdigest.Add(10, 1)
}
tdigest.Add(20, 1)
tdigest.Compress()
for _, q := range []float64{0, 0.5, 0.8, 0.9, 0.99, 0.999} {
if q == 0.999 {
// Test for 0.999 disabled since it doesn't
// pass in the reference implementation
continue
}
result := tdigest.Quantile(q)
if !closeEnough(result, 10) {
t.Errorf("Expected Quantile(%.3f) = 10, but got %.4f (size=%d)", q, result, tdigest.Len())
}
}
result := tdigest.Quantile(1)
if result != 20 {
t.Errorf("Expected Quantile(1) = 20, but got %.4f (size=%d)", result, tdigest.Len())
}
}
func TestRespectBounds(t *testing.T) {
tdigest := New(10)
data := []float64{0, 279, 2, 281}
for _, f := range data {
tdigest.Add(f, 1)
}
quantiles := []float64{0.01, 0.25, 0.5, 0.75, 0.999}
for _, q := range quantiles {
if tdigest.Quantile(q) < 0 {
t.Errorf("should never return a result less than the min")
}
if tdigest.Quantile(q) > 281 {
t.Errorf("should never return a result larger than the max")
}
}
}
func TestWeights(t *testing.T) {
tdigest := New(10)
// Create data slice with repeats matching weights we gave to tdigest
data := []float64{}
for i := 0; i < 100; i++ {
_ = tdigest.Add(float64(i), uint32(i))
for j := 0; j < i; j++ {
data = append(data, float64(i))
}
}
assertDifferenceFromQuantile(data, tdigest, 0.001, 1.0+0.001*100.0, t)
assertDifferenceFromQuantile(data, tdigest, 0.01, 1.0+0.005*100.0, t)
assertDifferenceFromQuantile(data, tdigest, 0.05, 1.0+0.01*100.0, t)
assertDifferenceFromQuantile(data, tdigest, 0.25, 1.0+0.01*100.0, t)
assertDifferenceFromQuantile(data, tdigest, 0.5, 1.0+0.02*100.0, t)
assertDifferenceFromQuantile(data, tdigest, 0.75, 1.0+0.01*100.0, t)
assertDifferenceFromQuantile(data, tdigest, 0.95, 1.0+0.01*100.0, t)
assertDifferenceFromQuantile(data, tdigest, 0.99, 1.0+0.005*100.0, t)
assertDifferenceFromQuantile(data, tdigest, 0.999, 1.0+0.001*100.0, t)
}
func TestIntegers(t *testing.T) {
tdigest := New(100)
_ = tdigest.Add(1, 1)
_ = tdigest.Add(2, 1)
_ = tdigest.Add(3, 1)
if tdigest.Quantile(0.5) != 2 {
t.Errorf("Expected p(0.5) = 2, Got %.2f instead", tdigest.Quantile(0.5))
}
tdigest = New(100)
for _, i := range []float64{1, 2, 2, 2, 2, 2, 2, 2, 3} {
_ = tdigest.Add(i, 1)
}
if tdigest.Quantile(0.5) != 2 {
t.Errorf("Expected p(0.5) = 2, Got %.2f instead", tdigest.Quantile(0.5))
}
var tot uint32
tdigest.summary.Iterate(func(item centroid) bool {
tot += item.count
return true
})
if tot != 9 {
t.Errorf("Expected the centroid count to be 9, Got %d instead", tot)
}
}
func quantile(q float64, data []float64) float64 {
if len(data) == 0 {
return math.NaN()
}
if q == 1 || len(data) == 1 {
return data[len(data)-1]
}
index := q * (float64(len(data)) - 1)
return data[int(index)+1]*(index-float64(int(index))) + data[int(index)]*(float64(int(index)+1)-index)
}
func TestMerge(t *testing.T) {
if testing.Short() {
t.Skipf("Skipping merge test. Short flag is on")
}
const numItems = 10000
const numSubs = 5
data := make([]float64, numItems)
var subs [numSubs]*TDigest
dist1 := New(10)
for i := 0; i < numSubs; i++ {
subs[i] = New(10)
}
for i := 0; i < numItems; i++ {
num := rand.Float64()
data[i] = num
_ = dist1.Add(num, 1)
for j := 0; j < numSubs; j++ {
_ = subs[j].Add(num, 1)
}
}
dist2 := New(10)
for i := 0; i < numSubs; i++ {
_ = dist2.Merge(subs[i])
}
// Merge empty. Should be no-op
err := dist2.Merge(New(10))
if err != nil {
t.Errorf("Merge() with an empty digest should be a noop. Got %s", err)
}
sort.Float64s(data)
for _, p := range []float64{0.001, 0.01, 0.1, 0.2, 0.3, 0.5} {
q := quantile(p, data)
p1 := dist1.Quantile(p)
p2 := dist2.Quantile(p)
e1 := math.Abs(p1 - q)
e2 := math.Abs(p1 - q)
if e2/p >= 0.3 {
t.Errorf("Relative error for %f above threshold. q=%f p1=%f p2=%f e1=%f e2=%f", p, q, p1, p2, e1, e2)
}
if e2 >= 0.015 {
t.Errorf("Absolute error for %f above threshold. q=%f p1=%f p2=%f e1=%f e2=%f", p, q, p1, p2, e1, e2)
}
}
}
func TestCompressDoesntChangeCount(t *testing.T) {
tdigest := New(100)
for i := 0; i < 1000; i++ {
_ = tdigest.Add(rand.Float64(), 1)
}
initialCount := tdigest.count
err := tdigest.Compress()
if err != nil {
t.Errorf("Compress() triggered an unexpected error: %s", err)
}
if tdigest.count != initialCount {
t.Errorf("Compress() should not change count. Wanted %d, got %d", initialCount, tdigest.count)
}
}
func shouldPanic(f func(), t *testing.T, message string) {
defer func() {
tryRecover := recover()
if tryRecover == nil {
t.Errorf(message)
}
}()
f()
}
func TestPanic(t *testing.T) {
shouldPanic(func() {
New(0.5)
}, t, "Compression < 1 should panic!")
tdigest := New(100)
shouldPanic(func() {
tdigest.Quantile(-42)
}, t, "Quantile < 0 should panic!")
shouldPanic(func() {
tdigest.Quantile(42)
}, t, "Quantile > 1 should panic!")
shouldPanic(func() {
tdigest.findNearestCentroids(0.2)
}, t, "findNearestCentroids on empty summary should panic!")
}
func TestForEachCentroid(t *testing.T) {
tdigest := New(10)
for i := 0; i < 100; i++ {
_ = tdigest.Add(float64(i), 1)
}
// Iterate limited number.
means := []float64{}
tdigest.ForEachCentroid(func(mean float64, count uint32) bool {
means = append(means, mean)
return len(means) != 3
})
if len(means) != 3 {
t.Errorf("ForEachCentroid handled incorrect number of data items")
}
// Iterate all datapoints.
means = []float64{}
tdigest.ForEachCentroid(func(mean float64, count uint32) bool {
means = append(means, mean)
return true
})
if len(means) != tdigest.Len() {
t.Errorf("ForEachCentroid did not handle all data")
}
}
func benchmarkAdd(compression float64, b *testing.B) {
t := New(compression)
data := make([]float64, b.N)
for n := 0; n < b.N; n++ {
data[n] = rand.Float64()
}
b.ResetTimer()
for n := 0; n < b.N; n++ {
err := t.Add(data[n], 1)
if err != nil {
b.Error(err)
}
}
b.StopTimer()
}
func BenchmarkAdd1(b *testing.B) {
benchmarkAdd(1, b)
}
func BenchmarkAdd10(b *testing.B) {
benchmarkAdd(10, b)
}
func BenchmarkAdd100(b *testing.B) {
benchmarkAdd(100, b)
}
|