Got sick of dealing with errors
- just return 0.0 for empty OnlineStats
This commit is contained in:
parent
ca6120d392
commit
84b41cf7fe
18
ostat.go
18
ostat.go
@ -1,7 +1,6 @@
|
||||
package ostat
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"math"
|
||||
)
|
||||
|
||||
@ -46,21 +45,20 @@ func (os *OnlineStat) Push(v float64) {
|
||||
os.m2 = os.m2 + delta*(v-os.mean)
|
||||
}
|
||||
|
||||
func (os *OnlineStat) Mean() (float64, error) {
|
||||
func (os *OnlineStat) Mean() float64 {
|
||||
if os.n == 0 {
|
||||
return 0.0, errors.New("no data")
|
||||
return 0.0
|
||||
}
|
||||
return os.mean, nil
|
||||
return os.mean
|
||||
}
|
||||
|
||||
func (os *OnlineStat) Variance() (float64, error) {
|
||||
func (os *OnlineStat) Variance() float64 {
|
||||
if os.n == 0 {
|
||||
return 0.0, errors.New("no data")
|
||||
return 0.0
|
||||
}
|
||||
return os.m2 / float64(os.n-os.typ), nil
|
||||
return os.m2 / float64(os.n-os.typ)
|
||||
}
|
||||
|
||||
func (os *OnlineStat) StdDev() (float64, error) {
|
||||
variance, err := os.Variance()
|
||||
return math.Sqrt(variance), err
|
||||
func (os *OnlineStat) StdDev() float64 {
|
||||
return math.Sqrt(os.Variance())
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package ostat
|
||||
|
||||
import (
|
||||
"log"
|
||||
"math"
|
||||
"testing"
|
||||
)
|
||||
@ -93,43 +94,47 @@ func TestInsert(t *testing.T) {
|
||||
t.Errorf("incorrectly calculated max: %f != %f", ps.Max, test.max)
|
||||
}
|
||||
|
||||
pmean, _ := ps.Mean()
|
||||
smean, _ := ss.Mean()
|
||||
if pmean != smean {
|
||||
if ps.Mean() != ss.Mean() {
|
||||
t.Errorf("Means don't match")
|
||||
}
|
||||
if m, _ := ps.Mean(); math.Abs(m-test.mean) > tolerance {
|
||||
t.Errorf("incorrect mean: %f != %f", m, test.mean)
|
||||
if math.Abs(ps.Mean()-test.mean) > tolerance {
|
||||
t.Errorf("incorrect mean: %f != %f", ps.Mean(), test.mean)
|
||||
}
|
||||
|
||||
if variance, _ := ps.Variance(); math.Abs(variance-test.pvariance) > tolerance {
|
||||
t.Errorf("incorrect variance: %f != %f", variance, test.pvariance)
|
||||
if math.Abs(ps.Variance()-test.pvariance) > tolerance {
|
||||
t.Errorf("incorrect variance: %f != %f", ps.Variance(), test.pvariance)
|
||||
}
|
||||
if stdev, _ := ps.StdDev(); math.Abs(stdev-test.pstdev) > tolerance {
|
||||
t.Errorf("incorrect stdev: %f != %f", stdev, test.pstdev)
|
||||
if math.Abs(ps.StdDev()-test.pstdev) > tolerance {
|
||||
t.Errorf("incorrect stdev: %f != %f", ps.StdDev(), test.pstdev)
|
||||
}
|
||||
|
||||
if variance, _ := ss.Variance(); math.Abs(variance-test.svariance) > tolerance {
|
||||
t.Errorf("incorrect variance: %f != %f", variance, test.svariance)
|
||||
if math.Abs(ss.Variance()-test.svariance) > tolerance {
|
||||
t.Errorf("incorrect variance: %f != %f", ss.Variance(), test.svariance)
|
||||
}
|
||||
if stdev, _ := ss.StdDev(); math.Abs(stdev-test.sstdev) > tolerance {
|
||||
t.Errorf("incorrect stdev: %f != %f", stdev, test.sstdev)
|
||||
if math.Abs(ss.StdDev()-test.sstdev) > tolerance {
|
||||
t.Errorf("incorrect stdev: %f != %f", ss.StdDev(), test.sstdev)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestEmpty(t *testing.T) {
|
||||
ps := NewSampleStat()
|
||||
_, err := ps.Mean()
|
||||
if err == nil {
|
||||
if ps.Mean() != 0.0 {
|
||||
t.Errorf("failure to notify the running stat was empty")
|
||||
}
|
||||
_, err = ps.Variance()
|
||||
if err == nil {
|
||||
if ps.Variance() != 0.0 {
|
||||
t.Errorf("failure to notify the running stat was empty")
|
||||
}
|
||||
_, err = ps.StdDev()
|
||||
if err == nil {
|
||||
if ps.StdDev() != 0.0 {
|
||||
t.Errorf("failure to notify the running stat was empty")
|
||||
}
|
||||
}
|
||||
|
||||
func TestReconstitutedStat(t *testing.T) {
|
||||
os := OnlineStat{
|
||||
n: 3,
|
||||
mean: 8,
|
||||
m2: 21 * 3,
|
||||
}
|
||||
log.Printf("%+v: mean: %f, stddev: %f", os, os.Mean(), os.StdDev())
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user