siruiji / ml4frm Goto Github PK
View Code? Open in Web Editor NEWMachine learning for financial risk management
Home Page: https://www.oreilly.com/library/view/machine-learning-for/9781492085249/
Machine learning for financial risk management
Home Page: https://www.oreilly.com/library/view/machine-learning-for/9781492085249/
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!
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()
Thank you for your work. It would be nice if you could give the relevant data in the code
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.