Refactoring to allow more tasks automotion
This commit is contained in:
parent
30ffb89465
commit
b909bdac5d
@ -11,19 +11,58 @@ 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
|
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):
|
def getIntervalStatistics(original, models):
|
||||||
ret = "Model & RMSE & MAPE & Sharpness & Resolution & Coverage \\ \n"
|
ret = "Model & RMSE & MAPE & Sharpness & Resolution & Coverage \\ \n"
|
||||||
for fts in models:
|
for fts in models:
|
||||||
forecasts = fts.forecast(original)
|
forecasts = fts.forecastInterval(original)
|
||||||
ret = ret + fts.shortname + " & "
|
ret += fts.shortname + " & "
|
||||||
ret = ret + str(round(Measures.rmse_interval(original[fts.order - 1:], forecasts), 2)) + " & "
|
ret += str(round(Measures.rmse_interval(original[fts.order:], forecasts[:-1]), 2)) + " & "
|
||||||
ret = ret + str(round(Measures.mape_interval(original[fts.order - 1:], forecasts), 2)) + " & "
|
ret += str(round(Measures.mape_interval(original[fts.order:], forecasts[:-1]), 2)) + " & "
|
||||||
ret = ret + str(round(Measures.sharpness(forecasts), 2)) + " & "
|
ret += str(round(Measures.sharpness(forecasts), 2)) + " & "
|
||||||
ret = ret + str(round(Measures.resolution(forecasts), 2)) + " & "
|
ret += str(round(Measures.resolution(forecasts), 2)) + " & "
|
||||||
ret = ret + str(round(Measures.coverage(original[fts.order - 1:], forecasts), 2)) + " \\ \n"
|
ret += str(round(Measures.coverage(original[fts.order:], forecasts[:-1]), 2)) + " \\ \n"
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
|
|
||||||
|
2
chen.py
2
chen.py
@ -38,7 +38,7 @@ class ConventionalFTS(fts.FTS):
|
|||||||
flrgs[flr.LHS.name].append(flr.RHS)
|
flrgs[flr.LHS.name].append(flr.RHS)
|
||||||
return (flrgs)
|
return (flrgs)
|
||||||
|
|
||||||
def train(self, data, sets):
|
def train(self, data, sets,order=1,parameters=None):
|
||||||
self.sets = sets
|
self.sets = sets
|
||||||
tmpdata = FuzzySet.fuzzySeries(data, sets)
|
tmpdata = FuzzySet.fuzzySeries(data, sets)
|
||||||
flrs = FLR.generateNonRecurrentFLRs(tmpdata)
|
flrs = FLR.generateNonRecurrentFLRs(tmpdata)
|
||||||
|
@ -3,10 +3,10 @@ import math
|
|||||||
from pyFTS import *
|
from pyFTS import *
|
||||||
|
|
||||||
|
|
||||||
def differential(original):
|
def differential(original, lags=1):
|
||||||
n = len(original)
|
n = len(original)
|
||||||
diff = [original[t - 1] - original[t] for t in np.arange(1, n)]
|
diff = [original[t - lags] - original[t] for t in np.arange(lags, n)]
|
||||||
diff.insert(0, 0)
|
for t in np.arange(0, lags): diff.insert(0, None)
|
||||||
return np.array(diff)
|
return np.array(diff)
|
||||||
|
|
||||||
|
|
||||||
@ -24,3 +24,12 @@ def Z(original):
|
|||||||
sigma = np.std(original)
|
sigma = np.std(original)
|
||||||
z = [(k - mu)/sigma for k in original]
|
z = [(k - mu)/sigma for k in original]
|
||||||
return z
|
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
3
fts.py
@ -10,6 +10,7 @@ class FTS:
|
|||||||
self.shortname = name
|
self.shortname = name
|
||||||
self.name = name
|
self.name = name
|
||||||
self.detail = name
|
self.detail = name
|
||||||
|
self.isHighOrder = False
|
||||||
self.hasSeasonality = False
|
self.hasSeasonality = False
|
||||||
self.hasPointForecasting = True
|
self.hasPointForecasting = True
|
||||||
self.hasIntervalForecasting = False
|
self.hasIntervalForecasting = False
|
||||||
@ -45,7 +46,7 @@ class FTS:
|
|||||||
def forecastAheadDistribution(self, data, steps):
|
def forecastAheadDistribution(self, data, steps):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def train(self, data, sets, order=1):
|
def train(self, data, sets,order=1, parameters=None):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def getMidpoints(self, flrg):
|
def getMidpoints(self, flrg):
|
||||||
|
3
hofts.py
3
hofts.py
@ -41,6 +41,7 @@ class HighOrderFTS(fts.FTS):
|
|||||||
self.detail = "Chen"
|
self.detail = "Chen"
|
||||||
self.order = 1
|
self.order = 1
|
||||||
self.setsDict = {}
|
self.setsDict = {}
|
||||||
|
self.isHighOrder = True
|
||||||
|
|
||||||
def generateFLRG(self, flrs):
|
def generateFLRG(self, flrs):
|
||||||
flrgs = {}
|
flrgs = {}
|
||||||
@ -58,7 +59,7 @@ class HighOrderFTS(fts.FTS):
|
|||||||
flrgs[flrg.strLHS()].appendRHS(flrs[k].RHS)
|
flrgs[flrg.strLHS()].appendRHS(flrs[k].RHS)
|
||||||
return (flrgs)
|
return (flrgs)
|
||||||
|
|
||||||
def train(self, data, sets, order):
|
def train(self, data, sets, order=1,parameters=None):
|
||||||
self.order = order
|
self.order = order
|
||||||
self.sets = sets
|
self.sets = sets
|
||||||
for s in self.sets: self.setsDict[s.name] = s
|
for s in self.sets: self.setsDict[s.name] = s
|
||||||
|
20
hwang.py
20
hwang.py
@ -6,13 +6,18 @@ from pyFTS import fts
|
|||||||
class HighOrderFTS(fts.FTS):
|
class HighOrderFTS(fts.FTS):
|
||||||
def __init__(self, order, name):
|
def __init__(self, order, name):
|
||||||
super(HighOrderFTS, self).__init__(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))])
|
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)])
|
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)])
|
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))])
|
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)):
|
for s in range(len(self.sets)):
|
||||||
cn[s] = self.sets[s].membership(data[t])
|
cn[s] = self.sets[s].membership(data[t])
|
||||||
for w in range(self.order - 1):
|
for w in range(self.order - 1):
|
||||||
@ -26,13 +31,10 @@ class HighOrderFTS(fts.FTS):
|
|||||||
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 = 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
|
self.sets = sets
|
||||||
|
self.order = order
|
||||||
def predict(self, data, t):
|
|
||||||
return self.forecast(data, t)
|
|
||||||
|
|
||||||
def predictDiff(self, data, t):
|
|
||||||
return data[t] + self.forecast(Transformations.differential(data), t)
|
|
1
ifts.py
1
ifts.py
@ -15,6 +15,7 @@ class IntervalFTS(hofts.HighOrderFTS):
|
|||||||
self.flrgs = {}
|
self.flrgs = {}
|
||||||
self.hasPointForecasting = False
|
self.hasPointForecasting = False
|
||||||
self.hasIntervalForecasting = True
|
self.hasIntervalForecasting = True
|
||||||
|
self.isHighOrder = True
|
||||||
|
|
||||||
def getUpper(self, flrg):
|
def getUpper(self, flrg):
|
||||||
if flrg.strLHS() in self.flrgs:
|
if flrg.strLHS() in self.flrgs:
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import numpy as np
|
import numpy as np
|
||||||
from pyFTS.common import FuzzySet,FLR
|
from pyFTS.common import FuzzySet,FLR
|
||||||
import fts
|
from pyFTS import fts
|
||||||
|
|
||||||
|
|
||||||
class ImprovedWeightedFLRG:
|
class ImprovedWeightedFLRG:
|
||||||
@ -46,7 +46,7 @@ class ImprovedWeightedFTS(fts.FTS):
|
|||||||
flrgs[flr.LHS.name].append(flr.RHS)
|
flrgs[flr.LHS.name].append(flr.RHS)
|
||||||
return (flrgs)
|
return (flrgs)
|
||||||
|
|
||||||
def train(self, data, sets):
|
def train(self, data, sets,order=1,parameters=None):
|
||||||
self.sets = sets
|
self.sets = sets
|
||||||
|
|
||||||
for s in self.sets: self.setsDict[s.name] = s
|
for s in self.sets: self.setsDict[s.name] = s
|
||||||
|
1
pfts.py
1
pfts.py
@ -46,6 +46,7 @@ class ProbabilisticFTS(ifts.IntervalFTS):
|
|||||||
self.hasPointForecasting = True
|
self.hasPointForecasting = True
|
||||||
self.hasIntervalForecasting = True
|
self.hasIntervalForecasting = True
|
||||||
self.hasDistributionForecasting = True
|
self.hasDistributionForecasting = True
|
||||||
|
self.isHighOrder = True
|
||||||
|
|
||||||
def generateFLRG(self, flrs):
|
def generateFLRG(self, flrs):
|
||||||
flrgs = {}
|
flrgs = {}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import numpy as np
|
import numpy as np
|
||||||
from pyFTS.common import FuzzySet,FLR
|
from pyFTS.common import FuzzySet,FLR
|
||||||
import fts
|
from pyFTS import fts
|
||||||
|
|
||||||
class ExponentialyWeightedFLRG:
|
class ExponentialyWeightedFLRG:
|
||||||
def __init__(self, LHS, c):
|
def __init__(self, LHS, c):
|
||||||
@ -49,8 +49,8 @@ class ExponentialyWeightedFTS(fts.FTS):
|
|||||||
flrgs[flr.LHS.name].append(flr.RHS)
|
flrgs[flr.LHS.name].append(flr.RHS)
|
||||||
return (flrgs)
|
return (flrgs)
|
||||||
|
|
||||||
def train(self, data, sets, c):
|
def train(self, data, sets,order=1,parameters=2):
|
||||||
self.c = c
|
self.c = parameters
|
||||||
self.sets = sets
|
self.sets = sets
|
||||||
tmpdata = FuzzySet.fuzzySeries(data, sets)
|
tmpdata = FuzzySet.fuzzySeries(data, sets)
|
||||||
flrs = FLR.generateRecurrentFLRs(tmpdata)
|
flrs = FLR.generateRecurrentFLRs(tmpdata)
|
||||||
|
4
sfts.py
4
sfts.py
@ -44,9 +44,9 @@ class SeasonalFTS(fts.FTS):
|
|||||||
|
|
||||||
return (flrgs)
|
return (flrgs)
|
||||||
|
|
||||||
def train(self, data, sets, seasonality):
|
def train(self, data, sets, order=1,parameters=12):
|
||||||
self.sets = sets
|
self.sets = sets
|
||||||
self.seasonality = seasonality
|
self.seasonality = parameters
|
||||||
tmpdata = FuzzySet.fuzzySeries(data, sets)
|
tmpdata = FuzzySet.fuzzySeries(data, sets)
|
||||||
flrs = FLR.generateRecurrentFLRs(tmpdata)
|
flrs = FLR.generateRecurrentFLRs(tmpdata)
|
||||||
self.flrgs = self.generateFLRG(flrs)
|
self.flrgs = self.generateFLRG(flrs)
|
||||||
|
4
yu.py
4
yu.py
@ -1,6 +1,6 @@
|
|||||||
import numpy as np
|
import numpy as np
|
||||||
from pyFTS.common import FuzzySet,FLR
|
from pyFTS.common import FuzzySet,FLR
|
||||||
import fts
|
from pyFTS import fts
|
||||||
|
|
||||||
|
|
||||||
class WeightedFLRG(fts.FTS):
|
class WeightedFLRG(fts.FTS):
|
||||||
@ -46,7 +46,7 @@ class WeightedFTS(fts.FTS):
|
|||||||
flrgs[flr.LHS.name].append(flr.RHS)
|
flrgs[flr.LHS.name].append(flr.RHS)
|
||||||
return (flrgs)
|
return (flrgs)
|
||||||
|
|
||||||
def train(self, data, sets):
|
def train(self, data, sets,order=1,parameters=None):
|
||||||
self.sets = sets
|
self.sets = sets
|
||||||
tmpdata = FuzzySet.fuzzySeries(data, sets)
|
tmpdata = FuzzySet.fuzzySeries(data, sets)
|
||||||
flrs = FLR.generateRecurrentFLRs(tmpdata)
|
flrs = FLR.generateRecurrentFLRs(tmpdata)
|
||||||
|
Loading…
Reference in New Issue
Block a user