Added comments for godoc

This commit is contained in:
Stephen McQuay 2014-01-01 16:04:33 -08:00
parent 1b669ee5f1
commit 3287aa13a1

View File

@ -7,8 +7,12 @@ import (
// from http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Online_algorithm // from http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Online_algorithm
// These constants are used in initialization of the OnlineStat
const ( const (
// http://en.wikipedia.org/wiki/Statistical_population
Population = iota Population = iota
// http://en.wikipedia.org/wiki/Sample_(statistics)
Sample Sample
) )
@ -37,6 +41,10 @@ func NewPopulationStat() *OnlineStat {
} }
} }
// Let's say you have already stored some values and want to start an
// OnlineStat mid-stream; This is the function for you! Just provide it with
// the data in the sinature, and you'll get a properly initialized OnlineStat.
// N.b. the typ is either ostat.Population or ostat.Sample
func MidStreamStat(n uint64, mean, stddev, min, max float64, typ uint64) *OnlineStat { func MidStreamStat(n uint64, mean, stddev, min, max float64, typ uint64) *OnlineStat {
return &OnlineStat{ return &OnlineStat{
n: n, n: n,
@ -48,6 +56,7 @@ func MidStreamStat(n uint64, mean, stddev, min, max float64, typ uint64) *Online
} }
} }
// this is how you feed new values into your OnlineStat.
func (os *OnlineStat) Push(v float64) { func (os *OnlineStat) Push(v float64) {
os.n += 1 os.n += 1
if v < os.Min { if v < os.Min {
@ -62,6 +71,7 @@ func (os *OnlineStat) Push(v float64) {
os.m2 = os.m2 + delta*(v-os.mean) os.m2 = os.m2 + delta*(v-os.mean)
} }
// http://en.wikipedia.org/wiki/Expected_value
func (os *OnlineStat) Mean() float64 { func (os *OnlineStat) Mean() float64 {
if os.n == 0 { if os.n == 0 {
return 0.0 return 0.0
@ -69,6 +79,7 @@ func (os *OnlineStat) Mean() float64 {
return os.mean return os.mean
} }
// http://en.wikipedia.org/wiki/Variance
func (os *OnlineStat) Variance() float64 { func (os *OnlineStat) Variance() float64 {
if os.n == 0 { if os.n == 0 {
return 0.0 return 0.0
@ -76,6 +87,7 @@ func (os *OnlineStat) Variance() float64 {
return os.m2 / float64(os.n-os.typ) return os.m2 / float64(os.n-os.typ)
} }
// http://en.wikipedia.org/wiki/Variance
func (os *OnlineStat) StdDev() float64 { func (os *OnlineStat) StdDev() float64 {
return math.Sqrt(os.Variance()) return math.Sqrt(os.Variance())
} }