/
BaseReqSketch.java
318 lines (282 loc) · 12.5 KB
/
BaseReqSketch.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.datasketches.req;
/**
* This abstract class provides a single place to define and document the public API
* for the Relative Error Quantiles Sketch.
*
* @author Lee Rhodes
*/
abstract class BaseReqSketch {
/**
* Returns an approximation to the Cumulative Distribution Function (CDF), which is the
* cumulative analog of the PMF, of the input stream given a set of splitPoint (values).
*
* <p>The resulting approximations have a probabilistic guarantee that be obtained, a priori,
* from the <i>getRSE(int, double, boolean, long)</i> function.
*
* <p>If the sketch is empty this returns null.</p>
*
* @param splitPoints an array of <i>m</i> unique, monotonically increasing double values
* that divide the real number line into <i>m+1</i> consecutive disjoint intervals.
* The definition of an "interval" is inclusive of the left splitPoint (or minimum value) and
* exclusive of the right splitPoint, with the exception that the last interval will include
* the maximum value.
* It is not necessary to include either the min or max values in these split points.
*
* @param inclusive if true the weight of a given value is included into its rank.
*
* @return an array of m+1 double values, which are a consecutive approximation to the CDF
* of the input stream given the splitPoints. The value at array position j of the returned
* CDF array is the sum of the returned values in positions 0 through j of the returned PMF
* array.
*/
public abstract double[] getCDF(float[] splitPoints, boolean inclusive);
/**
* Same as {@link #getCDF(float[], boolean) getCDF(float[] splitPoints, isLessThanOrEqual())}
* @param splitPoints splitPoints
* @return CDF
*/
public abstract double[] getCDF(float[] splitPoints);
/**
* If true, the high ranks are prioritized for better accuracy. Otherwise
* the low ranks are prioritized for better accuracy. This state is chosen during sketch
* construction.
* @return the high ranks accuracy state.
*/
public abstract boolean getHighRankAccuracy();
/**
* Gets the largest value seen by this sketch
* @return the largest value seen by this sketch
*/
public abstract float getMaxValue();
/**
* Gets the smallest value seen by this sketch
* @return the smallest value seen by this sketch
*/
public abstract float getMinValue();
/**
* Returns an a priori estimate of relative standard error (RSE, expressed as a number in [0,1]).
* Derived from Lemma 12 in https://arxiv.org/abs/2004.01668v2, but the constant factors were
* modified based on empirical measurements.
*
* @param k the given value of k
* @param rank the given normalized rank, a number in [0,1].
* @param hra if true High Rank Accuracy mode is being selected, otherwise, Low Rank Accuracy.
* @param totalN an estimate of the total number of items submitted to the sketch.
* @return an a priori estimate of relative standard error (RSE, expressed as a number in [0,1]).
*/
public abstract double getRSE(int k, double rank, boolean hra, long totalN);
/**
* Gets the total number of items offered to the sketch.
* @return the total number of items offered to the sketch.
*/
public abstract long getN();
/**
* Returns an approximation to the Probability Mass Function (PMF) of the input stream
* given a set of splitPoints (values).
*
* <p>The resulting approximations have a probabilistic guarantee that be obtained, a priori,
* from the <i>getRSE(int, double, boolean, long)</i> function.
*
* <p>If the sketch is empty this returns null.</p>
*
* @param splitPoints an array of <i>m</i> unique, monotonically increasing double values
* that divide the real number line into <i>m+1</i> consecutive disjoint intervals.
* The definition of an "interval" is inclusive of the left splitPoint (or minimum value) and
* exclusive of the right splitPoint, with the exception that the last interval will include
* the maximum value.
* It is not necessary to include either the min or max values in these splitpoints.
*
* @param inclusive if true the weight of a given value is included into its rank.
*
* @return an array of m+1 doubles each of which is an approximation
* to the fraction of the input stream values (the mass) that fall into one of those intervals.
* The definition of an "interval" is inclusive of the left splitPoint and exclusive of the right
* splitPoint, with the exception that the last interval will include maximum value.
*/
public abstract double[] getPMF(float[] splitPoints, boolean inclusive);
/**
* Same as {@link #getPMF(float[], boolean) getPMF(float[] splitPoints, isLessThanOrEqual())}
* @param splitPoints splitPoints
* @return PMF
*/
public abstract double[] getPMF(float[] splitPoints);
/**
* Gets the approximate quantile of the given normalized rank based on the given criterion.
* The normalized rank must be in the range [0.0, 1.0] (inclusive, inclusive).
* @param normRank the given normalized rank.
* @param inclusive if true, the given rank is considered inclusive.
* @return the approximate quantile given the normalized rank.
*/
public abstract float getQuantile(double normRank, boolean inclusive);
/**
* Same as {@link #getQuantile(double, boolean) getQuantile(double fraction, isLessThanOrEqual())}
* @param normRank fractional rank
* @return quantile
*/
public abstract float getQuantile(double normRank);
/**
* Gets an array of quantiles that correspond to the given array of normalized ranks.
* @param normRanks the given array of normalized ranks.
* @param inclusive if true, the given ranks are considered inclusive.
* @return the array of quantiles that correspond to the given array of normalized ranks.
* See <i>getQuantile(double)</i>
*/
public abstract float[] getQuantiles(double[] normRanks, boolean inclusive);
/**
* Same as {@link #getQuantiles(double[], boolean) getQuantiles(double[] fractions, isLessThanOrEqual())}
* @param normRanks normalized ranks
* @return quantiles
*/
public abstract float[] getQuantiles(double[] normRanks);
/**
* Computes the normalized rank of the given value in the stream.
* The normalized rank is the fraction of values less than the given value;
* or if inclusive is true, the fraction of values less than or equal to the given value.
* @param value the given value.
* @param inclusive if true the weight of the given value is included into its rank.
* @return the normalized rank of the given value in the stream.
*/
public abstract double getRank(float value, boolean inclusive);
/**
* Same as {@link #getRank(float, boolean) getRank(float value, isLessThanOrEqual())}
* @param value value to be ranked
* @return normalized rank
*/
public abstract double getRank(float value);
/**
* returns an approximate lower bound rank of the given normalized rank.
* @param rank the given rank, a value between 0 and 1.0.
* @param numStdDev the number of standard deviations. Must be 1, 2, or 3.
* @return an approximate lower bound rank.
*/
public abstract double getRankLowerBound(double rank, int numStdDev);
/**
* Gets an array of normalized ranks that correspond to the given array of values.
* @param values the given array of values.
* @param inclusive if true the weight of the given value is included into its rank.
* @return the array of normalized ranks that correspond to the given array of values.
* See <i>getRank(float)</i>
*/
public abstract double[] getRanks(float[] values, boolean inclusive);
/**
* Same as {@link #getRanks(float[], boolean) getRanks(float[] values, isLessThanOrEqual())}
* @param values the given array of values to be ranked
* @return array of normalized ranks
*/
public abstract double[] getRanks(float[] values);
/**
* Returns an approximate upper bound rank of the given rank.
* @param rank the given rank, a value between 0 and 1.0.
* @param numStdDev the number of standard deviations. Must be 1, 2, or 3.
* @return an approximate upper bound rank.
*/
public abstract double getRankUpperBound(double rank, int numStdDev);
/**
* Gets the number of retained items of this sketch
* @return the number of retained entries of this sketch
*/
public abstract int getRetainedItems();
/**
* Gets the number of bytes when serialized.
* @return the number of bytes when serialized.
*/
public abstract int getSerializationBytes();
/**
* Gets the sorted view of the current state of this sketch
* @return the sorted view of the current state of this sketch
*/
public abstract ReqSketchSortedView getSortedView();
/**
* Returns true if this sketch is empty.
* @return empty flag
*/
public abstract boolean isEmpty();
/**
* Returns true if this sketch is in estimation mode.
* @return estimation mode flag
*/
public abstract boolean isEstimationMode();
/**
* Returns the current comparison criterion. If true the value comparison criterion is
* ≤, otherwise it will be the default, which is <.
* @return the current comparison criterion
* @deprecated in the future the ltEq comparison parameter will not be saved at the class level in preference to
* the comparison parameter being specified for each API call. This method will be removed.
*/
@Deprecated
public abstract boolean isLessThanOrEqual();
/**
* Returns an iterator for all the items in this sketch.
* @return an iterator for all the items in this sketch.
*/
public abstract ReqIterator iterator();
/**
* Merge other sketch into this one. The other sketch is not modified.
* @param other sketch to be merged into this one.
* @return this
*/
public abstract ReqSketch merge(final ReqSketch other);
/**
* Resets this sketch by removing all data and setting all data related variables to their
* virgin state.
* The parameters k, highRankAccuracy, reqDebug and LessThanOrEqual will not change.
* @return this
*/
public abstract ReqSketch reset();
/**
* Sets the chosen criterion for value comparison
* @param ltEq (Less-than-or Equals) If true, the sketch will use the ≤ criterion for comparing
* values. Otherwise, the criterion is strictly <, the default.
* This can be set anytime prior to a <i>getRank(float)</i> or <i>getQuantile(double)</i> or
* equivalent query.
* @return this
* @deprecated in the future the ltEq comparison parameter will not be saved at the class level in preference to
* the comparison parameter being specified for each API call. This method will be removed.
*/
@Deprecated
public abstract ReqSketch setLessThanOrEqual(final boolean ltEq);
/**
* Returns a byte array representation of this sketch.
* @return a byte array representation of this sketch.
*/
public abstract byte[] toByteArray();
/**
* Returns a summary of the key parameters of the sketch.
* @return a summary of the key parameters of the sketch.
*/
@Override
public abstract String toString();
/**
* Updates this sketch with the given item.
* @param item the given item
*/
public abstract void update(final float item);
/**
* A detailed, human readable view of the sketch compactors and their data.
* Each compactor string is prepended by the compactor lgWeight, the current number of retained
* items of the compactor and the current nominal capacity of the compactor.
* @param fmt the format string for the data items; example: "%4.0f".
* @param allData all the retained items for the sketch will be output by
* compactor level. Otherwise, just a summary will be output.
* @return a detailed view of the compactors and their data
*/
public abstract String viewCompactorDetail(String fmt, boolean allData);
}