diff --git a/benchmarks/benchmarks.py b/benchmarks/benchmarks.py index 7d23254..1829fcd 100644 --- a/benchmarks/benchmarks.py +++ b/benchmarks/benchmarks.py @@ -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 -def allPointForecasters(data_train, data_test, partitions, max_order=2,save=False, file=None, tam=[20, 5]): - models = [chen.ConventionalFTS, yu.WeightedFTS, ismailefendi.ImprovedWeightedFTS, sadaei.ExponentialyWeightedFTS, - hwang.HighOrderFTS, hofts.HighOrderFTS, pfts.ProbabilisticFTS ] +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, 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) + count = 1 + + colors = [] + for model in models: - fts = model("") - if not fts.isHighOrder: - fts.train(data_train, data_train_fs) - objects.append(fts) + #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): - fts.train(data_train, data_train_fs, order=order) - fts.shortname += str(order) - objects.append(fts) + 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(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): - ret = "Model & RMSE & MAPE \\ \n" +def getPointStatistics(data, models, externalmodels = None, externalforecasts = None): + ret = "Model & Order & RMSE & MAPE \\\\ \n" for fts in models: - forecasts = fts.forecast(original) + forecasts = fts.forecast(data) ret += fts.shortname + " & " - ret += str(round(Measures.rmse(original[fts.order:], forecasts[:-1]), 2)) + " & " - ret += str(round(Measures.mape(original[fts.order:], forecasts[:-1]), 2)) + " & " - ret += " \\ \n" - l = len(externalmodels) - for k in np.arange(0,l): - ret += externalmodels[k] + " & " - ret += str(round(Measures.rmse(original[fts.order:], externalforecasts[k][:-1]), 2)) + " & " - ret += str(round(Measures.mape(original[fts.order:], externalforecasts[k][:-1]), 2)) + " & " - ret += " \\ \n" + ret += str(fts.order) + " & " + ret += str(round(Measures.rmse(np.array(data[fts.order:]), np.array(forecasts[:-1])), 2)) + " & " + 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) + for k in np.arange(0,l): + ret += externalmodels[k] + " & " + ret += " 1 & " + ret += str(round(Measures.rmse(data[fts.order:], externalforecasts[k][:-1]), 2)) + " & " + ret += str(round(Measures.mape(data[fts.order:], externalforecasts[k][:-1]), 2)) + ret += " \\\\ \n" 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): - ret = "Model & RMSE & MAPE & Sharpness & Resolution & Coverage \\ \n" + ret = "Model & Order & Sharpness & Resolution & Coverage \\ \n" for fts in models: forecasts = fts.forecastInterval(original) ret += fts.shortname + " & " - ret += str(round(Measures.rmse_interval(original[fts.order:], forecasts[:-1]), 2)) + " & " - ret += str(round(Measures.mape_interval(original[fts.order:], forecasts[:-1]), 2)) + " & " + ret += str(fts.order) + " & " ret += str(round(Measures.sharpness(forecasts), 2)) + " & " ret += str(round(Measures.resolution(forecasts), 2)) + " & " 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) count = 0 for fts in models: - if fts.hasPointForecasting: + if fts.hasPointForecasting and not intervals: forecasted = fts.forecast(original) mi.append(min(forecasted) * 0.95) ma.append(max(forecasted) * 1.05) @@ -107,8 +155,8 @@ def plotComparedSeries(original, models, colors, typeonlegend=False, save=False, upper.insert(0, None) lbl = fts.shortname if typeonlegend: lbl += " (Interval)" - ax.plot(lower, color=colors[count], label=lbl, ls="--") - ax.plot(upper, color=colors[count], ls="--") + ax.plot(lower, color=colors[count], label=lbl, ls="-") + ax.plot(upper, color=colors[count], ls="-") handles0, labels0 = ax.get_legend_handles_labels() ax.legend(handles0, labels0, loc=2) diff --git a/chen.py b/chen.py index 86ffbfd..60feedd 100644 --- a/chen.py +++ b/chen.py @@ -3,7 +3,7 @@ from pyFTS.common import FuzzySet, FLR from pyFTS import fts -class ConventionalFLRG: +class ConventionalFLRG(object): def __init__(self, LHS): self.LHS = LHS self.RHS = set() diff --git a/common/Transformations.py b/common/Transformations.py index 5bb1e12..ad435f2 100644 --- a/common/Transformations.py +++ b/common/Transformations.py @@ -6,7 +6,7 @@ from pyFTS import * def differential(original, lags=1): n = len(original) 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) diff --git a/fts.py b/fts.py index e8ee012..8c8998e 100644 --- a/fts.py +++ b/fts.py @@ -2,7 +2,7 @@ import numpy as np from pyFTS import * -class FTS: +class FTS(object): def __init__(self, order, name): self.sets = {} self.flrgs = {} diff --git a/hofts.py b/hofts.py index 7c5b144..f805ece 100644 --- a/hofts.py +++ b/hofts.py @@ -38,6 +38,7 @@ class HighOrderFTS(fts.FTS): def __init__(self, name): super(HighOrderFTS, self).__init__(1, "HOFTS" + name) self.name = "High Order FTS" + self.shortname = "HOFTS" + name self.detail = "Chen" self.order = 1 self.setsDict = {} @@ -80,7 +81,7 @@ class HighOrderFTS(fts.FTS): if l <= self.order: 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) tmpflrg = HighOrderFLRG(self.order) diff --git a/hwang.py b/hwang.py index db080e1..39248a6 100644 --- a/hwang.py +++ b/hwang.py @@ -4,9 +4,12 @@ from pyFTS import fts class HighOrderFTS(fts.FTS): - def __init__(self, order, name): - super(HighOrderFTS, self).__init__(order, name) + def __init__(self, name): + super(HighOrderFTS, self).__init__(1, name) self.isHighOrder = True + self.name = "Hwang High Order FTS" + self.shortname = "Hwang" + name + self.detail = "Hwang" def forecast(self, data): cn = np.array([0.0 for k in range(len(self.sets))]) @@ -16,7 +19,7 @@ class HighOrderFTS(fts.FTS): 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)): cn[s] = self.sets[s].membership(data[t]) @@ -30,11 +33,11 @@ class HighOrderFTS(fts.FTS): for s in range(len(self.sets)): if ft[s] == mft: out = out + self.sets[s].centroid - count = count + 1.0 + count += 1.0 ret.append(out / count) return ret - def train(self, data, sets, order=2, parameters=None): + def train(self, data, sets, order=1, parameters=None): self.sets = sets self.order = order \ No newline at end of file diff --git a/ifts.py b/ifts.py index 32800c3..1c5e5c7 100644 --- a/ifts.py +++ b/ifts.py @@ -47,7 +47,7 @@ class IntervalFTS(hofts.HighOrderFTS): for child in node.getChildren(): self.buildTree(child, lags, level + 1) - def forecast(self, data): + def forecastInterval(self, data): ndata = np.array(data) diff --git a/pfts.py b/pfts.py index 1826e74..609efa4 100644 --- a/pfts.py +++ b/pfts.py @@ -5,11 +5,10 @@ import numpy as np import pandas as pd import math from operator import itemgetter -from pyFTS.common import FuzzySet, FLR, SortedCollection +from pyFTS.common import FuzzySet, SortedCollection from pyFTS import hofts, ifts, tree - class ProbabilisticFLRG(hofts.HighOrderFLRG): def __init__(self, order): super(ProbabilisticFLRG, self).__init__(order) diff --git a/sadaei.py b/sadaei.py index 1bd75de..b01a77c 100644 --- a/sadaei.py +++ b/sadaei.py @@ -16,7 +16,7 @@ class ExponentialyWeightedFLRG: def weights(self): wei = [self.c ** k for k in np.arange(0.0, self.count, 1.0)] tot = sum(wei) - return np.iarray([k / tot for k in wei]) + return np.array([k / tot for k in wei]) def __str__(self): tmp = self.LHS.name + " -> " @@ -54,7 +54,7 @@ class ExponentialyWeightedFTS(fts.FTS): self.sets = sets tmpdata = FuzzySet.fuzzySeries(data, sets) flrs = FLR.generateRecurrentFLRs(tmpdata) - self.flrgs = self.generateFLRG(flrs, c) + self.flrgs = self.generateFLRG(flrs, self.c) def forecast(self, data): l = 1 diff --git a/tests/pfts.py b/tests/pfts.py index 38d107e..69c50d7 100644 --- a/tests/pfts.py +++ b/tests/pfts.py @@ -11,28 +11,26 @@ from mpl_toolkits.mplot3d import Axes3D import pandas as pd from pyFTS.partitioners import Grid from pyFTS.common import FLR,FuzzySet,Membership -from pyFTS import fts -from pyFTS import hofts -from pyFTS import ifts -from pyFTS import pfts -from pyFTS import tree +from pyFTS import fts,hofts,ifts,pfts,tree, chen from pyFTS.benchmarks import benchmarks as bchmk os.chdir("/home/petronio/dados/Dropbox/Doutorado/Disciplinas/AdvancedFuzzyTimeSeriesModels/") -enrollments = pd.read_csv("DataSets/Enrollments.csv", sep=";") -enrollments = np.array(enrollments["Enrollments"]) +#enrollments = pd.read_csv("DataSets/Enrollments.csv", sep=";") +#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.train(enrollments,enrollments_fs1,1) -pfts1_enrollments.shortname = "1st Order" -pfts2_enrollments = pfts.ProbabilisticFTS("2") -pfts2_enrollments.dump = False -pfts2_enrollments.shortname = "2nd Order" -pfts2_enrollments.train(enrollments,enrollments_fs1,2) +#pfts1_enrollments.train(enrollments,enrollments_fs1,1) +#pfts1_enrollments.shortname = "1st Order" +#pfts2_enrollments = pfts.ProbabilisticFTS("2") +#pfts2_enrollments.dump = False +#pfts2_enrollments.shortname = "2nd Order" +#pfts2_enrollments.train(enrollments,enrollments_fs1,2) -pfts1_enrollments.forecastAheadDistribution2(enrollments[:15],5,100) +#pfts1_enrollments.forecastAheadDistribution2(enrollments[:15],5,100)