File size: 1,440 Bytes
47fe089
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import numpy as np
from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score

def calculate_metrics(y, y_hat, y_train=None):
    def smape(a, f):
        return 1/len(a) * np.sum(2 * np.abs(f - a) / (np.abs(a) + np.abs(f) + np.finfo(float).eps))

    def mase(y_actual, y_pred, y_train):
        n = y_train.shape[1]
        d = np.abs(np.diff(y_train)).sum() / (n - 1)
        errors = np.abs(y_actual - y_pred)
        return errors.mean() / d

    def phase_space_distance(y_actual, y_pred):
        return np.sqrt(np.sum(np.square(y_actual - y_pred)))

    SMAPE = np.mean([smape(yi.reshape(-1), y_hati.reshape(-1)) for yi, y_hati in zip(y, y_hat)])
    MSE = np.mean([mean_squared_error(yi.reshape(-1), y_hati.reshape(-1)) for yi, y_hati in zip(y, y_hat)])
    RMSE = np.mean([np.sqrt(mean_squared_error(yi.reshape(-1), y_hati.reshape(-1))) for yi, y_hati in zip(y, y_hat)])
    MAE = np.mean([mean_absolute_error(yi.reshape(-1), y_hati.reshape(-1)) for yi, y_hati in zip(y, y_hat)])
    R2 = np.mean([r2_score(yi.reshape(-1), y_hati.reshape(-1)) for yi, y_hati in zip(y, y_hat)])
    PSD = np.mean([phase_space_distance(yi.reshape(-1), y_hati.reshape(-1)) for yi, y_hati in zip(y, y_hat)])

    if y_train is None:
        return SMAPE, MSE, RMSE, MAE, R2, PSD
    else:
        MASE = np.mean([mase(yi, y_hati, yt) for yi, y_hati, yt in zip(y, y_hat, y_train)])
        return SMAPE, MSE, RMSE, MAE, R2, MASE, PSD