0

In R, I'm simulating variable following a Wiener process.

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 is a real, e.g. 0.23 ret is a real too

the error seems to be in the line : 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)
Jilber Urbina
  • 58,147
  • 10
  • 114
  • 138
Henri
  • 1,571
  • 5
  • 23
  • 38
  • 3
    how do you suppose we reproduce this error? – Arun Mar 08 '13 at 14:33
  • we need reproducible example. – CHP Mar 08 '13 at 14:34
  • @Henri pretty sure either `dz`, `ivol`, `dt` or `ret` is coming out to be vector instead of single value in your calculations. – CHP Mar 08 '13 at 14:36
  • 3
    try `options(error=recover)` to enter a browser at the failure point and diagnose what happened? – Ben Bolker Mar 08 '13 at 14:39
  • and alos ..`Sys.setenv(LANG = "en")` to switch from French error message to English...see [this](http://stackoverflow.com/questions/13575180/how-to-change-the-language-of-errors-in-r) for more details – agstudy Mar 08 '13 at 14:39
  • but probably the question is `too localized`? not sure. – Arun Mar 08 '13 at 14:43
  • Here is the error in english : Error in spot[i + 1, n] = spot[i, n] * exp(ret) : number of items to replace is not a multiple of replacement length – Henri Mar 08 '13 at 15:17
  • You were right. ivol is not a real as I was expecting, but well a data.frame... Thanks ! – Henri Mar 08 '13 at 15:23

1 Answers1

1

(After trying out different operations with errors)
Here's what I think is happening:

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

Basically, you're trying to replace an element of a matrix with more than one element. I speculate this is what's happening (your exp(.) returns a vector of more than 1 element).

Arun
  • 116,683
  • 26
  • 284
  • 387