Generating correlated stock prices for simulating herd behaviour

Discussion in 'App Development' started by botpro, Feb 12, 2016.

  1. botpro

    botpro

    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: Feb 12, 2016
  2. 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
     
  3. botpro

    botpro

    Thx, but...
    Too bad I have no experience with Python ;-(
    C/C++/Java/C# would be fine, or just the plain algo.
     
    Last edited: Feb 12, 2016
  4. botpro

    botpro

  5. botpro

    botpro

    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: Feb 12, 2016
  6. 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
     
  7. botpro

    botpro

    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: Feb 12, 2016
  8. 2rosy

    2rosy

    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()
    
    
     
  9. And people wonder why I like python so much....

    GAT
     
    #10     Feb 12, 2016