使用 `arch` 包通过 EGARCH(1,1) 预测波动率

Forecasting Volatility by EGARCH(1,1) using `arch` Package

目的

我想通过 EGARCH(1,1) 模型使用 arch package 预测每日波动率 .
预测区间:01-04-201512-06-2018(mm-dd-yyyy格式)

因此我应该从 20132015 获取数据(例如)以在其上拟合 EGARCH(1,1) 模型,然后预测 01-04-2015 的每日波动率到12-06-2018


代码

所以我试着这样写:

# Packages That we need
from pandas_datareader import data as web
from arch import arch_model
import pandas as pd
#---------------------------------------

# grab Microsoft daily adjusted close price data from '01-03-2013' to '12-06-2018' and store it in DataFrame
df = pd.DataFrame(web.get_data_yahoo('MSFT' , start='01-03-2013' , end='12-06-2018')['Adj Close'])

#---------------------------------------

# calculate daily rate of return that is necessary for predicting daily Volatility by EGARCH
daily_rate_of_return_EGARCH = np.log(df.loc[ : '01-04-2015']/df.loc[ : '01-04-2015'].shift())
# drop NaN values
daily_rate_of_return_EGARCH = daily_rate_of_return_EGARCH.dropna()

#---------------------------------------

# Volatility Forecasting By EGARCH(1,1)
model_EGARCH = arch_model(daily_rate_of_return_EGARCH, vol='EGARCH' , p = 1 , o = 0 , q = 1)
fitted_EGARCH = model_EGARCH.fit(disp='off')

#---------------------------------------

# and finally, Forecasting step
# Note that as mentioned in `purpose` section, predict interval should be from '01-04-2015' to end of the data frame
horizon = len(df.loc['01-04-2015' : ])
volatility_FORECASTED = fitted_EGARCH.forecast(horizon = horizon , method='simulation')

错误

然后我得到这个错误:

MemoryError                               Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_12900/1021856026.py in <module>
      1 horizon = len(df.loc['01-04-2015':])
----> 2 volatility_FORECASTED = fitted_EGARCH.forecast(horizon = horizon , method='simulation') 

MemoryError: Unable to allocate 3.71 GiB for an array with shape (503, 1000, 989) and data type float64

arch 似乎要保存大量数据。

预期结果

我期望的是一个简单的 pandas.Series,其中包含从 '01-04-2015''12-06-2018' 每日波动率预测 。准确地说,我的意思是这样的:
(注意:日期格式 --> mm-dd-yyyy)

    (DATE)     (VOLATILITY)
'01-04-2015'      .....
'01-05-2015'      .....
'01-06-2015'      .....
.                   .
.                   .
.                   .
'12-06-2018'      .....

我怎样才能做到这一点?

你只需要传递reindex=False关键字,内存需求就会大幅下降。您需要最新版本的 arch 包才能使用此功能,该功能会将预测的输出形状更改为仅包含预测值,因此对齐方式与历史行为不同。

# Packages That we need
from pandas_datareader import data as web
from arch import arch_model
import pandas as pd
#---------------------------------------

# grab Microsoft daily adjusted close price data from '01-03-2013' to '12-06-2018' and store it in DataFrame
df = pd.DataFrame(web.get_data_yahoo('MSFT' , start='01-03-2013' , end='12-06-2018')['Adj Close'])

#---------------------------------------

# calculate daily rate of return that is necessary for predicting daily Volatility by EGARCH
daily_rate_of_return_EGARCH = np.log(df.loc[ : '01-04-2015']/df.loc[ : '01-04-2015'].shift())
# drop NaN values
daily_rate_of_return_EGARCH = daily_rate_of_return_EGARCH.dropna()

#---------------------------------------

# Volatility Forecasting By EGARCH(1,1)
model_EGARCH = arch_model(daily_rate_of_return_EGARCH, vol='EGARCH' , p = 1 , o = 0 , q = 1)
fitted_EGARCH = model_EGARCH.fit(disp='off')

#---------------------------------------

# and finally, Forecasting step
# Note that as mentioned in `purpose` section, predict interval should be from '01-04-2015' to end of the data frame
horizon = len(df.loc['01-04-2015' : ])
volatility_FORECASTED = fitted_EGARCH.forecast(horizon = horizon , method='simulation', reindex=False)