Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (50.2k points)
I want to predict weekly sales using ARMA models. I want to work with a function for tuning the order(p,d,q) in statsmodels. Apparently R has that function forecast::auto.arima() which will tune the (p,d,q) arguments.

How can I select the right order for my model? Are there any packages available in python for this purpose?

1 Answer

0 votes
by (108k points)

There are many approaches, the first approach is, to use the ARIMAResults that includes aic and bic. By their description, these criteria penalize the number of arguments in the model. Therefore, you can use these numbers to compare the models. Hence 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 brute

grid = (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 with finish=None.

The 2nd approach is you can use ARIMAResults.predict() to cross-validate alternative models. The best way is to keep the end of the time series (say most recent 5% of data) out of the sample and use this feature to obtain the test error of the fitted models.

Interested in learning Python? Enroll in our Python Course now!

Related questions

0 votes
1 answer
0 votes
1 answer
asked Dec 22, 2020 in Python by ashely (50.2k points)
0 votes
1 answer
asked Dec 12, 2020 in Python by ashely (50.2k points)
0 votes
1 answer

Browse Categories

...