auto.arima() equivalent for python
You can implement a number of approaches:
ARIMAResults
includeaic
andbic
. By their definition, (see here and here), these criteria penalize for the number of parameters in the model. So you may use these numbers to compare the models. Also scipy hasoptimize.brute
which does grid search on the specified parameters space. So a workflow like this should work:def objfunc(order, exog, endog): from statsmodels.tsa.arima_model import ARIMA fit = ARIMA(endog, order, exog).fit() return fit.aic()from scipy.optimize import brutegrid = (slice(1, 3, 1), slice(1, 3, 1), slice(1, 3, 1))brute(objfunc, grid, args=(exog, endog), finish=None)
Make sure you call
brute
withfinish=None
.You may obtain
pvalues
fromARIMAResults
. So a sort of step-forward algorithm is easy to implement where the degree of the model is increased across the dimension which obtains lowest p-value for the added parameter.Use
ARIMAResults.predict
to cross-validate alternative models. The best approach would be to keep the tail of the time series (say most recent 5% of data) out of sample, and use these points to obtain the test error of the fitted models.
There is now a proper python package to do auto-arima. https://github.com/tgsmith61591/pmdarima
Docs:http://alkaline-ml.com/pmdarima
Example usage: https://github.com/tgsmith61591/pmdarima/blob/master/examples/quick_start_example.ipynb
def evaluate_arima_model(X, arima_order): # prepare training dataset train_size = int(len(X) * 0.90) train, test = X[0:train_size], X[train_size:] history = [x for x in train] # make predictions predictions = list() for t in range(len(test)): model = ARIMA(history, order=arima_order) model_fit = model.fit(disp=0) yhat = model_fit.forecast()[0] predictions.append(yhat) history.append(test[t]) # calculate out of sample error error = mean_squared_error(test, predictions) return error# evaluate combinations of p, d and q values for an ARIMA modeldef evaluate_models(dataset, p_values, d_values, q_values): dataset = dataset.astype('float32') best_score, best_cfg = float("inf"), None for p in p_values: for d in d_values: for q in q_values: order = (p,d,q) try: mse = evaluate_arima_model(dataset, order) if mse < best_score: best_score, best_cfg = mse, order print('ARIMA%s MSE=%.3f' % (order,mse)) except: continue print('Best ARIMA%s MSE=%.3f' % (best_cfg, best_score))# load datasetdef parser(x): return datetime.strptime('190'+x, '%Y-%m')import datetimep_values = [4,5,6,7,8]d_values = [0,1,2]q_values = [2,3,4,5,6]warnings.filterwarnings("ignore")evaluate_models(train, p_values, d_values, q_values)
This will give you the p,d,q values, then use the values for your ARIMA model