forked from QData/spacetimeformer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheval_stats.py
48 lines (32 loc) · 1.24 KB
/
eval_stats.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
import numpy as np
EPSILON = 1e-7
def r_squared(actual: np.ndarray, predicted: np.ndarray):
rss = (_error(actual, predicted) ** 2).sum(1)
tss = (_error(actual, actual.mean(1, keepdims=True)) ** 2).sum(1)
r2 = 1.0 - rss / (tss + EPSILON)
return r2.mean()
def _error(actual: np.ndarray, predicted: np.ndarray):
"""Simple error"""
return actual - predicted
def _percentage_error(actual: np.ndarray, predicted: np.ndarray):
"""
Percentage error
Note: result is NOT multiplied by 100
"""
return _error(actual, predicted) / (actual + EPSILON)
def mse(actual: np.ndarray, predicted: np.ndarray):
"""Mean Squared Error"""
return np.mean(np.square(_error(actual, predicted)))
def mae(actual: np.ndarray, predicted: np.ndarray):
"""Mean Absolute Error"""
return np.mean(np.abs(_error(actual, predicted)))
def mape(actual: np.ndarray, predicted: np.ndarray):
"""Mean Absolute Percentage Error"""
return np.mean(np.abs(_percentage_error(actual, predicted)))
def smape(actual: np.ndarray, predicted: np.ndarray):
"""Symmetric Mean Absolute Percentage Error"""
return np.mean(
2.0
* np.abs(actual - predicted)
/ ((np.abs(actual) + np.abs(predicted)) + EPSILON)
)