github.com/ncruces/go-sqlite3@v0.15.1-0.20240520133447-53eef1510ff0/ext/stats/welford_test.go (about) 1 package stats 2 3 import ( 4 "math" 5 "testing" 6 ) 7 8 func Test_welford(t *testing.T) { 9 t.Parallel() 10 11 var s1, s2 welford 12 13 s1.enqueue(4) 14 s1.enqueue(7) 15 s1.enqueue(13) 16 s1.enqueue(16) 17 if got := s1.average(); got != 10 { 18 t.Errorf("got %v, want 10", got) 19 } 20 if got := s1.var_samp(); got != 30 { 21 t.Errorf("got %v, want 30", got) 22 } 23 if got := s1.var_pop(); got != 22.5 { 24 t.Errorf("got %v, want 22.5", got) 25 } 26 if got := s1.stddev_samp(); got != math.Sqrt(30) { 27 t.Errorf("got %v, want √30", got) 28 } 29 if got := s1.stddev_pop(); got != math.Sqrt(22.5) { 30 t.Errorf("got %v, want √22.5", got) 31 } 32 33 s1.dequeue(4) 34 s2.enqueue(7) 35 s2.enqueue(13) 36 s2.enqueue(16) 37 if s1.var_pop() != s2.var_pop() { 38 t.Errorf("got %v, want %v", s1, s2) 39 } 40 } 41 42 func Test_covar(t *testing.T) { 43 t.Parallel() 44 45 var c1, c2 welford2 46 47 c1.enqueue(3, 70) 48 c1.enqueue(5, 80) 49 c1.enqueue(2, 60) 50 c1.enqueue(7, 90) 51 c1.enqueue(4, 75) 52 53 if got := c1.covar_samp(); got != 21.25 { 54 t.Errorf("got %v, want 21.25", got) 55 } 56 if got := c1.covar_pop(); got != 17 { 57 t.Errorf("got %v, want 17", got) 58 } 59 60 c1.dequeue(3, 70) 61 c2.enqueue(5, 80) 62 c2.enqueue(2, 60) 63 c2.enqueue(7, 90) 64 c2.enqueue(4, 75) 65 if c1.covar_pop() != c2.covar_pop() { 66 t.Errorf("got %v, want %v", c1.covar_pop(), c2.covar_pop()) 67 } 68 } 69 70 func Test_correlation(t *testing.T) { 71 t.Parallel() 72 73 var c welford2 74 c.enqueue(1, 3) 75 c.enqueue(2, 2) 76 c.enqueue(3, 1) 77 78 if got := c.correlation(); got != -1 { 79 t.Errorf("got %v, want -1", got) 80 } 81 }