forked from montanaflynn/stats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
correlation_test.go
50 lines (40 loc) · 976 Bytes
/
correlation_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
package stats
import (
"testing"
)
func TestCorrelation(t *testing.T) {
s1 := []float64{1, 2, 3, 4, 5}
s2 := []float64{10, -51.2, 8}
s3 := []float64{1, 2, 3, 5, 6}
s4 := []float64{}
s5 := []float64{0, 0, 0}
a, err := Correlation(s5, s5)
if err != nil {
t.Errorf("Should not have returned an error")
}
if a != 0 {
t.Errorf("Should have returned 0")
}
_, err = Correlation(s1, s2)
if err == nil {
t.Errorf("Mismatched slice lengths should have returned an error")
}
a, err = Correlation(s1, s3)
if err != nil {
t.Errorf("Should not have returned an error")
}
if a != 0.9912407071619302 {
t.Errorf("Correlation %v != %v", a, 0.9912407071619302)
}
_, err = Correlation(s1, s4)
if err == nil {
t.Errorf("Empty slice should have returned an error")
}
a, err = Pearson(s1, s3)
if err != nil {
t.Errorf("Should not have returned an error")
}
if a != 0.9912407071619302 {
t.Errorf("Correlation %v != %v", a, 0.9912407071619302)
}
}