forked from rai-project/dlframework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
103 lines (93 loc) · 2.38 KB
/
error.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
package metrics
import "math"
func MeanSquaredError(a, b []float64) float64 {
n := len(a)
if n != len(b) {
panic("length not equal")
}
sum := 0.0
for i := 0; i < n; i++ {
delta := a[i] - b[i]
sum += delta * delta
}
return sum / float64(n)
}
// MSPE computes the mean-square-percentage error.
func MeanSquaredPercentageError(y, yhat []float64) float64 {
Σ := 0.0
for i := range y {
ε := (yhat[i] - y[i]) / y[i]
Σ += ε * ε
}
return Σ / float64(len(y))
}
// NRMSE computes the normalized root-mean-square error.
//
// https://en.wikipedia.org/wiki/Root-mean-square_deviation#Normalized_root-mean-square_deviation
func NormalizedRootMeanSquaredError(y, yhat []float64) float64 {
count := len(y)
min, max := y[0], y[0]
for i := 1; i < count; i++ {
if y[i] < min {
min = y[i]
}
if y[i] > max {
max = y[i]
}
}
return RootMeanSquaredError(y, yhat) / (max - min)
}
// RMSE computes the root-mean-square error.
//
// https://en.wikipedia.org/wiki/Root-mean-square_deviation
func RootMeanSquaredError(y, yhat []float64) float64 {
return math.Sqrt(MeanSquaredError(y, yhat))
}
// RMSPE computes the root-mean-square-percentage error.
func RootMeanSquaredPercentageError(y, yhat []float64) float64 {
return math.Sqrt(MeanSquaredPercentageError(y, yhat))
}
func RelativeAbsoluteError(a, b []float64) float64 {
return Sum(AbsoluteError(a, b)) / Sum(AbsoluteError(a, Broadcast(Mean(a), len(a))))
}
// Compute the squared error
// This function computes the elementwise squared error for a vector
func SquaredError(a, b []float64) []float64 {
n := len(a)
if n != len(b) {
panic("length not equal")
}
res := make([]float64, n)
for ii := 0; ii < n; ii++ {
diff := a[ii] - b[ii]
res[ii] = diff * diff
}
return res
}
// Compute the squared log error
// This function computes the elementwise squared log error for a vector
func SquaredLogError(a, b []float64) []float64 {
n := len(a)
if n != len(b) {
panic("length not equal")
}
res := make([]float64, n)
for ii := 0; ii < n; ii++ {
e := (math.Log(1+a[ii]) - math.Log(1+b[ii]))
res[ii] = e * e
}
return res
}
// Compute the absolute error
// This function computes the elementwise absolute error for a vector
func AbsoluteError(a, b []float64) []float64 {
n := len(a)
if n != len(b) {
panic("length not equal")
}
res := make([]float64, n)
for ii := 0; ii < n; ii++ {
res[ii] = math.Abs(a[ii] - b[ii])
}
return res
}