From e336a0670e245b9bb0563e53e36d1d1617c7a950 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=C3=B4nio=20C=C3=A2ndido=20de=20Lima=20e=20Silva?= Date: Tue, 1 Nov 2016 16:03:10 -0200 Subject: [PATCH] PIFTS forecast n steps ahead --- benchmarks.py | 49 ++++++++++++++++++++++++++++++++++++++++++++----- pifts.py | 16 ++++++++++++---- 2 files changed, 56 insertions(+), 9 deletions(-) diff --git a/benchmarks.py b/benchmarks.py index ae35aa6..57390fc 100644 --- a/benchmarks.py +++ b/benchmarks.py @@ -46,7 +46,7 @@ def coverage(targets,forecasts): if targets[i] >= forecasts[i][0] and targets[i] <= forecasts[i][1] : preds.append(1) else: - preds.append(1) + preds.append(0) return np.mean(preds) def getIntervalStatistics(original,models): @@ -54,11 +54,11 @@ def getIntervalStatistics(original,models): for fts in models: forecasts = fts.forecast(original) ret = ret + fts.shortname + " & " - ret = ret + str( round(rmse_interval(original[fts.order :],forecasts),2)) + " & " - ret = ret + str( round(mape_interval(original[fts.order :],forecasts),2)) + " & " + ret = ret + str( round(rmse_interval(original[fts.order-1 :],forecasts),2)) + " & " + ret = ret + str( round(mape_interval(original[fts.order-1 :],forecasts),2)) + " & " ret = ret + str( round(sharpness(forecasts),2)) + " & " ret = ret + str( round(resolution(forecasts),2)) + " & " - ret = ret + str( round(coverage(original[fts.order :],forecasts),2)) + " \\ \n" + ret = ret + str( round(coverage(original[fts.order-1 :],forecasts),2)) + " \\ \n" return ret def plotComparedSeries(original,models, colors): @@ -97,9 +97,48 @@ def plotComparedSeries(original,models, colors): ax.set_ylabel('F(T)') ax.set_xlabel('T') ax.set_xlim([0,len(original)]) + + +def plotComparedIntervalsAhead(original,models, colors, time_from, time_to): + fig = plt.figure(figsize=[25,10]) + ax = fig.add_subplot(111) + mi = [] + ma = [] -def plotCompared(original,forecasted,labels,title): + ax.plot(original,color='black',label="Original") + count = 0 + for fts in models: + if fts.isInterval: + forecasted = fts.forecastAhead(original[:time_from],time_to) + lower = [kk[0] for kk in forecasted] + upper = [kk[1] for kk in forecasted] + mi.append(min(lower)) + ma.append(max(upper)) + for k in np.arange(0,time_from): + lower.insert(0,None) + upper.insert(0,None) + ax.plot(lower,color=colors[count],label=fts.shortname) + ax.plot(upper,color=colors[count]) + + else: + forecasted = fts.forecast(original) + mi.append(min(forecasted)) + ma.append(max(forecasted)) + forecasted.insert(0,None) + ax.plot(forecasted,color=colors[count],label=fts.shortname) + + handles0, labels0 = ax.get_legend_handles_labels() + ax.legend(handles0,labels0) + count = count + 1 + #ax.set_title(fts.name) + ax.set_ylim([min(mi),max(ma)]) + ax.set_ylabel('F(T)') + ax.set_xlabel('T') + ax.set_xlim([0,len(original)]) + + +def plotCompared(original,forecasts,labels,title): fig = plt.figure(figsize=[13,6]) ax = fig.add_subplot(111) ax.plot(original,color='k',label="Original") diff --git a/pifts.py b/pifts.py index 952b384..20018f9 100644 --- a/pifts.py +++ b/pifts.py @@ -83,7 +83,7 @@ class ProbabilisticIntervalFTS(ifts.IntervalFTS): ret = [] - for k in np.arange(self.order,l): + for k in np.arange(self.order-1,l): affected_flrgs = [] affected_flrgs_memberships = [] @@ -96,8 +96,7 @@ class ProbabilisticIntervalFTS(ifts.IntervalFTS): count = 0 lags = {} if self.order > 1: - subset = ndata[k-self.order : k ] - + subset = ndata[k-(self.order-1) : k+1 ] for instance in subset: mb = common.fuzzyInstance(instance, self.sets) tmp = np.argwhere( mb ) @@ -148,7 +147,16 @@ class ProbabilisticIntervalFTS(ifts.IntervalFTS): ret.append( [ sum(lo)/norm, sum(up)/norm ] ) return ret - + + def forecastAhead(self,data,steps): + ret = [[data[k],data[k]] for k in np.arange(len(data)-self.order,len(data))] + for k in np.arange(self.order,steps): + lower = self.forecast( [ret[x][0] for x in np.arange(k-self.order,k)] ) + upper = self.forecast( [ret[x][1] for x in np.arange(k-self.order,k)] ) + ret.append([np.min(lower),np.max(upper)]) + + return ret + def __str__(self): tmp = self.name + ":\n" for r in sorted(self.flrgs):