Open dibbydoob opened 5 months ago
Hello, Thanks for putting the code to the excellent book up in Github. I notice a very small error in the code - I don't know if it is local of whether the error is common to all of the AR model codes but I'm considering the file: Volatility_prediction_GARCH.py:
Do let me know if you disagree with my reasoning regarding the square root!
- the plots should read: plt.plot(np.sqrt(forecast.variance.iloc[-len(split_date):]), label='Volatility Prediction-GARCH') where one should take the square root of the arch_model forecat since this model actually forecasts variance, not volatility in order to plot it against the realised volatility.
My revised code: def load_raw_data(ticker, start_date, end_date): price = yf.download(ticker, start_date, end_date)['Adj Close'] ret = price.pct_change()[1:] # take the scaling out here ret.dropna(inplace=True) return ret
def model_train(ret): global garch, q, best_param bic_garch = [] for p in range(1, 5): for q in range(1, 5): garch = arch_model(ret, mean='zero', vol='GARCH', p=p, o=0, q=q, rescale=True).fit(disp='off') # add scaling here by adding rescale=True bic_garch.append(garch.bic) if garch.bic == np.min(bic_garch): best_param = p, q garch = arch_model(ret, mean='zero', vol='GARCH', p=best_param[0], o=0, q=best_param[1], rescale=True).fit(disp='off') scale = garch.scale # take scale value here print(garch.summary())
realized_vol = ret.rolling(5).std() n = 252 split_date = ret.iloc[-n:].index forecast = garch.forecast(start=split_date[0]) plt.figure(figsize=(10, 6)) plt.plot(realized_vol, label='Realized Volatility') plt.plot(np.sqrt(forecast.variance.iloc[-len(split_date):] / np.power(scale, 2)), label='Volatility Prediction-GARCH') # divide by scaling squared hereand take the square root plt.title('Volatility Prediction with GARCH') plt.legend() plt.savefig('corrected_plot.png') plt.show()
Hello,
I want to say thank you so much for spend your time and patience in interview this file, and I believe that you did a fantistic job in correcting code. You provide a great idea in scaling raw data, and correct my error in present variance as volitatility. Your methology is not only suitable for the current code, but also can be used to correct rest of volitatility prediction files.
Thank you again for your amazing work.
Bro do you mind providing me a Read.txt or guide to run the project. That would really help me. Thank you
Hello, Thanks for putting the code to the excellent book up in Github. I notice a very small error in the code - I don't know if it is local of whether the error is common to all of the AR model codes but I'm considering the file: Volatility_prediction_GARCH.py:
Do let me know if you disagree with my reasoning regarding the square root! 1) the plots should read: plt.plot(np.sqrt(forecast.variance.iloc[-len(split_date):]), label='Volatility Prediction-GARCH') where one should take the square root of the arch_model forecat since this model actually forecasts variance, not volatility in order to plot it against the realised volatility.
My revised code: def load_raw_data(ticker, start_date, end_date): price = yf.download(ticker, start_date, end_date)['Adj Close'] ret = price.pct_change()[1:] # take the scaling out here ret.dropna(inplace=True) return ret
def model_train(ret): global garch, q, best_param bic_garch = [] for p in range(1, 5): for q in range(1, 5): garch = arch_model(ret, mean='zero', vol='GARCH', p=p, o=0, q=q, rescale=True).fit(disp='off') # add scaling here by adding rescale=True bic_garch.append(garch.bic) if garch.bic == np.min(bic_garch): best_param = p, q garch = arch_model(ret, mean='zero', vol='GARCH', p=best_param[0], o=0, q=best_param[1], rescale=True).fit(disp='off') scale = garch.scale # take scale value here print(garch.summary())