diff --git a/benchmarks/arima.py b/benchmarks/arima.py index 06da158..fd94c62 100644 --- a/benchmarks/arima.py +++ b/benchmarks/arima.py @@ -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:]]) - - return ret + 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 \ No newline at end of file diff --git a/benchmarks/benchmarks.py b/benchmarks/benchmarks.py index 2fb3419..d08df03 100644 --- a/benchmarks/benchmarks.py +++ b/benchmarks/benchmarks.py @@ -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 = [] diff --git a/benchmarks/naive.py b/benchmarks/naive.py index 7563094..80d338c 100644 --- a/benchmarks/naive.py +++ b/benchmarks/naive.py @@ -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] diff --git a/benchmarks/quantreg.py b/benchmarks/quantreg.py index 20ca04f..987fdd7 100644 --- a/benchmarks/quantreg.py +++ b/benchmarks/quantreg.py @@ -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)) diff --git a/chen.py b/chen.py index eb1ab49..4dc1a86 100644 --- a/chen.py +++ b/chen.py @@ -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)) diff --git a/ensemble.py b/ensemble.py index c0d0fc7..bf1a6d6 100644 --- a/ensemble.py +++ b/ensemble.py @@ -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 diff --git a/fts.py b/fts.py index e1a8306..d50619a 100644 --- a/fts.py +++ b/fts.py @@ -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): diff --git a/hofts.py b/hofts.py index 43a7af8..bd352dc 100644 --- a/hofts.py +++ b/hofts.py @@ -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 = [] diff --git a/hwang.py b/hwang.py index 8ad42aa..ffb752b 100644 --- a/hwang.py +++ b/hwang.py @@ -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) diff --git a/ifts.py b/ifts.py index f772347..660dc5b 100644 --- a/ifts.py +++ b/ifts.py @@ -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)) diff --git a/ismailefendi.py b/ismailefendi.py index 60d1e44..5fd9a34 100644 --- a/ismailefendi.py +++ b/ismailefendi.py @@ -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) diff --git a/models/cmsfts.py b/models/cmsfts.py index 598515a..ffa705b 100644 --- a/models/cmsfts.py +++ b/models/cmsfts.py @@ -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)] diff --git a/models/msfts.py b/models/msfts.py index c24487f..bcfdd29 100644 --- a/models/msfts.py +++ b/models/msfts.py @@ -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)] diff --git a/pwfts.py b/pwfts.py index 5cf6b9f..2f9996f 100644 --- a/pwfts.py +++ b/pwfts.py @@ -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 = [] diff --git a/sadaei.py b/sadaei.py index 1258f4e..4870f39 100644 --- a/sadaei.py +++ b/sadaei.py @@ -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) diff --git a/sfts.py b/sfts.py index 1688930..76f3a92 100644 --- a/sfts.py +++ b/sfts.py @@ -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)) diff --git a/yu.py b/yu.py index 5464b58..11e99f2 100644 --- a/yu.py +++ b/yu.py @@ -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)