Class KllItemsSketch<T>
- Type Parameters:
T- The sketch data type.
- All Implemented Interfaces:
MemorySegmentStatus, PartitioningFeature<T>, QuantilesAPI, QuantilesGenericAPI<T>, SketchPartitionLimits
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class KllSketch
KllSketch.SketchStructure, KllSketch.SketchType -
Field Summary
Fields inherited from interface QuantilesAPI
EMPTY_MSG, MEM_REQ_SVR_NULL_MSG, NOT_SINGLE_ITEM_MSG, SELF_MERGE_MSG, TGT_IS_READ_ONLY_MSG, UNSUPPORTED_MSG -
Method Summary
Modifier and TypeMethodDescriptiondouble[]getCDF(T[] splitPoints, QuantileSearchCriteria searchCrit) Returns an approximation to the Cumulative Distribution Function (CDF) of the input stream as a monotonically increasing array of double ranks (or cumulative probabilities) on the interval [0.0, 1.0], given a set of splitPoints.Returns the sketch item classComparator<? super T> Returns the Comparator of TgetPartitionBoundariesFromNumParts(int numEquallySizedParts, QuantileSearchCriteria searchCrit) This method returns an instance ofGenericPartitionBoundarieswhich provides sufficient information for the user to create the given number of equally sized partitions, where "equally sized" refers to an approximately equal number of items per partition.getPartitionBoundariesFromPartSize(long nominalPartSizeItems, QuantileSearchCriteria searchCrit) This method returns an instance ofGenericPartitionBoundarieswhich provides sufficient information for the user to create the given number of equally sized partitions, where "equally sized" refers to an approximately equal number of items per partition.double[]getPMF(T[] splitPoints, QuantileSearchCriteria searchCrit) Returns an approximation to the Probability Mass Function (PMF) of the input stream as an array of probability masses as doubles on the interval [0.0, 1.0], given a set of splitPoints.getQuantile(double rank, QuantileSearchCriteria searchCrit) Gets the approximate quantile of the given normalized rank and the given search criterion.getQuantileLowerBound(double rank) Gets the lower bound of the quantile confidence interval in which the quantile of the given rank exists.T[]getQuantiles(double[] ranks, QuantileSearchCriteria searchCrit) Gets an array of quantiles from the given array of normalized ranks.getQuantileUpperBound(double rank) Gets the upper bound of the quantile confidence interval in which the true quantile of the given rank exists.doublegetRank(T quantile, QuantileSearchCriteria searchCrit) Gets the normalized rank corresponding to the given a quantile.doublegetRankLowerBound(double rank) Gets the lower bound of the rank confidence interval in which the true rank of the given rank exists.double[]getRanks(T[] quantiles, QuantileSearchCriteria searchCrit) Gets an array of normalized ranks corresponding to the given array of quantiles and the given search criterion.doublegetRankUpperBound(double rank) Gets the upper bound of the rank confidence interval in which the true rank of the given rank exists.final ItemsSketchSortedView<T> Gets the sorted view of this sketchstatic <T> KllItemsSketch<T> heapify(MemorySegment srcSeg, Comparator<? super T> comparator, ArrayOfItemsSerDe<T> serDe) Factory heapify takes a compact sketch image in MemorySegment and instantiates an on-heap sketch.iterator()Gets the iterator for this sketch, which is not sorted.final voidMerges another sketch into this one.static <T> KllItemsSketch<T> newHeapInstance(int k, Comparator<? super T> comparator, ArrayOfItemsSerDe<T> serDe) Create a new heap instance of this sketch with a given parameter k.static <T> KllItemsSketch<T> newHeapInstance(Comparator<? super T> comparator, ArrayOfItemsSerDe<T> serDe) Create a new heap instance of this sketch with the default k = 200.voidreset()Resets this sketch to the empty state.byte[]Export the current sketch as a compact byte array.toString(boolean withLevels, boolean withLevelsAndItems) Returns human readable summary information about this sketch.voidUpdates this sketch with the given item.voidWeighted update.static <T> KllItemsSketch<T> wrap(MemorySegment srcSeg, Comparator<? super T> comparator, ArrayOfItemsSerDe<T> serDe) Constructs a thin wrapper on the heap around a MemorySegment (or MemorySegment) already initialized with a validated sketch image of a type T consistent with the given comparator and serDe.Methods inherited from class KllSketch
getKFromEpsilon, getMaxSerializedSizeBytes, getNormalizedRankError, getNormalizedRankError, getNumRetained, getSerializedSizeBytes, hasMemorySegment, isCompactMemorySegmentFormat, isEmpty, isEstimationMode, isMemorySegmentUpdatableFormat, isOffHeap, isReadOnly, isSameResource, toStringMethods inherited from interface PartitioningFeature
getPartitionBoundariesFromNumParts, getPartitionBoundariesFromPartSizeMethods inherited from interface QuantilesAPI
getK, getN, getNormalizedRankError, getNumRetained, isEmpty, isEstimationMode, isReadOnly, toStringMethods inherited from interface QuantilesGenericAPI
getCDF, getMaxItem, getMaxPartitions, getMinItem, getPMF, getQuantile, getQuantiles, getRank, getRanksMethods inherited from interface SketchPartitionLimits
getMinPartitionSizeItems, getN
-
Method Details
-
newHeapInstance
public static <T> KllItemsSketch<T> newHeapInstance(Comparator<? super T> comparator, ArrayOfItemsSerDe<T> serDe) Create a new heap instance of this sketch with the default k = 200. The default k = 200 results in a normalized rank error of about 1.65%. Larger K will have smaller error but the sketch will be larger (and slower).- Type Parameters:
T- The sketch data type.- Parameters:
comparator- to compare itemsserDe- Serializer / deserializer for an array of items, T[].- Returns:
- new KllItemsSketch on the Java heap.
-
newHeapInstance
public static <T> KllItemsSketch<T> newHeapInstance(int k, Comparator<? super T> comparator, ArrayOfItemsSerDe<T> serDe) Create a new heap instance of this sketch with a given parameter k. k can be between DEFAULT_M and 65535, inclusive. The default k = 200 results in a normalized rank error of about 1.65%. Larger K will have smaller error but the sketch will be larger (and slower).- Type Parameters:
T- The sketch data type- Parameters:
k- parameter that controls size of the sketch and accuracy of estimates.comparator- to compare itemsserDe- Serializer / deserializer for items of type T and T[].- Returns:
- new KllItemsSketch on the heap.
-
heapify
public static <T> KllItemsSketch<T> heapify(MemorySegment srcSeg, Comparator<? super T> comparator, ArrayOfItemsSerDe<T> serDe) Factory heapify takes a compact sketch image in MemorySegment and instantiates an on-heap sketch. The resulting sketch will not retain any link to the source MemorySegment.- Type Parameters:
T- The sketch data type- Parameters:
srcSeg- a compact MemorySegment image of a sketch serialized by this sketch and of the same type of T.comparator- to compare itemsserDe- Serializer / deserializer for items of type T and T[].- Returns:
- a heap-based sketch based on the given MemorySegment.
-
wrap
public static <T> KllItemsSketch<T> wrap(MemorySegment srcSeg, Comparator<? super T> comparator, ArrayOfItemsSerDe<T> serDe) Constructs a thin wrapper on the heap around a MemorySegment (or MemorySegment) already initialized with a validated sketch image of a type T consistent with the given comparator and serDe. A reference to the MemorySegment is kept in the sketch and must remain in scope consistent with the temporal scope of this sketch. The amount of data kept on the heap is very small. All of the item data originally collected by the given MemorySegment sketch object remains in the MemorySegment object.- Type Parameters:
T- The sketch data type- Parameters:
srcSeg- the MemorySegment object that this sketch will wrap. It will not be modified.comparator- to compare itemsserDe- Serializer / deserializer for items of type T and T[].- Returns:
- a heap-base sketch that is a thin wrapper around the given srcSeg.
-
getCDF
Description copied from interface:QuantilesGenericAPIReturns an approximation to the Cumulative Distribution Function (CDF) of the input stream as a monotonically increasing array of double ranks (or cumulative probabilities) on the interval [0.0, 1.0], given a set of splitPoints.The resulting approximations have a probabilistic guarantee that can be obtained from the getNormalizedRankError(false) function.
- Specified by:
getCDFin interfaceQuantilesGenericAPI<T>- Parameters:
splitPoints- an array of m unique, monotonically increasing items (of the same type as the input items) that divide the item input domain into m+1 overlapping intervals.The start of each interval is below the lowest item retained by the sketch corresponding to a zero rank or zero probability, and the end of the interval is the rank or cumulative probability corresponding to the split point.
The (m+1)th interval represents 100% of the distribution represented by the sketch and consistent with the definition of a cumulative probability distribution, thus the (m+1)th rank or probability in the returned array is always 1.0.
If a split point exactly equals a retained item of the sketch and the search criterion is:
- INCLUSIVE, the resulting cumulative probability will include that item.
- EXCLUSIVE, the resulting cumulative probability will not include the weight of that split point.
It is not recommended to include either the minimum or maximum items of the input stream.
searchCrit- the desired search criteria.- Returns:
- a discrete CDF array of m+1 double ranks (or cumulative probabilities) on the interval [0.0, 1.0].
-
getClassOfT
Description copied from interface:QuantilesGenericAPIReturns the sketch item class- Specified by:
getClassOfTin interfaceQuantilesGenericAPI<T>- Returns:
- the sketch item class
-
getComparator
Description copied from interface:QuantilesGenericAPIReturns the Comparator of T- Specified by:
getComparatorin interfaceQuantilesGenericAPI<T>- Returns:
- Comparator of the sketch
-
getPartitionBoundariesFromNumParts
public GenericPartitionBoundaries<T> getPartitionBoundariesFromNumParts(int numEquallySizedParts, QuantileSearchCriteria searchCrit) Description copied from interface:PartitioningFeatureThis method returns an instance ofGenericPartitionBoundarieswhich provides sufficient information for the user to create the given number of equally sized partitions, where "equally sized" refers to an approximately equal number of items per partition.The sketch must not be empty.
- Specified by:
getPartitionBoundariesFromNumPartsin interfacePartitioningFeature<T>- Parameters:
numEquallySizedParts- an integer that specifies the number of equally sized partitions betweengetMinItem()andgetMaxItem(). This must be a positive integer less thangetMaxPartitions()- A 1 will return: minItem, maxItem.
- A 2 will return: minItem, median quantile, maxItem.
- Etc.
searchCrit- If INCLUSIVE, all the returned quantiles are the upper boundaries of the equally sized partitions with the exception of the lowest returned quantile, which is the lowest boundary of the lowest ranked partition. If EXCLUSIVE, all the returned quantiles are the lower boundaries of the equally sized partitions with the exception of the highest returned quantile, which is the upper boundary of the highest ranked partition.- Returns:
- an instance of
GenericPartitionBoundaries.
-
getPartitionBoundariesFromPartSize
public GenericPartitionBoundaries<T> getPartitionBoundariesFromPartSize(long nominalPartSizeItems, QuantileSearchCriteria searchCrit) Description copied from interface:PartitioningFeatureThis method returns an instance ofGenericPartitionBoundarieswhich provides sufficient information for the user to create the given number of equally sized partitions, where "equally sized" refers to an approximately equal number of items per partition.The sketch must not be empty.
- Specified by:
getPartitionBoundariesFromPartSizein interfacePartitioningFeature<T>- Parameters:
nominalPartSizeItems- an integer that specifies the nominal size, in items, of each target partition. This must be a positive integer greater thangetMinPartitionSizeItems().searchCrit- If INCLUSIVE, all the returned quantiles are the upper boundaries of the equally sized partitions with the exception of the lowest returned quantile, which is the lowest boundary of the lowest ranked partition. If EXCLUSIVE, all the returned quantiles are the lower boundaries of the equally sized partitions with the exception of the highest returned quantile, which is the upper boundary of the highest ranked partition.- Returns:
- an instance of
GenericPartitionBoundaries.
-
getPMF
Description copied from interface:QuantilesGenericAPIReturns an approximation to the Probability Mass Function (PMF) of the input stream as an array of probability masses as doubles on the interval [0.0, 1.0], given a set of splitPoints.The resulting approximations have a probabilistic guarantee that can be obtained from the getNormalizedRankError(true) function.
- Specified by:
getPMFin interfaceQuantilesGenericAPI<T>- Parameters:
splitPoints- an array of m unique, monotonically increasing items (of the same type as the input items) that divide the item input domain into m+1 consecutive, non-overlapping intervals.Each interval except for the end intervals starts with a split point and ends with the next split point in sequence.
The first interval starts below the lowest item retained by the sketch corresponding to a zero rank or zero probability, and ends with the first split point
The last (m+1)th interval starts with the last split point and ends after the last item retained by the sketch corresponding to a rank or probability of 1.0.
The sum of the probability masses of all (m+1) intervals is 1.0.
If the search criterion is:
- INCLUSIVE, and the upper split point of an interval equals an item retained by the sketch, the interval will include that item. If the lower split point equals an item retained by the sketch, the interval will exclude that item.
- EXCLUSIVE, and the upper split point of an interval equals an item retained by the sketch, the interval will exclude that item. If the lower split point equals an item retained by the sketch, the interval will include that item.
It is not recommended to include either the minimum or maximum items of the input stream.
searchCrit- the desired search criteria.- Returns:
- a PMF array of m+1 probability masses as doubles on the interval [0.0, 1.0].
-
getQuantile
Description copied from interface:QuantilesGenericAPIGets the approximate quantile of the given normalized rank and the given search criterion.- Specified by:
getQuantilein interfaceQuantilesGenericAPI<T>- Parameters:
rank- the given normalized rank, a double in the range [0.0, 1.0].searchCrit- If INCLUSIVE, the given rank includes all quantiles ≤ the quantile directly corresponding to the given rank. If EXCLUSIVE, he given rank includes all quantiles < the quantile directly corresponding to the given rank.- Returns:
- the approximate quantile given the normalized rank.
- See Also:
-
getQuantiles
Description copied from interface:QuantilesGenericAPIGets an array of quantiles from the given array of normalized ranks.- Specified by:
getQuantilesin interfaceQuantilesGenericAPI<T>- Parameters:
ranks- the given array of normalized ranks, each of which must be in the interval [0.0,1.0].searchCrit- if INCLUSIVE, the given ranks include all quantiles ≤ the quantile directly corresponding to each rank.- Returns:
- an array of quantiles corresponding to the given array of normalized ranks.
- See Also:
-
getQuantileLowerBound
Description copied from interface:QuantilesGenericAPIGets the lower bound of the quantile confidence interval in which the quantile of the given rank exists.Although it is possible to estimate the probability that the true quantile exists within the quantile confidence interval specified by the upper and lower quantile bounds, it is not possible to guarantee the width of the quantile confidence interval as an additive or multiplicative percent of the true quantile.
- Specified by:
getQuantileLowerBoundin interfaceQuantilesGenericAPI<T>- Parameters:
rank- the given normalized rank- Returns:
- the lower bound of the quantile confidence interval in which the quantile of the given rank exists.
-
getQuantileUpperBound
Description copied from interface:QuantilesGenericAPIGets the upper bound of the quantile confidence interval in which the true quantile of the given rank exists.Although it is possible to estimate the probability that the true quantile exists within the quantile confidence interval specified by the upper and lower quantile bounds, it is not possible to guarantee the width of the quantile interval as an additive or multiplicative percent of the true quantile.
- Specified by:
getQuantileUpperBoundin interfaceQuantilesGenericAPI<T>- Parameters:
rank- the given normalized rank- Returns:
- the upper bound of the quantile confidence interval in which the true quantile of the given rank exists.
-
getRank
Description copied from interface:QuantilesGenericAPIGets the normalized rank corresponding to the given a quantile.- Specified by:
getRankin interfaceQuantilesGenericAPI<T>- Parameters:
quantile- the given quantilesearchCrit- if INCLUSIVE the given quantile is included into the rank.- Returns:
- the normalized rank corresponding to the given quantile.
- See Also:
-
getRankLowerBound
public double getRankLowerBound(double rank) Gets the lower bound of the rank confidence interval in which the true rank of the given rank exists. The approximate probability that the true rank is within the confidence interval specified by the upper and lower rank bounds for this sketch is 0.99.- Specified by:
getRankLowerBoundin interfaceQuantilesAPI- Parameters:
rank- the given normalized rank.- Returns:
- the lower bound of the rank confidence interval in which the true rank of the given rank exists.
-
getRankUpperBound
public double getRankUpperBound(double rank) Gets the upper bound of the rank confidence interval in which the true rank of the given rank exists. The approximate probability that the true rank is within the confidence interval specified by the upper and lower rank bounds for this sketch is 0.99.- Specified by:
getRankUpperBoundin interfaceQuantilesAPI- Parameters:
rank- the given normalized rank.- Returns:
- the upper bound of the rank confidence interval in which the true rank of the given rank exists.
-
getRanks
Description copied from interface:QuantilesGenericAPIGets an array of normalized ranks corresponding to the given array of quantiles and the given search criterion.- Specified by:
getRanksin interfaceQuantilesGenericAPI<T>- Parameters:
quantiles- the given array of quantilessearchCrit- if INCLUSIVE, the given quantiles include the rank directly corresponding to each quantile.- Returns:
- an array of normalized ranks corresponding to the given array of quantiles.
- See Also:
-
getSortedView
Description copied from interface:QuantilesGenericAPIGets the sorted view of this sketch- Specified by:
getSortedViewin interfaceQuantilesGenericAPI<T>- Returns:
- the sorted view of this sketch
-
iterator
Description copied from interface:QuantilesGenericAPIGets the iterator for this sketch, which is not sorted.- Specified by:
iteratorin interfaceQuantilesGenericAPI<T>- Returns:
- the iterator for this sketch
-
merge
-
reset
public void reset()Description copied from interface:QuantilesAPIResets this sketch to the empty state. If the sketch is read only this does nothing.The parameter k will not change.
- Specified by:
resetin interfaceQuantilesAPI
-
toByteArray
public byte[] toByteArray()Export the current sketch as a compact byte array.- Returns:
- the current sketch as a compact byte array.
-
toString
Description copied from class:KllSketchReturns human readable summary information about this sketch. Used for debugging. -
update
Description copied from interface:QuantilesGenericAPIUpdates this sketch with the given item.- Specified by:
updatein interfaceQuantilesGenericAPI<T>- Parameters:
item- from a stream of items. Nulls are ignored.
-
update
Weighted update. Updates this sketch with the given item the number of times specified by the given integer weight.- Parameters:
item- the item to be repeated. NaNs are ignored.weight- the number of times the update of item is to be repeated. It must be ≥ one.
-