開始使用免費開始

估計模型階數:資訊準則

判斷模型階數的另一個方法是檢視 Akaike 資訊準則(AIC)與貝葉斯資訊準則(BIC)。這些指標會根據估計出的參數評估擬合優度,同時對模型中的參數數量施加懲罰項。你將使用上一個練習中的 AR(2) 模擬資料(已存為 simulated_data_2),並在 AR(p) 中將階數 p 從 0 變動到 6,計算相對應的 BIC。

本練習屬於課程

Python 中的時間序列分析

檢視課程

練習說明

  • 匯入可用來估計參數並計算 BIC 的 ARIMA 模組。
  • 初始化一個 numpy 陣列 BIC,用來儲存每個 AR(p) 模型的 BIC。
  • 針對階數 p 進行迴圈,p = 0,…,6。
    • 對每個 p,將資料擬合為階數為 p 的 AR 模型。
    • 對每個 p,使用 res.bic 屬性(不加括號)來儲存 BIC 值。
  • 繪製 BIC 隨 p 變化的圖(作圖時略過 p=0,僅繪製 p=1,…,6)。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Import the module for estimating an ARIMA model
from statsmodels.tsa.arima.model import ARIMA

# Fit the data to an AR(p) for p = 0,...,6 , and save the BIC
BIC = np.zeros(7)
for p in range(7):
    mod = ARIMA(simulated_data_2, order=(___,___,___))
    res = mod.fit()
# Save BIC for AR(p)    
    BIC[p] = res.___
    
# Plot the BIC as a function of p
plt.plot(range(1,7), BIC[1:7], marker='o')
plt.xlabel('Order of AR Model')
plt.ylabel('Bayesian Information Criterion')
plt.show()
編輯並執行程式碼