R PROGRAM for calculating accumulated balances ============================================== 2/13/06 > BCalc.D function (Amts, Times, Rates, Cum=T) { ### Discrete payment stream Balance Calculation ### Deposit & withdrawals made in amounts Amts ### at times Times , the last of which is ### the final time at which the balance is to ### be evaluated. Interest rates are in the ### form of instantaneous-compounded (m=infty) ### annualized rates, or forces of mortality, ### and are taken to be piecewise constant ### between times in vector Times. ### NB. Amts and Times are vectors of same length, ### and Rates has one fewer component. ### Cum is Boolean optional parameter, which, ### if True, indicates that output Balance is ### to be given at times Times[-1]. ### FUTURE version may give continuous-time ### calculation, but for now, finely-discretized ### Times vector can serve the same purpose. M <- length(Rates) Accum <- c(1,cumprod(exp(Rates*diff(Times)))) Depos.Disc <- Amts/Accum Balance <- Accum*cumsum(Depos.Disc) list(Balance=if(Cum) Balance else Balance[length(Times)], Time = if(Cum) Times else Times[length(Times)]) } ## Here Amts is a vector of dimension the same as Times, and one component longer than the vector Rates. > BCalc.D(c(0,rep(.25,40)), seq(0,10,.25), rep(log(1.05),40)) $Balance [1] 0.0000000 0.2500000 0.5030681 0.7592418 1.0185594 1.2810594 [7] 1.5467809 1.8157633 2.0880468 2.3636718 2.6426793 2.9251109 [13] 3.2110086 3.5004148 3.7933727 4.0899259 4.3901184 4.6939950 [19] 5.0016008 5.3129816 5.6281838 5.9472542 6.2702403 6.5971901 [25] 6.9281524 7.2631763 7.6023117 7.9456090 8.2931194 8.6448945 [31] 9.0009867 9.3614489 9.7263348 10.0956987 10.4695955 10.8480808 [37] 11.2312110 11.6190430 12.0116346 12.4090442 12.8113309 $Time [1] 0.00 0.25 0.50 0.75 1.00 1.25 1.50 1.75 2.00 2.25 2.50 2.75 [13] 3.00 3.25 3.50 3.75 4.00 4.25 4.50 4.75 5.00 5.25 5.50 5.75 [25] 6.00 6.25 6.50 6.75 7.00 7.25 7.50 7.75 8.00 8.25 8.50 8.75 [37] 9.00 9.25 9.50 9.75 10.00 ### This illustration shows that the accumulated balance for an immediate annuity (quarterly payments, $1/yr for 10 years, at 5%APR), valued at time 10, is 12.81133, which should agree with our formula: > (1.05^10)*(1-1.05^(-10))/(4*(1.05^.25-1)) [1] 12.81133 ### The virtue of this little program is that it implements the calculation for time-varying interest rates. Thus, if we want to know the accumulated balance for r(t) = .04*(1+.01*t) of continuous deposits at rate D(t) = 1000*(1-.02*(t-5)^2) for 10 years is (to not very high accuracy, using 1e3, 1e4, or 1e5 points): > BCalc.D(1000*(1-.02*((0:1000)/100-5)^2)/100, seq(0,10,.01), .04*(1+.01*seq(0,10-.01,.01)),F) $Balance [1] 10387.67 $Time [1] 10 > BCalc.D(1000*(1-.02*((0:1e4)/1e3-5)^2)/1e3, seq(0,10,1e-3), + .04*(1+.01*seq(0,10-1e-3,1e-3)),F)$Balance [1] 10382.10 > BCalc.D(1000*(1-.02*((0:1e5)/1e4-5)^2)/1e4, seq(0,10,1e-4), + .04*(1+.01*seq(0,10-1e-4,1e-4)),F)$Balance [1] 10381.54 ### The exact value, to high accuracy, from the formula, is > integrate(function(t) 1000*(1-.02*(t-5)^2)*exp(-.04*t-.0002*t^2), 0,10)$val*exp(.04*10 + .0002*100) [1] 10381.48