Metrics to evaluate time-series.
def np_smape(a, b):
"""
Calculates sMAPE
:param a: actual values
:param b: predicted values
:return: sMAPE
"""
a = np.reshape(a, (-1,))
b = np.reshape(b, (-1,))
return np.mean(2.0 * np.abs(a - b) / (np.abs(a) + np.abs(b) + 1e-16)) # BT changed 2 to 200
def torch_smape(model_outputs,target_outputs):
numerator = torch.abs(model_outputs - target_outputs)
denominator = torch.abs(model_outputs) + torch.abs(target_outputs)
elementwise_smape = 2*torch.div(numerator, denominator)
nan_mask = torch.isnan(elementwise_smape)
loss = elementwise_smape[~nan_mask].mean()
assert ~torch.isnan(loss)
return loss
y_hat2 = noise + y_hat
plt.plot(np.arange(10),y[0,0])
plt.plot(np.arange(10),y_hat[0,0])
plt.plot(np.arange(10),y_hat2[0,0])
print(f"y_hat: {smape(y,y_hat,reduction='mean')}")
print(f"y_hat2:{smape(y,y_hat2,reduction='mean')}")
def np_mase(y_test, y_hat_test,insample, freq):
"""
Calculates MAsE
:param insample: insample data
:param y_test: out of sample target values
:param y_hat_test: predicted values
:param freq: data frequency
:return:
"""
y_hat_naive = []
for i in range(freq, len(insample)):
y_hat_naive.append(insample[(i - freq)])
masep = np.mean(abs(insample[freq:] - y_hat_naive))
return np.mean(abs(y_test - y_hat_test)) / masep
y_hat2=noise + y_hat
plt.plot(np.arange(21),y_hat[0,0,-21:])
plt.plot(np.arange(21),y_hat2[0,0,-21:])
plt.plot(np.arange(21),y[0,0,-21:])
err_y_hat = mase(x[0, 0, -horizon:][None,:], y_hat [0, 0, -horizon:][None,:], x[0, 0, :-horizon][None,:], 7, reduction = 'mean')
err_y_hat2 = mase(x[0, 0, -horizon:][None,:], y_hat2[0, 0, -horizon:][None,:],x[0, 0, :-horizon][None,:], 7, reduction = 'mean')
print(f"y_hat: {err_y_hat}")
print(f"y_hat2:{err_y_hat2}")