Giter VIP home page Giter VIP logo

tdigest's Introduction

tdigest

This 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/influxdata/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))
}

tdigest's People

Contributors

aviau avatar dim avatar goller avatar ianwilkes avatar nathanielc avatar scbrickley avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

tdigest's Issues

A crash

panic: runtime error: index out of range

goroutine 20 [running]:
github.com/influxdata/tdigest.(*TDigest).Quantile(0xc420133380, 0x3fd0000000000000, 0x3ff0000000000000)
/home/cc/gocode/src/github.com/influxdata/tdigest/tdigest.go:135 +0x277

panic: runtime error: index out of range in tdigest.CentroidList.Less(...)

Hello,
seeing panics like below. I have not been able to reliably reproduce yet. sometimes it seems to work, sometimes not.
Basically my code creates a tdigest like so td := tdigest.NewWithCompression(100)
and then calls this a bunch of times : td.Add(s, 1) where s is some time.Duration converted to a float64 (typically a few millisecods)

any ideas? thanks.

panic: runtime error: index out of range

goroutine 1998768 [running]:
github.com/influxdata/tdigest.CentroidList.Less(...)
	/home/dieter/go/src/github.com/influxdata/tdigest/centroid.go:51
sort.doPivot(0xb3d600, 0xc0a257f4b0, 0xce, 0x10a, 0xc, 0x10b)
	/usr/lib/go/src/sort/sort.go:121 +0x502
sort.quickSort(0xb3d600, 0xc0a257f4b0, 0xce, 0x10a, 0x11)
	/usr/lib/go/src/sort/sort.go:190 +0x9a
sort.quickSort(0xb3d600, 0xc0a257f4b0, 0xce, 0x250, 0x13)
	/usr/lib/go/src/sort/sort.go:194 +0xf8
sort.Sort(0xb3d600, 0xc0a257f4b0)
	/usr/lib/go/src/sort/sort.go:218 +0x79
github.com/influxdata/tdigest.(*TDigest).process(0xc0a257f480)
	/home/dieter/go/src/github.com/influxdata/tdigest/tdigest.go:77 +0xdf
github.com/influxdata/tdigest.(*TDigest).AddCentroid(0xc0a257f480, 0x40a6c20000000000, 0x3ff0000000000000)
	/home/dieter/go/src/github.com/influxdata/tdigest/tdigest.go:67 +0x80
github.com/influxdata/tdigest.(*TDigest).Add(...)
	/home/dieter/go/src/github.com/influxdata/tdigest/tdigest.go:42
github.com/tsenart/vegeta/lib.(*tdigestEstimator).Add(0xc003efe168, 0x40a6c20000000000)

Marshaler and Unmarshaler

I'd like to add marhsalling and unmarshalling to this package. I'd implement encoding.BinaryMarshaler and the smallBytes format from the Java reference implementation. Alternatively, I could add AsBytes and AsSmallBytes method. Is there any interest?

I could also go straight to Protobuf if there's interest. E.g. DDSketch Go comes with a Protobuf definition.

Pack new features into 0.0.2 release

There are several features that have been added since the initial 0.0.1 release, in particular AddCentroidList() and Merge() features. The suggestion here is to release this features under 0.0.2, in turn that would simplify an integration of that package into the build pipelines (instead of fetching a particular commit)

ByteSizeForCompression underestimates memory consumption

The calculation for ByteSizeForCompression states

Unprocessed and processed can grow up to length c

https://github.com/influxdata/tdigest/blob/master/tdigest.go#L50

But unprocessed and processed are allocated in https://github.com/influxdata/tdigest/blob/master/tdigest.go#L30 with a capacity of maxUnprocessed and maxProcessed which are 8 and 2 times c (see e.g. https://github.com/influxdata/tdigest/blob/master/tdigest.go#L304)

This leads to ByteSizeForCompression underestimating the memory consumption of these two buffers by a factor of 10 which is not neglible.

I think the result should be

8 * (2 * (8c + 2c) + c)   //  8bytes/float * ( (8*c unprocessed + 2*c compresses) * 2 values   +  c values 

cumulative )
= 168 * c

(Btw: the 40 is not correct even if the factors 8 and 2 for the capacity of the unprocessed and processed buffers are ignored.)

BUT: This 168 * c still underestimates the memory consumpotion as in
https://github.com/influxdata/tdigest/blob/master/tdigest.go#L121

t.unprocessed = append(t.unprocessed, t.processed...)

processed gets appended to unprocessed and both slices might be full resulting in unprocessed to grow to (maxUnprocessed + maxProcessed) = 10 * c

So the correct value should be 200 * c.

This is quite a number. I'm wondering if float32 and smaller unprocessed buffer would allow to reduce memory consumption while keeping enough numerical stability and accuracy.

It's possible to merge two tdigest structs?

I don't have the mathematical background to answer this question, but it's possible to merge two tdigest structs and keep the same or similar results as if all the values were computed into a single struct?

I'm working in a distributed application and would be nice to be possible to do the process on multiple nodes, later we could shuffle the results around and merge them.

add the text "incremental quantile estimation" to the README so that this library is discoverable via that name

I'm skimming through Numerical Recipes and IQAgent (8.5.2 - Single-Pass Estimation of Arbitrary Quantiles) caught my eye. After searching a bit I found various names for this. When I searched pkg.go.dev for "incremental quantile estimation" this module did not show as a result. IMO it should.

Would you accept a README update PR with some additional description? I guess in some sense this is SEO.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.