import matplotlib.pyplot as plt
import pandas as pd
df = pd.read_excel('beer.xlsx')
df.head()
quarter
production
0
1956-01
284
1
1956-04
213
2
1956-07
227
3
1956-10
308
4
1957-01
262
y = df.production
y.plot()
<Axes: >
ARMA(1, 1)
from statsmodels.tsa.api import SARIMAX
arma = SARIMAX(y, order=(1, 0, 1)).fit()
arma.summary()
SARIMAX Results
Dep. Variable:
production
No. Observations:
218
Model:
SARIMAX(1, 0, 1)
Log Likelihood
-1168.017
Date:
Sat, 01 Jul 2023
AIC
2342.034
Time:
19:01:18
BIC
2352.187
Sample:
0
HQIC
2346.135
- 218
Covariance Type:
opg
coef
std err
z
P>|z|
[0.025
0.975]
ar.L1
0.9998
0.001
1031.315
0.000
0.998
1.002
ma.L1
-0.8493
0.040
-21.298
0.000
-0.927
-0.771
sigma2
2576.5261
301.003
8.560
0.000
1986.571
3166.482
Ljung-Box (L1) (Q):
0.52
Jarque-Bera (JB):
19.61
Prob(Q):
0.47
Prob(JB):
0.00
Heteroskedasticity (H):
0.97
Skew:
0.68
Prob(H) (two-sided):
0.88
Kurtosis:
2.45
Warnings: [1] Covariance matrix calculated using the outer product of gradients (complex-step).
y.plot()
arma.predict(0, 250).plot()
<Axes: >
from statsmodels.tsa.stattools import kpss
kpss(y)
C:\Users\eupho\AppData\Local\Temp\ipykernel_28656\4027067367.py:2: InterpolationWarning: The test statistic is outside of the range of p-values available in the
look-up table. The actual p-value is smaller than the p-value returned.
kpss(y)
C:\Users\eupho\AppData\Local\Temp\ipykernel_28656\757044700.py:1: InterpolationWarning: The test statistic is outside of the range of p-values available in the
look-up table. The actual p-value is greater than the p-value returned.
kpss(yd)
c:\Users\eupho\anaconda3\lib\site-packages\statsmodels\tsa\base\tsa_model.py:473: ValueWarning: An unsupported index was provided and will be ignored when e.g. forecasting.
self._init_dates(dates, freq)
c:\Users\eupho\anaconda3\lib\site-packages\statsmodels\tsa\base\tsa_model.py:473: ValueWarning: An unsupported index was provided and will be ignored when e.g. forecasting.
self._init_dates(dates, freq)
c:\Users\eupho\anaconda3\lib\site-packages\statsmodels\tsa\statespace\sarimax.py:978: UserWarning: Non-invertible starting MA parameters found. Using zeros as starting parameters.
warn('Non-invertible starting MA parameters found.'
SARIMAX Results
Dep. Variable:
production
No. Observations:
217
Model:
SARIMAX(1, 0, 1)
Log Likelihood
-1160.621
Date:
Sat, 01 Jul 2023
AIC
2327.242
Time:
19:01:32
BIC
2337.382
Sample:
0
HQIC
2331.338
- 217
Covariance Type:
opg
coef
std err
z
P>|z|
[0.025
0.975]
ar.L1
-0.0363
0.132
-0.276
0.782
-0.294
0.222
ma.L1
-0.8450
0.050
-16.926
0.000
-0.943
-0.747
sigma2
2573.5432
317.043
8.117
0.000
1952.150
3194.936
Ljung-Box (L1) (Q):
0.26
Jarque-Bera (JB):
18.56
Prob(Q):
0.61
Prob(JB):
0.00
Heteroskedasticity (H):
0.95
Skew:
0.65
Prob(H) (two-sided):
0.82
Kurtosis:
2.42
Warnings: [1] Covariance matrix calculated using the outer product of gradients (complex-step).
yd.plot()
d_arma.predict(0, 250).plot()
c:\Users\eupho\anaconda3\lib\site-packages\statsmodels\tsa\base\tsa_model.py:836: ValueWarning: No supported index is available. Prediction results will be given with an integer index beginning at `start`.
return get_prediction_index(
c:\Users\eupho\anaconda3\lib\site-packages\statsmodels\tsa\base\tsa_model.py:836: FutureWarning: No supported index is available. In the next version, calling this method in a model without a supported index will result in an exception.
return get_prediction_index(
<Axes: >
ARIMA(p, d, q) 모형은 d번 차분한 값을 ARMA(p, q)으로 분석하는 것과 같습니다.
c:\Users\eupho\anaconda3\lib\site-packages\statsmodels\tsa\statespace\sarimax.py:978: UserWarning: Non-invertible starting MA parameters found. Using zeros as starting parameters.
warn('Non-invertible starting MA parameters found.'
SARIMAX Results
Dep. Variable:
production
No. Observations:
218
Model:
SARIMAX(1, 1, 1)
Log Likelihood
-1160.613
Date:
Sat, 01 Jul 2023
AIC
2327.227
Time:
19:01:49
BIC
2337.366
Sample:
0
HQIC
2331.323
- 218
Covariance Type:
opg
coef
std err
z
P>|z|
[0.025
0.975]
ar.L1
-0.0364
0.132
-0.277
0.782
-0.294
0.222
ma.L1
-0.8450
0.050
-16.924
0.000
-0.943
-0.747
sigma2
2573.5643
317.059
8.117
0.000
1952.139
3194.989
Ljung-Box (L1) (Q):
0.26
Jarque-Bera (JB):
18.56
Prob(Q):
0.61
Prob(JB):
0.00
Heteroskedasticity (H):
0.95
Skew:
0.65
Prob(H) (two-sided):
0.82
Kurtosis:
2.42
Warnings: [1] Covariance matrix calculated using the outer product of gradients (complex-step).
y.plot()
arima.predict(0, 250).plot()
<Axes: >
계절성 ARIMA 모형은 계절성에 대해서 별도의 ARIMA 모형으로 분석합니다. seasonal_order의 마지막 값은 계절성의 주기를 나타냅니다.
Warnings: [1] Covariance matrix calculated using the outer product of gradients (complex-step).
y.plot()
sarima.predict(0, 250).plot()
<Axes: >
계절성 ARIMA 모형은 계절성의 주기를 제외하고 p, d, q, P, D, Q 모두 6개의 값을 설정해야 합니다. 아래 코드는 이 6개의 값에 0, 1, 2 세 가지를 대입하여 총 729가지 모형을 시도합니다. 그중 AIC가 가장 낮은 모형을 best_sarima에 설정합니다.