Refactoring to allow more tasks automotion

This commit is contained in:
Petrônio Cândido de Lima e Silva 2017-01-23 11:00:27 -02:00
parent 30ffb89465
commit b909bdac5d
12 changed files with 100 additions and 46 deletions

View File

@ -11,19 +11,58 @@ from mpl_toolkits.mplot3d import Axes3D
from pyFTS.benchmarks import Measures
from pyFTS.partitioners import Grid
from pyFTS.common import Membership, FuzzySet, FLR, Transformations, Util
from pyFTS import pfts
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 ]
objects = []
data_train_fs = Grid.GridPartitionerTrimf(data_train,partitions)
for model in models:
fts = model("")
if not fts.isHighOrder:
fts.train(data_train, data_train_fs)
objects.append(fts)
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)
print(getPointStatistics(data_test, objects))
def getPointStatistics(original, models, externalmodels = None, externalforecasts = None):
ret = "Model & RMSE & MAPE \\ \n"
for fts in models:
forecasts = fts.forecast(original)
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"
return ret
def getIntervalStatistics(original, models):
ret = "Model & RMSE & MAPE & Sharpness & Resolution & Coverage \\ \n"
for fts in models:
forecasts = fts.forecast(original)
ret = ret + fts.shortname + " & "
ret = ret + str(round(Measures.rmse_interval(original[fts.order - 1:], forecasts), 2)) + " & "
ret = ret + str(round(Measures.mape_interval(original[fts.order - 1:], forecasts), 2)) + " & "
ret = ret + str(round(Measures.sharpness(forecasts), 2)) + " & "
ret = ret + str(round(Measures.resolution(forecasts), 2)) + " & "
ret = ret + str(round(Measures.coverage(original[fts.order - 1:], forecasts), 2)) + " \\ \n"
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(round(Measures.sharpness(forecasts), 2)) + " & "
ret += str(round(Measures.resolution(forecasts), 2)) + " & "
ret += str(round(Measures.coverage(original[fts.order:], forecasts[:-1]), 2)) + " \\ \n"
return ret

View File

@ -38,7 +38,7 @@ class ConventionalFTS(fts.FTS):
flrgs[flr.LHS.name].append(flr.RHS)
return (flrgs)
def train(self, data, sets):
def train(self, data, sets,order=1,parameters=None):
self.sets = sets
tmpdata = FuzzySet.fuzzySeries(data, sets)
flrs = FLR.generateNonRecurrentFLRs(tmpdata)

View File

@ -3,10 +3,10 @@ import math
from pyFTS import *
def differential(original):
def differential(original, lags=1):
n = len(original)
diff = [original[t - 1] - original[t] for t in np.arange(1, n)]
diff.insert(0, 0)
diff = [original[t - lags] - original[t] for t in np.arange(lags, n)]
for t in np.arange(0, lags): diff.insert(0, None)
return np.array(diff)
@ -24,3 +24,12 @@ def Z(original):
sigma = np.std(original)
z = [(k - mu)/sigma for k in original]
return z
# retrieved from Sadaei and Lee (2014) - Multilayer Stock ForecastingModel Using Fuzzy Time Series
def roi(original):
n = len(original)
roi = []
for t in np.arange(0, n-1):
roi.append( (original[t+1] - original[t])/original[t] )
return roi

3
fts.py
View File

@ -10,6 +10,7 @@ class FTS:
self.shortname = name
self.name = name
self.detail = name
self.isHighOrder = False
self.hasSeasonality = False
self.hasPointForecasting = True
self.hasIntervalForecasting = False
@ -45,7 +46,7 @@ class FTS:
def forecastAheadDistribution(self, data, steps):
pass
def train(self, data, sets, order=1):
def train(self, data, sets,order=1, parameters=None):
pass
def getMidpoints(self, flrg):

View File

@ -41,6 +41,7 @@ class HighOrderFTS(fts.FTS):
self.detail = "Chen"
self.order = 1
self.setsDict = {}
self.isHighOrder = True
def generateFLRG(self, flrs):
flrgs = {}
@ -58,7 +59,7 @@ class HighOrderFTS(fts.FTS):
flrgs[flrg.strLHS()].appendRHS(flrs[k].RHS)
return (flrgs)
def train(self, data, sets, order):
def train(self, data, sets, order=1,parameters=None):
self.order = order
self.sets = sets
for s in self.sets: self.setsDict[s.name] = s

View File

@ -6,13 +6,18 @@ from pyFTS import fts
class HighOrderFTS(fts.FTS):
def __init__(self, order, name):
super(HighOrderFTS, self).__init__(order, name)
self.isHighOrder = True
def forecast(self, data, t):
def forecast(self, data):
cn = np.array([0.0 for k in range(len(self.sets))])
ow = np.array([[0.0 for k in range(len(self.sets))] for z in range(self.order - 1)])
rn = np.array([[0.0 for k in range(len(self.sets))] for z in range(self.order - 1)])
ft = np.array([0.0 for k in range(len(self.sets))])
ret = []
for t in np.arange(self.order, len(data)):
for s in range(len(self.sets)):
cn[s] = self.sets[s].membership(data[t])
for w in range(self.order - 1):
@ -26,13 +31,10 @@ class HighOrderFTS(fts.FTS):
if ft[s] == mft:
out = out + self.sets[s].centroid
count = count + 1.0
return out / count
ret.append(out / count)
def train(self, data, sets):
return ret
def train(self, data, sets, order=2, parameters=None):
self.sets = sets
def predict(self, data, t):
return self.forecast(data, t)
def predictDiff(self, data, t):
return data[t] + self.forecast(Transformations.differential(data), t)
self.order = order

View File

@ -15,6 +15,7 @@ class IntervalFTS(hofts.HighOrderFTS):
self.flrgs = {}
self.hasPointForecasting = False
self.hasIntervalForecasting = True
self.isHighOrder = True
def getUpper(self, flrg):
if flrg.strLHS() in self.flrgs:

View File

@ -1,6 +1,6 @@
import numpy as np
from pyFTS.common import FuzzySet,FLR
import fts
from pyFTS import fts
class ImprovedWeightedFLRG:
@ -46,7 +46,7 @@ class ImprovedWeightedFTS(fts.FTS):
flrgs[flr.LHS.name].append(flr.RHS)
return (flrgs)
def train(self, data, sets):
def train(self, data, sets,order=1,parameters=None):
self.sets = sets
for s in self.sets: self.setsDict[s.name] = s

View File

@ -46,6 +46,7 @@ class ProbabilisticFTS(ifts.IntervalFTS):
self.hasPointForecasting = True
self.hasIntervalForecasting = True
self.hasDistributionForecasting = True
self.isHighOrder = True
def generateFLRG(self, flrs):
flrgs = {}

View File

@ -1,6 +1,6 @@
import numpy as np
from pyFTS.common import FuzzySet,FLR
import fts
from pyFTS import fts
class ExponentialyWeightedFLRG:
def __init__(self, LHS, c):
@ -49,8 +49,8 @@ class ExponentialyWeightedFTS(fts.FTS):
flrgs[flr.LHS.name].append(flr.RHS)
return (flrgs)
def train(self, data, sets, c):
self.c = c
def train(self, data, sets,order=1,parameters=2):
self.c = parameters
self.sets = sets
tmpdata = FuzzySet.fuzzySeries(data, sets)
flrs = FLR.generateRecurrentFLRs(tmpdata)

View File

@ -44,9 +44,9 @@ class SeasonalFTS(fts.FTS):
return (flrgs)
def train(self, data, sets, seasonality):
def train(self, data, sets, order=1,parameters=12):
self.sets = sets
self.seasonality = seasonality
self.seasonality = parameters
tmpdata = FuzzySet.fuzzySeries(data, sets)
flrs = FLR.generateRecurrentFLRs(tmpdata)
self.flrgs = self.generateFLRG(flrs)

4
yu.py
View File

@ -1,6 +1,6 @@
import numpy as np
from pyFTS.common import FuzzySet,FLR
import fts
from pyFTS import fts
class WeightedFLRG(fts.FTS):
@ -46,7 +46,7 @@ class WeightedFTS(fts.FTS):
flrgs[flr.LHS.name].append(flr.RHS)
return (flrgs)
def train(self, data, sets):
def train(self, data, sets,order=1,parameters=None):
self.sets = sets
tmpdata = FuzzySet.fuzzySeries(data, sets)
flrs = FLR.generateRecurrentFLRs(tmpdata)