牽繩上的狗?(第 2 部分)
為了驗證取暖油與天然氣價格具有共整合關係,先分別對兩者套用 Dickey-Fuller 檢定,顯示它們是隨機漫步。接著對兩者的差值進行檢定,結果應該會強烈否決隨機漫步假說。取暖油與天然氣價格已預先載入至 DataFrame HO 與 NG。
本練習屬於課程
Python 中的時間序列分析
練習說明
- 分別對
HO與NG進行 adfuller 檢定,並將結果儲存起來(結果為清單)- adfuller 的引數必須是 series,因此你需要包含欄位
'Close' - 只列印 p-value(清單中的第 [1] 個項目)
- adfuller 的引數必須是 series,因此你需要包含欄位
- 對價差做同樣的事,再次將
HO換算單位,並使用各自 DataFrame 的'Close'欄位
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Import the adfuller module from statsmodels
from statsmodels.tsa.stattools import adfuller
# Compute the ADF for HO and NG
result_HO = adfuller(____)
print("The p-value for the ADF test on HO is ", result_HO[1])
result_NG = ___(NG['Close'])
print("The p-value for the ADF test on NG is ", result_NG[1])
# Compute the ADF of the spread
result_spread = adfuller(7.25 * ___ - ___)
print("The p-value for the ADF test on the spread is ", result_spread[1])