Monte-carlo stochastic simulation of correlated assets - risk computation

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

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