Hello.
I have 2 problems with the file "Implementation_of_the_GARCH_Model.ipynb" in the "Financial Time Series Analysis for Trading" course.
The first one is regarding the "predict_volatility" function in this file. If some one could explain me with some detail why the function makes the first shift:
- forecasted_variance = gm_fit.forecast(horizon=1).variance.values[-1]
and then why to create the column 'GARCH_predicted_volatility' makes the second shift:
- data['GARCH_predicted_volatility'] = data['GARCH_predicted_volatility'].shift(1)
This first problem is related with the second one. And it is that the file only offers the 'GARCH_predicted_volatility' as per the dates in the data file (past dates), but not for the next date to the last one (future date), that is what I´d expected if I have to predict volatility.
I apreciate your comments on that fact.
Thank you very much in advance.
Hi Alejandro,
The function, "predict_volatility" is not exactly shifting any value. It is only storing the forecast for the next day in a variable called "forecasted_variance".
In essence, the code, SP500_data['GARCH_predicted_volatility'] = SP500_data['log_returns'].rolling(252).apply(predict_volatility, raw=True) is passing a rolling window of size 252, which means that for each data point, it considers the past 252 data points (one year of trading days). These data points are the logarithmic returns of the S&P 500. Within each rolling window, it applies the predict_volatility
function defined earlier in the code to calculate the GARCH predicted volatility. The raw=True
argument indicates that the rolling window should be passed as a NumPy array directly to the function without any additional processing.
The result is the predicted volatility from the GARCH model for the next day, as defined in the function "predict_volatility"
Basically, on 28 August date index, the 'GARCH_predicted_volatility' column will have the predicted volatility for 29 August (which is the next day), and so on.
Now, you have the actual historical volatility in the column ['actual_historical_volatility'] of SP500_data. So
So on 28 August, imagine that 'GARCH_predicted_volatility' for 29 August was 16. But you have to compare it with the actual historical volatility on 29 August.
This is the reason why the line of code "SP500_data['GARCH_predicted_volatility'] = SP500_data['GARCH_predicted_volatility'].shift(1)" is used.
I hope this was helpful.
Thank you very much Rekhit. Yes, it has been very helpful. But my point is that if I use this code to predict tomorrow's volatility, and the last day of the time series is today, the code gives me the prediction till today, but I still need the tomorrow's volatility prediction that do not appear in the dataframe due to the shifted one position of the 'GARCH_predicted_volatility' column. So, how could I modify the code to get the next day predicted volatility in the same dataframe or in a separate sentence? Thank you in advance for your answer.
Hi Alejandro,
In the notebook, before you shift the 'GARCH_predicted_volatility' using the following code
Shift the GARCH predicted volatility to match with the actual historical volatility on each day
SP500_data['GARCH_predicted_volatility'] = SP500_data['GARCH_predicted_volatility'].shift(1)
You can create a variable, for example, last_day_volatility and store the predicted volatility in the variable using the code as "last_day_volatility = SP500_data['GARCH_predicted_volatility'][-1]"
However, do note that the notebook is provided to give you an understanding of how GARCH model is used for prediction and how it compares to the actual volatility.
The trading strategy based on GARCH model is present in section 25 unit 9.
Hope this helps.
Thank you very much Rekhit. It has been very useful. Regards.