Metrics to evaluate time-series.

mape[source]

mape(truth, pred, reduction='mean')

Computes mean absolute percentage error (MAPE)

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

smape[source]

smape(truth, pred, agg=None, reduction='mean')

Computes symmetric mean absolute percentage error (SMAPE) on the mean

Arguments:

* data_samples (``np.array``): Sampled predictions (n_timeseries, n_variables, n_timesteps).
* data_truth (``np.array``): Ground truth time series values (n_timeseries, n_variables, n_timesteps).
* agg: Aggregation function applied to sampled predictions (defaults to ``np.median``).

SMAPELossFlat[source]

SMAPELossFlat(*args, axis=-1, floatify=True, **kwargs)

Same as smape, but flattens input and target. DOES not work yet

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')}")
y_hat: 0.6969027519226074
y_hat2:1.0056720972061157
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

mase[source]

mase(y_test, y_hat_test, insample, freq, reduction=None)

Computes mean absolute scaled error (MASE) as in the M4 competition <https://www.m4.unic.ac.cy/wp-content/uploads/2018/03/M4-Competitors-Guide.pdf>_. Arguments: *

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}")
y_hat: 0.09310071915388107
y_hat2:0.18620143830776215