caio.co/de/go-tdigest

Ensure returned values stay within bounds

Id
2481326432989d6424f4c4b18ce0060cc3801b2a
Author
Christine Yen
Commit time
2017-10-10T18:55:43-07:00

Modified tdigest.go

@@ -83,7 +83,9
})

if found {
- return result
+ min := t.summary.keys[0]
+ max := t.summary.keys[len(t.summary.keys)-1]
+ return math.Min(max, math.Max(result, min))
}
return t.summary.Max().mean
}

Modified tdigest_test.go

@@ -200,6 +200,25
}
}

+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)