title: How can I simulate correlated stock returns with a geometric brownian motion in R?
body: <p>I'm trying to extend a code I already have. My goal is to simulate portfolio returns (log returns) of 5 correlated stocks with a geometric brownian motion by using historical drift and volatility. I used the code before to simulate the return of only one stock and it worked perfectly.</p>
  
  <p><em>Background:</em> I started with downloading stock prices of 5 companies (going 5 years in the past) from yahoo finance and extracted the closing price (so each stock price is only a column vector now, not a matrix). Then I transformed each of those 5 price vectors into vectors with log returns by using <code>returns.DAI = diff(log(price.DAI))</code> (that's only one example of the Daimler stock). Then I created a matrix to combine the 5 log return vectors (returns.matrix) and calculated the covariance matrix of the returns matrix. Afterwards I did a Cholesky decomposition of the covariance matrix with <code>chol.decomp=chol(cov.matrix)</code>. I also created a vector with the historical drifts (mu's) <code>mu.portfolio=rbind(mu.DAI,mu.ADS,mu.BAS,mu.FME,mu.HEI)</code></p>
  
  <p><strong>You might have to load some libraries with
  library(quantmod);library(PerformanceAnalytics)</strong></p>
  
  <p>Here's the full code now:</p>
  
  <pre><code># download prices from yahoo for 5 stocks manually because there are
  # mistakes in the data when you use getSymbols()!!!
  
  # ALSO, unfortunately I had to sort the csv's in Excel because 
  # when you download it from yahoo, it shows the newest price first. ^^
  DAI=read.csv("LOCATION ON YOUR COMPUTER/NAME OF YOUR FILE.csv")
  ADS=read.csv("LOCATION ON YOUR COMPUTER/NAME OF YOUR FILE.csv")
  BAS=read.csv("LOCATION ON YOUR COMPUTER/NAME OF YOUR FILE.csv")
  FME=read.csv("LOCATION ON YOUR COMPUTER/NAME OF YOUR FILE.csv")
  HEI=read.csv("LOCATION ON YOUR COMPUTER/NAME OF YOUR FILE.csv")
  
  # extracting close prices
  price.DAI=DAI[,5]
  price.ADS=ADS[,5]
  price.BAS=BAS[,5]
  price.FME=FME[,5]
  price.HEI=HEI[,5]
  
  # calculating log returns
  returns.DAI = diff(log(price.DAI))
  returns.ADS = diff(log(price.ADS))
  returns.BAS = diff(log(price.BAS))
  returns.FME = diff(log(price.FME))
  returns.HEI = diff(log(price.HEI))
  
  # matrix of returns
  returns.matrix=cbind(returns.DAI,returns.ADS,returns.BAS,returns.FME,returns.HEI)
  
  # calculate mu (drift)
  mu.DAI=mean(returns.DAI)
  mu.ADS=mean(returns.ADS)
  mu.BAS=mean(returns.BAS)
  mu.FME=mean(returns.FME)
  mu.HEI=mean(returns.HEI)
  mu.portfolio=rbind(mu.DAI,mu.ADS,mu.BAS,mu.FME,mu.HEI)
  
  # calculate sigma
  sigma.DAI=sd(returns.DAI)
  sigma.ADS=sd(returns.ADS)
  sigma.BAS=sd(returns.BAS)
  sigma.FME=sd(returns.FME)
  sigma.HEI=sd(returns.HEI)
  sigma.pf=rbind(sigma.DAI,sigma.ADS,sigma.BAS,sigma.FME,sigma.HEI)
  
  # covariance matrix
  cov.matrix=cov(returns.matrix)
  
  # Cholesky decomposition
  chol.decomp=chol(cov.matrix)
  </code></pre>
  
  <p>Now, here's my already extended but defective function:</p>
  
  <pre><code>brownian.motion = function(number,dt,mu=c(0,0,0,0,0),sigma=cov.matrix){
    t=seq(from=0,to=number*dt,by=dt)
    n=length(t)
    # normally distributed gains
    dw=rnorm(n-1,mean=mu.portfolio*dt,sd=chol.decomp*sqrt(dt))
    # start in 0
    w=c(0,lapply(lapply(dw, cumsum), cumsum))
    return(w)
  }
  
  number=250       #DAX stocks are approximately traded on 250 days per year so that's a one year simulation.
  dt=1/number      #delta (time steps)
  sim=10000        #10000 simulations for each stock return
  bm=matrix(nrow = number+1,ncol = sim)
  
  for (i in 1:sim){
    bm[,i]=brownian.motion(number,dt,mu.portfolio,cov.matrix)
  }
  </code></pre>
  
  <p><strong>When I run this code I get the following error:
  "Error in bm[, i] = brownian.motion(number, dt, mu.portfolio, cov.matrix) : incorrect number of subscripts on matrix"</strong></p>
  
  <p>Could anyone please help me? I'm getting desperate because I tried so much and there's always another error. I'm not even a 100% sure if it's mathematically correct where I put the cholesky decomposition and covariance matrix.</p>