在 R 中模拟一个过程 n 次

问题描述

我编写了一个 R 脚本(源自 here)来模拟股票价格的几何布朗运动路径,我需要模拟运行 1000 次,以便生成 1000 条过程路径Ut = Ste^-mu*t,通过对从 Ut 导出的运动定律进行离散化,Ut 是解决问题 here 的底线。

该过程还有 n = 252 步和离散化步长 = 1/252,还有 sigma = 0.4 和瞬时漂移 mu 的风险,我将其视为零,尽管我对此不确定。我正在努力模拟过程的 1000 条路径,但能够生成一条路径,我不确定我需要更改哪些变量,或者我的 for 循环中是否存在限制我生成所有 1000 条路径的问题。也可能是脚本模拟了每个单独的点进行 252 实现,而不是模拟整个过程?如果是这样,这会限制我生成所有 1000 条路径吗?是否也有可能我生成的数组定义为 U 没有被我正确生成? U[0] 必须等于 1,因此第一个实现 U(1) = 1 也必须如此。代码如下,我很难弄清楚这一点,因此感谢任何帮助。

#Simulating  Geometric Brownian motion (GMB)
tau <- 1 #time to expiry
N <- 253 #number of sub intervals
dt <- tau/N #length of each time sub interval
time <- seq(from=0,to=N,by=dt) #time moments in which we simulate the process
length(time) #it should be N+1

mu <- 0 #GBM parameter 1
sigma <- 0.4 #GBM parameter 2
s0 <- 1 #GBM parameter 3

#simulate Geometric Brownian motion path
dwt <- rnorm(N,mean = 0,sd = 1) #standard normal sample of N elements
dW <- dwt*sqrt(dt) #Brownian motion increments
W <- c(0,cumsum(dW)) #Brownian motion at each time instant N+1 elements


#Define U Array and set initial values of U
U <- array(0,c(N,1)) #array of U
U[0] = 1
U[1] <- s0 #first element of U is s0. with the for loop we find the other N elements

for(i in 2:length(U)){
  U[i] <- (U[1]*exp(mu - 0.5*sigma^2*i*dt + sigma*W[i-1]))*exp(-mu*i)
}

#Plot 
plot(ts(U),main = expression(paste("Simulation of Ut")))

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)