Moving Towards Real Time Calculation of VaR – Real Time VaR Part 4

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 4th 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.

                             Ri = ln(Si/Si-1)

      Calculate the new price values Pi = (1 + Ri) * Slatest where Slatest is the current price of that asset.

      Multiply this price value by the quantity held for that asset. Then the ith portfolio value can be calculated as (summation for each asset):

                              Portfolioi =  ∑ P* quantity_held

      Calculate the current portfolio as (summation for each asset):

                              Portfolio_Value = ∑ Slatest * quantity_held

      The loss value can be calculated as:

                              Lossi = Portfolio_Value – Portfolioi

      From these Lossi 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.

  • 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

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s