-
Notifications
You must be signed in to change notification settings - Fork 0
/
tdigest.go
281 lines (196 loc) · 5.6 KB
/
tdigest.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
package gotdigest
import (
"math"
"slices"
"github.com/vmihailenco/msgpack/v5"
)
type Centroid struct {
Size int
Mean float64
}
type TDigest struct {
Centroids []Centroid
delta float64 `msgpack:"-"`
shouldBuffer bool `msgpack:"-"`
bufferMaxSize int `msgpack:"-"`
buffer []float64 `msgpack:"-"`
useNormalized bool `msgpack:"-"`
}
func (d *TDigest) ToMsgPack() ([]byte, error) {
b, err := msgpack.Marshal(d)
if err != nil {
return nil, err
}
return b, nil
}
func FromMsgPack(b []byte) (TDigest, error) {
var digest TDigest
err := msgpack.Unmarshal(b, &digest)
if err != nil {
return digest, err
}
return digest, nil
}
func last(c *[]Centroid) *Centroid {
return &((*c)[len(*c)-1])
}
type unNormalizedPotential func(float64, float64) float64
type normalizedPotential func(float64, float64, int) float64
func k0Potential(quantile float64, delta float64) float64 {
scalar := delta / (2.0)
inverse := quantile
return scalar * inverse
}
func k1Potential(quantile float64, delta float64) float64 {
scalar := delta / (2.0 * math.Pi)
inverse := math.Asin(2.0*quantile - 1.0)
return scalar * inverse
}
func k2Potential(quantile float64, delta float64, n int) float64 {
if quantile == 0 {
quantile = 0.000000001
}
if quantile == 1 {
quantile = .999999999
}
denom := (4.0*math.Log(float64(n)/delta) + 24.0)
lhs := delta / denom
rhs := math.Log(quantile / (1.0 - quantile))
return lhs * rhs
}
func NewCentroidWithValue(val float64) Centroid {
return Centroid{1, val}
}
func NewDigestFromValues(vals ...float64) TDigest {
centroids := make([]Centroid, len(vals))
digest := TDigest{Centroids: centroids}
for val := range vals {
digest.Centroids = append(digest.Centroids, NewCentroidWithValue(float64(val)))
}
return digest
}
func (c *Centroid) weight() float64 {
return c.Mean * float64(c.Size)
}
func (c *Centroid) update(other Centroid) {
weight := c.weight() + other.weight()
c.Size += other.Size
c.Mean = weight / float64(c.Size)
}
func mergeCentroids(first Centroid, second Centroid) Centroid {
size := (first.Size + second.Size)
weight := first.weight() + second.weight()
return Centroid{size, weight / float64(size)}
}
func NewDigest(delta float64, shouldBuffer bool, bufferMax int, useNormalized bool) TDigest {
bins := make([]Centroid, 0)
buffer := make([]float64, 0, bufferMax)
return TDigest{bins, delta, shouldBuffer, bufferMax, buffer, useNormalized}
}
func NewDigestFromBin(bin Centroid) TDigest {
bins := make([]Centroid, 1)
bins[0] = bin
return TDigest{Centroids: bins}
}
func (d *TDigest) append(value float64) {
if d.shouldBuffer {
d.buffer = append(d.buffer, value)
if len(d.buffer) == d.bufferMaxSize {
slices.Sort(d.buffer)
centroids := make([]Centroid, 0, len(d.buffer))
for _, data := range d.buffer {
centroids = append(centroids, Centroid{1, data})
}
d.buffer = make([]float64, 0, d.bufferMaxSize)
digest := TDigest{Centroids: centroids}
d.merge(&digest)
d.Centroids = compressBins(d.Centroids, d.delta)
}
} else {
b := NewCentroidWithValue(value)
digest := NewDigestFromBin(b)
d.merge(&digest)
d.Centroids = compressBins(d.Centroids, d.delta)
}
}
func (d *TDigest) count() int {
count := 0
for _, bin := range d.Centroids {
count += bin.Size
}
return count
}
func (d *TDigest) merge(other *TDigest) {
size := len(d.Centroids) + len(other.Centroids)
merged := make([]Centroid, 0, size)
dCount := 0
otherCount := 0
for dCount < len(d.Centroids) && otherCount < len(other.Centroids) {
if d.Centroids[dCount].Mean < other.Centroids[otherCount].Mean {
merged = append(merged, d.Centroids[dCount])
dCount++
} else {
merged = append(merged, other.Centroids[otherCount])
otherCount++
}
}
for dCount < len(d.Centroids) {
merged = append(merged, d.Centroids[dCount])
dCount++
}
for otherCount < len(other.Centroids) {
merged = append(merged, other.Centroids[otherCount])
otherCount++
}
d.Centroids = merged
}
func compressBins(centroids []Centroid, delta float64) []Centroid {
if len(centroids) == 0 {
return centroids
}
totalSize := 0
for _, centroid := range centroids {
totalSize += centroid.Size
}
compressedCentroids := make([]Centroid, 0)
compressedCentroids = append(compressedCentroids, centroids[0])
accumulatedSize := compressedCentroids[0].Size
minPotential := k1Potential(0, delta)
i := 1
for i < len(centroids) {
nextCentroid := centroids[i]
i++
quotientIndex := float64((accumulatedSize + nextCentroid.Size)) / float64(totalSize)
if quotientIndex > 1.0 {
panic("Cannot have quantiles greater than 1.0")
}
if k1Potential(quotientIndex, delta)-minPotential <= 1 {
last(&compressedCentroids).update(nextCentroid)
} else {
compressedCentroids = append(compressedCentroids, nextCentroid)
quantile := float64(accumulatedSize) / float64(totalSize)
minPotential = k1Potential(quantile, delta)
}
accumulatedSize += nextCentroid.Size
}
return compressedCentroids
}
func (d *TDigest) quantile(quantile float64) float64 {
quantileIndex := quantile * float64(d.count())
maxQuantileIndex := float64(d.Centroids[0].Size) / 2.0
if quantileIndex <= maxQuantileIndex {
return d.Centroids[0].Mean
}
for idx := 0; idx < len(d.Centroids)-1; idx++ {
c1 := d.Centroids[idx]
c2 := d.Centroids[idx+1]
interval := float64(c1.Size+c2.Size) / 2.0
if quantileIndex <= maxQuantileIndex+interval {
k := (quantileIndex - maxQuantileIndex) / interval
return c1.Mean*float64(1-k) + c2.Mean*float64(k)
}
maxQuantileIndex += interval
}
lastCentroid := last(&d.Centroids)
return lastCentroid.Mean
}