套用 MA 模型
股價在買入與賣出報價之間來回跳動,會造成一階(lag 1)自相關為負,但在超過 1 的滯後期則沒有自相關。使用 MA(1) 模型會得到相同的 ACF 型態。因此,你將把 MA(1) 模型套用到上一個練習中的日內股價資料。
第一步是從 intraday 中的價格計算逐分鐘報酬率,並繪製自相關函數圖。你應該會觀察到 ACF 看起來像 MA(1) 程序。接著,像你對模擬資料所做的一樣,把資料配適到 MA(1) 模型。
本練習屬於課程
Python 中的時間序列分析
練習說明
- 從 statsmodels 匯入
plot_acf和ARIMA模組 - 由價格計算逐分鐘報酬率:
- 使用
.pct_change()方法計算報酬率 - 使用 pandas 的
.dropna()方法刪除第一列為 NaN 的報酬率
- 使用
- 繪製最長到 60 分鐘滯後的 ACF 圖
- 將報酬率資料配適到 MA(1) 模型,並印出 MA(1) 的參數
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Import plot_acf and ARIMA modules from statsmodels
from statsmodels.graphics.tsaplots import plot_acf
from statsmodels.tsa.arima.model import ARIMA
# Compute returns from prices and drop the NaN
returns = intraday.___
returns = returns.___
# Plot ACF of returns with lags up to 60 minutes
plot_acf(___, ___)
plt.show()
# Fit the data to an MA(1) model
mod = ARIMA(___, order=(0,0,1))
res = mod.fit()
print(res.params[1])