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

View File

@ -773,7 +773,7 @@ def get_distribution_statistics(original, model, steps, resolution):
ret = list()
try:
_s1 = time.time()
densities1 = model.forecastAheadDistribution(original,steps,resolution, parameters=3)
densities1 = model.forecastAheadDistribution(original, steps, parameters=3)
_e1 = time.time()
ret.append(round(Measures.crps(original, densities1), 3))
ret.append(round(_e1 - _s1, 3))
@ -784,7 +784,7 @@ def get_distribution_statistics(original, model, steps, resolution):
try:
_s2 = time.time()
densities2 = model.forecastAheadDistribution(original, steps, resolution, parameters=2)
densities2 = model.forecastAheadDistribution(original, steps, parameters=2)
_e2 = time.time()
ret.append( round(Measures.crps(original, densities2), 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):
if fts.hasDistributionForecasting and distributions[count]:
density = fts.forecastAheadDistribution(original[time_from - fts.order:time_from],
time_to, resolution, parameters=option)
density = fts.forecastAheadDistribution(original[time_from - fts.order:time_from], time_to,
parameters=option)
Y = []
X = []

View File

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

View File

@ -8,7 +8,7 @@ from pyFTS import fts
class QuantileRegression(fts.FTS):
def __init__(self, name):
def __init__(self, order, **kwargs):
super(QuantileRegression, self).__init__(1, "QR")
self.name = "QR"
self.detail = "Quantile Regression"
@ -38,7 +38,7 @@ class QuantileRegression(fts.FTS):
def linearmodel(self,data,params):
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))
l = len(ndata)
@ -53,7 +53,7 @@ class QuantileRegression(fts.FTS):
return ret
def forecastInterval(self, data):
def forecastInterval(self, data, **kwargs):
ndata = np.array(self.doTransformations(data))

View File

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

View File

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

14
fts.py
View File

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

View File

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

View File

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

View File

@ -7,7 +7,7 @@ from pyFTS import hofts, fts, tree
class IntervalFTS(hofts.HighOrderFTS):
def __init__(self, name):
def __init__(self, order, **kwargs):
super(IntervalFTS, self).__init__("IFTS " + name)
self.shortname = "IFTS " + name
self.name = "Interval FTS"
@ -47,7 +47,7 @@ class IntervalFTS(hofts.HighOrderFTS):
for child in node.getChildren():
self.buildTree(child, lags, level + 1)
def forecastInterval(self, data):
def forecastInterval(self, data, **kwargs):
ndata = np.array(self.doTransformations(data))

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

6
yu.py
View File

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