stats

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 23, 2017 License: MIT Imports: 2 Imported by: 4

README

Build Status

Go Report Card

GoDoc

An implementation of B.P. Welford's algorithm to maintain a running variance over a stream of observations.

This code is based on a John D Cook's blogpost.

Example usage

s := stats.NewSink()
s.Push(-1.1813782)
s.Push(-0.2449577)
s.Push(0.8799429)

fmt.Printf("Mean = %v\n", s.Mean()) // -0.182131...
fmt.Printf("Standard Deviation = %v\n", s.StandardDeviation()) // 1.032095...

Todos

  • Add a method that returns the running median.
  • Make the implementation multi-goroutine safe.

License

  • Code is released under MIT license.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidValue = errors.New("NaN or +-Inf are not allowed")

ErrInvalidValue means that the item you supplied is a NaN or an Inf which is considered illegal.

Functions

This section is empty.

Types

type Sink

type Sink interface {
	Push(x float64) error
	Name() string
}

Sink accepts a stream of observations.

type Variability

type Variability interface {
	Min() float64
	Max() float64
	Mean() float64
	StandardDeviation() float64
	Count() int
}

Variability is a measure of how spread out is your dataset.

type WelfordSink

type WelfordSink struct {
	Sink
	Variability
	// contains filtered or unexported fields
}

WelfordSink is an implementation of a Sink and Variability @TODO make this goroutine-safe

func NewSink

func NewSink() *WelfordSink

NewSink returns a pointer to an implementation of interfaces Sink & Variability

func (*WelfordSink) Count

func (s *WelfordSink) Count() int

Count returns the number of observations that was seen thus far.

func (*WelfordSink) Max

func (s *WelfordSink) Max() float64

Max returns the largest observation seen.

func (*WelfordSink) Mean

func (s *WelfordSink) Mean() float64

Mean returns the arithmetic mean of all the observations.

func (*WelfordSink) Min

func (s *WelfordSink) Min() float64

Min returns the smallest observation seen.

func (*WelfordSink) Name

func (s *WelfordSink) Name() string

Name returns a descriptive comment

func (*WelfordSink) Push

func (s *WelfordSink) Push(x float64) error

Push accepts an observation. On failure, it returns an ErrInvalidValue.

func (*WelfordSink) StandardDeviation

func (s *WelfordSink) StandardDeviation() float64

StandardDeviation returns the running sd of the observations

Jump to

Keyboard shortcuts

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