-
-
Notifications
You must be signed in to change notification settings - Fork 168
/
regression_test.go
139 lines (125 loc) · 2.72 KB
/
regression_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
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
package stats_test
import (
"fmt"
"testing"
"github.com/montanaflynn/stats"
)
func ExampleLinearRegression() {
data := []stats.Coordinate{
{1, 2.3},
{2, 3.3},
{3, 3.7},
}
r, _ := stats.LinearRegression(data)
fmt.Println(r)
// Output: [{1 2.400000000000001} {2 3.1} {3 3.7999999999999994}]
}
func TestLinearRegression(t *testing.T) {
data := []stats.Coordinate{
{1, 2.3},
{2, 3.3},
{3, 3.7},
{4, 4.3},
{5, 5.3},
}
r, _ := stats.LinearRegression(data)
a := 2.3800000000000026
if !close(r[0].Y, a) {
t.Errorf("%v != %v", r[0].Y, a)
}
a = 3.0800000000000014
if !veryclose(r[1].Y, a) {
t.Errorf("%v != %v", r[1].Y, a)
}
a = 3.7800000000000002
if r[2].Y != a {
t.Errorf("%v != %v", r[2].Y, a)
}
a = 4.479999999999999
if !veryclose(r[3].Y, a) {
t.Errorf("%v != %v", r[3].Y, a)
}
a = 5.179999999999998
if !veryclose(r[4].Y, a) {
t.Errorf("%v != %v", r[4].Y, a)
}
_, err := stats.LinearRegression([]stats.Coordinate{})
if err == nil {
t.Errorf("Empty slice should have returned an error")
}
}
func TestExponentialRegression(t *testing.T) {
data := []stats.Coordinate{
{1, 2.3},
{2, 3.3},
{3, 3.7},
{4, 4.3},
{5, 5.3},
}
r, _ := stats.ExponentialRegression(data)
a, _ := stats.Round(r[0].Y, 3)
if a != 2.515 {
t.Errorf("%v != %v", r[0].Y, 2.515)
}
a, _ = stats.Round(r[1].Y, 3)
if a != 3.032 {
t.Errorf("%v != %v", r[1].Y, 3.032)
}
a, _ = stats.Round(r[2].Y, 3)
if a != 3.655 {
t.Errorf("%v != %v", r[2].Y, 3.655)
}
a, _ = stats.Round(r[3].Y, 3)
if a != 4.407 {
t.Errorf("%v != %v", r[3].Y, 4.407)
}
a, _ = stats.Round(r[4].Y, 3)
if a != 5.313 {
t.Errorf("%v != %v", r[4].Y, 5.313)
}
_, err := stats.ExponentialRegression([]stats.Coordinate{})
if err == nil {
t.Errorf("Empty slice should have returned an error")
}
}
func TestExponentialRegressionYCoordErr(t *testing.T) {
c := []stats.Coordinate{{1, -5}, {4, 25}, {6, 5}}
_, err := stats.ExponentialRegression(c)
if err != stats.YCoordErr {
t.Errorf(err.Error())
}
}
func TestLogarithmicRegression(t *testing.T) {
data := []stats.Coordinate{
{1, 2.3},
{2, 3.3},
{3, 3.7},
{4, 4.3},
{5, 5.3},
}
r, _ := stats.LogarithmicRegression(data)
a := 2.1520822363811702
if !close(r[0].Y, a) {
t.Errorf("%v != %v", r[0].Y, a)
}
a = 3.3305559222492214
if !veryclose(r[1].Y, a) {
t.Errorf("%v != %v", r[1].Y, a)
}
a = 4.019918836568674
if !veryclose(r[2].Y, a) {
t.Errorf("%v != %v", r[2].Y, a)
}
a = 4.509029608117273
if !veryclose(r[3].Y, a) {
t.Errorf("%v != %v", r[3].Y, a)
}
a = 4.888413396683663
if !veryclose(r[4].Y, a) {
t.Errorf("%v != %v", r[4].Y, a)
}
_, err := stats.LogarithmicRegression([]stats.Coordinate{})
if err == nil {
t.Errorf("Empty slice should have returned an error")
}
}