caio.co/de/go-tdigest

Rename TDigest.Percentile to Quantile

Quantile is the correct name given that the range is [0,1]
Id
1c3eafcf9c337d86d97d5fbb118ded0377b171e0
Author
Caio
Commit time
2015-09-02T10:38:58-03:00

Modified README.md

@@ -32,7 +32,7
t.Add(rand.Float64(), 1)
}

- fmt.Printf("p(.5) = %.6f\n", t.Percentile(0.5))
+ fmt.Printf("p(.5) = %.6f\n", t.Quantile(0.5))
}

## Disclaimer

Modified tdigest.go

@@ -32,9 +32,9

// Percentile returns the desired percentile estimation.
// Values of p must be between 0 and 1 (inclusive), will panic otherwise.
-func (t *TDigest) Percentile(p float64) float64 {
- if p < 0 || p > 1 {
- panic("Percentiles must be between 0 and 1 (inclusive)")
+func (t *TDigest) Quantile(q float64) float64 {
+ if q < 0 || q > 1 {
+ panic("q must be between 0 and 1 (inclusive)")
}

if t.summary.Len() == 0 {
@@ -43,7 +43,7
return t.summary.Min().mean
}

- p *= float64(t.count)
+ q *= float64(t.count)
var total float64
i := 0

@@ -53,7 +53,7
t.summary.Iterate(func(item centroid) bool {
k := float64(item.count)

- if p < total+k {
+ if q < total+k {
if i == 0 || i+1 == t.summary.Len() {
result = item.mean
found = true
@@ -61,7 +61,7
}
succ, pred := t.summary.successorAndPredecessorItems(item.mean)
delta := (succ.mean - pred.mean) / 2
- result = item.mean + ((p-total)/k-0.5)*delta
+ result = item.mean + ((q-total)/k-0.5)*delta
found = true
return false
}

Modified tdigest_test.go

@@ -14,14 +14,14

tdigest := New(100)

- if !math.IsNaN(tdigest.Percentile(0.1)) {
- t.Errorf("Percentile() on an empty digest should return NaN. Got: %.4f", tdigest.Percentile(0.1))
+ 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.Percentile(0.1) != 0.4 {
- t.Errorf("Percentile() on a single-sample digest should return the samples's mean. Got %.4f", tdigest.Percentile(0.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)
@@ -62,19 +62,19
t.Errorf("Expected Add() to error out with input (0,0)")
}

- if tdigest.Percentile(0.9999999) != tdigest.summary.Max().mean {
+ 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.Percentile(0.0000001) != tdigest.summary.Min().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.Percentile(p)
+ tp := tdigest.Quantile(p)
if math.Abs(tp-p) >= m {
- t.Errorf("T-Digest.Percentile(%.4f) = %.4f. Diff (%.4f) >= %.4f", p, tp, math.Abs(tp-p), m)
+ t.Errorf("T-Digest.Quantile(%.4f) = %.4f. Diff (%.4f) >= %.4f", p, tp, math.Abs(tp-p), m)
}
}

@@ -116,8 +116,8
tdigest.Add(2, 1)
tdigest.Add(3, 1)

- if tdigest.Percentile(0.5) != 2 {
- t.Errorf("Expected p(0.5) = 2, Got %.2f instead", tdigest.Percentile(0.5))
+ if tdigest.Quantile(0.5) != 2 {
+ t.Errorf("Expected p(0.5) = 2, Got %.2f instead", tdigest.Quantile(0.5))
}

tdigest = New(100)
@@ -126,8 +126,8
tdigest.Add(i, 1)
}

- if tdigest.Percentile(0.5) != 2 {
- t.Errorf("Expected p(0.5) = 2, Got %.2f instead", tdigest.Percentile(0.5))
+ if tdigest.Quantile(0.5) != 2 {
+ t.Errorf("Expected p(0.5) = 2, Got %.2f instead", tdigest.Quantile(0.5))
}

var tot uint32
@@ -196,8 +196,8

for _, p := range []float64{0.001, 0.01, 0.1, 0.2, 0.3, 0.5} {
q := quantile(p, data)
- p1 := dist1.Percentile(p)
- p2 := dist2.Percentile(p)
+ p1 := dist1.Quantile(p)
+ p2 := dist2.Quantile(p)

e1 := math.Abs(p1 - q)
e2 := math.Abs(p1 - q)