-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevaluation_utils.py
184 lines (145 loc) · 5.02 KB
/
evaluation_utils.py
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
from typing import Dict
import numpy as np
EPSILON = 1e-10 # Small value to avoid division by zero
def mae(actual: np.ndarray, predicted: np.ndarray) -> float:
"""
Calculate Mean Absolute Error (MAE).
Formula:
MAE = (1 / N) * Σ |y_i - ŷ_i|
Where:
N is the number of samples
y_i is the i-th actual value
ŷ_i is the i-th predicted value
Σ denotes the sum over all samples
Args:
actual (np.ndarray): Actual values
predicted (np.ndarray): Predicted values
Returns:
float: MAE value
"""
return np.mean(np.abs(actual - predicted))
def mse(actual: np.ndarray, predicted: np.ndarray) -> float:
"""
Calculate Mean Squared Error (MSE).
Formula:
MSE = (1 / N) * Σ (y_i - ŷ_i)^2
Where:
N is the number of samples
y_i is the i-th actual value
ŷ_i is the i-th predicted value
Σ denotes the sum over all samples
Args:
actual (np.ndarray): Actual values
predicted (np.ndarray): Predicted values
Returns:
float: MSE value
"""
return np.mean((actual - predicted) ** 2)
def rmse(actual: np.ndarray, predicted: np.ndarray) -> float:
"""
Calculate Root Mean Square Error (RMSE).
Formula:
RMSE = sqrt((1 / N) * Σ (y_i - ŷ_i)^2)
Where:
N is the number of samples
y_i is the i-th actual value
ŷ_i is the i-th predicted value
Σ denotes the sum over all samples
sqrt denotes the square root
Args:
actual (np.ndarray): Actual values
predicted (np.ndarray): Predicted values
Returns:
float: RMSE value
"""
return np.sqrt(np.mean((actual - predicted)**2))
def mape(actual: np.ndarray, predicted: np.ndarray) -> float:
"""
Calculate Mean Absolute Percentage Error (MAPE).
Formula:
MAPE = (100% / N) * Σ |y_i - ŷ_i| / |y_i|
Where:
N is the number of samples
y_i is the i-th actual value
ŷ_i is the i-th predicted value
Σ denotes the sum over all samples
| | denotes the absolute value
Args:
actual (np.ndarray): Actual values
predicted (np.ndarray): Predicted values
Returns:
float: MAPE value
"""
# add a small epsilon to actual values to avoid division by zero if actual is zero
actual = np.where(actual == 0, EPSILON, actual)
return 100 * np.mean(np.abs((actual - predicted) / actual))
def smape(actual: np.ndarray, predicted: np.ndarray) -> float:
"""
Calculate Symmetric Mean Absolute Percentage Error (sMAPE).
Formula:
sMAPE = (200% / N) * Σ |y_i - ŷ_i| / (|y_i| + |ŷ_i|)
Where:
N is the number of samples
y_i is the i-th actual value
ŷ_i is the i-th predicted value
Σ denotes the sum over all samples
| | denotes the absolute value
Args:
actual (np.ndarray): Actual values
predicted (np.ndarray): Predicted values
Returns:
float: sMAPE value
"""
# add a small epsilon to actual values to avoid division by zero if actual is zero
actual = np.where(actual == 0, EPSILON, actual)
return 200 * np.mean(np.abs(actual - predicted) / (np.abs(actual) + np.abs(predicted)))
def rmae(actual: np.ndarray, predicted: np.ndarray, naive_forecast: np.ndarray) -> float:
"""
Calculate Relative Mean Absolute Error (rMAE).
Formula:
rMAE = MAE(actual, predicted) / MAE(actual, naive_forecast)
Where:
MAE(a, b) is the Mean Absolute Error between a and b
naive_forecast is typically the previous day's actual values
Args:
actual (np.ndarray): Actual values
predicted (np.ndarray): Predicted values
naive_forecast (np.ndarray): Naive forecast values
Returns:
float: rMAE value
"""
return mae(actual, predicted) / mae(actual, naive_forecast)
def calculate_all_metrics(predicted: np.ndarray, actual: np.ndarray, naive_forecast: np.ndarray) -> Dict[str, float]:
"""
Calculate all evaluation metrics.
Args:
actual (np.ndarray): Actual values
predicted (np.ndarray): Predicted values
naive_forecast (np.ndarray): Naive forecast values
Returns:
Dict[str, float]: Dictionary containing all calculated metrics
"""
return {
"MAE": mae(actual, predicted),
"MSE": mse(actual, predicted),
"RMSE": rmse(actual, predicted),
"MAPE": mape(actual, predicted),
"sMAPE": smape(actual, predicted),
"rMAE": rmae(actual, predicted, naive_forecast)
}
def calculate_opt_metrics(actual: np.ndarray, predicted: np.ndarray) -> Dict[str, float]:
"""
Calculate all evaluation metrics except rMAE for use within optuna trials
Args:
actual (np.ndarray): Actual values
predicted (np.ndarray): Predicted values
Returns:
Dict[str, float]: Dictionary containing all calculated metrics except rMAE
"""
return {
"MAE": mae(actual, predicted),
"MSE": mse(actual, predicted),
"RMSE": rmse(actual, predicted),
"MAPE": mape(actual, predicted),
"sMAPE": smape(actual, predicted)
}