A quick recap from my last blog post “Moving towards Real Time Calculation of VaR – Real Time VaR Part 3”:

I discussed about the technical background of the project. In doing so I explained the structure of a basic Siddhi query, the types of extensions provided in WSO2 Data Analytics Server, and the Siddhi query for the VaR calculation using Historical Simulation.

This is the 4^{th} blog post of the series. As promised in the last one, I discuss more details about how the Historical Simulation was implemented.

To start with, I will explain the structure of the classes.

**VarPortfolioCalculator**– this class maintains an asset list that stores the historical values of each asset. Size of historical value list of one asset is equal to the batch size (in our case it is 250). It contains the following methods:- addEvent(Object data[]) – events coming from the input stream are added to the asset list. data[] object contains the symbol and the price value. The price value is added to the corresponding historical value list of the asset represented by the symbol.
- removeEvent(String symbol) – removes the oldest historical value of the given asset. This simulates the sliding window concept.
- processData(Portfolio portfolio) – an abstract method that will calculate the VaR using the historical values.
- calculateValueAtRisk(Object data[]) – call the corresponding processData() method to get VaR for each portfolio that contains the symbol given in data[].

**HistoricalVaRCalculator**– this class extends the VarPortfolioCalculator class and implements the following method.- processData(Portfolio portfolio) – calculate the return values for each asset using the historical values.
R

_{i}= ln(S_{i}/S_{i-1})Calculate the new price values P

_{i}= (1 + R_{i}) * S_{latest}where S_{latest }is the current price of that asset.Multiply this price value by the quantity held for that asset. Then the i

^{th}portfolio value can be calculated as (summation for each asset):Portfolio

_{i}= ∑ P_{i }* quantity_heldCalculate the current portfolio as (summation for each asset):

Portfolio_Value = ∑ S

_{latest }* quantity_heldThe loss value can be calculated as:

Loss

_{i}= Portfolio_Value – Portfolio_{i}From these Loss

_{i}values, we construct a histogram. To construct it we use DescriptiveStatistics provided in org.apache.commons.math3.stat.descriptive package.Then using the given confidence level, we calculate the VaR value for that portfolio.

This VaR value is given as the output of the processData(Object data[]) method.

- processData(Portfolio portfolio) – calculate the return values for each asset using the historical values.
**HistoricalVaRStreamProcessor**– this class is used to implement the Historical Simulation extension. It extends the StreamProcessor class. It has the following methods.- init() – within this method the general parameters are initialized. They are the confidence level, the batch size and the VaR calculator.
- process() – gets the input values from the stream and process them as chunks. Finally populate the output value to the stream.

This is how the Historical Simulation is used to calculate the VaR whenever a stock price change happens. This implementation can further be improved in various ways. They are discussed later in this series.

## One thought on “Moving Towards Real Time Calculation of VaR – Real Time VaR Part 4”