Financial risk forecasting book code

Book code

The following codes implement all the methods covered in the book, where possible.

In addition to the original R and MATLAB used in the book, Python and Julia implementations are also provided.

All code was verified in June 2022 to run on Python 3.9.12, Matlab R2022a, R 4.2.0 and Julia 1.7.3 with libraries current at that time. Some libraries change all the time, like Python's Pandas and Numpy, as well as various Matlab libraries, so be alert to that, especially if you use Python or Matlab.

The data files to be used with the code are:

Packages

We use external packages for estimating GARCH in every language. Currently, the packages used are:

R

The rugarch package for estimation and simulation of univariate models, and the rmgarch for multivariate models.

MATLAB

The MFE Toolbox by Kevin Sheppard includes both univariate and multivariate volatility models estimation. DCC in multivariate will not run with the latest Matlab version.

Python

The ARCH package by Kevin Sheppard allows univariate volatility estimations. Multivariate volatility estimation is currently not supported by this nor other Python package.

Julia

The ARCHModels.jl package by Simon Broda allows for direct univariate GARCH estimations, along with CCC and DCC multivariate volatility modelling.

If anybody suggests alternative implementations to what is here, we would be happy to include a link.

Any bug fixes are more than welcome.

I would like to thank Alvaro Aguirre, Jia Fan and Yuyang Lin for developing and updating the code and notebooks

Pairwise code listings

The following code is presented pairwise (e.g. R and MATLAB, R and Python etc) for comparison.

Listing numbers correspond to the numbered R/MATLAB listing pairs in the book.

An additional Appendix section is provided as a short introduction, based on Appendix B/C in the book.

For more detailed documentation, please consult the book.

Each piece of code is labeled by the last date it got updated. If the date is 2011, then it is identical to the book. If it is more recent, some bug fix or improvement has been implemented.

Chapter Name R/MATLAB R/Python R/Julia MATLAB/Python MATLAB/Julia Python/Julia
1. Financial Markets, Prices and Risk
2. Univariate Volatility Modeling
3. Multivariate Volatility Models
4. Risk Measures
5. Implementing Risk Forecasts
6. Analytical Value–at–Risk for Options and Bonds
7. Simulation Methods for VaR for Options and Bonds
8. Backtesting and Stress Testing
9. Extreme Value Theory
Appendix: Introduction

Jupyter notebook implementation

Below is an implementation of the code using Jupyter notebooks. The formatted output is also downloadable as a .html file, for reference.

Format R MATLAB Python Julia
Jupyter (.ipynb)
Webpage (.html)


Financial Risk Forecasting
Market risk forecasting with R, Julia, Python and Matlab. Code, lecture slides, implementation notes, seminar assignments and questions.
© All rights reserved, Jon Danielsson,