Skip to content

Instantly share code, notes, and snippets.

@abhijeetpanda12
Created May 14, 2018 03:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abhijeetpanda12/1dad1bdab503edf2bec98934591d8ced to your computer and use it in GitHub Desktop.
Save abhijeetpanda12/1dad1bdab503edf2bec98934591d8ced to your computer and use it in GitHub Desktop.
"""automatic forecasting for SARIMAX models."""
import numpy as np
import pandas as pd
import statsmodels.api as sm
# Dataset
wpi1 = requests.get('http://www.stata-press.com/data/r12/wpi1.dta').content
data = pd.read_stata(BytesIO(wpi1))
data.index = data.t
def auto_order(endog, criteria='aic', d=0, max_order=(3, 3), spec=None):
"""Auto order function for SARIMAX models."""
aic_matrix = np.zeros(p, q)
for p in range(max_order[0]):
for q in range(max_order[1]):
if p == 0 and q == 0:
continue
# fit the model
mod = sm.tsa.statespace.SARIMAX(data['wpi'], order=(p, d, q))
res = mod.fit(disp=False)
aic_matrix[p, q] = res.aic
min_aic = aic_matrix.min()
p, q = np.where(aic_matrix == min_aic)
@abhijeetpanda12
Copy link
Author

I have used the dataset for testing purpose now.

@ChadFulton
Copy link

Great start, thanks for putting this up here! Can you make this a commit on your PR? It's actually easier to do the formal code review that way (I can put in line comments, etc.).

I'll go ahead and make some additional comments in the PR messages right now, just to keep things all in one place.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment