Efficient Simulation of Brownian Motion in R -
data=data.frame(matrix(rnorm(1000*300,0,1),1000,300)) weiner.matrix=data.frame(cumsum(data)) mu=0 sigma=.15 dt=1/1000 bmot=data.frame(matrix(na,1000,300) bmot[1,]=100 (j in 1:ncol(data)){ (i in 2:nrow(data)){ bmot[i,j]=bmot[i-1,j]*(1+mu*dt+sigma*sqrt(dt)*(weiner.matrix[i,j]-weiner.matrix[i-1,j])) } }
i trying simulate matrix of 1000 rows , 300 columns, 300 variables of geometric brownian motion. initial value starts @ 100 , randomness kicks in periods after t=1/row=1.
is there way run 300 brownian motion simulation without going cell-by-cell have in loop??
you can use cumsum
on set of normal variables produce single variable of brownian motion.
random <- rnorm(1000, 0, sqrt(0.15)) x <- 100 + cumsum(random) nsim <- 300
you can use apply, for
loop fast:
x <- matrix(rnorm(n = nsim * 1000, sd = sqrt(0.15)), nrow = 1000, ncol = 300) (i in 1:nsim) x[,i] <- cumsum(x[,i])
Comments
Post a Comment