Novos benchmarks

This commit is contained in:
Petrônio Cândido de Lima e Silva 2017-01-21 00:38:32 -02:00
parent 3cc0e0ebda
commit 9a90c4d4e4
4 changed files with 62 additions and 97 deletions

View File

@ -35,32 +35,32 @@ 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]): 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)
mi = [] mi = []
ma = [] ma = []
ax.plot(original, color='black', label="Original") ax.plot(original, color='black', label="Original",linewidth=1.5)
count = 0 count = 0
for fts in models: for fts in models:
if fts.hasPointForecasting: if fts.hasPointForecasting:
forecasted = fts.forecast(original) forecasted = fts.forecast(original)
mi.append(min(forecasted)) mi.append(min(forecasted)*0.95)
ma.append(max(forecasted)) ma.append(max(forecasted)*1.05)
for k in np.arange(0, fts.order): for k in np.arange(0, fts.order):
forecasted.insert(0, None) forecasted.insert(0, None)
lbl = fts.shortname lbl = fts.shortname
if typeonlegend: lbl += " (Point)" if typeonlegend: lbl += " (Point)"
ax.plot(forecasted, color=colors[count], label=lbl, ls="-") ax.plot(forecasted, color=colors[count], label=lbl, ls="-")
if fts.hasIntervalForecasting: if fts.hasIntervalForecasting and intervals:
forecasted = fts.forecastInterval(original) forecasted = fts.forecastInterval(original)
lower = [kk[0] for kk in forecasted] lower = [kk[0] for kk in forecasted]
upper = [kk[1] for kk in forecasted] upper = [kk[1] for kk in forecasted]
mi.append(min(lower)) mi.append(min(lower)*0.95)
ma.append(max(upper)) ma.append(max(upper)*1.05)
for k in np.arange(0, fts.order): for k in np.arange(0, fts.order):
lower.insert(0, None) lower.insert(0, None)
upper.insert(0, None) upper.insert(0, None)
@ -430,101 +430,61 @@ def compareModelsTable(original, models_fo, models_ho):
return sup + header + body + "\\end{tabular}" return sup + header + body + "\\end{tabular}"
from pyFTS import hwang def simpleSearch_RMSE(original, model, partitions, orders, save=False, file=None,tam=[10, 15],plotforecasts=False,elev=30, azim=144):
def HOSelecaoSimples_MenorRMSE(original, parameters, orders):
ret = [] ret = []
errors = np.array([[0 for k in range(len(parameters))] 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 = []
print("Série Original") fig = plt.figure(figsize=tam)
fig = plt.figure(figsize=[20, 12]) #fig.suptitle("Comparação de modelos ")
fig.suptitle("Comparação de modelos ") if plotforecasts:
ax0 = fig.add_axes([0, 0.5, 0.6, 0.45]) # left, bottom, width, height ax0 = fig.add_axes([0, 0.5, 0.9, 0.45]) # left, bottom, width, height
ax0.set_xlim([0, len(original)]) ax0.set_xlim([0, len(original)])
ax0.set_ylim([min(original), max(original)]) ax0.set_ylim([min(original)*0.9, max(original)*1.1])
ax0.set_title('Série Temporal') ax0.set_title('Forecasts')
ax0.set_ylabel('F(T)') ax0.set_ylabel('F(T)')
ax0.set_xlabel('T') ax0.set_xlabel('T')
ax0.plot(original, label="Original") min_rmse = 1000000.0
min_rmse = 100000.0
best = None best = None
pc = 0 pc = 0
for p in parameters: for p in partitions:
oc = 0 oc = 0
for o in orders: for o in orders:
sets = Grid.GridPartitionerTrimf(original, p) sets = Grid.GridPartitionerTrimf(original, p)
fts = hwang.HighOrderFTS(o, "k = " + str(p) + " w = " + str(o)) fts = model("q = " + str(p) + " n = " + str(o))
fts.train(original, sets) fts.train(original, sets, o)
forecasted = [fts.forecast(original, xx) for xx in range(o, len(original))] forecasted = fts.forecast(original)
error = Measures.rmse(np.array(forecasted), np.array(original[o:])) error = Measures.rmse(np.array(original[o:]),np.array(forecasted[:-1]))
mape = Measures.mape(np.array(original[o:]), np.array(forecasted[:-1]))
#print(original[o:])
#print(forecasted[-1])
for kk in range(o): for kk in range(o):
forecasted.insert(0, None) forecasted.insert(0, None)
ax0.plot(forecasted, label=fts.name) if plotforecasts: ax0.plot(forecasted, label=fts.name)
print(o, p, error) #print(o, p, mape)
errors[oc, pc] = error errors[oc, pc] = error
if error < min_rmse: if error < min_rmse:
min_rmse = error min_rmse = error
best = fts best = fts
forecasted_best = forecasted forecasted_best = forecasted
oc = oc + 1 oc += 1
pc = pc + 1 pc += 1
handles0, labels0 = ax0.get_legend_handles_labels() #print(min_rmse)
ax0.legend(handles0, labels0) if plotforecasts:
ax1 = Axes3D(fig, rect=[0.6, 0.5, 0.45, 0.45], elev=30, azim=144) #handles0, labels0 = ax0.get_legend_handles_labels()
#ax0.legend(handles0, labels0)
ax0.plot(original, label="Original", linewidth=3.0, color="black")
ax1 = Axes3D(fig, rect=[0, 1, 0.9, 0.45], 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('Comparação dos Erros Quadráticos Médios por tamanho da janela') ax1.set_title('Error Surface')
ax1.set_ylabel('RMSE') ax1.set_ylabel('Model order')
ax1.set_xlabel('Quantidade de Partições') ax1.set_xlabel('Number of partitions')
ax1.set_zlabel('W') ax1.set_zlabel('RMSE')
X, Y = np.meshgrid(parameters, orders) X, Y = np.meshgrid(partitions, orders)
surf = ax1.plot_surface(X, Y, errors, rstride=1, cstride=1, antialiased=True) surf = ax1.plot_surface(X, Y, errors, rstride=1, cstride=1, antialiased=True)
ret.append(best) ret.append(best)
ret.append(forecasted_best) ret.append(forecasted_best)
# Modelo diferencial Util.showAndSaveImage(fig,file,save)
print("\nSérie Diferencial")
errors = np.array([[0 for k in range(len(parameters))] for kk in range(len(orders))])
forecastedd_best = []
ax2 = fig.add_axes([0, 0, 0.6, 0.45]) # left, bottom, width, height
ax2.set_xlim([0, len(original)])
ax2.set_ylim([min(original), max(original)])
ax2.set_title('Série Temporal')
ax2.set_ylabel('F(T)')
ax2.set_xlabel('T')
ax2.plot(original, label="Original")
min_rmse = 100000.0
bestd = None
pc = 0
for p in parameters:
oc = 0
for o in orders:
sets = Grid.GridPartitionerTrimf(Transformations.differential(original), p)
fts = hwang.HighOrderFTS(o, "k = " + str(p) + " w = " + str(o))
fts.train(original, sets)
forecasted = [fts.forecastDiff(original, xx) for xx in range(o, len(original))]
error = Measures.rmse(np.array(forecasted), np.array(original[o:]))
for kk in range(o):
forecasted.insert(0, None)
ax2.plot(forecasted, label=fts.name)
print(o, p, error)
errors[oc, pc] = error
if error < min_rmse:
min_rmse = error
bestd = fts
forecastedd_best = forecasted
oc = oc + 1
pc = pc + 1
handles0, labels0 = ax2.get_legend_handles_labels()
ax2.legend(handles0, labels0)
ax3 = Axes3D(fig, rect=[0.6, 0.0, 0.45, 0.45], elev=30, azim=144)
# ax3 = fig.add_axes([0.6, 0.0, 0.45, 0.45], projection='3d')
ax3.set_title('Comparação dos Erros Quadráticos Médios')
ax3.set_ylabel('RMSE')
ax3.set_xlabel('Quantidade de Partições')
ax3.set_zlabel('W')
X, Y = np.meshgrid(parameters, orders)
surf = ax3.plot_surface(X, Y, errors, rstride=1, cstride=1, antialiased=True)
ret.append(bestd)
ret.append(forecastedd_best)
return ret return ret

2
fts.py
View File

@ -45,7 +45,7 @@ class FTS:
def forecastAheadDistribution(self, data, steps): def forecastAheadDistribution(self, data, steps):
pass pass
def train(self, data, sets): def train(self, data, sets, order=1):
pass pass
def getMidpoints(self, flrg): def getMidpoints(self, flrg):

View File

@ -9,21 +9,26 @@ from pyFTS.common import Membership, Util
def plotSets(data, sets, titles, tam=[12, 10], save=False, file=None): def plotSets(data, sets, titles, tam=[12, 10], save=False, file=None):
num = len(sets) num = len(sets)
fig = plt.figure(figsize=tam) #fig = plt.figure(figsize=tam)
maxx = max(data) maxx = max(data)
minx = min(data) minx = min(data)
h = 1/num #h = 1/num
for k in range(num): #print(h)
ax0 = fig.add_axes([0, (k+1)*h, 0.65, h*0.7]) # left, bottom, width, height fig, axes = plt.subplots(nrows=num, ncols=1,figsize=tam)
ax0.set_title(titles[k]) for k in np.arange(0,num):
ax0.set_ylim([0, 1]) #ax = fig.add_axes([0.05, 1-(k*h), 0.9, h*0.7]) # left, bottom, width, height
ax0.set_xlim([minx, maxx]) ax = axes[k]
ax.set_title(titles[k])
ax.set_ylim([0, 1])
ax.set_xlim([minx, maxx])
for s in sets[k]: for s in sets[k]:
if s.mf == Membership.trimf: if s.mf == Membership.trimf:
ax0.plot([s.parameters[0],s.parameters[1],s.parameters[2]],[0,1,0]) ax.plot([s.parameters[0],s.parameters[1],s.parameters[2]],[0,1,0])
elif s.mf == Membership.gaussmf: elif s.mf == Membership.gaussmf:
tmpx = [ kk for kk in np.arange(s.lower, s.upper)] tmpx = [ kk for kk in np.arange(s.lower, s.upper)]
tmpy = [s.membership(kk) for kk in np.arange(s.lower, s.upper)] tmpy = [s.membership(kk) for kk in np.arange(s.lower, s.upper)]
ax0.plot(tmpx, tmpy) ax.plot(tmpx, tmpy)
plt.tight_layout()
Util.showAndSaveImage(fig, file, save) Util.showAndSaveImage(fig, file, save)

View File

@ -37,8 +37,8 @@ class ProbabilisticFLRG(hofts.HighOrderFLRG):
class ProbabilisticFTS(ifts.IntervalFTS): class ProbabilisticFTS(ifts.IntervalFTS):
def __init__(self, name): def __init__(self, name):
super(ProbabilisticFTS, self).__init__("PIFTS") super(ProbabilisticFTS, self).__init__("PFTS")
self.shortname = "PIFTS " + name self.shortname = "PFTS " + name
self.name = "Probabilistic FTS" self.name = "Probabilistic FTS"
self.detail = "Silva, P.; Guimarães, F.; Sadaei, H." self.detail = "Silva, P.; Guimarães, F.; Sadaei, H."
self.flrgs = {} self.flrgs = {}