Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Image: ubuntu2004
Backtesting
Camilo Garcia Trillos 2020
In this notebook
In this notebook, we continue our work on estimating Value at Risk (at level 99%) and Expected Shortfall (at level 95%) for one stock (simplest case), by focusing in backtetsing these risk measures.
Since we are continuing the work we initiated the previous week, we will run from this notebook notebook with '11. Risk measure estimation', and build on top of the results. It is recommended that you open also this file in parallel to this one, in case you have any doubts in the notation.
Now, to import that file and its results in Jupyter notebooks, we can use the magic command 'run' (To do so in pure Python we can import one file into another using import).
Rolling calculation and backtesting
Having recovered the calculations of that notebook, we can start the backtesting task by constructing a database with only the information that we need.
Now, we are going to build a function that, receives the above database, and returns the database after adding columns for:
The closed form approximated VaR at level alpha_VaR
The historical simulated VaR at level alpha_VaR
The closed form approximated ES at level alpha_ES
The historical simulated ES at level alpha_ES
The function starts to fill the rows at a given date 'first_calc_date' and, unless specified the number of days in rolling history to use, uses the whole history up to that point for the calculation.
Note: The function is not designed for optimal performance.
Note that we calculated the risk measures for each date using only the information available before that date.
In the previous plot we show the calculated risk measures and the exceedances over the closed form value at risk, that is the occasions for which the losses where larger than the estimated value. Note that they seem to concentrate before year 2010. Let us make a zoom
It seems quite clear that the risk measure estimation in this time frame is not good.
Let us repeat the exercise but this time with a rolling window of 5 years (approximately 1250 dates).
We can see that in this case, because the information during the crisis is lost, we have excesses also in the latest part of the database.
Homework
Consider the database in '~/Data/basket_20171201_10y.csv'. Perform a backtest of a historical simulation estimation for a portfolio with equal weights on this basket.