開始使用免費開始

估計模型階數:PACF

辨識 AR 模型階數的一個實用工具是觀察偏自相關函數(Partial Autocorrelation Function,PACF)。在這個練習中,你會模擬兩個時間序列:AR(1) 與 AR(2),並各自計算樣本 PACF。你會發現,對於 AR(1),PACF 在落後 1 期(lag-1)應該顯著,而之後大致為 0;而對於 AR(2),樣本 PACF 在落後 1 與落後 2 期(lag-1 與 lag-2)應該顯著,之後為 0。

就像你先前使用過的 plot_acf 函式,這裡你會使用 statsmodels 模組中的 plot_pacf 函式。

本練習屬於課程

Python 中的時間序列分析

檢視課程

練習說明

  • 匯入用來模擬資料與繪製 PACF 的模組。
  • 模擬一個參數為 \(\small \phi=0.6\) 的 AR(1)(記得 AR 參數的號誌需要反向)。
  • 使用 plot_pacfsimulated_data_1 繪製 PACF 圖。
  • 模擬一個 AR(2),其參數為 \(\small \phi_1=0.6, \phi_2=0.3\)(同樣需要反轉號誌)。
  • 使用 plot_pacfsimulated_data_2 繪製 PACF 圖。

動手互動練習

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

# Import the modules for simulating data and for plotting the PACF
from statsmodels.tsa.arima_process import ArmaProcess
from statsmodels.graphics.tsaplots import plot_pacf

# Simulate AR(1) with phi=+0.6
ma = np.array([1])
ar = np.array([1, -0.6])
AR_object = ArmaProcess(ar, ma)
simulated_data_1 = ___.generate_sample(nsample=5000)

# Plot PACF for AR(1)
plot_pacf(___, lags=20)
plt.show()

# Simulate AR(2) with phi1=+0.6, phi2=+0.3
ma = np.array([1])
ar = np.array([1, ___, ___])
AR_object = ArmaProcess(ar, ma)
simulated_data_2 = ___.generate_sample(nsample=5000)

# Plot PACF for AR(2)
plot_pacf(___, lags=20)
plt.show()
編輯並執行程式碼