-2

我希望这段代码能够工作,但 (t) 的长度和 w(t) 的长度不同,所以它阻塞了我所有的代码。我能知道两者如何具有相同的长度吗?

    S(t)= S(0)exp(0.06t+0.20w(t))  #(1)
    with S(0) =20
    w(t) = standard Brownian movement

    t <- seq(1,900,length=900) #the path or step=1 for 3 years (1y=300)
    length(t)
    v = matrix(rnorm(log(20)+0.06*t,sd=sqrt((0.20)^(2)*t)),nrow=5)
    z = matrix(NA, ncol=900, nrow=5)
    w = function(t)
    {c(0,cumsum(v))}
    length(w(t))
    Bn <-log(20)+ 0.06*t +0.20*w(t)
    Br <- log(Bn)
    plot(t,Br,type="l",xlab="Temps") 

    for (i in 1:5) # I need to draw 5 path of the function(1)
    {z[i,] = c(0,cumsum(Br[i,]))}
    dim(z)
    u= apply(z,2,mean) #mean of the 5 path

    plot(t,z[1,],xlab="temps",type="l",ylab="Brownian Movement")
    for (i in 1:5){lines(t,z[i,])}
    lines(t,u,lwd=2,col="red")

感谢您的时间

4

1 回答 1

0

使用w= function(t){c(cumsum(v))}使所有变量都有 900 个元素。Br只有一维,所以你必须使用Br[i]. 这有效:

t <- seq(1,900,length=900) #the path or step=1 for 3 years (1y=300)
length(t)
v = matrix(rnorm(log(20)+0.06*t,sd=sqrt((0.20)^(2)*t)),nrow=5)
z = matrix(NA, ncol=900, nrow=5)
w = function(t)
{c(cumsum(v))}
length(w(t))
Bn <-log(20)+ 0.06*t +0.20*w(t)
Br <- log(Bn)
length(Bn)
 length(Br)
plot(t,Br,type="l",xlab="Temps")

for (i in 1:5) # I need to draw 5 path of the function(1)
{z[i,] = c(cumsum(Br[i]))}
dim(z)
u= apply(z,2,mean) #mean of the 5 path

plot(t,z[1,],xlab="temps",type="l",ylab="Brownian Movement")
for (i in 1:5){lines(t,z[i,])}
lines(t,u,lwd=2,col="red")

在此处输入图像描述

于 2015-11-01T13:08:53.917 回答