0

在 R 中,我正在模拟维纳过程之后的变量。

mcsim <- function(drift, dt, spot, spotdate, nap, maturity, sim)
{
  for(n in 1:sim)
  {
    for(i in 1:maturity)
    {
      dz = rnorm(1, mean=0, sd=1);
      ivol = findnap(spot[i,n], spotdate[i], nap)
      ret = (drift-(ivol^2)/2)*dt+ivol*sqrt(dt)*dz;
      spot[i+1,n] = spot[i,n]*exp(ret);

      #display counter
      cat(n/sim, "% of 100% \r");
      #flush.console()
    }
  }
  return(spot);
}

ivol是一个实数,例如 0.23 ret也是一个实数

错误似乎在行中:spot[i+1,n] = spot[i,n]*exp(ret);

>Error in FR : le nombre d'objets à remplacer n'est pas multiple de la taille du remplacement

>Error in EN : the number of objects that must be replaced is not a multiple of the size of the replacement. (sorry for the rough translation)
4

1 回答 1

1

(在尝试了不同的错误操作之后)
这是我认为正在发生的事情:

m <- matrix(1:4, ncol=2)

#      [,1] [,2]
# [1,]    1    3
# [2,]    2    4

m[2,2] <- m[2,1] * 4
# works fine
> m
#      [,1] [,2]
# [1,]    1    3
# [2,]    2    8

# will result in your error
m[2,2] <- m[1,2] * (1:2)

# Error in m[2, 2] <- m[1, 2] * (1:2) : 
#   number of items to replace is not a multiple of replacement length

基本上,您正在尝试用多个元素替换矩阵的元素。我推测这是正在发生的事情(您返回一个超过 1 个元素的向量)。exp(.)

于 2013-03-08T14:39:31.387 回答