Naïve forecaster; Theil's U Statistic in Measures

This commit is contained in:
Petrônio Cândido de Lima e Silva 2017-01-22 18:41:42 -02:00
parent 9a90c4d4e4
commit 72610e9dc3
3 changed files with 129 additions and 36 deletions

View File

@ -24,6 +24,18 @@ def mape_interval(targets, forecasts):
return np.mean(abs(fmean - targets) / fmean) * 100 return np.mean(abs(fmean - targets) / fmean) * 100
# Theil's U Statistic
def U(targets, forecasts):
#forecasts.insert(0,None)
l = len(targets)
naive = []
y = []
for k in np.arange(0,l-1):
y.append(((targets[k+1]-forecasts[k])/targets[k]) ** 2)
naive.append(((targets[k + 1] - targets[k]) / targets[k]) ** 2)
return np.sqrt(sum(y)/sum(naive))
# Sharpness - Mean size of the intervals # Sharpness - Mean size of the intervals
def sharpness(forecasts): def sharpness(forecasts):
tmp = [i[1] - i[0] for i in forecasts] tmp = [i[1] - i[0] for i in forecasts]

View File

@ -11,6 +11,7 @@ from mpl_toolkits.mplot3d import Axes3D
from pyFTS.benchmarks import Measures from pyFTS.benchmarks import Measures
from pyFTS.partitioners import Grid from pyFTS.partitioners import Grid
from pyFTS.common import Membership, FuzzySet, FLR, Transformations, Util from pyFTS.common import Membership, FuzzySet, FLR, Transformations, Util
from pyFTS import pfts
def getIntervalStatistics(original, models): def getIntervalStatistics(original, models):
@ -35,7 +36,8 @@ def plotDistribution(dist):
vmin=0, vmax=1, edgecolors=None) vmin=0, vmax=1, edgecolors=None)
def plotComparedSeries(original, models, colors, typeonlegend=False, save=False, file=None,tam=[20, 5],intervals=True): def plotComparedSeries(original, models, colors, typeonlegend=False, save=False, file=None, tam=[20, 5],
intervals=True):
fig = plt.figure(figsize=tam) fig = plt.figure(figsize=tam)
ax = fig.add_subplot(111) ax = fig.add_subplot(111)
@ -81,8 +83,6 @@ def plotComparedSeries(original, models, colors, typeonlegend=False, save=False,
Util.showAndSaveImage(fig, file, save) Util.showAndSaveImage(fig, file, save)
def plotComparedIntervalsAhead(original, models, colors, distributions, time_from, time_to, def plotComparedIntervalsAhead(original, models, colors, distributions, time_from, time_to,
interpol=False, save=False, file=None, tam=[20, 5], resolution=None): interpol=False, save=False, file=None, tam=[20, 5], resolution=None):
fig = plt.figure(figsize=tam) fig = plt.figure(figsize=tam)
@ -96,7 +96,8 @@ def plotComparedIntervalsAhead(original, models, colors, distributions, time_fro
count = 0 count = 0
for fts in models: for fts in models:
if fts.hasDistributionForecasting and distributions[count]: if fts.hasDistributionForecasting and distributions[count]:
density = fts.forecastAheadDistribution(original[time_from - fts.order:time_from], time_to, resolution, parameters=None) density = fts.forecastAheadDistribution(original[time_from - fts.order:time_from], time_to, resolution,
parameters=None)
y = density.columns y = density.columns
t = len(y) t = len(y)
@ -112,7 +113,8 @@ def plotComparedIntervalsAhead(original, models, colors, distributions, time_fro
diffs = [(density[q][k + 1] - density[q][k]) / 50 for q in density] diffs = [(density[q][k + 1] - density[q][k]) / 50 for q in density]
for p in np.arange(0, 50): for p in np.arange(0, 50):
xx = [time_from + k + 0.02 * p for q in np.arange(0, t)] xx = [time_from + k + 0.02 * p for q in np.arange(0, t)]
alpha2 = np.array([density[density.columns[q]][k] + diffs[q]*p for q in np.arange(0, t)]) * 100 alpha2 = np.array(
[density[density.columns[q]][k] + diffs[q] * p for q in np.arange(0, t)]) * 100
ax.scatter(xx, y, c=alpha2, marker='s', linewidths=0, cmap='Oranges', ax.scatter(xx, y, c=alpha2, marker='s', linewidths=0, cmap='Oranges',
norm=pltcolors.Normalize(vmin=0, vmax=1), vmin=0, vmax=1, edgecolors=None) norm=pltcolors.Normalize(vmin=0, vmax=1), vmin=0, vmax=1, edgecolors=None)
@ -430,14 +432,15 @@ def compareModelsTable(original, models_fo, models_ho):
return sup + header + body + "\\end{tabular}" return sup + header + body + "\\end{tabular}"
def simpleSearch_RMSE(original, model, partitions, orders, save=False, file=None,tam=[10, 15],plotforecasts=False,elev=30, azim=144): def simpleSearch_RMSE(original, model, partitions, orders, save=False, file=None, tam=[10, 15], plotforecasts=False,
elev=30, azim=144):
ret = [] ret = []
errors = np.array([[0 for k in range(len(partitions))] for kk in range(len(orders))]) errors = np.array([[0 for k in range(len(partitions))] for kk in range(len(orders))])
forecasted_best = [] forecasted_best = []
fig = plt.figure(figsize=tam) fig = plt.figure(figsize=tam)
# fig.suptitle("Comparação de modelos ") # fig.suptitle("Comparação de modelos ")
if plotforecasts: if plotforecasts:
ax0 = fig.add_axes([0, 0.5, 0.9, 0.45]) # left, bottom, width, height ax0 = fig.add_axes([0, 0.4, 0.9, 0.5]) # left, bottom, width, height
ax0.set_xlim([0, len(original)]) ax0.set_xlim([0, len(original)])
ax0.set_ylim([min(original) * 0.9, max(original) * 1.1]) ax0.set_ylim([min(original) * 0.9, max(original) * 1.1])
ax0.set_title('Forecasts') ax0.set_title('Forecasts')
@ -473,7 +476,7 @@ def simpleSearch_RMSE(original, model, partitions, orders, save=False, file=None
# handles0, labels0 = ax0.get_legend_handles_labels() # handles0, labels0 = ax0.get_legend_handles_labels()
# ax0.legend(handles0, labels0) # ax0.legend(handles0, labels0)
ax0.plot(original, label="Original", linewidth=3.0, color="black") ax0.plot(original, label="Original", linewidth=3.0, color="black")
ax1 = Axes3D(fig, rect=[0, 1, 0.9, 0.45], elev=elev, azim=azim) ax1 = Axes3D(fig, rect=[0, 1, 0.9, 0.9], elev=elev, azim=azim)
if not plotforecasts: ax1 = Axes3D(fig, rect=[0, 1, 0.9, 0.9], elev=elev, azim=azim) if not plotforecasts: ax1 = Axes3D(fig, rect=[0, 1, 0.9, 0.9], elev=elev, azim=azim)
# ax1 = fig.add_axes([0.6, 0.5, 0.45, 0.45], projection='3d') # ax1 = fig.add_axes([0.6, 0.5, 0.45, 0.45], projection='3d')
ax1.set_title('Error Surface') ax1.set_title('Error Surface')
@ -485,6 +488,70 @@ def simpleSearch_RMSE(original, model, partitions, orders, save=False, file=None
ret.append(best) ret.append(best)
ret.append(forecasted_best) ret.append(forecasted_best)
# plt.tight_layout()
Util.showAndSaveImage(fig, file, save) Util.showAndSaveImage(fig, file, save)
return ret return ret
def pftsExploreOrderAndPartitions(data,save=False, file=None):
fig, axes = plt.subplots(nrows=4, ncols=1, figsize=[6, 8])
data_fs1 = Grid.GridPartitionerTrimf(data, 10)
mi = []
ma = []
axes[0].set_title('Point Forecasts by Order')
axes[2].set_title('Interval Forecasts by Order')
for order in np.arange(1, 6):
fts = pfts.ProbabilisticFTS("")
fts.shortname = "n = " + str(order)
fts.train(data, data_fs1, order=order)
point_forecasts = fts.forecast(data)
interval_forecasts = fts.forecastInterval(data)
lower = [kk[0] for kk in interval_forecasts]
upper = [kk[1] for kk in interval_forecasts]
mi.append(min(lower) * 0.95)
ma.append(max(upper) * 1.05)
for k in np.arange(0, order):
point_forecasts.insert(0, None)
lower.insert(0, None)
upper.insert(0, None)
axes[0].plot(point_forecasts, label=fts.shortname)
axes[2].plot(lower, label=fts.shortname)
axes[2].plot(upper)
axes[1].set_title('Point Forecasts by Number of Partitions')
axes[3].set_title('Interval Forecasts by Number of Partitions')
for partitions in np.arange(5, 11):
data_fs = Grid.GridPartitionerTrimf(data, partitions)
fts = pfts.ProbabilisticFTS("")
fts.shortname = "q = " + str(partitions)
fts.train(data, data_fs, 1)
point_forecasts = fts.forecast(data)
interval_forecasts = fts.forecastInterval(data)
lower = [kk[0] for kk in interval_forecasts]
upper = [kk[1] for kk in interval_forecasts]
mi.append(min(lower) * 0.95)
ma.append(max(upper) * 1.05)
point_forecasts.insert(0, None)
lower.insert(0, None)
upper.insert(0, None)
axes[1].plot(point_forecasts, label=fts.shortname)
axes[3].plot(lower, label=fts.shortname)
axes[3].plot(upper)
for ax in axes:
ax.set_ylabel('F(T)')
ax.set_xlabel('T')
ax.plot(data, label="Original", color="black", linewidth=1.5)
handles, labels = ax.get_legend_handles_labels()
ax.legend(handles, labels, loc=2, bbox_to_anchor=(1, 1))
ax.set_ylim([min(mi), max(ma)])
ax.set_xlim([0, len(data)])
plt.tight_layout()
Util.showAndSaveImage(fig, file, save)

14
benchmarks/naive.py Normal file
View File

@ -0,0 +1,14 @@
#!/usr/bin/python
# -*- coding: utf8 -*-
from pyFTS import fts
class Naive(fts.FTS):
def __init__(self, name):
super(Naive, self).__init__(1, "Naïve" + name)
self.name = "Naïve Model"
self.detail = "Naïve Model"
def forecast(self, data):
return data