From 49300f7bee2765d34f0d08cac1ffa5b5ec124fde Mon Sep 17 00:00:00 2001 From: Felipe Vianna Date: Wed, 3 Mar 2021 10:05:31 +0100 Subject: [PATCH] - making nmrse metric compatible with predictions using step_to and steps_ahead - fixing prediction forecast_step loop to go till last data point. --- pyFTS/benchmarks/Measures.py | 18 ++++++++++++------ pyFTS/common/fts.py | 2 +- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/pyFTS/benchmarks/Measures.py b/pyFTS/benchmarks/Measures.py index 260a975..d2951bc 100644 --- a/pyFTS/benchmarks/Measures.py +++ b/pyFTS/benchmarks/Measures.py @@ -29,24 +29,30 @@ def acf(data, k): return 1 / ((n - k) * sigma) * s -def rmse(targets, forecasts, order, steps_ahead): +def rmse(targets, forecasts, order=0, offset=0): """ Root Mean Squared Error - :param targets: - :param forecasts: + :param targets: array of targets + :param forecasts: array of forecasts + :param order: model order + :param offset: forecast offset related to target. :return: """ if isinstance(targets, list): targets = np.array(targets) if isinstance(forecasts, list): forecasts = np.array(forecasts) - return np.sqrt(np.nanmean((targets[order+steps_ahead:] - forecasts[:-steps_ahead]) ** 2)) + + if offset == 0: + return np.sqrt(np.nanmean((targets[order:] - forecasts[:]) ** 2)) + else: + return np.sqrt(np.nanmean((targets[order+offset:] - forecasts[:-offset]) ** 2)) -def nmrse(targets, forecasts, order, steps_ahead): +def nmrse(targets, forecasts, order=0, offset=0): """ Normalized Root Mean Squared Error """ - return rmse(targets, forecasts, order, steps_ahead) / (np.max(targets) - np.min(targets)) ## normalizing in targets because on forecasts might explode to inf (when model predict a line) + return rmse(targets, forecasts, order, offset) / (np.max(targets) - np.min(targets)) ## normalizing in targets because on forecasts might explode to inf (when model predict a line) diff --git a/pyFTS/common/fts.py b/pyFTS/common/fts.py index 9895468..3a4f3a7 100644 --- a/pyFTS/common/fts.py +++ b/pyFTS/common/fts.py @@ -324,7 +324,7 @@ class FTS(object): start = kwargs.get('start_at',0) - for k in np.arange(start+self.max_lag, l): + for k in np.arange(start+self.max_lag, l+1): sample = data[k-self.max_lag:k] tmp = self.forecast_ahead(sample, step, **kwargs)