開始使用免費開始

使用 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()
編輯並執行程式碼