2
$\begingroup$

I want to simulate two correlated Geometric Brownian Motion processes in Python. I found an implementation from Matlab (https://www.goddardconsulting.ca/matlab-monte-carlo-assetpaths-corr.html) and another one in Python (https://mikejuniperhill.blogspot.com/2019/04/python-path-generator-for-correlated.html) which both should do exactly what I'm looking for, however I noticed something different between both and I'm not sure which one is correct.

Here are the specific parts I'm talking about:

Matlab implementation:

R = chol(corr);
x = randn(steps,size(corr,2));
ep = x*R;

Python implementation:

choleskyMatrix = np.linalg.cholesky(correlation)
e = np.random.normal(size = (nProcesses, nSteps))            
paths = np.dot(choleskyMatrix, e)

In both implementations the Cholesky Matrix is calculated, however then the two dimensions of the random sequence x and e respectively are flipped. As a result, the matrix multiplication/dot product yields to a different result caused by the different dimensions of the array. Which one of these implementations is correct?

$\endgroup$
6
  • $\begingroup$ In mapping the uncorrelated variables to the correlated variables, one of the variables is preserved. It does not matter too much if the variable that is preserved is the first variable or the last variable. (I.e. if we pre-multiply or postmultiply the Cholesky matrix). The actual values generated are different of course but both sets of variables represents a valid set of uncorrelated random variables. $\endgroup$ – noob2 Nov 12 '20 at 18:50
  • 2
    $\begingroup$ To add to @noob2 ‘s comment: it depends on how the software returns the cholesky matrix $M=LL^T$, either the upper matrix $L^T$ or the lower matrix $L$. And this is usually driven by how the user is supposed to Lay-out the data, $N\times K$ or $K \times N$ $\endgroup$ – Kermittfrog Nov 12 '20 at 19:38
  • 2
    $\begingroup$ I think you need to check carefully the documentation of the 2 cholesky functions to understand what is going on (i.e. the issue that Kermittfrog brought up) $\endgroup$ – noob2 Nov 12 '20 at 20:13
  • $\begingroup$ I think python and matlab gives the cholesky output as a transpose or inverse of one another's output. or you might have to send input to one of these programs as a transpose or inverse to what you would normally send as input to the other program to get the same output. $\endgroup$ – develarist Nov 12 '20 at 21:07
  • 1
    $\begingroup$ @Willart yes. Think about it like this: If we lay out a vector $x$ as a $K\times 1$ column vector we need to left-multiply with the lower cholesky matrix and obtain $z=Lx$. This also works perfectly for a $K\times N$ data matrix with $N\geq 1$ observations: simply left-multiply with the lower Cholesky and you are fine. If, on the other hand, you want to layout your data in $N\times K$ fashion, then you right-multiply with the upper Cholesky matrix in order to obtain correlated outputs. $Lx$ and $(x^TL^T)^T$ are equal. $\endgroup$ – Kermittfrog Nov 17 '20 at 11:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.