-
-
Notifications
You must be signed in to change notification settings - Fork 172
Expand file tree
/
Copy pathcorrelation_test.go
More file actions
217 lines (199 loc) · 5.77 KB
/
correlation_test.go
File metadata and controls
217 lines (199 loc) · 5.77 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package stats_test
import (
"fmt"
"math"
"testing"
"github.com/montanaflynn/stats"
)
func ExampleCorrelation() {
s1 := []float64{1, 2, 3, 4, 5}
s2 := []float64{1, 2, 3, 5, 6}
a, _ := stats.Correlation(s1, s2)
rounded, _ := stats.Round(a, 5)
fmt.Println(rounded)
// Output: 0.99124
}
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}
testCases := []struct {
name string
input [][]float64
output float64
err error
}{
{"Empty Slice Error", [][]float64{s4, s4}, math.NaN(), stats.EmptyInputErr},
{"Different Length Error", [][]float64{s1, s2}, math.NaN(), stats.SizeErr},
{"Correlation Value", [][]float64{s1, s3}, 0.9912407071619302, nil},
{"Same Input Value", [][]float64{s5, s5}, 0.00, nil},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
a, err := stats.Correlation(tc.input[0], tc.input[1])
if err != nil {
if err != tc.err {
t.Errorf("Should have returned error %s", tc.err)
}
} else if !veryclose(a, tc.output) {
t.Errorf("Result %.08f should be %.08f", a, tc.output)
}
a2, err2 := stats.Pearson(tc.input[0], tc.input[1])
if err2 != nil {
if err2 != tc.err {
t.Errorf("Should have returned error %s", tc.err)
}
} else if !veryclose(a2, tc.output) {
t.Errorf("Result %.08f should be %.08f", a2, tc.output)
}
})
}
}
func ExampleSpearman() {
s1 := []float64{1, 2, 3, 4, 5}
s2 := []float64{5, 6, 7, 8, 7}
a, _ := stats.Spearman(s1, s2)
rounded, _ := stats.Round(a, 5)
fmt.Println(rounded)
// Output: 0.82078
}
func TestSpearman(t *testing.T) {
testCases := []struct {
name string
data1 []float64
data2 []float64
output float64
err error
}{
// Error cases
{"Empty Slice Error", []float64{}, []float64{}, math.NaN(), stats.EmptyInputErr},
{"Different Length Error", []float64{1, 2, 3, 4, 5}, []float64{10, -51.2, 8}, math.NaN(), stats.SizeErr},
// Perfect correlation
{"Perfect Positive", []float64{1, 2, 3, 4, 5}, []float64{1, 2, 3, 4, 5}, 1.0, nil},
{"Perfect Negative", []float64{1, 2, 3, 4, 5}, []float64{5, 4, 3, 2, 1}, -1.0, nil},
// Verified against SciPy spearmanr and R cor.test(method="spearman")
{
"Basic with tie in y (SciPy/R)",
[]float64{1, 2, 3, 4, 5},
[]float64{5, 6, 7, 8, 7},
0.8207826816681233,
nil,
},
{
"Negative with tie (SciPy mstats)",
[]float64{5.05, 6.75, 3.21, 2.66},
[]float64{1.65, 2.64, 2.64, 6.95},
-0.632455532033676,
nil,
},
{
"15 elements with ties in both (SciPy/R)",
[]float64{2.0, 47.4, 42.0, 10.8, 60.1, 1.7, 64.0, 63.1, 1.0, 1.4, 7.9, 0.3, 3.9, 0.3, 6.7},
[]float64{22.6, 8.3, 44.4, 11.9, 24.6, 0.6, 5.7, 41.6, 0.0, 0.6, 6.7, 3.8, 1.0, 1.2, 1.4},
0.6887298747763864,
nil,
},
{
"Adjacent swap (SciPy)",
[]float64{0, 1, 2, 3, 4, 5},
[]float64{0, 1, 2, 3, 5, 4},
0.9428571428571429,
nil,
},
{
"Heavy ties in both (SciPy)",
[]float64{1, 1, 1, 2, 2, 2},
[]float64{1, 1, 2, 2, 3, 3},
0.8164965809277261,
nil,
},
// Zero standard deviation case (all same values)
{"Same Input Value", []float64{0, 0, 0}, []float64{0, 0, 0}, 0.00, nil},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
a, err := stats.Spearman(tc.data1, tc.data2)
if err != nil {
if err != tc.err {
t.Errorf("Should have returned error %s, got %s", tc.err, err)
}
} else if !veryclose(a, tc.output) {
t.Errorf("Result %.16f should be %.16f", a, tc.output)
}
// Also test the Float64Data method
a2, err2 := stats.Float64Data(tc.data1).Spearman(tc.data2)
if err2 != nil {
if err2 != tc.err {
t.Errorf("Should have returned error %s, got %s", tc.err, err2)
}
} else if !veryclose(a2, tc.output) {
t.Errorf("Result %.16f should be %.16f", a2, tc.output)
}
})
}
}
func BenchmarkSpearman(b *testing.B) {
s1 := []float64{2.0, 47.4, 42.0, 10.8, 60.1, 1.7, 64.0, 63.1, 1.0, 1.4, 7.9, 0.3, 3.9, 0.3, 6.7}
s2 := []float64{22.6, 8.3, 44.4, 11.9, 24.6, 0.6, 5.7, 41.6, 0.0, 0.6, 6.7, 3.8, 1.0, 1.2, 1.4}
for i := 0; i < b.N; i++ {
_, _ = stats.Spearman(s1, s2)
}
}
func ExampleAutoCorrelation() {
s1 := []float64{1, 2, 3, 4, 5}
a, _ := stats.AutoCorrelation(s1, 1)
fmt.Println(a)
// Output: 0.4
}
func TestAutoCorrelation(t *testing.T) {
s1 := []float64{1, 2, 3, 4, 5}
s2 := []float64{}
a, err := stats.AutoCorrelation(s1, 1)
if err != nil {
t.Errorf("Should not have returned an error")
}
if a != 0.4 {
t.Errorf("Should have returned 0.4")
}
_, err = stats.AutoCorrelation(s2, 1)
if err != stats.EmptyInputErr {
t.Errorf("Should have returned empty input error")
}
// Reference values cross-checked against statsmodels.tsa.stattools.acf,
// R acf(), and MATLAB autocorr (issue #83).
s3 := []float64{22, 24, 25, 25, 28, 29, 34, 37, 40, 44, 51, 48, 47, 50, 51}
expected := []float64{
1.0,
0.83174224,
0.65632458,
0.49105012,
0.27863962,
0.03102625,
-0.16527446,
-0.30369928,
-0.40095465,
-0.45823389,
-0.45047733,
}
for lag, want := range expected {
got, err := stats.AutoCorrelation(s3, lag)
if err != nil {
t.Errorf("AutoCorrelation(s3, %d) returned error: %v", lag, err)
}
if math.Abs(got-want) > 1e-8 {
t.Errorf("AutoCorrelation(s3, %d) = %v, want %v", lag, got, want)
}
}
if _, err := stats.AutoCorrelation(s1, -1); err != stats.BoundsErr {
t.Errorf("Should have returned bounds error for negative lag")
}
if _, err := stats.AutoCorrelation(s1, len(s1)); err != stats.BoundsErr {
t.Errorf("Should have returned bounds error for lag >= len(data)")
}
constant := []float64{3, 3, 3, 3, 3}
if a, err := stats.AutoCorrelation(constant, 1); err != nil || a != 0 {
t.Errorf("AutoCorrelation of constant series should be 0, got %v err=%v", a, err)
}
}