Coder Social home page Coder Social logo

siruiji / ml4frm Goto Github PK

View Code? Open in Web Editor NEW
9.0 1.0 3.0 84 KB

Machine learning for financial risk management

Home Page: https://www.oreilly.com/library/view/machine-learning-for/9781492085249/

Python 100.00%
market-risk time-series-analysis volatility-modeling credit-risk liquidity-risk operational-risk other-financial-risk-modeling

ml4frm's People

Contributors

siruiji avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

ml4frm's Issues

Error

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())

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()

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.