- General refactoring to include **kwargs on methods signatures

- Ensemble FTS
This commit is contained in:
Petrônio Cândido de Lima e Silva 2017-04-14 19:57:39 -03:00
parent cd2e2b7586
commit 728e8414b7
17 changed files with 69 additions and 91 deletions

View File

@ -3,12 +3,11 @@
import numpy as np import numpy as np
from statsmodels.tsa.arima_model import ARIMA as stats_arima from statsmodels.tsa.arima_model import ARIMA as stats_arima
from statsmodels.tsa.arima_model import ARMA
from pyFTS import fts from pyFTS import fts
class ARIMA(fts.FTS): class ARIMA(fts.FTS):
def __init__(self, name): def __init__(self, order, **kwargs):
super(ARIMA, self).__init__(1, "ARIMA") super(ARIMA, self).__init__(1, "ARIMA")
self.name = "ARIMA" self.name = "ARIMA"
self.detail = "Auto Regressive Integrated Moving Average" self.detail = "Auto Regressive Integrated Moving Average"
@ -23,8 +22,6 @@ class ARIMA(fts.FTS):
self.minOrder = 1 self.minOrder = 1
def train(self, data, sets, order=1, parameters=None): def train(self, data, sets, order=1, parameters=None):
ndata = np.array(self.doTransformations(data))
if parameters is not None: if parameters is not None:
self.p = parameters[0] self.p = parameters[0]
self.d = parameters[1] self.d = parameters[1]
@ -33,44 +30,25 @@ class ARIMA(fts.FTS):
self.shortname = "ARIMA(" + str(self.p) + "," + str(self.d) + "," + str(self.q) + ")" self.shortname = "ARIMA(" + str(self.p) + "," + str(self.d) + "," + str(self.q) + ")"
old_fit = self.model_fit old_fit = self.model_fit
self.model = stats_arima(ndata, order=(self.p, self.d, self.q)) self.model = stats_arima(data, order=(self.p, self.d, self.q))
#try: try:
self.model_fit = self.model.fit(disp=0) self.model_fit = self.model.fit(disp=0)
#except: except:
# try: try:
# self.model = stats_arima(data, order=(self.p, self.d, self.q)) self.model = stats_arima(data, order=(self.p, self.d, self.q))
# self.model_fit = self.model.fit(disp=1) self.model_fit = self.model.fit(disp=1)
# except: except:
# self.model_fit = old_fit self.model_fit = old_fit
#self.trained_data = data #.tolist() self.trained_data = data #.tolist()
def ar(self, data): def forecast(self, data, **kwargs):
return data.dot(self.model_fit.arparams)
def ma(self, data):
return data.dot(self.model_fit.maparams)
def forecast(self, data):
if self.model_fit is None: if self.model_fit is None:
return np.nan return np.nan
order = self.p
ndata = np.array(self.doTransformations(data))
l = len(ndata)
ret = [] ret = []
for t in data:
ar = np.array([self.ar(ndata[k - self.p: k]) for k in np.arange(self.p, l)]) output = self.model_fit.forecast()
ret.append( output[0] )
residuals = np.array([ar[k - self.p] - ndata[k] for k in np.arange(self.p, l)]) self.trained_data = np.append(self.trained_data, t) #.append(t)
self.train(self.trained_data,None,order=self.order, parameters=(self.p, self.d, self.q))
ma = np.array([self.ma(residuals[k - self.q : k]) for k in np.arange(self.q, len(ar)+1)])
ret = ar + ma
ret = self.doInverseTransformations(ret, params=[data[order - 1:]])
return ret return ret

View File

@ -773,7 +773,7 @@ def get_distribution_statistics(original, model, steps, resolution):
ret = list() ret = list()
try: try:
_s1 = time.time() _s1 = time.time()
densities1 = model.forecastAheadDistribution(original,steps,resolution, parameters=3) densities1 = model.forecastAheadDistribution(original, steps, parameters=3)
_e1 = time.time() _e1 = time.time()
ret.append(round(Measures.crps(original, densities1), 3)) ret.append(round(Measures.crps(original, densities1), 3))
ret.append(round(_e1 - _s1, 3)) ret.append(round(_e1 - _s1, 3))
@ -784,7 +784,7 @@ def get_distribution_statistics(original, model, steps, resolution):
try: try:
_s2 = time.time() _s2 = time.time()
densities2 = model.forecastAheadDistribution(original, steps, resolution, parameters=2) densities2 = model.forecastAheadDistribution(original, steps, parameters=2)
_e2 = time.time() _e2 = time.time()
ret.append( round(Measures.crps(original, densities2), 3)) ret.append( round(Measures.crps(original, densities2), 3))
ret.append(round(_e2 - _s2, 3)) ret.append(round(_e2 - _s2, 3))
@ -823,8 +823,8 @@ def plotComparedIntervalsAhead(original, models, colors, distributions, time_fro
for count, fts in enumerate(models, start=0): for count, fts in enumerate(models, start=0):
if fts.hasDistributionForecasting and distributions[count]: if fts.hasDistributionForecasting and distributions[count]:
density = fts.forecastAheadDistribution(original[time_from - fts.order:time_from], density = fts.forecastAheadDistribution(original[time_from - fts.order:time_from], time_to,
time_to, resolution, parameters=option) parameters=option)
Y = [] Y = []
X = [] X = []

View File

@ -5,13 +5,13 @@ from pyFTS import fts
class Naive(fts.FTS): class Naive(fts.FTS):
def __init__(self, name): def __init__(self, order, name, **kwargs):
super(Naive, self).__init__(1, "Naive " + name) super(Naive, self).__init__(1, "Naive " + name)
self.name = "Naïve Model" self.name = "Naïve Model"
self.detail = "Naïve Model" self.detail = "Naïve Model"
self.benchmark_only = True self.benchmark_only = True
self.isHighOrder = False self.isHighOrder = False
def forecast(self, data): def forecast(self, data, **kwargs):
return [k for k in data] return [k for k in data]

View File

@ -8,7 +8,7 @@ from pyFTS import fts
class QuantileRegression(fts.FTS): class QuantileRegression(fts.FTS):
def __init__(self, name): def __init__(self, order, **kwargs):
super(QuantileRegression, self).__init__(1, "QR") super(QuantileRegression, self).__init__(1, "QR")
self.name = "QR" self.name = "QR"
self.detail = "Quantile Regression" self.detail = "Quantile Regression"
@ -38,7 +38,7 @@ class QuantileRegression(fts.FTS):
def linearmodel(self,data,params): def linearmodel(self,data,params):
return params[0] + sum([ data[k] * params[k+1] for k in np.arange(0, self.order) ]) return params[0] + sum([ data[k] * params[k+1] for k in np.arange(0, self.order) ])
def forecast(self, data): def forecast(self, data, **kwargs):
ndata = np.array(self.doTransformations(data)) ndata = np.array(self.doTransformations(data))
l = len(ndata) l = len(ndata)
@ -53,7 +53,7 @@ class QuantileRegression(fts.FTS):
return ret return ret
def forecastInterval(self, data): def forecastInterval(self, data, **kwargs):
ndata = np.array(self.doTransformations(data)) ndata = np.array(self.doTransformations(data))

View File

@ -25,7 +25,7 @@ class ConventionalFLRG(object):
class ConventionalFTS(fts.FTS): class ConventionalFTS(fts.FTS):
def __init__(self, name): def __init__(self, order, **kwargs):
super(ConventionalFTS, self).__init__(1, "CFTS " + name) super(ConventionalFTS, self).__init__(1, "CFTS " + name)
self.name = "Conventional FTS" self.name = "Conventional FTS"
self.detail = "Chen" self.detail = "Chen"
@ -48,7 +48,7 @@ class ConventionalFTS(fts.FTS):
flrs = FLR.generateNonRecurrentFLRs(tmpdata) flrs = FLR.generateNonRecurrentFLRs(tmpdata)
self.flrgs = self.generateFLRG(flrs) self.flrgs = self.generateFLRG(flrs)
def forecast(self, data): def forecast(self, data, **kwargs):
ndata = np.array(self.doTransformations(data)) ndata = np.array(self.doTransformations(data))

View File

@ -9,7 +9,7 @@ from pyFTS.common import FLR, FuzzySet, SortedCollection
from pyFTS import fts from pyFTS import fts
class EnsembleFTS(fts.FTS): class EnsembleFTS(fts.FTS):
def __init__(self, name, update=True): def __init__(self, order, name, **kwargs):
super(EnsembleFTS, self).__init__("Ensemble FTS") super(EnsembleFTS, self).__init__("Ensemble FTS")
self.shortname = "Ensemble FTS " + name self.shortname = "Ensemble FTS " + name
self.name = "Ensemble FTS" self.name = "Ensemble FTS"
@ -25,7 +25,7 @@ class EnsembleFTS(fts.FTS):
pass pass
def forecast(self, data): def forecast(self, data, **kwargs):
ndata = np.array(self.doTransformations(data)) ndata = np.array(self.doTransformations(data))
@ -40,7 +40,7 @@ class EnsembleFTS(fts.FTS):
return ret return ret
def forecastInterval(self, data): def forecastInterval(self, data, **kwargs):
ndata = np.array(self.doTransformations(data)) ndata = np.array(self.doTransformations(data))
@ -53,10 +53,10 @@ class EnsembleFTS(fts.FTS):
return ret return ret
def forecastAhead(self, data, steps): def forecastAhead(self, data, steps, **kwargs):
pass pass
def forecastAheadInterval(self, data, steps): def forecastAheadInterval(self, data, steps, **kwargs):
pass pass
@ -88,6 +88,6 @@ class EnsembleFTS(fts.FTS):
grid[k] += 1 grid[k] += 1
return grid return grid
def forecastAheadDistribution(self, data, steps, resolution, parameters=2): def forecastAheadDistribution(self, data, steps, **kwargs):
pass pass

14
fts.py
View File

@ -4,7 +4,7 @@ from pyFTS import tree
from pyFTS.common import FuzzySet, SortedCollection from pyFTS.common import FuzzySet, SortedCollection
class FTS(object): class FTS(object):
def __init__(self, order, name): def __init__(self, order, name, **kwargs):
self.sets = {} self.sets = {}
self.flrgs = {} self.flrgs = {}
self.order = order self.order = order
@ -38,22 +38,22 @@ class FTS(object):
return best return best
def forecast(self, data): def forecast(self, data, **kwargs):
pass pass
def forecastInterval(self, data): def forecastInterval(self, data, **kwargs):
pass pass
def forecastDistribution(self, data): def forecastDistribution(self, data, **kwargs):
pass pass
def forecastAhead(self, data, steps): def forecastAhead(self, data, steps, **kwargs):
pass pass
def forecastAheadInterval(self, data, steps): def forecastAheadInterval(self, data, steps, **kwargs):
pass pass
def forecastAheadDistribution(self, data, steps): def forecastAheadDistribution(self, data, steps, **kwargs):
pass pass
def train(self, data, sets, order=1, parameters=None): def train(self, data, sets, order=1, parameters=None):

View File

@ -39,7 +39,7 @@ class HighOrderFLRG(object):
class HighOrderFTS(fts.FTS): class HighOrderFTS(fts.FTS):
def __init__(self, name): def __init__(self, order, **kwargs):
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.shortname = "HOFTS" + name
@ -79,7 +79,7 @@ class HighOrderFTS(fts.FTS):
ret = np.array([self.setsDict[s].centroid for s in flrg.RHS]) ret = np.array([self.setsDict[s].centroid for s in flrg.RHS])
return ret return ret
def forecast(self, data): def forecast(self, data, **kwargs):
ret = [] ret = []

View File

@ -4,7 +4,7 @@ from pyFTS import fts
class HighOrderFTS(fts.FTS): class HighOrderFTS(fts.FTS):
def __init__(self, name): def __init__(self, order, **kwargs):
super(HighOrderFTS, self).__init__(1, name) super(HighOrderFTS, self).__init__(1, name)
self.isHighOrder = True self.isHighOrder = True
self.minOrder = 2 self.minOrder = 2
@ -12,7 +12,7 @@ class HighOrderFTS(fts.FTS):
self.shortname = "Hwang" + name self.shortname = "Hwang" + name
self.detail = "Hwang" self.detail = "Hwang"
def forecast(self, data): def forecast(self, data, **kwargs):
ndata = self.doTransformations(data) ndata = self.doTransformations(data)

View File

@ -7,7 +7,7 @@ from pyFTS import hofts, fts, tree
class IntervalFTS(hofts.HighOrderFTS): class IntervalFTS(hofts.HighOrderFTS):
def __init__(self, name): def __init__(self, order, **kwargs):
super(IntervalFTS, self).__init__("IFTS " + name) super(IntervalFTS, self).__init__("IFTS " + name)
self.shortname = "IFTS " + name self.shortname = "IFTS " + name
self.name = "Interval FTS" self.name = "Interval FTS"
@ -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 forecastInterval(self, data): def forecastInterval(self, data, **kwargs):
ndata = np.array(self.doTransformations(data)) ndata = np.array(self.doTransformations(data))

View File

@ -33,7 +33,7 @@ class ImprovedWeightedFLRG(object):
class ImprovedWeightedFTS(fts.FTS): class ImprovedWeightedFTS(fts.FTS):
def __init__(self, name): def __init__(self, order, **kwargs):
super(ImprovedWeightedFTS, self).__init__(1, "IWFTS " + name) super(ImprovedWeightedFTS, self).__init__(1, "IWFTS " + name)
self.name = "Improved Weighted FTS" self.name = "Improved Weighted FTS"
self.detail = "Ismail & Efendi" self.detail = "Ismail & Efendi"
@ -64,7 +64,7 @@ class ImprovedWeightedFTS(fts.FTS):
ret = np.array([self.setsDict[s].centroid for s in flrg.RHS]) ret = np.array([self.setsDict[s].centroid for s in flrg.RHS])
return ret return ret
def forecast(self, data): def forecast(self, data, **kwargs):
l = 1 l = 1
data = np.array(data) data = np.array(data)

View File

@ -24,7 +24,7 @@ class ContextualSeasonalFLRG(object):
class ContextualMultiSeasonalFTS(sfts.SeasonalFTS): class ContextualMultiSeasonalFTS(sfts.SeasonalFTS):
def __init__(self, name, indexer): def __init__(self, order, name, **kwargs):
super(ContextualMultiSeasonalFTS, self).__init__("CMSFTS") super(ContextualMultiSeasonalFTS, self).__init__("CMSFTS")
self.name = "Contextual Multi Seasonal FTS" self.name = "Contextual Multi Seasonal FTS"
self.shortname = "CMSFTS " + name self.shortname = "CMSFTS " + name
@ -62,7 +62,7 @@ class ContextualMultiSeasonalFTS(sfts.SeasonalFTS):
else: else:
return np.array([data.centroid]) return np.array([data.centroid])
def forecast(self, data): def forecast(self, data, **kwargs):
ret = [] ret = []
@ -83,7 +83,7 @@ class ContextualMultiSeasonalFTS(sfts.SeasonalFTS):
return ret return ret
def forecastAhead(self, data, steps): def forecastAhead(self, data, steps, **kwargs):
ret = [] ret = []
for i in steps: for i in steps:
flrg = self.flrgs[str(i)] flrg = self.flrgs[str(i)]

View File

@ -4,7 +4,7 @@ from pyFTS import fts, sfts
class MultiSeasonalFTS(sfts.SeasonalFTS): class MultiSeasonalFTS(sfts.SeasonalFTS):
def __init__(self, name, indexer): def __init__(self, order, name, **kwargs):
super(MultiSeasonalFTS, self).__init__("MSFTS") super(MultiSeasonalFTS, self).__init__("MSFTS")
self.name = "Multi Seasonal FTS" self.name = "Multi Seasonal FTS"
self.shortname = "MSFTS " + name self.shortname = "MSFTS " + name
@ -36,7 +36,7 @@ class MultiSeasonalFTS(sfts.SeasonalFTS):
flrs = FLR.generateIndexedFLRs(self.sets, self.indexer, data) flrs = FLR.generateIndexedFLRs(self.sets, self.indexer, data)
self.flrgs = self.generateFLRG(flrs) self.flrgs = self.generateFLRG(flrs)
def forecast(self, data): def forecast(self, data, **kwargs):
ret = [] ret = []
@ -55,7 +55,7 @@ class MultiSeasonalFTS(sfts.SeasonalFTS):
return ret return ret
def forecastAhead(self, data, steps): def forecastAhead(self, data, steps, **kwargs):
ret = [] ret = []
for i in steps: for i in steps:
flrg = self.flrgs[str(i)] flrg = self.flrgs[str(i)]

View File

@ -42,7 +42,7 @@ class ProbabilisticWeightedFLRG(hofts.HighOrderFLRG):
class ProbabilisticWeightedFTS(ifts.IntervalFTS): class ProbabilisticWeightedFTS(ifts.IntervalFTS):
def __init__(self, name, update=True): def __init__(self, order, name, **kwargs):
super(ProbabilisticWeightedFTS, self).__init__("PWFTS") super(ProbabilisticWeightedFTS, self).__init__("PWFTS")
self.shortname = "PWFTS " + name self.shortname = "PWFTS " + name
self.name = "Probabilistic FTS" self.name = "Probabilistic FTS"
@ -191,7 +191,7 @@ class ProbabilisticWeightedFTS(ifts.IntervalFTS):
ret = sum(np.array([pi * s.lower for s in flrg.LHS])) ret = sum(np.array([pi * s.lower for s in flrg.LHS]))
return ret return ret
def forecast(self, data): def forecast(self, data, **kwargs):
ndata = np.array(self.doTransformations(data)) ndata = np.array(self.doTransformations(data))
@ -296,7 +296,7 @@ class ProbabilisticWeightedFTS(ifts.IntervalFTS):
return ret return ret
def forecastInterval(self, data): def forecastInterval(self, data, **kwargs):
ndata = np.array(self.doTransformations(data)) ndata = np.array(self.doTransformations(data))
@ -402,7 +402,7 @@ class ProbabilisticWeightedFTS(ifts.IntervalFTS):
return ret return ret
def forecastAhead(self, data, steps): def forecastAhead(self, data, steps, **kwargs):
ret = [data[k] for k in np.arange(len(data) - self.order, len(data))] ret = [data[k] for k in np.arange(len(data) - self.order, len(data))]
for k in np.arange(self.order - 1, steps): for k in np.arange(self.order - 1, steps):
@ -416,7 +416,7 @@ class ProbabilisticWeightedFTS(ifts.IntervalFTS):
return ret return ret
def forecastAheadInterval(self, data, steps): def forecastAheadInterval(self, data, steps, **kwargs):
l = len(data) l = len(data)
@ -464,7 +464,7 @@ class ProbabilisticWeightedFTS(ifts.IntervalFTS):
grid[k] += 1 grid[k] += 1
return grid return grid
def forecastAheadDistribution(self, data, steps, resolution, parameters=2): def forecastAheadDistribution(self, data, steps, **kwargs):
ret = [] ret = []

View File

@ -36,7 +36,7 @@ class ExponentialyWeightedFLRG(object):
class ExponentialyWeightedFTS(fts.FTS): class ExponentialyWeightedFTS(fts.FTS):
def __init__(self, name): def __init__(self, order, **kwargs):
super(ExponentialyWeightedFTS, self).__init__(1, "EWFTS") super(ExponentialyWeightedFTS, self).__init__(1, "EWFTS")
self.name = "Exponentialy Weighted FTS" self.name = "Exponentialy Weighted FTS"
self.detail = "Sadaei" self.detail = "Sadaei"
@ -60,7 +60,7 @@ class ExponentialyWeightedFTS(fts.FTS):
flrs = FLR.generateRecurrentFLRs(tmpdata) flrs = FLR.generateRecurrentFLRs(tmpdata)
self.flrgs = self.generateFLRG(flrs, self.c) self.flrgs = self.generateFLRG(flrs, self.c)
def forecast(self, data): def forecast(self, data, **kwargs):
l = 1 l = 1
data = np.array(data) data = np.array(data)

View File

@ -26,7 +26,7 @@ class SeasonalFLRG(FLR.FLR):
class SeasonalFTS(fts.FTS): class SeasonalFTS(fts.FTS):
def __init__(self, name): def __init__(self, order, **kwargs):
super(SeasonalFTS, self).__init__(1, "SFTS") super(SeasonalFTS, self).__init__(1, "SFTS")
self.name = "Seasonal FTS" self.name = "Seasonal FTS"
self.detail = "Chen" self.detail = "Chen"
@ -60,7 +60,7 @@ class SeasonalFTS(fts.FTS):
flrs = FLR.generateRecurrentFLRs(tmpdata) flrs = FLR.generateRecurrentFLRs(tmpdata)
self.flrgs = self.generateFLRG(flrs) self.flrgs = self.generateFLRG(flrs)
def forecast(self, data): def forecast(self, data, **kwargs):
ndata = np.array(self.doTransformations(data)) ndata = np.array(self.doTransformations(data))

6
yu.py
View File

@ -4,7 +4,7 @@ from pyFTS import fts
class WeightedFLRG(fts.FTS): class WeightedFLRG(fts.FTS):
def __init__(self, LHS): def __init__(self, order, **kwargs):
self.LHS = LHS self.LHS = LHS
self.RHS = [] self.RHS = []
self.count = 1.0 self.count = 1.0
@ -31,7 +31,7 @@ class WeightedFLRG(fts.FTS):
class WeightedFTS(fts.FTS): class WeightedFTS(fts.FTS):
def __init__(self, name): def __init__(self, order, **kwargs):
super(WeightedFTS, self).__init__(1, "WFTS " + name) super(WeightedFTS, self).__init__(1, "WFTS " + name)
self.name = "Weighted FTS" self.name = "Weighted FTS"
self.detail = "Yu" self.detail = "Yu"
@ -53,7 +53,7 @@ class WeightedFTS(fts.FTS):
flrs = FLR.generateRecurrentFLRs(tmpdata) flrs = FLR.generateRecurrentFLRs(tmpdata)
self.flrgs = self.generateFLRG(flrs) self.flrgs = self.generateFLRG(flrs)
def forecast(self, data): def forecast(self, data, **kwargs):
l = 1 l = 1
data = np.array(data) data = np.array(data)