rgraphsimulationreplicationmontecarlo

Brownian motion simulation using R


Simulation of Brownian motion in the invertal of time [0,100] and the paths were drawn by simulating n = 1000 points. I generate the following code:

 n <- 1000
 t <- 100
 bm <- c(0, cumsum(rnorm(n,0,sqrt(t/n))))
 steps <- seq(0,t,length=n+1)
 plot(steps,bm,type="l")

How could I simulate 50 sample paths of a standard Brownian motion and show every path in a different colour, like a bunch of trajectories?

I think it will be something like replicate(50,bm)but when I do it there is an error in xy.coords. Thanks for helping!

Simulation of Brownian Bridge on [0,1] and the paths were drawn by simulating n = 1000 points. I generate the following code

n <- 1000
t <- seq(0,1,length=n)
No.Ex<-10
bm <- c(0,cumsum(rnorm(n-1,0,1)))/sqrt(n)
B = replicate(No.Ex,{
  bb <- bm - t*bm[n]
})
matplot(B, type = "l", col = cols, lty = 1)

Code to generate sample paths of a Geometric Brownian Motion

simGBM<- function(P0, mu, sigma, T, nSteps, nRepl){
  dt<- T/nSteps
  muT<- (mu-sigma^2/2)*dt
  sigmaT<- sqrt(dt)*sigma
  pathMatrix<- matrix(nrow = nRepl, ncol = nSteps+1)
  pathMatrix[,1]<- P0
  for(i in 1:nRepl){
    for(j in 2:(nSteps+1)){
      pathMatrix[i,j]<- pathMatrix[i,j-1]*exp(rnorm(1, muT, sigmaT))
    }
  }
  return(pathMatrix)
}

P0<- 1 #initial price
mu<- 0.1 #drift
sigma<- 0.5 #volatility
T<- 100/360 #100 days of a commercial year
nSteps<- 50 #No of steps
nRepl<- 100 #No of replications

paths<- simGBM(P0, mu, sigma, T, nSteps, nRepl)
yBounds<- c(min(paths),max(paths)) #bounds of simulated prices

plot(paths[1,], ylim = yBounds, type = 'l',col = 1, main = "Simulation of sample paths of GBM", xlab = "Time", ylab = "Price")
for(k in 2:numRepl) lines(paths[k,], col = k)

I'm trying to use matplot function but I cannot generate the same graph

cols = rainbow(nSteps)
matplot(paths, ylim = yBounds, type = "l", col = cols, lty = 1, main = "Simulation of sample paths of GBM", xlab = "Time", ylab = "Price")

Solution

  • How about this

    n = 1000
    t = 100
    No.Ex = 10
    steps = seq(0,t,length=n+1)
    A = replicate(No.Ex, {
      bm <- c(0, cumsum(rnorm(n,0,sqrt(t/n))))
    }) 
    
    cols = rainbow(No.Ex)
    matplot(A, type = "l", col = cols, lty = 1)
    

    I modified my answer and incorporated Stephane Laurent's matplot suggestion. This gives the following image.

    enter image description here

    EDIT:

    To respond to your question in the comments, I think you should keep my initial code for bm which is bm <- c(0, cumsum(rnorm(n,0,sqrt(t/n)))). Then it all works pretty well! Thanks for pointing out the nice matplot command @Stephane Laurent.

    EDIT2: I just realized you posed a new question with regard to the Brown bridge. You could try this code

    n <- 1000
    t <- seq(0,1,length=n)
    No.Ex<-10
    B = replicate(No.Ex,{
      bm <- c(0, cumsum(rnorm(n - 1,0,sqrt(t/n))))
      bb <- bm - t*rep(bm[length(bm)], length.out = length(bm))
    })
    matplot(B, type = "l", col = cols, lty = 1)
    

    This produces

    enter image description here

    Also, for Geometric Brownian Motian try this modification of your code with fewer replications

    simGBM<- function(P0, mu, sigma, T, nSteps, nRepl){
      dt<- T/nSteps
      muT<- (mu-sigma^2/2)*dt
      sigmaT<- sqrt(dt)*sigma
      pathMatrix<- matrix(nrow = nRepl, ncol = nSteps+1)
      pathMatrix[,1]<- P0
      for(i in 1:nRepl){
        for(j in 2:(nSteps+1)){
          pathMatrix[i,j]<- pathMatrix[i,j-1]*exp(rnorm(1, muT, sigmaT))
        }
      }
      return(pathMatrix)
    }
    
    P0<- 1 #initial price
    mu<- 0.1 #drift
    sigma<- 0.5 #volatility
    T<- 100/360 #100 days of a commercial year
    nSteps<- 50 #No of steps
    nRepl<- 10 #No of replications
    
    paths<- simGBM(P0, mu, sigma, T, nSteps, nRepl)
    yBounds<- c(min(paths),max(paths)) #bounds of simulated prices
    
    plot(paths[1,], ylim = yBounds, type = 'l',col = 1, main = "Simulation of sample paths of GBM", xlab = "Time", ylab = "Price")
    for(k in 2:nRepl) lines(paths[k,], col = k)
    
    cols = rainbow(nSteps)
    matplot(paths, ylim = yBounds, type = "l", col = cols, lty = 1, main = "Simulation of sample paths of GBM", xlab = "Time", ylab = "Price")
    

    On my machine, this produces

    enter image description here