-
Notifications
You must be signed in to change notification settings - Fork 0
/
psqr.go
209 lines (178 loc) · 3.51 KB
/
psqr.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
package psqr
import (
"sort"
)
type PSQuantile struct {
s []*Quantile
m map[float64]int
}
func NewPSQuantile(f ...float64) *PSQuantile {
q := &PSQuantile{
m: make(map[float64]int),
}
q.Init(f...)
return q
}
func (s *PSQuantile) Init(f ...float64) {
for _, v := range f {
idx := len(s.s)
s.s = append(s.s, NewQuantile(v))
s.m[v] = idx
}
}
func (s *PSQuantile) Append(val float64) {
for _, v := range s.s {
v.Append(val)
}
}
func (s *PSQuantile) Quantile(val float64) float64 {
return s.s[s.m[val]].Value()
}
// P-square maitains five markers that store points.
const nMarkers = 5
// Quantile represents an estimated p-quantile of a stream of observations.
type Quantile struct {
p float64
filled bool
cnt int
// marker positions, 1..nMarkers
pos [nMarkers]int
// desired marker positions
npos [nMarkers]float64
// increament in desired marker positions
dn [nMarkers]float64
// marker heights that store observations
heights [nMarkers]float64
}
// NewQuantile returns new p-quantile.
func NewQuantile(p float64) *Quantile {
if p < 0 || p > 1 {
panic("p-quantile is out of range")
}
q := &Quantile{
p: p,
}
q.Reset()
return q
}
// Reset resets the quantile.
func (q *Quantile) Reset() {
p := q.p
q.filled = false
q.cnt = 0
for i := 0; i < len(q.pos); i++ {
q.pos[i] = i
}
q.npos = [...]float64{
0,
2 * p,
4 * p,
2 + 2*p,
4,
}
q.dn = [...]float64{
0,
p / 2,
p,
(1 + p) / 2,
1,
}
}
// Append appends v to the stream of observations.
func (q *Quantile) Append(v float64) {
if q.cnt != nMarkers {
// no required number of observations has been appended yet
q.heights[q.cnt] = v
q.cnt++
return
}
if !q.filled {
q.filled = true
sort.Float64s(q.heights[:q.cnt])
}
q.append(v)
}
func (q *Quantile) append(v float64) {
l := q.cnt - 1
k := -1
if v < q.heights[0] {
k = 0
q.heights[0] = v
} else if q.heights[l] <= v {
k = l - 1
q.heights[l] = v
} else {
for i := 1; i <= l; i++ {
if q.heights[i-1] <= v && v < q.heights[i] {
k = i - 1
break
}
}
}
for i := 0; i < nMarkers; i++ {
// increment positions greater than k
if i > k {
q.pos[i]++
}
// update desired positions for all markers
q.npos[i] += q.dn[i]
}
q.adjustHeights()
}
func (q *Quantile) adjustHeights() {
for i := 1; i < len(q.heights)-1; i++ {
d := int(q.npos[i]) - q.pos[i]
ni1 := q.pos[i+1] - q.pos[i]
ni2 := q.pos[i] - q.pos[i-1]
n1 := float64(ni1)
n2 := float64(ni2)
h := q.heights[i]
hp1 := q.heights[i+1]
hm1 := q.heights[i-1]
h1 := hp1 - h
h2 := h - hm1
z1 := h1 / n1
z2 := h2 / n2
if d >= 1 && ni1 > 1 {
b1 := (n2 + 1) * z1
b2 := (n1 - 1) * z2
hi := h + (b1+b2)/(n1+n2)
if hm1 < hi && hi < hp1 {
q.heights[i] = hi
} else {
// use linear formula
q.heights[i] = h + z1
}
q.pos[i]++
} else if d <= -1 && ni2 > 1 {
b1 := (n2 - 1) * z1
b2 := (n1 + 1) * z2
hi := h - (b1+b2)/(n1+n2)
if hm1 < hi && hi < hp1 {
q.heights[i] = hi
} else {
// use linear formula
q.heights[i] = h - z2
}
q.pos[i]--
}
}
}
// Value returns the current estimate of p-quantile.
func (q *Quantile) Value() float64 {
if !q.filled {
// a fast path when not enought observations has been stored yet
switch q.cnt {
case 0:
return 0
case 1:
return q.heights[0]
}
sort.Float64s(q.heights[:q.cnt])
rank := int(q.p * float64(q.cnt))
return q.heights[rank]
}
// if initialised with nMarkers observations third height stores current
// estimate of p-quantile
return q.heights[2]
}