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 |
A shotcut:
If your risk factors are gaussian ( if you generate prices with a GBM, log returns will be gaussian) and your instruments are linear in factors (it is the case if they are stocks), then your VaR wlll analytical and equal to k - times your portfolio volatility (k being the inverse normal of the choosen confidence level, 95%,99%..).
Port volatiliy =sqrt(w'*COV *w) where w are the market values of your positions, and COV is the generator of your GBM.
With these assumptions, your MC should converge to the analytical VaR.
MC will come at hand with nonlinear instruments (rate options, equity options, bonds with high convexity etc...)
Simone
On 28 September 2012 13:51, Sergei _ <[hidden email]> wrote: Hello friends, ------------------------------------------------------------------------------ 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 |
Hi Simone,
Thank you for your reply. I'm actually using a simple example first, in order to extend it to more complicated products later with full factor modeling (IR curves, volatilities, etc), so will need a full MC approach (parametric VaR would be a good fit for this case as you noted). While the algorithm is clear, I'm struggling to apply QuantLib to implement it (because I'm just starting to use the library). If you could advise how to proceed with MC approach using QuantLib, it would be great. MultiPath and MultipathGenerator seem to be the way forward, but finding examples is not easy. Thanks again, Serhio On Fri, Sep 28, 2012 at 6:59 PM, simone pilozzi <[hidden email]> wrote:
------------------------------------------------------------------------------ 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 |
In reply to this post by QEngineer
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 3. Generate N scenarios for T timesteps
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 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, ------------------------------------------------------------------------------ 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 |
Free forum by Nabble | Edit this page |