使用 MA 模型進行預測
就像你對 AR 模型所做的,你將使用 MA 模型搭配 statsmodels 的 plot_predict() 函式,來產生樣本內與樣本外的預測。
對於參數 \(\small \theta=-0.9\) 的模擬序列 simulated_data_1,你會繪製樣本內與樣本外的預測。你會觀察到的一個重要差異是:使用 MA(1) 模型做超過 1 期以後的樣本外預測時,預測值會趨近於樣本平均數;而 AR(1) 模型則不然。
本練習屬於課程
Python 中的時間序列分析
練習說明
- 匯入類別
ARIMA,並匯入函式plot_predict - 使用模擬資料
simulated_data_1與模型的 (p,d,q) 次序(此為 MA(1)),order=(0,0,1),建立ARIMA類別的實例,命名為mod - 以
.fit()方法擬合模型mod,並將結果儲存為名為res的結果物件 - 從索引 950 的資料點開始,繪製樣本內資料
- 使用
plot_predict()函式,從資料點 950 開始到 1010 結束,繪製樣本外的預測與信賴區間
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Import the ARIMA and plot_predict from statsmodels
from statsmodels.tsa.arima.model import ARIMA
from statsmodels.graphics.tsaplots import plot_predict
# Forecast the first MA(1) model
mod = ARIMA(___, order=___)
res = mod.fit()
# Plot the data and the forecast
fig, ax = plt.subplots()
simulated_data_1.loc[950:].plot(ax=ax)
plot_predict(res, start=___, end=___, ax=ax)
plt.show()