2013-10-06 56 views
4

格路例如,如果我有一個格子,看起來像這樣:生成中的R

  133.1 
     /
     121 
    /\ 
    110 108.9 
/\/
100 99 
    \/\ 
    90 89.1 
     \/
     81 
     \ 
      72.9 

凡格從100開始,要麼用因子1.1上升,並用係數0.9下降。這個格子有3個週期,在這個週期中上升或下降。很明顯,這個矩陣可以填充更多的時間段。

矩陣形式的格子看起來是這樣的:

 [,1] [,2] [,3] [,4] 
[1,] 100 110 121 133.1 
[2,] NA 90 99 108.9 
[3,] NA NA 81 89.1 
[4,] NA NA NA 72.9 

我在工作R.的代碼來生成格矩陣如下:

#Parameters 
S0 <- 100 #price at t0 
u <- 1.1 #up factor 
d <- 0.9 #down factor 
n <- 3 #number of periods 

#Matrix for the prices 
prices <- matrix(data=NA, nrow=(n+1), ncol=(n+1)) 
prices[1,1] <- S0 

#Fill the matrix 
for(column in 2:(n+1)){ 

    for(row in 1:(column-1)){ 

    prices[row,column] <- u*prices[row,column-1]; 
    } 

    prices[column,column] <- d*prices[column-1,column-1]; 
} 

我想創建一個代碼生成一個矩陣,其中包含所有可能的路徑。在這個例子中,它應該是這樣的:

 [,1] [,2] [,3] [,4] 
[1,] 100 110 121 133.1 
[2,] 100 110 121 108.9 
[3,] 100 110 99 108.9 
[4,] 100 110 99 89.1 
[5,] 100 90 99 108.9 
[6,] 100 90 99 89.1 
[7,] 100 90 81 89.1 
[8,] 100 90 81 72.9 

我一直在掙扎着這片現在小時的代碼,所以任何幫助,將不勝感激!提前致謝! :)

回答

5

長度爲n的每個路徑對應於 以上下移動的序列: 您只需要枚舉所有這些序列。 如果你已經有長度n-1, 的序列作爲基質u, 長度n的序列可以得到

rbind( 
    cbind(u, .9), 
    cbind(u, 1.1) 
) 

你可以把它放在一個函數,並調用它n倍。

n <- 4 
up <- 1.1 
down <- .9 
m <- Reduce( 
    function(u,v) rbind(cbind(u, up), cbind(u, down)), 
    rep(NA,n), 
    100 
) 
t(apply(m, 1, cumprod)) 
# [1,] 100 110 121 133.1 146.41 
# [2,] 100 90 99 108.9 119.79 
# [3,] 100 110 99 108.9 119.79 
# [4,] 100 90 81 89.1 98.01 
# [5,] 100 110 121 108.9 119.79 
# [6,] 100 90 99 89.1 98.01 
# [7,] 100 110 99 89.1 98.01 
# [8,] 100 90 81 72.9 80.19 
# [9,] 100 110 121 133.1 119.79 
# [10,] 100 90 99 108.9 98.01 
# [11,] 100 110 99 108.9 98.01 
# [12,] 100 90 81 89.1 80.19 
# [13,] 100 110 121 108.9 98.01 
# [14,] 100 90 99 89.1 80.19 
# [15,] 100 110 99 89.1 80.19 
# [16,] 100 90 81 72.9 65.61 
2

同樣,你可以做這樣的事情:

next.period<-function(x) rep(x,2)*rep(c(u,d),each=length(x)) 

next.matrix<-function(x) { 
    next.period.col<-next.period(x[,ncol(x)]) 
    cbind(rbind(x,x),next.period.col) 
} 

lattice.paths<-t(S0) 
for (i in 1:(n-1)) lattice.paths<-next.matrix(lattice.paths) 

     next.period.col next.period.col 
[1,] 100    110    121 
[2,] 100    90    99 
[3,] 100    110    99 
[4,] 100    90    81 
2

另一個想法是首先建立一個「支架」,利用expand.grid,明年填充它。

#all possible paths of times (either 1.1. or 0.9) each previous value 
    aa <- expand.grid(1, c(1.1,0.9), c(1.1,0.9), c(1.1,0.9), c(1.1,0.9)) 

    aa[,1] <- aa[,1] * 100 # start with 100 

    for(i in 2:ncol(aa)) # fill by multiplying value of previous column 
    { 
     aa[,i] <- aa[,i] * aa[,i-1] 
    } 

    aa 
    #Var1 Var2 Var3 Var4 Var5 
    #1 100 110 121 133.1 146.41 
    #2 100 90 99 108.9 119.79 
    #3 100 110 99 108.9 119.79 
    #4 100 90 81 89.1 98.01 
    #5 100 110 121 108.9 119.79 
    #6 100 90 99 89.1 98.01 
    #7 100 110 99 89.1 98.01 
    #8 100 90 81 72.9 80.19 
    #9 100 110 121 133.1 119.79 
    #10 100 90 99 108.9 98.01 
    #11 100 110 99 108.9 98.01 
    #12 100 90 81 89.1 80.19 
    #13 100 110 121 108.9 98.01 
    #14 100 90 99 89.1 80.19 
    #15 100 110 99 89.1 80.19 
    #16 100 90 81 72.9 65.61 

更多時間,expand.grid需要另一個c(1.1,0.9)