gostat

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2021 License: MIT Imports: 1 Imported by: 0

README

gostat

Build Issues Pull Requests Go Doc License

An implementation of an Online (running) set of statistics for Golang.

Documentation

Overview

Package gostat provides basic statistical functions.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Online

type Online struct {
	Sample bool

	Count int

	Total float64
	Max   float64
	Min   float64
	Mean  float64

	M2 float64
	M3 float64
	// contains filtered or unexported fields
}

Online or running set of statistics that doesn't need to keep all the values to produce stats. Based on https://www.johndcook.com/blog/skewness_kurtosis/

Note: Altering the values of an Online statistic will invalidate the figures it produces.

func (*Online) Push

func (o *Online) Push(x int)

Push an integer value into the online set, updating the statistics to reflect the change.

Example
package main

import (
	"fmt"

	"bitbucket.org/idomdavis/gostat"
)

func main() {
	o := gostat.Online{}
	o.Push(1)
	o.Push(2)
	o.Push(3)

	fmt.Println(o.Count, o.Max, o.Min, o.Mean, o.Total)

}
Output:

3 3 1 2 6

func (*Online) PushFloat

func (o *Online) PushFloat(x float64)

PushFloat pushes a float64 value into the online set, updating the statistics to reflect the change.

func (*Online) PushTally added in v0.2.0

func (o *Online) PushTally()

PushTally pushes the current tally count to the online set. The tally count is reset to 0.

Example
package main

import (
	"fmt"

	"bitbucket.org/idomdavis/gostat"
)

func main() {
	o := gostat.Online{}
	o.Tally(1)
	o.PushTally()
	o.Tally(1)
	o.Tally(1)
	o.PushTally()
	o.Tally(3)
	o.PushTally()
	o.Tally(1)

	fmt.Println(o.Count, o.Max, o.Min, o.Mean, o.Total)

}
Output:

3 3 1 2 6

func (*Online) Skewness

func (o *Online) Skewness() float64

Skewness of the online set. A positive skewness typically means that the right-hand tail will be longer than the left-hand tail, and vice-versa for a negative tail. Skewness can return NaN under some conditions.

Note: Skewness is heavily dependent on sample size. For Count < 5000 the figure may not be accurate.

Example
package main

import (
	"fmt"

	"bitbucket.org/idomdavis/gostat"
)

func main() {
	o := gostat.Online{}

	o.Push(1)
	o.Push(1)
	o.Push(2)

	fmt.Println(o.Skewness())

	o.Push(2)
	o.Push(2)

	fmt.Println(o.Skewness())

}
Output:

0.7071067811865475
-0.4082482904638631

func (*Online) StandardDeviation

func (o *Online) StandardDeviation() float64

StandardDeviation of the online set.

Example
package main

import (
	"fmt"

	"bitbucket.org/idomdavis/gostat"
)

func main() {
	o := gostat.Online{}

	o.Push(1)
	o.Push(2)

	fmt.Println(o.StandardDeviation())

	o.Sample = true

	fmt.Println(o.StandardDeviation())

}
Output:

0.5
0.7071067811865476

func (*Online) Tally added in v0.2.0

func (o *Online) Tally(n int)

Tally records an increment to a value without pushing it to the online set. When PushTally is called the current tally count will be pushed.

func (*Online) Variance

func (o *Online) Variance() float64

Variance of the online set. If the count of items pushed is 1 then Variance will return 0.

Example
package main

import (
	"fmt"

	"bitbucket.org/idomdavis/gostat"
)

func main() {
	o := gostat.Online{Sample: true}

	o.Push(1)

	fmt.Println(o.Variance())

	o.Push(1)
	o.Push(4)

	fmt.Println(o.Variance())

	o.Sample = false

	fmt.Println(o.Variance())

}
Output:

0
3
2

Jump to

Keyboard shortcuts

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