2013-03-08 106 views
0

在R,我模擬變量以下維納過程。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) 
+3

你怎麼想,我們重現這個錯誤? – Arun 2013-03-08 14:33:08

+0

我們需要重現的例子。 – 2013-03-08 14:34:53

+0

@Henri相當肯定是'dz','ivol','dt'或'ret'是走出來是矢量,而不是單一的價值在你的計算。 – 2013-03-08 14:36:54

回答

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 

基本上,你想有更多的次更換元素的矩陣一個元素是。我推測這是發生了什麼事(你的exp(.)返回一個超過1個元素的向量)。