-
Notifications
You must be signed in to change notification settings - Fork 5
/
welford_test.go
64 lines (59 loc) · 1.28 KB
/
welford_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package welford
import (
"math"
"math/rand"
"testing"
)
func TestSimple(t *testing.T) {
s := New()
for i := 1; i < 6; i++ {
s.Add(float64(i))
}
if s.Count() != 5 {
t.Fatalf("count: expected 5, got %d\n", s.Count())
}
if s.Min() != 1 {
t.Fatalf("min: expected 0.0, got %f\n", s.Min())
}
if s.Max() != 5 {
t.Fatalf("max: expected 5.0, got %f\n", s.Max())
}
}
func TestNormal(t *testing.T) {
s := New()
for i := 0; i < 1000000; i++ {
s.Add(rand.NormFloat64())
}
// Allow .3% of error (.3% arbitrarily chosen)
if math.Abs(s.Mean()) > 0.003 {
t.Fatalf("mean: expected 0.0, got %f\n", s.Mean())
}
if s.Variance()-1 > 0.003 {
t.Fatalf("variance: expected 1.0, got %f\n", s.Variance())
}
if s.Stddev()-1 > 0.003 {
t.Fatalf("stddev: expected 1.0, got %f\n", s.Stddev())
}
}
func TestVariance(t *testing.T) {
s := New()
if s.Variance() != 0 {
t.Fatalf("variance: expected 0, got %f\n", s.Variance())
}
}
func TestReset(t *testing.T) {
s := New()
for i := 0; i < 1000000; i++ {
s.Add(rand.NormFloat64())
}
s.Reset()
if s.Mean() != 0 {
t.Fatalf("mean: expected 0.0, got %f\n", s.Mean())
}
if s.Variance() != 0 {
t.Fatalf("variance: expected 0.0, got %f\n", s.Variance())
}
if s.Stddev() != 0 {
t.Fatalf("stddev: expected 0.0, got %f\n", s.Stddev())
}
}