[英]Forecasting Volatility by EGARCH(1,1) using `arch` Package
我想通過EGARCH(1,1) model 使用arch
package預測每日波動率。
預測間隔: 01-04-2015
至12-06-2018
(mm-dd-yyyy 格式)
因此我應該從2013
到2015
獲取數據(例如)以適合 EGARCH(1,1) model ,然后預測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
關鍵字,memory 要求就會急劇下降。 您需要最新版本的 Arch package 才能使用此功能,該功能將預測的 output 形狀更改為僅包含預測值,因此 alignment 與歷史行為不同。
# 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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.