以增长率投影时避免循环

时间:2013-02-26 12:17:01

标签: r for-loop

我有一组模拟增长率,比如8个时间段(和5个模拟增长路径),

r <- matrix(rnorm(40,0.05,0.01),5,8)
r
           [,1]       [,2]       [,3]       [,4]       [,5]       [,6]       [,7]       [,8]
[1,] 0.04229559 0.02846659 0.04948458 0.06144443 0.05657848 0.05782358 0.05545835 0.04090866
[2,] 0.06270360 0.06045967 0.04213729 0.05413941 0.06291148 0.05382643 0.05844549 0.03824342
[3,] 0.07846056 0.05503713 0.06800700 0.05888937 0.05759237 0.03789024 0.05250413 0.05011601
[4,] 0.04248757 0.04632404 0.04199074 0.04542522 0.03473972 0.04129197 0.06614095 0.06024244
[5,] 0.04382759 0.03555406 0.06630673 0.06019894 0.05057905 0.06336362 0.04954486 0.05092946

然后,我想使用x_{t+1} = x_{t} (1+r_{t})使用这些费率投影。我可以使用for循环

来完成此操作
x.fn<-function(x,rr){
  xx<-cbind(x,rr)
  for(i in 1:ncol(rr)){
    xx[,i+1]<-xx[,i]*(1+rr[,i])
  }
  xx
}
x.fn(x=100, rr=r)
       x                                                                        
[1,] 100 104.4835 110.8389 116.7353 122.0573 128.6441 136.0210 142.7797 146.9829
[2,] 100 106.1199 111.8381 115.9955 120.3976 125.9980 132.5384 138.1477 142.3214
[3,] 100 103.5990 106.6866 111.5629 117.5999 124.9613 131.6552 137.2966 142.9163
[4,] 100 105.7215 109.7624 113.8707 120.5216 128.8663 136.1915 143.3542 150.7466
[5,] 100 104.1080 109.6981 112.7072 118.6346 126.1118 130.1564 135.8039 142.0652

是否可以加快速度/避免使用带有for类型函数的apply循环?

2 个答案:

答案 0 :(得分:7)

您似乎需要的是r + 1cumulative product。对于我的“数据”,

> r <- matrix(rnorm(40,0.05,0.01),5,8)
> r
           [,1]       [,2]       [,3]       [,4]       [,5]       [,6]
[1,] 0.04305611 0.03545166 0.05882694 0.03910892 0.04796011 0.05631498
[2,] 0.05623084 0.03747785 0.04809927 0.05644677 0.06747468 0.05405979
[3,] 0.04389437 0.05353846 0.04600529 0.04363427 0.05399780 0.04260270
[4,] 0.04644610 0.05471288 0.06279882 0.02195831 0.05971777 0.05024525
[5,] 0.04426485 0.03294009 0.04325665 0.06006569 0.07416615 0.06585176
           [,7]       [,8]
[1,] 0.02724187 0.03515898
[2,] 0.04285792 0.04220358
[3,] 0.04302487 0.06662048
[4,] 0.04948629 0.04256084
[5,] 0.04659738 0.03395883

这是如何得到它:

> t(apply(r+1, 1, cumprod))
         [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
[1,] 1.043056 1.080034 1.143569 1.188293 1.245284 1.315412 1.351246 1.398755
[2,] 1.056231 1.095816 1.148524 1.213355 1.295225 1.365245 1.423756 1.483844
[3,] 1.043894 1.099783 1.150379 1.200575 1.265403 1.319313 1.376076 1.467751
[4,] 1.046446 1.103700 1.173011 1.198769 1.270356 1.334186 1.400210 1.459804
[5,] 1.044265 1.078663 1.125322 1.192916 1.281390 1.365771 1.429413 1.477954

(不要问我为什么t()在这里是必要的。)

然后,一切都是与初始大小相乘的问题:

> t(apply(r+1, 1, cumprod)) * 100
         [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
[1,] 104.3056 108.0034 114.3569 118.8293 124.5284 131.5412 135.1246 139.8755
[2,] 105.6231 109.5816 114.8524 121.3355 129.5225 136.5245 142.3756 148.3844
[3,] 104.3894 109.9783 115.0379 120.0575 126.5403 131.9313 137.6076 146.7751
[4,] 104.6446 110.3700 117.3011 119.8769 127.0356 133.4186 140.0210 145.9804
[5,] 104.4265 107.8663 112.5322 119.2916 128.1390 136.5771 142.9413 147.7954

哦,不要忘记为初始大小添加一列:

> cbind(1, t(apply(r+1, 1, cumprod))) * 100
     [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
[1,]  100 104.3056 108.0034 114.3569 118.8293 124.5284 131.5412 135.1246
[2,]  100 105.6231 109.5816 114.8524 121.3355 129.5225 136.5245 142.3756
[3,]  100 104.3894 109.9783 115.0379 120.0575 126.5403 131.9313 137.6076
[4,]  100 104.6446 110.3700 117.3011 119.8769 127.0356 133.4186 140.0210
[5,]  100 104.4265 107.8663 112.5322 119.2916 128.1390 136.5771 142.9413
         [,9]
[1,] 139.8755
[2,] 148.3844
[3,] 146.7751
[4,] 145.9804
[5,] 147.7954

答案 1 :(得分:2)

您的任务取决于之前的值。这似乎是一个递归函数的工作。但我不确定是否会有任何加速。这是一个版本,不使用递归函数本身,而是独立计算值(使用递归原理):

my_fun <- function(x, rr, idx) {
    i <- 1
    xx <- rep(x, nrow(rr))
    while( i <= idx) {
        xx <- xx * (1 + rr[, i])
        i <- i + 1
    }
    xx
}
apply(as.matrix(0:ncol(r), ncol=1), 1, function(ix) my_fun(100, r, ix))

如果列太多,可以使用此函数并行计算所有列。我没有看到使用此功能的其他优点。但也许最好通过基准测试进行验证。