quantiles

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 4, 2026 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrEmpty = errors.New("operation is undefined for an empty sketch")
View Source
var ErrIndexOutOfValidRange = errors.New("index out of range")

Functions

This section is empty.

Types

type Number

type Number interface {
	float32 | float64 | int64
}

Number is a type constraint that permits numeric types used by quantile sketches.

type NumericSortedView

type NumericSortedView[T Number] struct {
	// contains filtered or unexported fields
}

The NumericSortedView provides a sorted view of the data retained by a numeric quantiles-type sketch that would be cumbersome to get any other way. One could use the sketch's iterator to iterate over the contents of the sketch, but the result would not be sorted.

The data from a NumericSortedView is an unbiased random sample of the input stream that can be used for other kinds of analysis not directly provided by the sketch.

func NewNumericSortedView

func NewNumericSortedView[T Number](
	quantiles []T, cumWeights []int64, n int64, maxItem, minItem T,
) *NumericSortedView[T]

NewNumericSortedView constructs a new NumericSortedView.

func (*NumericSortedView[T]) CDF

func (s *NumericSortedView[T]) CDF(splitPoints []T, isInclusive bool) ([]float64, error)

CDF returns an approximation of the stream's cumulative distribution function for the given split points.

It returns a monotonically increasing slice of cumulative probabilities in [0, 1]. The returned slice has len(splitPoints)+1 entries.

The approximation has the probabilistic guarantee implied by NormalizedRankError(false).

splitPoints must be unique and strictly increasing. They divide the item domain into len(splitPoints)+1 overlapping intervals. Each interval starts below the lowest retained item, which corresponds to cumulative probability 0, and ends at the cumulative probability of its split point.

The final interval represents the rest of the distribution, so the last returned value is always 1.

If a split point is exactly equal to a retained item, isInclusive=true includes that item's weight in the cumulative probability for that split point.

Callers generally should not include the true minimum or maximum stream item in splitPoints.

func (*NumericSortedView[T]) CumulativeWeights

func (s *NumericSortedView[T]) CumulativeWeights() []int64

CumulativeWeights returns a copy of the cumulative weights slice. Also known as the natural ranks, which are the Natural Numbers on the interval [1, N].

func (*NumericSortedView[T]) IsEmpty

func (s *NumericSortedView[T]) IsEmpty() bool

IsEmpty returns true if the view is empty.

func (*NumericSortedView[T]) Iterator

func (s *NumericSortedView[T]) Iterator() *NumericSortedViewIterator[T]

Iterator creates and returns a new iterator.

func (*NumericSortedView[T]) MaxItem

func (s *NumericSortedView[T]) MaxItem() (T, error)

MaxItem returns the maximum item in the view. If the sketch is empty, it returns an error. This may be distinct from the largest item retained by the sketch algorithm.

func (*NumericSortedView[T]) MinItem

func (s *NumericSortedView[T]) MinItem() (T, error)

MinItem returns the minimum item in the sketch. If the sketch is empty, it returns an error. This may be distinct from the smallest item retained by the sketch algorithm.

func (*NumericSortedView[T]) N

func (s *NumericSortedView[T]) N() int64

N returns the total number of items presented to the sourcing sketch.

func (*NumericSortedView[T]) NumRetained

func (s *NumericSortedView[T]) NumRetained() int

NumRetained returns the number of quantiles retained by this sorted view. This may be slightly different from the function with the same name when called from the originating sketch.

func (*NumericSortedView[T]) PMF

func (s *NumericSortedView[T]) PMF(splitPoints []T, isInclusive bool) ([]float64, error)

PMF returns an approximation of the stream's probability mass function for the given split points.

It returns len(splitPoints)+1 probability masses in [0, 1]. The returned intervals are consecutive and non-overlapping, and their sum is always 1.

The approximation has the probabilistic guarantee implied by NormalizedRankError(true).

splitPoints must be unique and strictly increasing. They divide the item domain into len(splitPoints)+1 intervals. Each interior interval starts at one split point and ends at the next. The first interval starts below the lowest retained item and ends at the first split point. The last interval starts at the last split point and extends past the largest retained item.

If a split point is exactly equal to a retained item, the interval boundary handling depends on searchCrit. With isInclusive=true, an interval includes an item equal to its upper split point and excludes an item equal to its lower split point. With isInclusive=false, an interval excludes an item equal to its upper split point and includes an item equal to its lower split point.

Callers generally should not include the true minimum or maximum stream item in splitPoints.

func (*NumericSortedView[T]) Quantile

func (s *NumericSortedView[T]) Quantile(rank float64, isInclusive bool) (T, error)

Quantile returns the approximate quantile of the given normalized rank. If inclusive, the given rank includes all quantiles less than or equal to the quantile directly corresponding to the given rank. If not, the given rank includes all quantiles less than the quantile directly corresponding to the given rank. If the sketch is empty, it returns an error.

func (*NumericSortedView[T]) Quantiles

func (s *NumericSortedView[T]) Quantiles() []T

Quantiles return a copy of the quantiles slice.

func (*NumericSortedView[T]) Rank

func (s *NumericSortedView[T]) Rank(quantile T, isInclusive bool) (float64, error)

Rank returns the normalized rank corresponding to the given a quantile. If the sketch is empty, it returns an error.

type NumericSortedViewIterator

type NumericSortedViewIterator[T Number] struct {
	// contains filtered or unexported fields
}

NumericSortedViewIterator is an iterator over sorted views of numeric quantile sketches of type.

func NewNumericSortedViewIterator

func NewNumericSortedViewIterator[T Number](quantiles []T, cumWeights []int64) *NumericSortedViewIterator[T]

NewNumericSortedViewIterator constructs a new NumericSortedViewIterator. The quantiles slice must be ordered and have the same length as cumWeights. The cumWeights slice must be ordered, start with the value one, and the last value must be equal to N, the total number of items updated to the sketch.

func (*NumericSortedViewIterator[T]) N

func (it *NumericSortedViewIterator[T]) N() int64

N returns the total count of all items presented to the sketch.

func (*NumericSortedViewIterator[T]) NaturalRank

func (it *NumericSortedViewIterator[T]) NaturalRank() (int64, error)

NaturalRank returns the natural rank at the current index. This is equivalent to NaturalRankWithCriterion(Inclusive). NOTE: Call Next() before calling this method.

func (*NumericSortedViewIterator[T]) NaturalRankWithCriterion

func (it *NumericSortedViewIterator[T]) NaturalRankWithCriterion(isInclusive bool) (int64, error)

NaturalRankWithCriterion returns the natural rank at the current index (or previous index) based on the chosen search criterion. The natural rank is a number in the range [1, N], where N (N()) is the total number of items fed to the sketch.

If inclusive, includes the weight of the item at the current index. Otherwise, returns the natural rank of the previous index. NOTE: Call Next() before calling this method.

func (*NumericSortedViewIterator[T]) Next

func (it *NumericSortedViewIterator[T]) Next() bool

Next advances the index and checks if it is valid. The state of the iterator is undefined before the first call of this method.

func (*NumericSortedViewIterator[T]) NormalizedRank

func (it *NumericSortedViewIterator[T]) NormalizedRank() (float64, error)

NormalizedRank returns the normalized rank at the current index. This is equivalent to NormalizedRankWithCriterion(true). NOTE: Call Next() before calling this method.

func (*NumericSortedViewIterator[T]) NormalizedRankWithCriterion

func (it *NumericSortedViewIterator[T]) NormalizedRankWithCriterion(isInclusive bool) (float64, error)

NormalizedRankWithCriterion returns the normalized rank at the current index (or previous index) based on the chosen search criterion. Normalized rank = natural rank / N (N()) and is a fraction in the range (0, 1.0]. NOTE: Call Next() before calling this method.

func (*NumericSortedViewIterator[T]) Quantile

func (it *NumericSortedViewIterator[T]) Quantile() (T, error)

Quantile returns the quantile at the current index. NOTE: Call Next() before calling this method.

func (*NumericSortedViewIterator[T]) Weight

func (it *NumericSortedViewIterator[T]) Weight() (int64, error)

Weight returns the weight contribution of the item at the current index. NOTE: Call Next() before calling this method.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL