tdigest

package module
v0.0.0-...-90e030c Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2025 License: Apache-2.0 Imports: 6 Imported by: 0

README

Build Status Go Report Card GoDoc

This is a fork of influxdata/tdigest; it is an implementation of Ted Dunning's t-digest in Go.

The implementation is based off Derrick Burns' C++ implementation.

Example

package main

import (
	"log"

	"github.com/RaduBerinde/tdigest"
)

func main() {
	td := tdigest.NewWithCompression(1000)
	for _, x := range []float64{1, 2, 3, 4, 5, 5, 4, 3, 2, 1} {
		td.Add(x, 1)
	}

	// Compute Quantiles
	log.Println("50th", td.Quantile(0.5))
	log.Println("75th", td.Quantile(0.75))
	log.Println("90th", td.Quantile(0.9))
	log.Println("99th", td.Quantile(0.99))

	// Compute CDFs
	log.Println("CDF(1) = ", td.CDF(1))
	log.Println("CDF(2) = ", td.CDF(2))
	log.Println("CDF(3) = ", td.CDF(3))
	log.Println("CDF(4) = ", td.CDF(4))
	log.Println("CDF(5) = ", td.CDF(5))
}

Documentation

Index

Constants

View Source
const ErrWeightLessThanZero = Error("centroid weight cannot be less than zero")

ErrWeightLessThanZero is used when the weight is not able to be processed.

Variables

This section is empty.

Functions

func Deserialize

func Deserialize(dest *TDigest, buf []byte) (bufOffset int, _ error)

Deserialize deserializes a t-digest from buf into dest.

The centroid slice in dest is reused.

Returns the number of bytes read from buf and any error encountered.

Types

type Builder

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

func MakeBuilder

func MakeBuilder(delta int) Builder

MakeBuilder initializes a Builder; delta is the compression factor, which determines the size of the digest: the digest has at most 2*Delta centroids, with ~1.3*Delta in practice.

func (*Builder) Add

func (b *Builder) Add(x, w float64)

Add adds a value x with a weight w to the distribution.

func (*Builder) Digest

func (b *Builder) Digest() TDigest

Digest returns the merged TDigest. The returned value is only valid until the next call to Add(), Merge(), or Reset().

func (*Builder) Merge

func (b *Builder) Merge(t *TDigest)

Merge a digest into the builder. The digest does not need to have the same compression factor (delta).

func (*Builder) Reset

func (b *Builder) Reset()

type Centroid

type Centroid struct {
	Mean   float64
	Weight float64
}

Centroid average position of all points in a shape

func (*Centroid) Add

func (c *Centroid) Add(r Centroid) error

Add averages the two centroids together and update this centroid

func (*Centroid) String

func (c *Centroid) String() string

type Error

type Error string

Error is a domain error encountered while processing tdigests

func (Error) Error

func (e Error) Error() string

type Merger

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

func MakeMerger

func MakeMerger(delta int) Merger

MakeMerger initializes a Merger; delta is the compression factor, which determines the size of the digest: the digest has at most 2*Delta centroids, with ~1.3*Delta in practice.

func (*Merger) Digest

func (m *Merger) Digest() TDigest

Digest returns the merged TDigest. The returned value is only valid until the next call to Merge() or Reset().

func (*Merger) Merge

func (m *Merger) Merge(t *TDigest)

Merge a digest into the merger. The digest does not need to have the same compression factor (delta).

func (*Merger) Reset

func (m *Merger) Reset()

Reset resets the Merger to an empty state.

type TDigest

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

TDigest is a t-digest, produced by a Builder or a Merger.

A TDigest is read-only (none of its methods modify it).

func (*TDigest) CDF

func (t *TDigest) CDF(x float64) float64

CDF returns the cumulative distribution function for a given value x.

func (*TDigest) Centroids

func (t *TDigest) Centroids() iter.Seq[Centroid]

Centroids returns an iterator over the centroids in the digest.

func (*TDigest) Clone

func (t *TDigest) Clone() TDigest

Clone creates a deep copy of the TDigest.

func (*TDigest) Delta

func (t *TDigest) Delta() int

Delta returns the compression factor that was used to generate this digest.

func (*TDigest) Quantile

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

Quantile returns the (approximate) quantile of the distribution. Accepted values for q are between 0.0 and 1.0.

func (*TDigest) Serialize

func (t *TDigest) Serialize(appendTo []byte) []byte

func (*TDigest) SerializedSize

func (t *TDigest) SerializedSize() int

Directories

Path Synopsis
gen command
validate command

Jump to

Keyboard shortcuts

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