Stock Price Simulation R code - Slow - Monte Carlo

后端 未结 1 1760
春和景丽
春和景丽 2021-01-20 10:51

I need to perform a stock price simulation using R code. The problem is that the code is a little bit slow. Basically I need to simulate the stock price for each time step (

相关标签:
1条回答
  • 2021-01-20 11:25

    Assuming S[0] = 1, you can build U as a follows:

    Ncols <- 252
    
    Nrows <- 100000
    
    U <- matrix(exp((r-v^2/2)*dt+v*sqrt(dt)*rnorm(Ncols*Nrows)), ncol=Ncols, nrow=Nrows)
    
    U <- do.call(rbind, lapply(1:Nrows, function(j)cumprod(U[j,])))
    

    EDIT: using Joshua's and Ben's suggestions:

    product version:

    U <- matrix(exp((r-v^2/2)*dt+v*sqrt(dt)*rnorm(Ncols*Nrows)), ncol=Ncols, nrow=Nrows)
    
    U <- t(apply(U, 1, cumprod))
    

    sum version:

    V <- matrix((r-v^2/2)*dt+v*sqrt(dt)*rnorm(Ncols*Nrows), ncol=Ncols, nrow=Nrows)
    
    V <- exp( t(apply(V, 1, cumsum)) )
    

    EDIT: as suggested by @Paul:

    Execution time for each proposal (using 10000 rows instead of 10^5):

    Using apply + cumprod

     user  system elapsed 
    0.61    0.01    0.62 
    

    Using apply + cumsum

     user  system elapsed 
    0.61    0.02    0.63 
    

    Using OP's original code

     user  system elapsed 
    67.38    0.00   67.52 
    

    Notes: The times shown above are the third measures of system.time. The first two measures for each code were discarded. I've used r <- sqrt(2), v <- sqrt(3) and dt <- pi. In his original code, I've also replaced S[i-1] for ifelse(i==1,1,S[i-1]), and preallocated U.

    0 讨论(0)
提交回复
热议问题