예제 순서
1.US Wholesale Price Index (WPI) 데이터 Set에 ARIMA order (1,1,1)을 넣어서 추정
2.1번 예제에 MA(4) 항을 넣어서 계절적 특성(seasonal effect) 을 추가한다
3.월별 항공사 데이터에 ARIMA order(2,1,0) 과 Seasonal order(1,1,0,12)를 넣기
4.외인성 회귀 변수가 있는 ARIMA (1,1) 모델의 사용
5.Dynamic Forecasting
===================================
Example - 5 - Dynamic Forecasting
Code# Datasetraw = pd.read_stata(BytesIO(friedman2))raw.index = raw.timeraw.index.freq = "QS-OCT"data = raw.loc[:'1981']# Variablesendog = data.loc['1959':, 'consump']exog = sm.add_constant(data.loc['1959':, 'm2'])nobs = endog.shape[0]# Fit the modelmod = sm.tsa.statespace.SARIMAX(endog.loc[:'1978-01-01'], exog=exog.loc[:'1978-01-01'], order=(1,0,1))fit_res = mod.fit(disp=False, maxiter=250)print(fit_res.summary())
mod = sm.tsa.statespace.SARIMAX(endog, exog=exog, order=(1,0,1))res = mod.filter(fit_res.params)# In-sample one-step-ahead predictionspredict = res.get_prediction()predict_ci = predict.conf_int()**get_prediction() 에 값이 들어있지 않으면 바로 다음 data를 예측해준다**# Dynamic predictionspredict_dy = res.get_prediction(dynamic='1978-01-01')predict_dy_ci = predict_dy.conf_int()**예측의 시작 날짜를 '1978-01-01'로 지정한다**# Graphfig, ax = plt.subplots(figsize=(9,4))npre = 4ax.set(title='Personal consumption', xlabel='Date', ylabel='Billions of dollars')# Plot data pointsdata.loc['1977-07-01':, 'consump'].plot(ax=ax, style='o', label='Observed')# Plot predictionspredict.predicted_mean.loc['1977-07-01':].plot(ax=ax, style='r--', label='One-step-ahead forecast')ci = predict_ci.loc['1977-07-01':]ax.fill_between(ci.index, ci.iloc[:,0], ci.iloc[:,1], color='r', alpha=0.1)predict_dy.predicted_mean.loc['1977-07-01':].plot(ax=ax, style='g', label='Dynamic forecast (1978)')ci = predict_dy_ci.loc['1977-07-01':]ax.fill_between(ci.index, ci.iloc[:,0], ci.iloc[:,1], color='g', alpha=0.1)legend = ax.legend(loc='lower right')
예측의 오차그래프 제작
# Prediction error# Graphfig, ax = plt.subplots(figsize=(9,4))npre = 4ax.set(title='Forecast error', xlabel='Date', ylabel='Forecast - Actual')# In-sample one-step-ahead predictions and 95% confidence intervalspredict_error = predict.predicted_mean - endogpredict_error.loc['1977-10-01':].plot(ax=ax, label='One-step-ahead forecast')ci = predict_ci.loc['1977-10-01':].copy()ci.iloc[:,0] -= endog.loc['1977-10-01':]ci.iloc[:,1] -= endog.loc['1977-10-01':]ax.fill_between(ci.index, ci.iloc[:,0], ci.iloc[:,1], alpha=0.1)# Dynamic predictions and 95% confidence intervalspredict_dy_error = predict_dy.predicted_mean - endogpredict_dy_error.loc['1977-10-01':].plot(ax=ax, style='r', label='Dynamic forecast (1978)')ci = predict_dy_ci.loc['1977-10-01':].copy()ci.iloc[:,0] -= endog.loc['1977-10-01':]ci.iloc[:,1] -= endog.loc['1977-10-01':]ax.fill_between(ci.index, ci.iloc[:,0], ci.iloc[:,1], color='r', alpha=0.1)legend = ax.legend(loc='lower left');legend.get_frame().set_facecolor('w')
댓글 없음:
댓글 쓰기