Added support for starting mid-stream

This commit is contained in:
sm
2014-01-01 12:00:15 -08:00
parent 84b41cf7fe
commit 1b669ee5f1
2 changed files with 176 additions and 9 deletions
+43 -5
View File
@@ -1,25 +1,31 @@
package ostat
import (
"fmt"
"math"
)
// from http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Online_algorithm
const (
Population = iota
Sample
)
type OnlineStat struct {
n int64
n uint64
mean float64
m2 float64
Max float64
Min float64
typ int64
Max float64
typ uint64
}
func NewSampleStat() *OnlineStat {
return &OnlineStat{
Min: math.Inf(1),
Max: math.Inf(-1),
typ: 1,
typ: Sample,
}
}
@@ -27,7 +33,18 @@ func NewPopulationStat() *OnlineStat {
return &OnlineStat{
Min: math.Inf(1),
Max: math.Inf(-1),
typ: 0,
typ: Population,
}
}
func MidStreamStat(n uint64, mean, stddev, min, max float64, typ uint64) *OnlineStat {
return &OnlineStat{
n: n,
mean: mean,
m2: stddev * stddev * float64(n),
Min: min,
Max: max,
typ: typ,
}
}
@@ -62,3 +79,24 @@ func (os *OnlineStat) Variance() float64 {
func (os *OnlineStat) StdDev() float64 {
return math.Sqrt(os.Variance())
}
func (os *OnlineStat) String() string {
return fmt.Sprintf(
"%+v",
struct {
n uint64
min float64
max float64
mean float64
variance float64
stdDev float64
}{
n: os.n,
min: os.Min,
max: os.Max,
mean: os.Mean(),
variance: os.Variance(),
stdDev: os.StdDev(),
},
)
}