number_sequence_stats

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2023 License: BSD-3-Clause Imports: 5 Imported by: 0

README

This library designed for fast and efficient calculate basic statistical data of a big sequence of numbers, without loading all data into memory.

It directly calculate:

* min
* max
* sum
* mean
* variance
* standard deviation

And use t-digest algorithm to approximately calculate quantiles and build a chart.

T-digest is optional. Without t-digest, library computing statistic few times faster. As example six seconds for a one billion numbers.

```
$go test -bench . -benchmem                                                                                                                       goos: linux
goarch: amd64
pkg: github.com/lynxzp/number_sequence_stats
cpu: Intel(R) Core(TM) i7-4790 CPU @ 3.60GHz
BenchmarkStat/input_size_1000-8                   276945              3612 ns/op               0 B/op          0 allocs/op
BenchmarkStat/input_size_1000000-8                   328           3700582 ns/op               0 B/op          0 allocs/op
BenchmarkStat/input_size_1000000000-8                  1        3643973531 ns/op               0 B/op          0 allocs/op
PASS
ok      github.com/lynxzp/number_sequence_stats 6.370s
```

## Example

```
import stat "github.com/lynxzp/number_sequence_stats"

const tDigestEnabled = true
s := stat.New[int64](tDigestEnabled)
for i := 0; i < 1000000; i++ {
    s.Add(int64(i))
}

fmt.Println(s.Min())
fmt.Println(s.Quantile(0.5))
fmt.Println(s)

writer := bytes.NewBuffer([]byte{})
const pointCount = 1000
_ = s.DrawPNG(writer, pointCount)
```
result:
```
0
499999.5
min: 0, max: 999999, avg: 499999.5, rms: 577349.8, stddev: 288675 sum: 4.999995e+11 amount: 1000000
0.1%: 999.5, 1%: 9999.5, 10%: 99999.5, 50%: 499999.5, 90%: 899999.5, 99%: 989999.5, 99.9%: 998999.5
```

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrTDigestDisabled = fmt.Errorf("tdigest is disabled")

Functions

This section is empty.

Types

type Number

type Number interface {
	float64 | float32 |
		int64 | int32 | int16 | int8 | int |
		uint64 | uint32 | uint16 | uint8 | uint
}

type Stat

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

func New

func New[T Number](tdigestEnable bool) *Stat[T]

func (*Stat[T]) Add

func (s *Stat[T]) Add(value T)

func (*Stat[T]) Amount

func (s *Stat[T]) Amount() int64

func (*Stat[T]) Avg

func (s *Stat[T]) Avg() float64

func (*Stat[T]) DrawPNG

func (s *Stat[T]) DrawPNG(w io.Writer, points int) error

func (*Stat[T]) Max

func (s *Stat[T]) Max() T

func (*Stat[T]) Min

func (s *Stat[T]) Min() T

func (*Stat[T]) Quantile

func (s *Stat[T]) Quantile(q float64) float64

func (*Stat[T]) Rms

func (s *Stat[T]) Rms() float64

func (*Stat[T]) Stddev

func (s *Stat[T]) Stddev() float64

func (*Stat[T]) String

func (s *Stat[T]) String() string

func (*Stat[T]) Sum

func (s *Stat[T]) Sum() float64

Jump to

Keyboard shortcuts

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