Several bugfixes in models and benchmarks
This commit is contained in:
parent
b909bdac5d
commit
f53eded47e
@ -14,52 +14,100 @@ from pyFTS.common import Membership, FuzzySet, FLR, Transformations, Util
|
|||||||
from pyFTS import fts, chen, yu, ismailefendi, sadaei, hofts, hwang, pfts, ifts
|
from pyFTS import fts, chen, yu, ismailefendi, sadaei, hofts, hwang, pfts, ifts
|
||||||
|
|
||||||
|
|
||||||
def allPointForecasters(data_train, data_test, partitions, max_order=2,save=False, file=None, tam=[20, 5]):
|
def allPointForecasters(data_train, data_test, partitions, max_order=3,save=False, file=None, tam=[20, 5]):
|
||||||
models = [chen.ConventionalFTS, yu.WeightedFTS, ismailefendi.ImprovedWeightedFTS, sadaei.ExponentialyWeightedFTS,
|
models = [chen.ConventionalFTS, yu.WeightedFTS, ismailefendi.ImprovedWeightedFTS,
|
||||||
hwang.HighOrderFTS, hofts.HighOrderFTS, pfts.ProbabilisticFTS ]
|
sadaei.ExponentialyWeightedFTS, hwang.HighOrderFTS, hofts.HighOrderFTS,
|
||||||
|
pfts.ProbabilisticFTS]
|
||||||
|
|
||||||
objects = []
|
objs = []
|
||||||
|
|
||||||
|
all_colors = [clr for clr in pltcolors.cnames.keys() ]
|
||||||
|
|
||||||
data_train_fs = Grid.GridPartitionerTrimf(data_train,partitions)
|
data_train_fs = Grid.GridPartitionerTrimf(data_train,partitions)
|
||||||
|
|
||||||
|
count = 1
|
||||||
|
|
||||||
|
colors = []
|
||||||
|
|
||||||
for model in models:
|
for model in models:
|
||||||
fts = model("")
|
#print(model)
|
||||||
if not fts.isHighOrder:
|
mfts = model("")
|
||||||
fts.train(data_train, data_train_fs)
|
if not mfts.isHighOrder:
|
||||||
objects.append(fts)
|
mfts.train(data_train, data_train_fs)
|
||||||
|
objs.append(mfts)
|
||||||
|
colors.append( all_colors[count] )
|
||||||
else:
|
else:
|
||||||
for order in np.arange(1,max_order+1):
|
for order in np.arange(1,max_order+1):
|
||||||
fts.train(data_train, data_train_fs, order=order)
|
mfts = model(" n = " + str(order))
|
||||||
fts.shortname += str(order)
|
mfts.train(data_train, data_train_fs, order=order)
|
||||||
objects.append(fts)
|
objs.append(mfts)
|
||||||
|
colors.append(all_colors[count])
|
||||||
|
count += 5
|
||||||
|
|
||||||
print(getPointStatistics(data_test, objects))
|
print(getPointStatistics(data_test, objs))
|
||||||
|
|
||||||
|
plotComparedSeries(data_test, objs, colors, typeonlegend=False, save=save, file=file, tam=tam, intervals=False)
|
||||||
|
|
||||||
|
|
||||||
def getPointStatistics(original, models, externalmodels = None, externalforecasts = None):
|
def getPointStatistics(data, models, externalmodels = None, externalforecasts = None):
|
||||||
ret = "Model & RMSE & MAPE \\ \n"
|
ret = "Model & Order & RMSE & MAPE \\\\ \n"
|
||||||
for fts in models:
|
for fts in models:
|
||||||
forecasts = fts.forecast(original)
|
forecasts = fts.forecast(data)
|
||||||
ret += fts.shortname + " & "
|
ret += fts.shortname + " & "
|
||||||
ret += str(round(Measures.rmse(original[fts.order:], forecasts[:-1]), 2)) + " & "
|
ret += str(fts.order) + " & "
|
||||||
ret += str(round(Measures.mape(original[fts.order:], forecasts[:-1]), 2)) + " & "
|
ret += str(round(Measures.rmse(np.array(data[fts.order:]), np.array(forecasts[:-1])), 2)) + " & "
|
||||||
ret += " \\ \n"
|
ret += str(round(Measures.mape(np.array(data[fts.order:]), np.array(forecasts[:-1])), 2))
|
||||||
|
ret += " \\\\ \n"
|
||||||
|
if externalmodels is not None:
|
||||||
l = len(externalmodels)
|
l = len(externalmodels)
|
||||||
for k in np.arange(0,l):
|
for k in np.arange(0,l):
|
||||||
ret += externalmodels[k] + " & "
|
ret += externalmodels[k] + " & "
|
||||||
ret += str(round(Measures.rmse(original[fts.order:], externalforecasts[k][:-1]), 2)) + " & "
|
ret += " 1 & "
|
||||||
ret += str(round(Measures.mape(original[fts.order:], externalforecasts[k][:-1]), 2)) + " & "
|
ret += str(round(Measures.rmse(data[fts.order:], externalforecasts[k][:-1]), 2)) + " & "
|
||||||
ret += " \\ \n"
|
ret += str(round(Measures.mape(data[fts.order:], externalforecasts[k][:-1]), 2))
|
||||||
|
ret += " \\\\ \n"
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
|
|
||||||
|
def allIntervalForecasters(data_train, data_test, partitions, max_order=3,save=False, file=None, tam=[20, 5]):
|
||||||
|
models = [ifts.IntervalFTS, pfts.ProbabilisticFTS]
|
||||||
|
|
||||||
|
objs = []
|
||||||
|
|
||||||
|
all_colors = [clr for clr in pltcolors.cnames.keys() ]
|
||||||
|
|
||||||
|
data_train_fs = Grid.GridPartitionerTrimf(data_train,partitions)
|
||||||
|
|
||||||
|
count = 1
|
||||||
|
|
||||||
|
colors = []
|
||||||
|
|
||||||
|
for model in models:
|
||||||
|
#print(model)
|
||||||
|
mfts = model("")
|
||||||
|
if not mfts.isHighOrder:
|
||||||
|
mfts.train(data_train, data_train_fs)
|
||||||
|
objs.append(mfts)
|
||||||
|
colors.append( all_colors[count] )
|
||||||
|
else:
|
||||||
|
for order in np.arange(1,max_order+1):
|
||||||
|
mfts = model(" n = " + str(order))
|
||||||
|
mfts.train(data_train, data_train_fs, order=order)
|
||||||
|
objs.append(mfts)
|
||||||
|
colors.append(all_colors[count])
|
||||||
|
count += 5
|
||||||
|
|
||||||
|
print(getIntervalStatistics(data_test, objs))
|
||||||
|
|
||||||
|
plotComparedSeries(data_test, objs, colors, typeonlegend=False, save=save, file=file, tam=tam, intervals=True)
|
||||||
|
|
||||||
|
|
||||||
def getIntervalStatistics(original, models):
|
def getIntervalStatistics(original, models):
|
||||||
ret = "Model & RMSE & MAPE & Sharpness & Resolution & Coverage \\ \n"
|
ret = "Model & Order & Sharpness & Resolution & Coverage \\ \n"
|
||||||
for fts in models:
|
for fts in models:
|
||||||
forecasts = fts.forecastInterval(original)
|
forecasts = fts.forecastInterval(original)
|
||||||
ret += fts.shortname + " & "
|
ret += fts.shortname + " & "
|
||||||
ret += str(round(Measures.rmse_interval(original[fts.order:], forecasts[:-1]), 2)) + " & "
|
ret += str(fts.order) + " & "
|
||||||
ret += str(round(Measures.mape_interval(original[fts.order:], forecasts[:-1]), 2)) + " & "
|
|
||||||
ret += str(round(Measures.sharpness(forecasts), 2)) + " & "
|
ret += str(round(Measures.sharpness(forecasts), 2)) + " & "
|
||||||
ret += str(round(Measures.resolution(forecasts), 2)) + " & "
|
ret += str(round(Measures.resolution(forecasts), 2)) + " & "
|
||||||
ret += str(round(Measures.coverage(original[fts.order:], forecasts[:-1]), 2)) + " \\ \n"
|
ret += str(round(Measures.coverage(original[fts.order:], forecasts[:-1]), 2)) + " \\ \n"
|
||||||
@ -86,7 +134,7 @@ def plotComparedSeries(original, models, colors, typeonlegend=False, save=False,
|
|||||||
ax.plot(original, color='black', label="Original", linewidth=1.5)
|
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 and not intervals:
|
||||||
forecasted = fts.forecast(original)
|
forecasted = fts.forecast(original)
|
||||||
mi.append(min(forecasted) * 0.95)
|
mi.append(min(forecasted) * 0.95)
|
||||||
ma.append(max(forecasted) * 1.05)
|
ma.append(max(forecasted) * 1.05)
|
||||||
@ -107,8 +155,8 @@ def plotComparedSeries(original, models, colors, typeonlegend=False, save=False,
|
|||||||
upper.insert(0, None)
|
upper.insert(0, None)
|
||||||
lbl = fts.shortname
|
lbl = fts.shortname
|
||||||
if typeonlegend: lbl += " (Interval)"
|
if typeonlegend: lbl += " (Interval)"
|
||||||
ax.plot(lower, color=colors[count], label=lbl, ls="--")
|
ax.plot(lower, color=colors[count], label=lbl, ls="-")
|
||||||
ax.plot(upper, color=colors[count], ls="--")
|
ax.plot(upper, color=colors[count], ls="-")
|
||||||
|
|
||||||
handles0, labels0 = ax.get_legend_handles_labels()
|
handles0, labels0 = ax.get_legend_handles_labels()
|
||||||
ax.legend(handles0, labels0, loc=2)
|
ax.legend(handles0, labels0, loc=2)
|
||||||
|
2
chen.py
2
chen.py
@ -3,7 +3,7 @@ from pyFTS.common import FuzzySet, FLR
|
|||||||
from pyFTS import fts
|
from pyFTS import fts
|
||||||
|
|
||||||
|
|
||||||
class ConventionalFLRG:
|
class ConventionalFLRG(object):
|
||||||
def __init__(self, LHS):
|
def __init__(self, LHS):
|
||||||
self.LHS = LHS
|
self.LHS = LHS
|
||||||
self.RHS = set()
|
self.RHS = set()
|
||||||
|
@ -6,7 +6,7 @@ from pyFTS import *
|
|||||||
def differential(original, lags=1):
|
def differential(original, lags=1):
|
||||||
n = len(original)
|
n = len(original)
|
||||||
diff = [original[t - lags] - original[t] for t in np.arange(lags, n)]
|
diff = [original[t - lags] - original[t] for t in np.arange(lags, n)]
|
||||||
for t in np.arange(0, lags): diff.insert(0, None)
|
for t in np.arange(0, lags): diff.insert(0, 0)
|
||||||
return np.array(diff)
|
return np.array(diff)
|
||||||
|
|
||||||
|
|
||||||
|
2
fts.py
2
fts.py
@ -2,7 +2,7 @@ import numpy as np
|
|||||||
from pyFTS import *
|
from pyFTS import *
|
||||||
|
|
||||||
|
|
||||||
class FTS:
|
class FTS(object):
|
||||||
def __init__(self, order, name):
|
def __init__(self, order, name):
|
||||||
self.sets = {}
|
self.sets = {}
|
||||||
self.flrgs = {}
|
self.flrgs = {}
|
||||||
|
3
hofts.py
3
hofts.py
@ -38,6 +38,7 @@ class HighOrderFTS(fts.FTS):
|
|||||||
def __init__(self, name):
|
def __init__(self, name):
|
||||||
super(HighOrderFTS, self).__init__(1, "HOFTS" + name)
|
super(HighOrderFTS, self).__init__(1, "HOFTS" + name)
|
||||||
self.name = "High Order FTS"
|
self.name = "High Order FTS"
|
||||||
|
self.shortname = "HOFTS" + name
|
||||||
self.detail = "Chen"
|
self.detail = "Chen"
|
||||||
self.order = 1
|
self.order = 1
|
||||||
self.setsDict = {}
|
self.setsDict = {}
|
||||||
@ -80,7 +81,7 @@ class HighOrderFTS(fts.FTS):
|
|||||||
if l <= self.order:
|
if l <= self.order:
|
||||||
return data
|
return data
|
||||||
|
|
||||||
for k in np.arange(self.order, l):
|
for k in np.arange(self.order, l+1):
|
||||||
tmpdata = FuzzySet.fuzzySeries(data[k - self.order: k], self.sets)
|
tmpdata = FuzzySet.fuzzySeries(data[k - self.order: k], self.sets)
|
||||||
tmpflrg = HighOrderFLRG(self.order)
|
tmpflrg = HighOrderFLRG(self.order)
|
||||||
|
|
||||||
|
13
hwang.py
13
hwang.py
@ -4,9 +4,12 @@ from pyFTS import fts
|
|||||||
|
|
||||||
|
|
||||||
class HighOrderFTS(fts.FTS):
|
class HighOrderFTS(fts.FTS):
|
||||||
def __init__(self, order, name):
|
def __init__(self, name):
|
||||||
super(HighOrderFTS, self).__init__(order, name)
|
super(HighOrderFTS, self).__init__(1, name)
|
||||||
self.isHighOrder = True
|
self.isHighOrder = True
|
||||||
|
self.name = "Hwang High Order FTS"
|
||||||
|
self.shortname = "Hwang" + name
|
||||||
|
self.detail = "Hwang"
|
||||||
|
|
||||||
def forecast(self, data):
|
def forecast(self, data):
|
||||||
cn = np.array([0.0 for k in range(len(self.sets))])
|
cn = np.array([0.0 for k in range(len(self.sets))])
|
||||||
@ -16,7 +19,7 @@ class HighOrderFTS(fts.FTS):
|
|||||||
|
|
||||||
ret = []
|
ret = []
|
||||||
|
|
||||||
for t in np.arange(self.order, len(data)):
|
for t in np.arange(self.order-1, len(data)):
|
||||||
|
|
||||||
for s in range(len(self.sets)):
|
for s in range(len(self.sets)):
|
||||||
cn[s] = self.sets[s].membership(data[t])
|
cn[s] = self.sets[s].membership(data[t])
|
||||||
@ -30,11 +33,11 @@ class HighOrderFTS(fts.FTS):
|
|||||||
for s in range(len(self.sets)):
|
for s in range(len(self.sets)):
|
||||||
if ft[s] == mft:
|
if ft[s] == mft:
|
||||||
out = out + self.sets[s].centroid
|
out = out + self.sets[s].centroid
|
||||||
count = count + 1.0
|
count += 1.0
|
||||||
ret.append(out / count)
|
ret.append(out / count)
|
||||||
|
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
def train(self, data, sets, order=2, parameters=None):
|
def train(self, data, sets, order=1, parameters=None):
|
||||||
self.sets = sets
|
self.sets = sets
|
||||||
self.order = order
|
self.order = order
|
2
ifts.py
2
ifts.py
@ -47,7 +47,7 @@ class IntervalFTS(hofts.HighOrderFTS):
|
|||||||
for child in node.getChildren():
|
for child in node.getChildren():
|
||||||
self.buildTree(child, lags, level + 1)
|
self.buildTree(child, lags, level + 1)
|
||||||
|
|
||||||
def forecast(self, data):
|
def forecastInterval(self, data):
|
||||||
|
|
||||||
ndata = np.array(data)
|
ndata = np.array(data)
|
||||||
|
|
||||||
|
3
pfts.py
3
pfts.py
@ -5,11 +5,10 @@ import numpy as np
|
|||||||
import pandas as pd
|
import pandas as pd
|
||||||
import math
|
import math
|
||||||
from operator import itemgetter
|
from operator import itemgetter
|
||||||
from pyFTS.common import FuzzySet, FLR, SortedCollection
|
from pyFTS.common import FuzzySet, SortedCollection
|
||||||
from pyFTS import hofts, ifts, tree
|
from pyFTS import hofts, ifts, tree
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class ProbabilisticFLRG(hofts.HighOrderFLRG):
|
class ProbabilisticFLRG(hofts.HighOrderFLRG):
|
||||||
def __init__(self, order):
|
def __init__(self, order):
|
||||||
super(ProbabilisticFLRG, self).__init__(order)
|
super(ProbabilisticFLRG, self).__init__(order)
|
||||||
|
@ -16,7 +16,7 @@ class ExponentialyWeightedFLRG:
|
|||||||
def weights(self):
|
def weights(self):
|
||||||
wei = [self.c ** k for k in np.arange(0.0, self.count, 1.0)]
|
wei = [self.c ** k for k in np.arange(0.0, self.count, 1.0)]
|
||||||
tot = sum(wei)
|
tot = sum(wei)
|
||||||
return np.iarray([k / tot for k in wei])
|
return np.array([k / tot for k in wei])
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
tmp = self.LHS.name + " -> "
|
tmp = self.LHS.name + " -> "
|
||||||
@ -54,7 +54,7 @@ class ExponentialyWeightedFTS(fts.FTS):
|
|||||||
self.sets = sets
|
self.sets = sets
|
||||||
tmpdata = FuzzySet.fuzzySeries(data, sets)
|
tmpdata = FuzzySet.fuzzySeries(data, sets)
|
||||||
flrs = FLR.generateRecurrentFLRs(tmpdata)
|
flrs = FLR.generateRecurrentFLRs(tmpdata)
|
||||||
self.flrgs = self.generateFLRG(flrs, c)
|
self.flrgs = self.generateFLRG(flrs, self.c)
|
||||||
|
|
||||||
def forecast(self, data):
|
def forecast(self, data):
|
||||||
l = 1
|
l = 1
|
||||||
|
@ -11,28 +11,26 @@ from mpl_toolkits.mplot3d import Axes3D
|
|||||||
import pandas as pd
|
import pandas as pd
|
||||||
from pyFTS.partitioners import Grid
|
from pyFTS.partitioners import Grid
|
||||||
from pyFTS.common import FLR,FuzzySet,Membership
|
from pyFTS.common import FLR,FuzzySet,Membership
|
||||||
from pyFTS import fts
|
from pyFTS import fts,hofts,ifts,pfts,tree, chen
|
||||||
from pyFTS import hofts
|
|
||||||
from pyFTS import ifts
|
|
||||||
from pyFTS import pfts
|
|
||||||
from pyFTS import tree
|
|
||||||
from pyFTS.benchmarks import benchmarks as bchmk
|
from pyFTS.benchmarks import benchmarks as bchmk
|
||||||
|
|
||||||
|
|
||||||
os.chdir("/home/petronio/dados/Dropbox/Doutorado/Disciplinas/AdvancedFuzzyTimeSeriesModels/")
|
os.chdir("/home/petronio/dados/Dropbox/Doutorado/Disciplinas/AdvancedFuzzyTimeSeriesModels/")
|
||||||
|
|
||||||
enrollments = pd.read_csv("DataSets/Enrollments.csv", sep=";")
|
#enrollments = pd.read_csv("DataSets/Enrollments.csv", sep=";")
|
||||||
enrollments = np.array(enrollments["Enrollments"])
|
#enrollments = np.array(enrollments["Enrollments"])
|
||||||
|
|
||||||
enrollments_fs1 = Grid.GridPartitionerTrimf(enrollments,6)
|
#enrollments_fs1 = Grid.GridPartitionerTrimf(enrollments,6)
|
||||||
|
|
||||||
|
#tmp = chen.ConventionalFTS("")
|
||||||
|
|
||||||
pfts1_enrollments = pfts.ProbabilisticFTS("1")
|
pfts1_enrollments = pfts.ProbabilisticFTS("1")
|
||||||
pfts1_enrollments.train(enrollments,enrollments_fs1,1)
|
#pfts1_enrollments.train(enrollments,enrollments_fs1,1)
|
||||||
pfts1_enrollments.shortname = "1st Order"
|
#pfts1_enrollments.shortname = "1st Order"
|
||||||
pfts2_enrollments = pfts.ProbabilisticFTS("2")
|
#pfts2_enrollments = pfts.ProbabilisticFTS("2")
|
||||||
pfts2_enrollments.dump = False
|
#pfts2_enrollments.dump = False
|
||||||
pfts2_enrollments.shortname = "2nd Order"
|
#pfts2_enrollments.shortname = "2nd Order"
|
||||||
pfts2_enrollments.train(enrollments,enrollments_fs1,2)
|
#pfts2_enrollments.train(enrollments,enrollments_fs1,2)
|
||||||
|
|
||||||
|
|
||||||
pfts1_enrollments.forecastAheadDistribution2(enrollments[:15],5,100)
|
#pfts1_enrollments.forecastAheadDistribution2(enrollments[:15],5,100)
|
||||||
|
Loading…
Reference in New Issue
Block a user