Generating correlated stock prices for simulating herd behaviour

Hi,
has anybody done this: generating correlated stock prices for simulating herd behaviour in the markets?
Ie. if a sector leader falls then other similar companies usually follow the path... Same when the Fed releases important news...
Would like to add such behaviour in to my test framework for trading a basket of say 10 instruments, which shall be to a certain degree correlated with each other.
Ie. correlated geometric Brownian motion...
 
Last edited:
Hi,
has anybody done this: generating correlated stock prices for simulating herd behaviour in the markets?
Ie. if a sector leader falls then other similar companies usually follow the path...
Same when Fed releases important news...
Would like to add such behaviour in to my test framework for trading a basket of say 10 instruments,
which shall be correlated to a certain degree with each other.
Ie. correlated geometric Brownian motion...

You can do this easily with python

corr ## np.array correlation matrix
stds ## np.array Nx1 standard deviations
plength=500 ## no of observations
covs=np.dot(stds, np.dot(corr, stds))

m = np.random.multivariate_normal(means, covs, plength).T

GAT
 
You can do this easily with python

corr ## np.array correlation matrix
stds ## np.array Nx1 standard deviations
plength=500 ## no of observations
covs=np.dot(stds, np.dot(corr, stds))

m = np.random.multivariate_normal(means, covs, plength).T

GAT
Thx, but...
Too bad I have no experience with Python ;-(
C/C++/Java/C# would be fine, or just the plain algo.
 
Last edited:
Currently I'm generating uncorrelated data, and now want to add correlation into the model.
Here's a method that looks simple to include into existing code:
https://en.wikipedia.org/wiki/Cholesky_decomposition#Monte_Carlo_simulation
"...say one needs to generate two correlated normal variables x1 and x2.
All one needs to do is to generate two uncorrelated Gaussian random variables z1 and z2.
We set x1 = z1 and x2 = rho * z1 + sqrt(1 - rho * rho) * z2
"
But I'm not sure if that method is restricted to just 2 items; I would need to apply it to more than 2 items.
 
Last edited:
Currently I'm generating uncorrelated data, and now want to add correlation into the model.
Here's a method that looks simple to include in existing code:
https://en.wikipedia.org/wiki/Cholesky_decomposition#Monte_Carlo_simulation
"...say one needs to generate two correlated normal variables x1 and x2.
All one needs to do is to generate two uncorrelated Gaussian random variables z1 and z2.
We set x1 = z1 and x2 = rho * z1 + sqrt(1 - rho * rho) * z2
"
But I'm not sure if that method is restricted to just 2 items; I would need to apply it to more than 2 items.

Yes; it's my understanding that is the closed form solution for 2x2 correlation matrix only; I think for anything bigger you need to use a numerical method for the decomposition.

GAT
 
Yes; it's my understanding that is the closed form solution for 2x2 correlation matrix only; I think for anything bigger you need to use a numerical method for the decomposition.

GAT
Ok, here I found Cholesky decomposition in source code for nearly all languages, incl. C:
http://rosettacode.org/wiki/Cholesky_decomposition

My problem is understanding what comes into this matrix (I guess it has to be the correlation matrix with the rho values),
and how do I perform after the matrix has been calculated (I guess just multiplying the generated price with the corrosponding element of the calculated matrix result),
and how often do I need to calc this matrix when generating say 1-minute time-series data for the 10 stocks... (I guess just once, but one can redo with a new/updated correlation matrix if desired).
Just thinking aloud... ;-)
Still studying the other resources...
 
Last edited:
Code:
import numpy as np
import pandas as pd

C=[ [ 1.0, 0.6 ,0.3],  [ 0.6 ,1.0 ,0.5],[   0.3, 0.5 ,1.0]]
mC=np.mat(C)
U=np.linalg.cholesky(mC)
cU=np.random.randn(10,3) * U
pd.DataFrame(cU).corr()
 
Code:
import numpy as np
import pandas as pd

C=[ [ 1.0, 0.6 ,0.3],  [ 0.6 ,1.0 ,0.5],[   0.3, 0.5 ,1.0]]
mC=np.mat(C)
U=np.linalg.cholesky(mC)
cU=np.random.randn(10,3) * U
pd.DataFrame(cU).corr()

And people wonder why I like python so much....

GAT
 
Back
Top