It has been a while since my last blog post Moving Towards Real Time Calculation of VaR – Real Time VaR Part 4 because I was in my final semester to complete my degree. We successfully finished the project on time and it will be merged into the main repository in near future (after which I will provide the link to the readers). There are a lot of changes happened after my last blog post and I am trying my best to blog them all to continue this blog post series.

As a quick recap from my previous blog post, I explained the basic Historical Simulation process that can be used to compute the Value at Risk (VaR) indicator. In doing so I described the structure of the associated classes and the algorithm as well.

Today on my blog I’m going to discuss the improvements I did for the Historical Simulation in order to convert the batch mode version to a more real-time version so that it can compute VaR as and when new events come from the stream. I did **three** (3) **improvements** and they are explained one by one as following.

**Improvement 1: **

The conversion of existing batch mode version of Historical Simulation was completed step by step. Due to the nature of batch mode Historical Simulation, if a new asset X is added to the input stream after some time, then to calculate VaR of all portfolios that had X, we had to wait until the batch_size number of events were collected. Due to this, the extension was not able to produce a VaR for those portfolios for a considerable amount of time (even though a VaR should be produced for each price change of X). Therefore, as the first improvement, we changed the considered time period. That is, the version 1 of real time Historical Simulation calculates VaR for each input event instead of waiting to fill a collection of events.

**Improvement 2: **

The second improvement was to prevent storing historical prices within each asset and incorporating only the latest price value for the calculation. All three methods require return values calculated from the historical prices. Therefore, instead of storing historical prices and later calculating the return values by iterating those prices, we decided to store the return values directly. Whenever a new event comes, it is incorporated to change the return values list of the corresponding asset. This reduced the number of calculation steps. It can be explained using an example as following.

If we have 10 historical prices, let’s define the return value calculated from i^{th} and (i + 1)^{th} prices as R(i, i+1). The return values are computed as;

R(i, i+1) = ln(P_{i+1}/P_{i})

WhereP_{i} is the i^{th} stock price. Then the return values are:

*R(1, 2), R(2, 3), R(3, 4), R(4, 5), R(5, 6) , R(6, 7), R(7, 8), R(8, 9), R(9, 10)*

When the 11^{th} value comes and if the batch size is 9, then we have to remove the oldest price and add the new price. Now the return values will be:

*R(2, 3), R(3, 4), R(4, 5), R(5, 6) , R(6, 7), R(7, 8), R(8, 9), R(9, 10), R(10, 11)*

The intermediate return values were not changed. The first return value was removed and the new return value was added. In this manner, we do not need to calculate the intermediate return values (which leads to the ultimate loss values) again and again.

**Improvement 3:**

The final improvement was to stop simulating all the assets when only a single asset was changed and calculating the VaR value of a portfolio in an incremental manner. VaR metric can be calculated incrementally using the following equation.

*New VaR metric = Previous VaR metric +Adjustment of changed asset*

When calculating VaR incrementally using Historical Simulation first the return values of each asset are plotted as histograms. Then from these histograms, the return values corresponding to the 95% percentile are selected. This return value is the one that corresponds to the worst risk of that asset. Each such return value is then multiplied by the latest price of the corresponding asset. Then this new price is multiplied by the quantity held by the portfolio for that asset. These give the individual contribution of the assets to the VaR metric of the portfolio. They are added together to get the final VaR value of the portfolio.

These improvements describe the version I of Historical Simulation we came up with. However, once we did performance and accuracy testing, we identified that this version does not give better accurate results. Hence, we decided to design another version of Historical Simulation which serves the purpose of real-time VaR computation. This will be discussed in the coming blogs of this series.

Hope you enjoyed this blog post!