格路例如,如果我有一个格子,看起来像这样:生成中的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
我一直在挣扎着这片现在小时的代码,所以任何帮助,将不胜感激!提前致谢! :)