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 August 2019 to run on R 3.6, MATLAB 2018a, Python 3.6.5 and Julia 0.6.2.

The following utility functions and files can be useful:

  1. Simulated stock index (index.csv) and stock prices (stocks.csv) for use with the code
  2. Black-Scholes routines that can be downloaded for R and MATLAB
  3. To download data in MATLAB from Yahoo Finance, hist_stock_data.m
  4. A GARCH(1,1), t-GARCH(1,1) and APARCH(1,1) estimation/simulation package in Julia

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

Any bug fixes are more than welcome.

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.

Some of the book code, especially those implementing GARCH, reflects the state of the available libraries in 2011.

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)