forked from narqo/psqr
-
Notifications
You must be signed in to change notification settings - Fork 1
/
psqr.go
171 lines (150 loc) · 3.33 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
// Package psqr implements P-Square algorithm for estimating quantiles without storing observations.
package psqr
import (
"sort"
)
// 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
// 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 []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,
heights: make([]float64, 0, nMarkers),
}
q.Reset()
return q
}
// Reset resets the quantile.
func (q *Quantile) Reset() {
p := q.p
q.filled = false
q.heights = q.heights[: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 len(q.heights) != nMarkers {
// no required number of observations has been appended yet
q.heights = append(q.heights, v)
return
}
if !q.filled {
q.filled = true
sort.Float64s(q.heights)
}
q.append(v)
}
func (q *Quantile) append(v float64) {
l := len(q.heights) - 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 < len(q.pos); 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++ {
n := q.pos[i]
np1 := q.pos[i+1]
nm1 := q.pos[i-1]
d := q.npos[i] - float64(n)
if (d >= 1 && np1-n > 1) || (d <= -1 && nm1-n < -1) {
if d >= 0 {
d = 1
} else {
d = -1
}
h := q.heights[i]
hp1 := q.heights[i+1]
hm1 := q.heights[i-1]
// try adjusting height using P-square formula
hi := parabolic(d, hp1, h, hm1, float64(np1), float64(n), float64(nm1))
if hm1 < hi && hi < hp1 {
q.heights[i] = hi
} else {
// use linear formula
hd := q.heights[i+int(d)]
nd := q.pos[i+int(d)]
q.heights[i] = h + d*(hd-h)/float64(nd-n)
}
q.pos[i] += int(d)
}
}
}
// 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
l := len(q.heights)
switch l {
case 0:
return 0
case 1:
return q.heights[0]
}
sort.Float64s(q.heights)
rank := int(q.p * float64(l))
return q.heights[rank]
}
// if initialised with nMarkers observations third height stores current
// estimate of p-quantile
return q.heights[2]
}
// calculates the adjustment of height using piecewise parabolic (PP) prediction formula.
func parabolic(d, qp1, q, qm1, np1, n, nm1 float64) float64 {
a := d / (np1 - nm1)
b1 := (n - nm1 + d) * (qp1 - q) / (np1 - n)
b2 := (np1 - n - d) * (q - qm1) / (n - nm1)
return q + a*(b1+b2)
}