Generating pairwise correlated GBM data

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Generating pairwise correlated GBM data

U.Mutlu
Hi,

1) Is it possible to generate correlated price data in quantlib
by using a correlation matrix? (needed for simulations)

2) Sometimes there can be a problem with the correlation matrix,
especially if it is not positive definite, ie. a defective correlation matrix.
Here's an example for a defective correlation matrix:
(
http://blogs.sas.com/content/iml/2012/09/12/when-is-a-correlation-matrix-not-a-correlation-matrix.html 
)
  1.0  0.6  0.9
  0.6  1.0  0.9
  0.9  0.9  1.0
In such cases one has to find a replacement matrix (Nearest Correlation Matrix
Problem).
Anybody know of C/C++/C#/Java code or library for computing/finding the
nearest correlation matrix?


--
Thx


------------------------------------------------------------------------------
Site24x7 APM Insight: Get Deep Visibility into Application Performance
APM + Mobile APM + RUM: Monitor 3 App instances at just $35/Month
Monitor end-to-end web transactions and take corrective actions now
Troubleshoot faster and improve end-user experience. Signup Now!
http://pubads.g.doubleclick.net/gampad/clk?id=272487151&iu=/4140
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users
Reply | Threaded
Open this post in threaded view
|

Re: Generating pairwise correlated GBM data

U.Mutlu
U.Mutlu wrote on 02/18/2016 11:11 AM:
>
> 1) Is it possible to generate correlated price data in quantlib
> by using a correlation matrix? (needed for simulations)

The following infos explain the procedure well. Converting this to one's own
language should be easy to do:
https://kurtverstegen.wordpress.com/2013/12/07/simulation/
http://yasermartinez.com/blog/posts/brownian-simulation-of-correlated-assets.html
http://www.goddardconsulting.ca/matlab-monte-carlo-assetpaths-corr.html

> 2) Sometimes there can be a problem with the correlation matrix,
> especially if it is not positive definite, ie. a defective correlation matrix.
> Here's an example for a defective correlation matrix:
> http://blogs.sas.com/content/iml/2012/09/12/when-is-a-correlation-matrix-not-a-correlation-matrix.html
>    1.0  0.6  0.9
>    0.6  1.0  0.9
>    0.9  0.9  1.0
> In such cases one has to find a replacement matrix (Nearest Correlation Matrix
> Problem).
> Anybody know of C/C++/C#/Java code or library for computing/finding the
> nearest correlation matrix?

For finding the nearest correlation matrix there exist implementations
for Matlab, R, Python etc, and here I found C code by P. Zaczkowski,
but it seems to have some memory issues (alloc/free) as it throws
sometimes SIGSEGV when I run it (I haven't analyzed/debugged yet):
http://www.math.nus.edu.sg/~matsundf/
"If you need a C/C++ code, download main.c and main.h"



------------------------------------------------------------------------------
Site24x7 APM Insight: Get Deep Visibility into Application Performance
APM + Mobile APM + RUM: Monitor 3 App instances at just $35/Month
Monitor end-to-end web transactions and take corrective actions now
Troubleshoot faster and improve end-user experience. Signup Now!
http://pubads.g.doubleclick.net/gampad/clk?id=272487151&iu=/4140
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users
Reply | Threaded
Open this post in threaded view
|

Re: Generating pairwise correlated GBM data

Luigi Ballabio
In reply to this post by U.Mutlu
Hello,
    you can use the StochasticProcessArray to take a number of 1-D processes and their correlation matrix and build the corresponding multi-dimensional process, that can then be passed to the MultiPathGenerator class to generate the prices.

By default, the StochasticProcessArray class fixes a defective correlation matrix using the spectral salvaging algorithm.  You can lookup the pseudoSqrt function in QuantLib to see its implementation. The function can use a few other algorithms, but at this time you can't choose them when you instantiate StochasticProcessArray.

You can look at QuantLib/test-suite/pathgenerator.cpp for examples.

Hope this helps,
    Luigi


On Thu, Feb 18, 2016 at 11:24 AM U.Mutlu <[hidden email]> wrote:
Hi,

1) Is it possible to generate correlated price data in quantlib
by using a correlation matrix? (needed for simulations)

2) Sometimes there can be a problem with the correlation matrix,
especially if it is not positive definite, ie. a defective correlation matrix.
Here's an example for a defective correlation matrix:
(
http://blogs.sas.com/content/iml/2012/09/12/when-is-a-correlation-matrix-not-a-correlation-matrix.html
)
  1.0  0.6  0.9
  0.6  1.0  0.9
  0.9  0.9  1.0
In such cases one has to find a replacement matrix (Nearest Correlation Matrix
Problem).
Anybody know of C/C++/C#/Java code or library for computing/finding the
nearest correlation matrix?


--
Thx


------------------------------------------------------------------------------
Site24x7 APM Insight: Get Deep Visibility into Application Performance
APM + Mobile APM + RUM: Monitor 3 App instances at just $35/Month
Monitor end-to-end web transactions and take corrective actions now
Troubleshoot faster and improve end-user experience. Signup Now!
http://pubads.g.doubleclick.net/gampad/clk?id=272487151&iu=/4140
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users

------------------------------------------------------------------------------
Site24x7 APM Insight: Get Deep Visibility into Application Performance
APM + Mobile APM + RUM: Monitor 3 App instances at just $35/Month
Monitor end-to-end web transactions and take corrective actions now
Troubleshoot faster and improve end-user experience. Signup Now!
http://pubads.g.doubleclick.net/gampad/clk?id=272487151&iu=/4140
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users
Reply | Threaded
Open this post in threaded view
|

Re: Generating pairwise correlated GBM data

U.Mutlu
Thanks Luigi,
but there is IMO an error either in the Higham algo itself,
or its implementation in QuantLib. Here's short demo.
As can be seen, the output matrix is not a correlation matrix,
ie. a bug:

/*
   test.cpp

   Written by U.Mutlu, 2016-02-19-Tu

   Testing finding nearest correlation matrix using Higham algorithm
   --> gives wrong result: the output is far from being a correlation matrix

   Compile:
     g++ -Wall -O2 -std=c++11 test.cpp -lQuantLib

   Output:
Input matrix: rows=3 cols=3 det=-0.0120000000
   1.0000000000  0.9000000000  0.7000000000
   0.9000000000  1.0000000000  0.3000000000
   0.7000000000  0.3000000000  1.0000000000

Output matrix: rows=3 cols=3 det=0.0000000000
   1.0000000000  0.0000000000  0.0000000000
   0.8945757928  0.4469162685  0.0000000000
   0.6966210765 -0.7174430809  0.0000000000
*/

#include <ql/math/matrix.hpp>
#include <ql/math/matrixutilities/pseudosqrt.hpp>
#include <cstdio>
#include <string>

using namespace QuantLib;
using namespace std;


void print_matrix(const Matrix& AM, const string AsTitle = "")
   {
     const size_t N = AM.columns();
     const string sFmt = " %13.10f";   // " %43.40f";
     printf("%s: rows=%zu cols=%zu det=%.10f\n",
       AsTitle.c_str(), AM.rows(), AM.columns(), determinant(AM));
     for (size_t i = 0; i < N; ++i, printf("\n"))
       for (size_t j = 0; j < N; ++j)
         printf(sFmt.c_str(), AM[i][j]);
   }

int main()
   {
     const size_t N = 3;

     Matrix M(N, N);
     M[0][0] = 1.0;  M[0][1] = 0.9;  M[0][2] = 0.7;
     M[1][0] = 0.9;  M[1][1] = 1.0;  M[1][2] = 0.3;
     M[2][0] = 0.7;  M[2][1] = 0.3;  M[2][2] = 1.0;

     Matrix R = pseudoSqrt(M, SalvagingAlgorithm::Higham);

     print_matrix(M, "Input matrix"), printf("\n");
     print_matrix(R, "Output matrix");

     return 0;
   }


Platform: linux
Compiler: GNU C++ version 4.9.2
STL     : GNU libstdc++ version 20141220
Boost   : 1.55.0


Luigi Ballabio wrote on 02/19/2016 03:43 PM:

> Hello,
>      you can use the StochasticProcessArray to take a number of 1-D
> processes and their correlation matrix and build the corresponding
> multi-dimensional process, that can then be passed to
> the MultiPathGenerator class to generate the prices.
>
> By default, the StochasticProcessArray class fixes a defective correlation
> matrix using the spectral salvaging algorithm.  You can lookup
> the pseudoSqrt function in QuantLib to see its implementation. The function
> can use a few other algorithms, but at this time you can't choose them when
> you instantiate StochasticProcessArray.
>
> You can look at QuantLib/test-suite/pathgenerator.cpp for examples.
>
> Hope this helps,
>      Luigi
>
>
> On Thu, Feb 18, 2016 at 11:24 AM U.Mutlu <[hidden email]> wrote:
>
>> Hi,
>>
>> 1) Is it possible to generate correlated price data in quantlib
>> by using a correlation matrix? (needed for simulations)
>>
>> 2) Sometimes there can be a problem with the correlation matrix,
>> especially if it is not positive definite, ie. a defective correlation
>> matrix.
>> Here's an example for a defective correlation matrix:
>> (
>>
>> http://blogs.sas.com/content/iml/2012/09/12/when-is-a-correlation-matrix-not-a-correlation-matrix.html
>> )
>>    1.0  0.6  0.9
>>    0.6  1.0  0.9
>>    0.9  0.9  1.0
>> In such cases one has to find a replacement matrix (Nearest Correlation
>> Matrix
>> Problem).
>> Anybody know of C/C++/C#/Java code or library for computing/finding the
>> nearest correlation matrix?
>>

------------------------------------------------------------------------------
Site24x7 APM Insight: Get Deep Visibility into Application Performance
APM + Mobile APM + RUM: Monitor 3 App instances at just $35/Month
Monitor end-to-end web transactions and take corrective actions now
Troubleshoot faster and improve end-user experience. Signup Now!
http://pubads.g.doubleclick.net/gampad/clk?id=272487151&iu=/4140
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users

test.cpp (1K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Generating pairwise correlated GBM data

Luigi Ballabio
The function is returning the pseudo square root of the correlation, which is more convenient for the path generator.
To retrieve the correlation, you can multiply the result by its transposed, as in:

Matrix C = R * transpose(R);

Luigi



On Tue, Feb 23, 2016 at 7:10 AM U.Mutlu <[hidden email]> wrote:
Thanks Luigi,
but there is IMO an error either in the Higham algo itself,
or its implementation in QuantLib. Here's short demo.
As can be seen, the output matrix is not a correlation matrix,
ie. a bug:

/*
   test.cpp

   Written by U.Mutlu, 2016-02-19-Tu

   Testing finding nearest correlation matrix using Higham algorithm
   --> gives wrong result: the output is far from being a correlation matrix

   Compile:
     g++ -Wall -O2 -std=c++11 test.cpp -lQuantLib

   Output:
Input matrix: rows=3 cols=3 det=-0.0120000000
   1.0000000000  0.9000000000  0.7000000000
   0.9000000000  1.0000000000  0.3000000000
   0.7000000000  0.3000000000  1.0000000000

Output matrix: rows=3 cols=3 det=0.0000000000
   1.0000000000  0.0000000000  0.0000000000
   0.8945757928  0.4469162685  0.0000000000
   0.6966210765 -0.7174430809  0.0000000000
*/

#include <ql/math/matrix.hpp>
#include <ql/math/matrixutilities/pseudosqrt.hpp>
#include <cstdio>
#include <string>

using namespace QuantLib;
using namespace std;


void print_matrix(const Matrix& AM, const string AsTitle = "")
   {
     const size_t N = AM.columns();
     const string sFmt = " %13.10f";   // " %43.40f";
     printf("%s: rows=%zu cols=%zu det=%.10f\n",
       AsTitle.c_str(), AM.rows(), AM.columns(), determinant(AM));
     for (size_t i = 0; i < N; ++i, printf("\n"))
       for (size_t j = 0; j < N; ++j)
         printf(sFmt.c_str(), AM[i][j]);
   }

int main()
   {
     const size_t N = 3;

     Matrix M(N, N);
     M[0][0] = 1.0;  M[0][1] = 0.9;  M[0][2] = 0.7;
     M[1][0] = 0.9;  M[1][1] = 1.0;  M[1][2] = 0.3;
     M[2][0] = 0.7;  M[2][1] = 0.3;  M[2][2] = 1.0;

     Matrix R = pseudoSqrt(M, SalvagingAlgorithm::Higham);

     print_matrix(M, "Input matrix"), printf("\n");
     print_matrix(R, "Output matrix");

     return 0;
   }


Platform: linux
Compiler: GNU C++ version 4.9.2
STL     : GNU libstdc++ version 20141220
Boost   : 1.55.0


Luigi Ballabio wrote on 02/19/2016 03:43 PM:
> Hello,
>      you can use the StochasticProcessArray to take a number of 1-D
> processes and their correlation matrix and build the corresponding
> multi-dimensional process, that can then be passed to
> the MultiPathGenerator class to generate the prices.
>
> By default, the StochasticProcessArray class fixes a defective correlation
> matrix using the spectral salvaging algorithm.  You can lookup
> the pseudoSqrt function in QuantLib to see its implementation. The function
> can use a few other algorithms, but at this time you can't choose them when
> you instantiate StochasticProcessArray.
>
> You can look at QuantLib/test-suite/pathgenerator.cpp for examples.
>
> Hope this helps,
>      Luigi
>
>
> On Thu, Feb 18, 2016 at 11:24 AM U.Mutlu <[hidden email]> wrote:
>
>> Hi,
>>
>> 1) Is it possible to generate correlated price data in quantlib
>> by using a correlation matrix? (needed for simulations)
>>
>> 2) Sometimes there can be a problem with the correlation matrix,
>> especially if it is not positive definite, ie. a defective correlation
>> matrix.
>> Here's an example for a defective correlation matrix:
>> (
>>
>> http://blogs.sas.com/content/iml/2012/09/12/when-is-a-correlation-matrix-not-a-correlation-matrix.html
>> )
>>    1.0  0.6  0.9
>>    0.6  1.0  0.9
>>    0.9  0.9  1.0
>> In such cases one has to find a replacement matrix (Nearest Correlation
>> Matrix
>> Problem).
>> Anybody know of C/C++/C#/Java code or library for computing/finding the
>> nearest correlation matrix?
>>

------------------------------------------------------------------------------
Site24x7 APM Insight: Get Deep Visibility into Application Performance
APM + Mobile APM + RUM: Monitor 3 App instances at just $35/Month
Monitor end-to-end web transactions and take corrective actions now
Troubleshoot faster and improve end-user experience. Signup Now!
http://pubads.g.doubleclick.net/gampad/clk?id=272487151&iu=/4140_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users

------------------------------------------------------------------------------
Site24x7 APM Insight: Get Deep Visibility into Application Performance
APM + Mobile APM + RUM: Monitor 3 App instances at just $35/Month
Monitor end-to-end web transactions and take corrective actions now
Troubleshoot faster and improve end-user experience. Signup Now!
http://pubads.g.doubleclick.net/gampad/clk?id=272487151&iu=/4140
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users
Reply | Threaded
Open this post in threaded view
|

Re: Generating pairwise correlated GBM data

U.Mutlu
Luigi Ballabio wrote on 02/23/2016 09:23 AM:
> The function is returning the pseudo square root of the correlation, which is
> more convenient for the path generator.
> To retrieve the correlation, you can multiply the result by its transposed, as in:
>
> Matrix C = R * transpose(R);

Ah, it now indeed gives the correlation matrix. Thank you very much.




------------------------------------------------------------------------------
Site24x7 APM Insight: Get Deep Visibility into Application Performance
APM + Mobile APM + RUM: Monitor 3 App instances at just $35/Month
Monitor end-to-end web transactions and take corrective actions now
Troubleshoot faster and improve end-user experience. Signup Now!
http://pubads.g.doubleclick.net/gampad/clk?id=272487151&iu=/4140
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users