--- title: ARIMA keywords: fastai sidebar: home_sidebar nb_path: "nbs/arima.ipynb" ---
{% raw %}
{% endraw %} {% raw %}
{% endraw %} {% raw %}
arima(ap, order=(2, 1, 1), seasonal={'order': (0, 1, 0), 'period': 12}, 
      include_mean=False, method='CSS-ML')['coef']
{'ar1': 0.47051736809750594,
 'ar2': 0.22174549894902976,
 'ma1': -1.088591942427617}
{% endraw %} {% raw %}

predict_arima[source]

predict_arima(model, n_ahead, newxreg=None, se_fit=True)

{% endraw %} {% raw %}
{% endraw %} {% raw %}
predict_arima(res, 10)
(array([448.08972925, 423.7922724 , 453.50022757, 496.68138142,
        508.6158662 , 572.31747306, 659.85480907, 644.26321316,
        546.57452622, 499.81156619]),
 array([11.52472983, 13.1634063 , 14.67466798, 15.63674637, 16.39162602,
        16.98465381, 17.47757493, 17.89954747, 18.27186588, 18.60809127]))
{% endraw %} {% raw %}
predict_arima(res_intercept, 10)
(array([470.00979488, 440.51641849, 442.19288043, 430.30732962,
        425.09870714, 417.37227647, 411.26942366, 404.89077868,
        399.07483755, 393.42076235]),
 array([ 30.69910081,  51.71193144,  61.8938269 ,  71.06720305,
         77.98073384,  83.94820591,  88.93458668,  93.25690786,
         96.99562864, 100.27009363]))
{% endraw %} {% raw %}
newdrift = np.arange(ap.size + 1, ap.size + 10 + 1).reshape(-1, 1)
newxreg = np.concatenate([newdrift, np.sqrt(newdrift)], axis=1)
predict_arima(res_xreg, 10, newxreg=newxreg)
(array([441.89788144, 463.67037804, 489.91183156, 513.32967966,
        528.76742686, 534.25615255, 530.97888695, 522.3581832 ,
        512.68418691, 505.75238209]),
 array([25.04154419, 31.85337573, 33.32711537, 33.3424047 , 34.68174014,
        37.33840844, 39.68800215, 40.77979968, 40.92280668, 40.99296291]))
{% endraw %} {% raw %}
myarima(ap, order=(2, 1, 1), seasonal={'order': (0, 1, 0), 'period': 12}, 
        constant=False, ic='aicc', method='CSS-ML')['aic']
1020.700659230761
{% endraw %} {% raw %}

arima_string[source]

arima_string(model, padding=False)

{% endraw %} {% raw %}
{% endraw %} {% raw %}
arima_string(res_Arima_ex)
'Regression with ARIMA(0,0,0) errors'
{% endraw %} {% raw %}
arima_string(res_Arima)
'ARIMA(0,0,0) with drift        '
{% endraw %} {% raw %}

forecast_arima[source]

forecast_arima(model, h=None, level=None, fan=False, xreg=None, blambda=None, bootstrap=False, npaths=5000, biasadj=None)

{% endraw %} {% raw %}
{% endraw %} {% raw %}

fitted_arima[source]

fitted_arima(model, h=1)

Returns h-step forecasts for the data used in fitting the model.

{% endraw %} {% raw %}
{% endraw %} {% raw %}

auto_arima_f[source]

auto_arima_f(x, d=None, D=None, max_p=5, max_q=5, max_P=2, max_Q=2, max_order=5, max_d=2, max_D=1, start_p=2, start_q=2, start_P=1, start_Q=1, stationary=False, seasonal=True, ic='aicc', stepwise=True, nmodels=94, trace=False, approximation=None, method=None, truncate=None, xreg=None, test='kpss', test_kwargs=None, seasonal_test='seas', seasonal_test_kwargs=None, allowdrift=True, allowmean=True, blambda=None, biasadj=False, parallel=False, num_cores=2, period=1)

{% endraw %} {% raw %}
{% endraw %} {% raw %}
mod = auto_arima_f(ap, period=12, method='CSS-ML', trace=True)
ARIMA(2,1,2)(1,1,1)[12]                   :inf

ARIMA(0,1,0)(0,1,0)[12]                   :1031.5393581671838

ARIMA(1,1,0)(1,1,0)[12]                   :1020.5919299313473

ARIMA(0,1,1)(0,1,1)[12]                   :1021.1978414716

ARIMA(1,1,0)(0,1,0)[12]                   :1020.4966626269295

ARIMA(1,1,0)(0,1,1)[12]                   :1021.1141377784866

ARIMA(1,1,0)(1,1,1)[12]                   :1022.6657137275511

ARIMA(2,1,0)(0,1,0)[12]                   :1022.5909032832317

ARIMA(1,1,1)(0,1,0)[12]                   :1022.5906051178998

ARIMA(0,1,1)(0,1,0)[12]                   :1020.7342476851575

ARIMA(2,1,1)(0,1,0)[12]                   :1021.020659230761

ARIMA(2,1,1)(0,1,0)[12]                   :1021.020659230761
Now re-fitting the best model(s) without approximations...


ARIMA(1,1,0)(0,1,0)[12]                   :1020.4966626269295
{% endraw %} {% raw %}

print_statsforecast_ARIMA(model, digits=3, se=True)

{% endraw %} {% raw %}
{% endraw %} {% raw %}
print_statsforecast_ARIMA(mod)
ARIMA(1,1,0)(0,1,0)[12]                   

Coefficients:
               ar1
coefficient -0.300
s.e.         0.007

sigma^2 = 139.156: log likelihood = -508.20

AIC=1020.4
{% endraw %} {% raw %}

class ARIMASummary[source]

ARIMASummary(model)

ARIMA Summary.

{% endraw %} {% raw %}
{% endraw %} {% raw %}

class AutoARIMA[source]

AutoARIMA(d:Optional[int]=None, D:Optional[int]=None, max_p:int=5, max_q:int=5, max_P:int=2, max_Q:int=2, max_order:int=5, max_d:int=2, max_D:int=1, start_p:int=2, start_q:int=2, start_P:int=1, start_Q:int=1, stationary:bool=False, seasonal:bool=True, ic:str='aicc', stepwise:bool=True, nmodels:int=94, trace:bool=False, approximation:Optional[bool]=None, method:Optional[str]=None, truncate:Optional[bool]=None, test:str='kpss', test_kwargs:Optional[str]=None, seasonal_test:str='seas', seasonal_test_kwargs:Optional[Dict[KT, VT]]=None, allowdrift:bool=True, allowmean:bool=True, blambda:Optional[float]=None, biasadj:bool=False, parallel:bool=False, num_cores:int=2, period:int=1)

An AutoARIMA estimator.

Returns best ARIMA model according to either AIC, AICc or BIC value. The function conducts a search over possible model within the order constraints provided.

Parameters

d: int optional (default None) Order of first-differencing. If missing, will choose a value based on test. D: int optional (default None) Order of seasonal-differencing. If missing, will choose a value based on season_test. max_p: int (default 5) Maximum value of p. max_q: int (default 5) Maximum value of q. max_P: int (default 2) Maximum value of P. max_Q: int (default 2) Maximum value of Q. max_order: int (default 5) Maximum value of p+q+P+Q if model selection is not stepwise. max_d: int (default 2) Maximum number of non-seasonal differences max_D: int (default 1) Maximum number of seasonal differences start_p: int (default 2) Starting value of p in stepwise procedure. start_q: int (default 2) Starting value of q in stepwise procedure. start_P: int (default 1) Starting value of P in stepwise procedure. start_Q: int (default 1) Starting value of Q in stepwise procedure. stationary: bool (default False) If True, restricts search to stationary models. seasonal: bool (default True) If False, restricts search to non-seasonal models. ic: str (default 'aicc') Information criterion to be used in model selection. stepwise: bool (default True) If True, will do stepwise selection (faster). Otherwise, it searches over all models. Non-stepwise selection can be very slow, especially for seasonal models. nmodels: int (default 94) Maximum number of models considered in the stepwise search. trace: bool (default False) If True, the list of ARIMA models considered will be reported. approximation: bool optional (default None) If True, estimation is via conditional sums of squares and the information criteria used for model selection are approximated. The final model is still computed using maximum likelihood estimation. Approximation should be used for long time series or a high seasonal period to avoid excessive computation times. method: str optional (default None) fitting method: maximum likelihood or minimize conditional sum-of-squares. The default (unless there are missing values) is to use conditional-sum-of-squares to find starting values, then maximum likelihood. Can be abbreviated. truncate: bool optional (default None) An integer value indicating how many observations to use in model selection. The last truncate values of the series are used to select a model when truncate is not None and approximation=True. All observations are used if either truncate=None or approximation=False. test: str (default 'kpss') Type of unit root test to use. See ndiffs for details. test_kwargs: str optional (default None) Additional arguments to be passed to the unit root test. seasonal_test: str (default 'seas') This determines which method is used to select the number of seasonal differences. The default method is to use a measure of seasonal strength computed from an STL decomposition. Other possibilities involve seasonal unit root tests. seasonal_test_kwargs: dict optional (default None) Additional arguments to be passed to the seasonal unit root test. See nsdiffs for details. allowdrift: bool (default True) If True, models with drift terms are considered. allowmean: bool (default True) If True, models with a non-zero mean are considered. blambda: float optional (default None) Box-Cox transformation parameter. If lambda="auto", then a transformation is automatically selected using BoxCox.lambda. The transformation is ignored if None. Otherwise, data transformed before model is estimated. biasadj: bool (default False) Use adjusted back-transformed mean for Box-Cox transformations. If transformed data is used to produce forecasts and fitted values, a regular back transformation will result in median forecasts. If biasadj is True, an adjustment will be made to produce mean forecasts and fitted values. parallel: bool (default False) If True and stepwise = False, then the specification search is done in parallel. This can give a significant speedup on multicore machines. num_cores: int (default 2) Allows the user to specify the amount of parallel processes to be used if parallel = True and stepwise = False. If None, then the number of logical cores is automatically detected and all available cores are used. period: int (default 1) Number of observations per unit of time. For example 24 for Hourly data.

Notes

  • This implementation is a mirror of Hyndman's forecast::auto.arima.

References

[1] https://github.com/robjhyndman/forecast

{% endraw %} {% raw %}
{% endraw %} {% raw %}
model = AutoARIMA()
{% endraw %} {% raw %}
model = model.fit(ap)
{% endraw %} {% raw %}
model.predict(h=7)
mean
0 464.631601
1 483.363283
2 490.196788
3 489.549976
4 485.653256
5 481.407363
6 478.248914
{% endraw %} {% raw %}
model.predict(h=7, level=80)
lo_80% mean hi_80%
0 426.326488 464.631601 502.936715
1 419.928953 483.363283 546.797613
2 411.105076 490.196788 569.288501
3 401.920876 489.549976 577.179076
4 393.609048 485.653256 577.697464
5 386.910310 481.407363 575.904416
6 382.073456 478.248914 574.424372
{% endraw %} {% raw %}
model.predict(h=7, level=(80, 90))
lo_90% lo_80% mean hi_80% hi_90%
0 415.467520 426.326488 464.631601 502.936715 513.795682
1 401.946202 419.928953 483.363283 546.797613 564.780365
2 388.683674 411.105076 490.196788 569.288501 591.709902
3 377.079244 401.920876 489.549976 577.179076 602.020708
4 367.515794 393.609048 485.653256 577.697464 603.790718
5 360.121709 386.910310 481.407363 575.904416 602.693017
6 354.809050 382.073456 478.248914 574.424372 601.688778
{% endraw %} {% raw %}
model.predict_in_sample()
mean
0 111.888000
1 112.688513
2 120.747483
3 136.303743
4 124.909752
... ...
139 630.376069
140 567.956065
141 451.814466
142 443.498419
143 374.088365

144 rows × 1 columns

{% endraw %} {% raw %}
model.predict_in_sample(level=50)
lo_50% mean hi_50%
0 91.727745 111.888000 132.048255
1 92.528257 112.688513 132.848768
2 100.587228 120.747483 140.907738
3 116.143488 136.303743 156.463999
4 104.749497 124.909752 145.070007
... ... ... ...
139 610.215814 630.376069 650.536324
140 547.795810 567.956065 588.116320
141 431.654211 451.814466 471.974721
142 423.338164 443.498419 463.658674
143 353.928110 374.088365 394.248620

144 rows × 3 columns

{% endraw %} {% raw %}
model.predict_in_sample(level=(80, 90))
lo_90% lo_80% mean hi_80% hi_90%
0 62.723919 73.582886 111.888000 150.193114 161.052081
1 63.524432 74.383399 112.688513 150.993626 161.852594
2 71.583402 82.442370 120.747483 159.052597 169.911564
3 87.139662 97.998630 136.303743 174.608857 185.467824
4 75.745671 86.604639 124.909752 163.214866 174.073833
... ... ... ... ... ...
139 581.211988 592.070955 630.376069 668.681182 679.540150
140 518.791984 529.650951 567.956065 606.261178 617.120146
141 402.650385 413.509352 451.814466 490.119580 500.978547
142 394.334338 405.193306 443.498419 481.803533 492.662500
143 324.924284 335.783252 374.088365 412.393479 423.252446

144 rows × 5 columns

{% endraw %} {% raw %}
model.model_.summary()
ARIMA(2,1,1)                   

Coefficients:
               ar1    ar2    ma1
coefficient  1.166 -0.460 -0.846
s.e.         0.004  0.006  0.007

sigma^2 = 893.391: log likelihood = -687.03

AIC=1382.05
{% endraw %} {% raw %}
model.summary()
ARIMA(2,1,1)                   

Coefficients:
               ar1    ar2    ma1
coefficient  1.166 -0.460 -0.846
s.e.         0.004  0.006  0.007

sigma^2 = 893.391: log likelihood = -687.03

AIC=1382.05
{% endraw %} {% raw %}
model_x = AutoARIMA(approximation=False)
{% endraw %} {% raw %}
model_x = model_x.fit(ap, np.hstack([np.sqrt(drift), np.log(drift)]))
{% endraw %} {% raw %}
model_x.predict(h=12, X=np.hstack([np.sqrt(newdrift), np.log(newdrift)]), level=(80, 90))
lo_90% lo_80% mean hi_80% hi_90%
0 434.808281 444.848076 480.263550 515.679025 525.718820
1 398.250812 414.132285 470.154335 526.176385 542.057858
2 403.968806 422.259587 486.780495 551.301404 569.592185
3 379.813843 398.925989 466.344269 533.762548 552.874694
4 381.849918 400.962063 468.380343 535.798623 554.910769
5 383.880201 402.992346 470.410626 537.828906 556.941052
6 385.904743 405.016888 472.435168 539.853448 558.965594
7 387.923594 407.035740 474.454019 541.872299 560.984445
8 389.936804 409.048950 476.467230 543.885510 562.997655
9 391.944422 411.056568 478.474848 545.893128 565.005273
{% endraw %} {% raw %}
model_x.predict_in_sample()
mean
0 73.205301
1 116.318916
2 100.318857
3 107.862716
4 108.927618
... ...
139 608.317510
140 564.682721
141 429.362373
142 442.344731
143 391.284051

144 rows × 1 columns

{% endraw %} {% raw %}
model_x.predict_in_sample(level=(80, 90))
lo_90% lo_80% mean hi_80% hi_90%
0 27.750032 37.789827 73.205301 108.620776 118.660570
1 70.863647 80.903442 116.318916 151.734390 161.774185
2 54.863588 64.903383 100.318857 135.734332 145.774127
3 62.407447 72.447242 107.862716 143.278191 153.317985
4 63.472349 73.512144 108.927618 144.343093 154.382887
... ... ... ... ... ...
139 562.862241 572.902036 608.317510 643.732985 653.772780
140 519.227452 529.267247 564.682721 600.098196 610.137991
141 383.907103 393.946898 429.362373 464.777847 474.817642
142 396.889462 406.929257 442.344731 477.760206 487.800001
143 345.828782 355.868577 391.284051 426.699526 436.739321

144 rows × 5 columns

{% endraw %} {% raw %}
model_x.summary()
Regression with ARIMA(0,0,3) errors

Coefficients:
               ma1    ma2    ma3    ex_1    ex_2
coefficient  1.226  0.904  0.552  57.136 -45.775
s.e.         0.023  0.038  0.021  15.549  44.130

sigma^2 = 763.684: log likelihood = -679.81

AIC=1371.61
{% endraw %}