如何在 Python 實作 GARCH 模型

Python 中的 GARCH 模型

Chelsea Yang

Data Science Instructor

Python「arch」套件

from arch import arch_model

1 Kevin Sheppard. (2019, March 28). bashtage/arch: Release 4.8.1 (Version 4.8.1). Zenodo. http://doi.org/10.5281/zenodo.2613877
Python 中的 GARCH 模型

工作流程

用三步驟建立 GARCH 模型:

  1. 指定模型
  2. 配適模型
  3. 建立預測
Python 中的 GARCH 模型

模型規格

模型假設:

  • 分配:"normal"(預設)、"t""skewt"
  • 平均模型:"constant"(預設)、"zero""AR"
  • 波動模型:"GARCH"(預設)、"ARCH""EGARCH"

 

basic_gm = arch_model(sp_data['Return'], p = 1, q = 1, 
                      mean = 'constant', vol = 'GARCH', dist = 'normal')
Python 中的 GARCH 模型

模型配適

每隔 n 次迭代顯示配適輸出:

gm_result = gm_model.fit(update_freq = 4)

模型配適過程

關閉顯示:

gm_result = gm_model.fit(disp = 'off')
Python 中的 GARCH 模型

配適結果:參數

以「極大概似法」估計。

print(gm_result.params)
mu          0.077239
omega       0.039587
alpha[1]    0.167963
beta[1]     0.786467
Name: params, dtype: float64
Python 中的 GARCH 模型

配適結果:摘要

print(gm_result.summary())

配適結果摘要

Python 中的 GARCH 模型

配適結果:圖形

gm_result.plot()

配適結果圖

Python 中的 GARCH 模型

模型預測

# Make 5-period ahead forecast
gm_forecast = gm_result.forecast(horizon = 5)
# Print out the last row of variance forecast
print(gm_forecast.variance[-1:])
                 h.1       h.2       h.3       h.4       h.5
Date                                                        
2019-10-10  0.994079  0.988366  0.982913  0.977708  0.972741

列「2019-10-10」中的 h.1:使用該日期(含)之前資料做的 1 期前預測。

Python 中的 GARCH 模型

一起來練習吧!

Python 中的 GARCH 模型

Preparing Video For Download...