/
QuantileEstimationCKMS.java
274 lines (236 loc) · 7.07 KB
/
QuantileEstimationCKMS.java
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
/*
Copyright 2012 Andrew Wang (andrew@umbrant.com)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.umbrant.quantile;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.Random;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
/**
* Implementation of the Cormode, Korn, Muthukrishnan, and Srivastava algorithm
* for streaming calculation of targeted high-percentile epsilon-approximate
* quantiles.
*
* This is a generalization of the earlier work by Greenwald and Khanna (GK),
* which essentially allows different error bounds on the targeted quantiles,
* which allows for far more efficient calculation of high-percentiles.
*
*
* See: Cormode, Korn, Muthukrishnan, and Srivastava
* "Effective Computation of Biased Quantiles over Data Streams" in ICDE 2005
*
* Greenwald and Khanna,
* "Space-efficient online computation of quantile summaries" in SIGMOD 2001
*
*/
public class QuantileEstimationCKMS {
private static Logger LOG = Logger.getLogger(QuantileEstimationCKMS.class);
static {
BasicConfigurator.configure();
LOG.setLevel(Level.INFO);
}
// Total number of items in stream
int count = 0;
// Used for tracking incremental compression
private int compressIdx = 0;
/**
* Current list of sampled items, maintained in sorted order with error bounds
*/
LinkedList<Item> sample;
/**
* Buffers incoming items to be inserted in batch.
*/
long[] buffer = new long[500];
int bufferCount = 0;
/**
* Array of Quantiles that we care about, along with desired error.
*/
final Quantile quantiles[];
public QuantileEstimationCKMS(Quantile[] quantiles) {
this.quantiles = quantiles;
this.sample = new LinkedList<Item>();
}
/**
* Specifies the allowable error for this rank, depending on which quantiles
* are being targeted.
*
* This is the f(r_i, n) function from the CKMS paper. It's basically how wide
* the range of this rank can be.
*
* @param rank
* the index in the list of samples
*/
private double allowableError(int rank) {
// NOTE: according to CKMS, this should be count, not size, but this leads
// to error larger than the error bounds. Leaving it like this is
// essentially a HACK, and blows up memory, but does "work".
//int size = count;
int size = sample.size();
double minError = size + 1;
for (Quantile q : quantiles) {
double error;
if (rank <= q.quantile * size) {
error = q.u * (size - rank);
} else {
error = q.v * rank;
}
if (error < minError) {
minError = error;
}
}
return minError;
}
private void printList() {
if (LOG.isDebugEnabled()) {
StringBuffer buf = new StringBuffer("sample = ");
for (Item i : sample) {
buf.append(String.format("(%s),", i));
}
LOG.debug(buf.toString());
}
}
private void printBuffer() {
if (LOG.isDebugEnabled()) {
StringBuffer buf = new StringBuffer("buffer = [");
for (int i = 0; i < bufferCount; i++) {
buf.append(buffer[i] + ", ");
}
buf.append("]");
LOG.debug(buf.toString());
}
}
/**
* Add a new value from the stream.
*
* @param v
*/
public void insert(long v) {
buffer[bufferCount] = v;
bufferCount++;
printBuffer();
if (bufferCount == buffer.length) {
insertBatch();
compress();
}
}
private void insertBatch() {
LOG.debug("insertBatch called");
if (bufferCount == 0) {
return;
}
printList();
Arrays.sort(buffer, 0, bufferCount);
printBuffer();
// Base case: no samples
int start = 0;
if (sample.size() == 0) {
Item newItem = new Item(buffer[0], 1, 0);
sample.add(newItem);
start++;
count++;
}
ListIterator<Item> it = sample.listIterator();
Item item = it.next();
for (int i = start; i < bufferCount; i++) {
long v = buffer[i];
while (it.nextIndex() < sample.size() && item.value < v) {
item = it.next();
}
// If we found that bigger item, back up so we insert ourselves before it
if (item.value > v) {
it.previous();
}
// We use different indexes for the edge comparisons, because of the above
// if statement that adjusts the iterator
int delta;
if (it.previousIndex() == 0 || it.nextIndex() == sample.size()) {
delta = 0;
} else {
delta = ((int) Math.floor(allowableError(it.nextIndex()))) - 1;
}
Item newItem = new Item(v, 1, delta);
it.add(newItem);
count++;
item = newItem;
printList();
}
bufferCount = 0;
printList();
LOG.debug("insertBatch finished");
}
/**
* Try to remove extraneous items from the set of sampled items. This checks
* if an item is unnecessary based on the desired error bounds, and merges it
* with the adjacent item if it is.
*/
public void compress() {
if (sample.size() < 2) {
return;
}
ListIterator<Item> it = sample.listIterator();
int removed = 0;
Item prev = null;
Item next = it.next();
while (it.hasNext()) {
prev = next;
next = it.next();
if (prev.g + next.g + next.delta <= allowableError(it.previousIndex())) {
next.g += prev.g;
// Remove prev. it.remove() kills the last thing returned.
it.previous();
it.previous();
it.remove();
// it.next() is now equal to next, skip it back forward again
it.next();
removed++;
}
}
LOG.debug("Removed " + removed + " items");
}
/**
* Get the estimated value at the specified quantile.
*
* @param quantile
* Queried quantile, e.g. 0.50 or 0.99.
* @return Estimated value at that quantile.
*/
public long query(double quantile) throws IOException {
// clear the buffer
insertBatch();
compress();
if (sample.size() == 0) {
throw new IOException("No samples present");
}
int rankMin = 0;
int desired = (int) (quantile * count);
ListIterator<Item> it = sample.listIterator();
Item prev, cur;
cur = it.next();
while (it.hasNext()) {
prev = cur;
cur = it.next();
rankMin += prev.g;
if (rankMin + cur.g + cur.delta > desired + (allowableError(desired) / 2)) {
return prev.value;
}
}
// edge case of wanting max value
return sample.getLast().value;
}
}