From cd2e2b75866d0bf6af25affe9e3e595c29a5a5bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=C3=B4nio=20C=C3=A2ndido=20de=20Lima=20e=20Silva?= Date: Thu, 13 Apr 2017 17:27:38 -0300 Subject: [PATCH] =?UTF-8?q?=20-=20QuantReg=20fa=C3=A7ade=20for=20statsmode?= =?UTF-8?q?ls?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- benchmarks/arima.py | 4 +++- benchmarks/quantreg.py | 52 ++++++++++++++++++++++++++++++++++++++++-- tests/general.py | 33 +++++++++------------------ 3 files changed, 64 insertions(+), 25 deletions(-) diff --git a/benchmarks/arima.py b/benchmarks/arima.py index 8719a38..06da158 100644 --- a/benchmarks/arima.py +++ b/benchmarks/arima.py @@ -23,6 +23,8 @@ 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] @@ -31,7 +33,7 @@ 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(data, order=(self.p, self.d, self.q)) + self.model = stats_arima(ndata, order=(self.p, self.d, self.q)) #try: self.model_fit = self.model.fit(disp=0) #except: diff --git a/benchmarks/quantreg.py b/benchmarks/quantreg.py index 8c0903e..20ca04f 100644 --- a/benchmarks/quantreg.py +++ b/benchmarks/quantreg.py @@ -3,6 +3,7 @@ import numpy as np from statsmodels.regression.quantile_regression import QuantReg +from statsmodels.tsa.tsatools import lagmat from pyFTS import fts @@ -12,13 +13,60 @@ class QuantileRegression(fts.FTS): self.name = "QR" self.detail = "Quantile Regression" self.isHighOrder = True + self.hasPointForecasting = True self.hasIntervalForecasting = True self.benchmark_only = True self.minOrder = 1 self.alpha = 0.5 + self.upper_qt = None + self.mean_qt = None + self.lower_qt = None def train(self, data, sets, order=1, parameters=None): - pass + self.order = order + tmp = np.array(self.doTransformations(data)) + + lagdata, ndata = lagmat(tmp, maxlag=order, trim="both", original='sep') + uqt = QuantReg(ndata, lagdata).fit(1 - self.alpha) + mqt = QuantReg(ndata, lagdata).fit(0.5) + lqt = QuantReg(ndata, lagdata).fit(self.alpha) + + self.upper_qt = [uqt.params[k] for k in uqt.params.keys()] + self.mean_qt = [mqt.params[k] for k in mqt.params.keys()] + self.lower_qt = [lqt.params[k] for k in lqt.params.keys()] + + 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): - pass + ndata = np.array(self.doTransformations(data)) + l = len(ndata) + + ret = [] + + for k in np.arange(self.order, l): + sample = ndata[k - self.order : k] + + ret.append(self.linearmodel(sample, self.mean_qt)) + + ret = self.doInverseTransformations(ret, params=[data[self.order - 1:]]) + + return ret + + def forecastInterval(self, data): + + ndata = np.array(self.doTransformations(data)) + + l = len(ndata) + + ret = [] + + for k in np.arange(self.order - 1, l): + sample = ndata[k - self.order: k] + up = self.linearmodel(sample, self.upper_qt) + down = self.linearmodel(sample, self.down_qt) + ret.append([up, down]) + + ret = self.doInverseTransformations(ret, params=[data[self.order - 1:]]) + + return ret diff --git a/tests/general.py b/tests/general.py index 7557062..f715570 100644 --- a/tests/general.py +++ b/tests/general.py @@ -28,38 +28,27 @@ os.chdir("/home/petronio/dados/Dropbox/Doutorado/Codigos/") taiexpd = pd.read_csv("DataSets/TAIEX.csv", sep=",") taiex = np.array(taiexpd["avg"][:5000]) -from statsmodels.tsa.arima_model import ARIMA as stats_arima +#from statsmodels.tsa.arima_model import ARIMA as stats_arima +from statsmodels.tsa.tsatools import lagmat -model = stats_arima(taiex[:1600], (2,0,1)).fit(disp=0) +tmp = np.arange(10) -ar = np.array(taiex[1598:1600]).dot( model.arparams ) - -#print(ar) - -res = ar - taiex[1600] - -#print(res) - -ma = np.array([res]).dot(model.maparams) - -#print(ma) - -print(ar + ma) -print(taiex[1598:1601]) -print(taiex[1600]) +lag, a = lagmat(tmp, maxlag=2, trim="both", original='sep') +print(lag) +print(a) #from pyFTS.benchmarks import distributed_benchmarks as bchmk #from pyFTS.benchmarks import parallel_benchmarks as bchmk #from pyFTS.benchmarks import benchmarks as bchmk -from pyFTS.benchmarks import arima +#from pyFTS.benchmarks import arima -tmp = arima.ARIMA("") -tmp.train(taiex[:1600],None,parameters=(2,0,1)) -teste = tmp.forecast(taiex[1598:1601]) +#tmp = arima.ARIMA("") +#tmp.train(taiex[:1600],None,parameters=(2,0,1)) +#teste = tmp.forecast(taiex[1598:1601]) -print(teste) +#print(teste) #bchmk.teste(taiex,['192.168.0.109', '192.168.0.101'])