2017-03-03 15:53:55 +04:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf8 -*-
|
|
|
|
|
|
|
|
import numpy as np
|
|
|
|
from statsmodels.tsa.arima_model import ARIMA as stats_arima
|
2017-04-13 19:36:22 +04:00
|
|
|
from statsmodels.tsa.arima_model import ARMA
|
2017-03-03 15:53:55 +04:00
|
|
|
from pyFTS import fts
|
|
|
|
|
|
|
|
|
|
|
|
class ARIMA(fts.FTS):
|
|
|
|
def __init__(self, name):
|
|
|
|
super(ARIMA, self).__init__(1, "ARIMA")
|
|
|
|
self.name = "ARIMA"
|
|
|
|
self.detail = "Auto Regressive Integrated Moving Average"
|
|
|
|
self.isHighOrder = True
|
|
|
|
self.model = None
|
|
|
|
self.model_fit = None
|
|
|
|
self.trained_data = None
|
|
|
|
self.p = 1
|
|
|
|
self.d = 0
|
|
|
|
self.q = 0
|
|
|
|
self.benchmark_only = True
|
|
|
|
self.minOrder = 1
|
|
|
|
|
|
|
|
def train(self, data, sets, order=1, parameters=None):
|
|
|
|
if parameters is not None:
|
|
|
|
self.p = parameters[0]
|
|
|
|
self.d = parameters[1]
|
|
|
|
self.q = parameters[2]
|
|
|
|
self.order = max([self.p, self.d, self.q])
|
|
|
|
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))
|
2017-04-13 19:36:22 +04:00
|
|
|
#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
|
2017-03-03 15:53:55 +04:00
|
|
|
|
2017-04-13 19:36:22 +04:00
|
|
|
#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)
|
2017-03-03 15:53:55 +04:00
|
|
|
|
|
|
|
def forecast(self, data):
|
2017-03-22 06:17:06 +04:00
|
|
|
if self.model_fit is None:
|
|
|
|
return np.nan
|
2017-04-13 19:36:22 +04:00
|
|
|
|
|
|
|
order = self.p
|
|
|
|
|
|
|
|
ndata = np.array(self.doTransformations(data))
|
|
|
|
|
|
|
|
l = len(ndata)
|
|
|
|
|
2017-03-03 15:53:55 +04:00
|
|
|
ret = []
|
2017-04-13 19:36:22 +04:00
|
|
|
|
|
|
|
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
|