gpt4 book ai didi

r - 使用 R 进行布朗运动模拟

转载 作者:行者123 更新时间:2023-12-04 01:27:11 26 4
gpt4 key购买 nike

模拟时间倒数[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")

最佳答案

这个怎么样

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建议。这给出了下图。

enter image description here

编辑:

为了在评论中回答您的问题,我认为您应该保留我的初始代码 bm这是 bm <- 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)

这产生

enter image description here

此外,对于几何布朗莫天,请尝试对您的代码进行这种修改,减少重复次数

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

在我的机器上,这会产生

enter image description here

关于r - 使用 R 进行布朗运动模拟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61701138/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com