Re: Monte-carlo stochastic simulation of correlated assets - risk computation

Posted by stephan buschmann on
URL: http://quantlib.414.s1.nabble.com/Monte-carlo-stochastic-simulation-of-correlated-assets-risk-computation-tp1012p1013.html

Hello,
you can find some examples using the multipathGenerator in the testSuite. They are easy and interesting to follow.

Concerning your steps:

1. Create timeseries matrix
Why dont create a equidistant timeSeries in the beginning. That keeps things simple. A variable timeSeries can be implemented at a later stage (including mandatory dates as maturity, cashflow payments, etc.)

2. Calculate COV matrix based on timeseries
Just use simple correlation assumptions in the beginning. At a later stage, you can include real correlations.

3. Generate N scenarios for T timesteps
That is a good idea.
4. Price stocks (portfolio) at each timestep T, and calculate 95% portfolio VaR
Dont forget to set the evaluation date. That pushes you towards a future date.

5. Report risk at each time point
There are plenty of tools available, as sequenceStatistics, etc.

What I am trying to say is: Try to focus on the main core: mutliPathGenerator, future valuation of simple instruments and some statistical tools. Keep the rest simple as possible (market data, etc.) in the very first approach. Thats make things like "exposure" or risk measures kind of understandable and easy to validate. All elements you need are somewhere in the testSuite or in the examples.

stephan

On Fri, Sep 28, 2012 at 1:51 PM, Sergei _ <[hidden email]> wrote:
Hello friends,

This is my first posting to the list. Please excuse me if some questions sound too newby-sh for seasoned QuantLib users.

I'm trying to code a monte-carlo stochastic simulation of several correlated assets and related risk factors (ex: portfolio of 3 options on stocks stock1, stock2, stock3) and evaluate portfolio risk at every simulation timestep at a given confidence (ex: 95%).
Stock price is following Geometric brownian motion process.
I'm using QLNet for quick prototyping, but answers based on QuantLib can do (I will extrapolate to QLNet, or use QL + Swig as the last resort).

I've done my due diligence and searched the web and forums, and have only been able to get as far as correlation matrix. Looking at the source code, it seems that the next step involve using MultiPath and MultipathGenerator, however it is not entirely clear how to wire the classes together, and use the resulting output for pricing.

Hopefully you can provide guidance in implementing this task. Any code samples will be greatly appreciated.

Steps:

1. Create timeseries matrix
2. Calculate COV matrix based on timeseries
3. Generate N scenarios for T timesteps
4. Price stocks (portfolio) at each timestep T, and calculate 95% portfolio VaR
5. Report risk at each time point

Code so far:

namespace ConsoleTestApp.QuantLib
{
    public class QLNetTest
    {
        public static void TestSimulation()
        {
            //Stock1,Stock2,Stock3
            //90,60,90
            //90,90,30
            //60,60,60
            //60,60,90
            //30,30,30
 
            Matrix t = new Matrix(5, 3);
            t[0, 0] = 90; t[0, 1] = 60; t[0, 2] = 90;
            t[1, 0] = 90; t[1, 1] = 90; t[1, 2] = 30;
            t[2, 0] = 60; t[2, 1] = 60; t[2, 2] = 60;
            t[3, 0] = 60; t[3, 1] = 60; t[3, 2] = 90;
            t[4, 0] = 30; t[4, 1] = 30; t[4, 2] = 30;
 
            SequenceStatistics priceValuesCorreration = new SequenceStatistics(0);
            int instrCount = t.columns();
 
            Matrix CorrelationMatrix = new Matrix(instrCount, instrCount);
 
            for (int i = 0; i < t.rows(); i++)
            {
                Vector vector = new Vector();
                for (int j = 0; j < instrCount; j++)
                {
                    vector.Add(t[i,j]);
                }
                priceValuesCorreration.add(vector.ToList<double>(), 1.0);
            }
 
            CorrelationMatrix = priceValuesCorreration.correlation();
            
        }
    }
}

Thanks,
Serhio


------------------------------------------------------------------------------
Got visibility?
Most devs has no idea what their production app looks like.
Find out how fast your code is with AppDynamics Lite.
http://ad.doubleclick.net/clk;262219671;13503038;y?
http://info.appdynamics.com/FreeJavaPerformanceDownload.html
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users



------------------------------------------------------------------------------
Don't let slow site performance ruin your business. Deploy New Relic APM
Deploy New Relic app performance management and know exactly
what is happening inside your Ruby, Python, PHP, Java, and .NET app
Try New Relic at no cost today and get our sweet Data Nerd shirt too!
http://p.sf.net/sfu/newrelic-dev2dev
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users