tdigest

package
v0.16.13 Latest Latest
Warning

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

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

Documentation

Overview

Package tdigest provides an approximate quantile estimator for streaming numeric data.

The implementation follows the t-digest algorithm by Ted Dunning (2013). A t-digest maintains a sorted list of centroids — (mean, weight) pairs — whose maximum size is bounded by the compression parameter δ. Centroids near the tails of the distribution are kept small (exact), while centroids near the median are allowed to be large (approximate). This trade-off gives good accuracy at the tails where precision matters most (P99, P999) and acceptable accuracy at the median.

Typical usage:

td := tdigest.New(100) // compression=100
for _, v := range values {
    if err := td.Add(v); err != nil { ... }
}
p50, err := td.Quantile(0.5)
p99, err := td.Quantile(0.99)
td.Reset() // reuse without reallocating

This package is internal to xolu. It intentionally omits serialisation, merge, CDF, TrimmedMean, and other features not required by the timeseries subsystem.

Core algorithm and data structures adapted from github.com/caio/go-tdigest by Caio Alonso. For general-purpose use, use that library instead of this one.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type TDigest

type TDigest struct {
	// contains filtered or unexported fields
}

TDigest is a streaming approximate quantile estimator. The zero value is not valid; use New.

func New

func New(compression float64) (*TDigest, error)

New creates a TDigest with the given compression parameter δ. δ controls the maximum number of centroids: larger δ means more centroids, more memory, and better accuracy. Typical values: 50–200. δ must be > 0.

func (*TDigest) Add

func (t *TDigest) Add(value float64) error

Add registers a single sample value. Returns an error if value is NaN.

func (*TDigest) AddWeighted

func (t *TDigest) AddWeighted(value float64, count uint64) error

AddWeighted registers a sample that occurred count times. Returns an error if value is NaN or count is zero.

func (*TDigest) Count

func (t *TDigest) Count() uint64

Count returns the total number of samples added to the digest.

func (*TDigest) Quantile

func (t *TDigest) Quantile(q float64) (float64, error)

Quantile returns the estimated value at quantile q. q must be in [0, 1]; returns an error otherwise. Returns (0, nil) on an empty digest.

func (*TDigest) Reset

func (t *TDigest) Reset()

Reset clears the digest without releasing memory. After Reset, the instance can be reused as if freshly created.

Jump to

Keyboard shortcuts

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