Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoiding for loops when projecting with a growth rate

Tags:

for-loop

r

I have a set of simulated growth rates, over say 8 time periods (and 5 simulated growth paths),

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

I then want to project using these rates using x_{t+1} = x_{t} (1+r_{t}). I can do this using a for loop,

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

Is it possible to speed things up/avoid the use of the for loop with an apply type function?

like image 847
guyabel Avatar asked Jan 15 '23 02:01

guyabel


1 Answers

What you seem to need is a row-wise cumulative product of r + 1. For my "data",

> 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

this is how to get it:

> 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

(Don't ask me why the t() is necessary here.)

Then, everything is a matter of multiplying with the initial size:

> 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

Oh, and don't forget to add a column for the initial size, too:

> 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
like image 148
krlmlr Avatar answered Jan 19 '23 10:01

krlmlr