Skip to content

Commit

Permalink
required changes were made
Browse files Browse the repository at this point in the history
  • Loading branch information
abhijeetpanda12 committed May 23, 2018
1 parent c6b4ad4 commit bca1cec
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 28 deletions.
82 changes: 58 additions & 24 deletions statsmodels/tsa/automatic/sarimax.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,41 @@
import statsmodels.api as sm


def auto_order(
endog, criteria='aic', d=0, max_order=(3, 3),
def auto_order(endog, criteria='aic', d=0, max_order=(3, 3),
stepwise=False, **spec):
"""Auto order function for SARIMAX models."""
"""Perform automatic calculation of the parameters used in SARIMAX models.
This uses two different approaches to clacluate the parameters:
1. Uses a brute force approach to traverse through all the possible
parameters of the model.
2. Uses the stepwise algorithm specified in Hyndman's Paper.
Parameters
----------
endog : list
input contains the time series data over a period of time.
criteria : str
specifies which information criteria to use for model evaluation.
d : int
differencing factor.
max_order : list
maximum possible values of the parameters p and q.
stepwise : Boolean
specifies whether to use the stepwise algorithm or not.
**spec : list of kwargs
Returns
-------
p : the autoregressive parameter for the SARIMAX model.
q : the moving average parameter for the SARIMAX model.
Notes
-----
Status : Work In Progress.
Citation : Hyndman, Rob J., and Yeasmin Khandakar.
"Automatic Time Series Forecasting: The forecast Package for R."
"""
if not stepwise:
aic_matrix = np.zeros((max_order[0], max_order[1]))
for p in range(max_order[0]):
Expand Down Expand Up @@ -41,54 +72,57 @@ def auto_order(
min_aic = aic_vals.min()
model = int(np.where(aic_vals == min_aic)[0])
p, q = order_init[model][0], order_init[model][1]
new_p, new_q = p, q
# only p varies by +1
if(p+1 <= max_order[0]):
mod = sm.tsa.statespace.SARIMAX(endog, order=(p+1, d, q), **spec)
if(p + 1 <= max_order[0]):
mod = sm.tsa.statespace.SARIMAX(endog, order=(p + 1, d, q), **spec)
res = mod.fit(disp=False)
# print(res.aic)
if (res.aic < min_aic):
p += 1
new_p = p + 1
min_aic = res.aic
# only p varies by -1
if(p-1 >= 0):
mod = sm.tsa.statespace.SARIMAX(endog, order=(p-1, d, q), **spec)
if(p - 1 >= 0):
mod = sm.tsa.statespace.SARIMAX(endog, order=(p - 1, d, q), **spec)
res = mod.fit(disp=False)
# print(res.aic)
if (res.aic < min_aic):
p -= 1
new_p = p - 1
min_aic = res.aic
# only q varies by +1
if(q+1 <= max_order[1]):
mod = sm.tsa.statespace.SARIMAX(endog, order=(p, d, q+1), **spec)
if(q + 1 <= max_order[1]):
mod = sm.tsa.statespace.SARIMAX(endog, order=(p, d, q + 1), **spec)
res = mod.fit(disp=False)
# print(res.aic)
if (res.aic < min_aic):
q += 1
new_q = q + 1
min_aic = res.aic
# only q varies by -1
if(q-1 >= 0):
mod = sm.tsa.statespace.SARIMAX(endog, order=(p, d, q-1), **spec)
if(q - 1 >= 0):
mod = sm.tsa.statespace.SARIMAX(endog, order=(p, d, q - 1), **spec)
res = mod.fit(disp=False)
# print(res.aic)
if (res.aic < min_aic):
q -= 1
new_q = q - 1
min_aic = res.aic
# both p and q vary by +1
if(p+1 <= max_order[0]) and (q+1 <= max_order[1]):
mod = sm.tsa.statespace.SARIMAX(endog, order=(p+1, d, q+1), **spec)
if(p + 1 <= max_order[0]) and (q + 1 <= max_order[1]):
mod = sm.tsa.statespace.SARIMAX(endog,
order=(p + 1, d, q + 1), **spec)
res = mod.fit(disp=False)
# print(res.aic)
if (res.aic < min_aic):
p += 1
q += 1
new_p = p + 1
new_q = q + 1
min_aic = res.aic
# both p and q vary by -1
if(q-1 >= 0) and (q-1 >= 0):
mod = sm.tsa.statespace.SARIMAX(endog, order=(p-1, d, q-1), **spec)
if(q - 1 >= 0) and (q - 1 >= 0):
mod = sm.tsa.statespace.SARIMAX(endog,
order=(p - 1, d, q - 1), **spec)
res = mod.fit(disp=False)
# print(res.aic)
if (res.aic < min_aic):
p -= 1
q -= 1
new_p = p - 1
new_q = q - 1
min_aic = res.aic
return p, q
return new_p, new_q
9 changes: 5 additions & 4 deletions statsmodels/tsa/automatic/tests/test_sarimax.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import statsmodels.api as sm
from statsmodels.tsa.automatic import sarimax
from statsmodels import datasets
from numpy.testing import assert_equal


current_path = os.path.dirname(os.path.abspath(__file__))
Expand All @@ -19,8 +20,8 @@ def test_non_stepwise():
# p, q = sarimax.auto_order(macrodata.infl, d=0, enforce_stationarity=False)
desired_p = 2
desired_q = 2
assert p == desired_p
assert q == desired_q
assert_equal(p, desired_p)
assert_equal(q, desired_q)


def test_stepwise():
Expand All @@ -30,5 +31,5 @@ def test_stepwise():
p, q = sarimax.auto_order(macrodata['infl'], stepwise=True)
desired_p = 3
desired_q = 3
assert p == desired_p
assert q == desired_q
assert_equal(p, desired_p)
assert_equal(q, desired_q)

0 comments on commit bca1cec

Please sign in to comment.