2

I am doing a multivariate forecasting using the Rossmann dataset. I now need to use the RMSPE metric to evaluate my model. I saw the relevant formula here. But I am not sure how to efficiently implement this using numpy. Any help is much appreciated.

Suleka_28
  • 2,761
  • 4
  • 27
  • 43

2 Answers2

5

You can take advantage of numpy's vectorisation capability for an error metric like this. The following function can be used to compute RMSPE:

def rmse(y_true, y_pred):
    '''
    Compute Root Mean Square Percentage Error between two arrays.
    '''
    loss = np.sqrt(np.mean(np.square(((y_true - y_pred) / y_true)), axis=0))

    return loss

(For the error between vectors, axis=0 makes it explicit that the error is computed row-wise, returning a vector. It isn't required, as this is the default behaviour for np.mean.)

Chris
  • 1,618
  • 13
  • 21
  • This gives you the right answer but the actual formula for percent error should be (y_true - y_pred) / y_true. When you square it, it makes no difference, but just for clarity... – Tim Johnsen May 09 '19 at 22:06
  • Sorry but I don't think this is correct, or at least, it doesn't match the formula currently used by [statsmodels](https://www.statsmodels.org/dev/_modules/statsmodels/tools/eval_measures.html#rmspe). – dmn Jul 12 '23 at 13:37
  • @dmn the `statsmodels` implementation is much more sophisticated but fundamentally does the same thing. Notice that they define `error = y - y_hat` and then compute `error / y`. This is the same as `(y_true - y_pred) / y_true`. Then `statsmodels` squares the error, takes the mean (`nanmean`) and finally returns the `sqrt`. I have just noticed a typo in the function name I wrote though, it should have been `rmspe`... – Chris Jul 12 '23 at 14:10
-2

It should be normalized by ground truths.

RMSPE equation

def rmspe(y_true, y_pred):
    return np.sqrt(np.nanmean(np.square(((y_true - y_pred) / y_true))))*100
Larry
  • 1
  • 1