【问题标题】:Brownian motion simulation using R使用 R 进行布朗运动模拟
【发布时间】:2020-08-25 07:23:32
【问题描述】:

模拟时间倒置[0,100]中的布朗运动,并通过模拟n = 1000个点绘制路径。我生成以下代码:

 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")

如何模拟标准布朗运动的 50 条样本路径,并以不同的颜色显示每条路径,就像一堆轨迹一样?

我认为它会类似于replicate(50,bm),但是当我这样做时,xy.coords 中会出现错误。 感谢您的帮助!

在[0,1]上模拟布朗桥,通过模拟n = 1000个点绘制路径。我生成以下代码

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)

生成几何布朗运动样本路径的代码

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)

我正在尝试使用 matplot 函数,但无法生成相同的图表

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

【问题讨论】:

    标签: r graph simulation replication montecarlo


    【解决方案1】:

    这个怎么样

    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)
    

    我修改了我的答案并采纳了 Stephane Laurent 的 matplot 建议。这给出了以下图像。

    编辑:

    为了在 cmets 中回答您的问题,我认为您应该保留我的 bm 的初始代码,即 bm &lt;- c(0, cumsum(rnorm(n,0,sqrt(t/n))))。然后一切都很好!感谢您指出好的matplot 命令@Stephane Laurent。

    EDIT2:我刚刚意识到您就布朗桥提出了一个新问题。你可以试试这段代码

    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)
    

    这会产生

    此外,对于 Geometric Brownian Motian,请尝试以更少的重复次数修改您的代码

    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")
    

    在我的机器上,这会产生

    【讨论】:

    • 你可以做matplot(A, type = "l", col = cols, lty = 1)。这会自动设置很好的限制。
    • 非常感谢您的快速回复!!我想做同样的方法来模拟布朗桥。请看上面的代码
    • 我做错了什么,请检查一下@Taufi
    • 查看我的编辑。我不知道您尝试通过构造bb &lt;- bm - t*bm[n] 来完成什么。由于bm 永远不会在replicate 函数中发生变化,因此您将始终获得相同的轨迹,即它们完全重叠。
    • 我意识到您就布朗桥提出了一个新问题,并编辑了答案,包括布朗桥的附加代码。你是这个意思吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-12
    • 1970-01-01
    • 1970-01-01
    • 2017-12-14
    • 1970-01-01
    相关资源
    最近更新 更多