Simple First Program

A very simple example to start and check if installs have worked.

Example is a trivial and contrived one but illustrative.

Given a starting amount of $100,000  analyze effect of varying withdrawal rates on balances from this amount over some planning horizon.

That is,  starting with rate of say 4%, withdraw this percentage from amount available  calculate yearly balances over horizon of 30 years.   This simple case assumes that investment is split 60% equity:40% fixed and that return on equity portion is a constant 8%, and return on fixed is a constant 4%.  We also want to see what the current inflation adjusted value would be and for that we use a constant CPI rate of 3%.    Then increase withdrawal constant by some other amount, and calculate balances again.  Repeat for any number of simulations desired.  We would want to set some boundaries of course.

All of these parameter values have been hard-coded into this code shown below which is not what we want to do eventually.  Real-world data unfortunately shows much more variability and we will try and include all of this later.

These assumptions can be changed as desired – just insert your own and run the code. I have used all uppercase names as a convention to indicate constants or user defined parameters.

Nominal balances are calculated over time horizon of analysis along with values adjusted for inflation in current dollars.  These are then plotted on graph. Results will be plotted within R-IDE itself. Notice the wide divergence between the two.

We could also have printed out results of each simulation but then we’d have a mass of numbers to wade through.

Attached are the images that one should get with the values as given.

The actual R code is shown below.  This can be copied from the box below and pasted directly into the R-IDE and executed.  Alternately, it can be saved to a file and then run via the source(filename) command.

### Simple example of annual withdrawals from initial capital
### with fixed assumptions of expected returns from various
### market instruments along with fixed duration of 30 years
### with graph plots to better visualize 
###
### While this can also be quickly done in Excel, see how effortlessly
### we can test out results at various WD values and do plots.
### And as we'll see not just one variable!!!
###
### Initial prefixed static values for:
### ( using uppercase for constants as convention)
### equity allocation; return on equity; return on fixed instruments
### withdrwal rate; CPI inflation rate
### initial balance of 100K
### 
###

ALLOCEQ=0.6
RETURNEQTY=.08
RETURNFXD=.04
WDRATE=0.4
CPIRATE=0.03
INITBAL =c(rep(100000,30))

### #######################################################################################
### simple case of a function which will interate through years
### and calculate balances after withdrawal of a given percentage
### this way we will keep balance always positive
##############################################################################################
case1<- function(x, alloceq=ALLOCEQ, rtnEqty=RETURNEQTY, rtnFxd=RETURNFXD, wdrate=WDRATE) {
#print (sprintf (" Processing Case Eq.Alloc=%f; Withdrawal=%f; EquityRet=%f; FixedRet=%f" , ALLOCEQ, WDRATE, RETURNEQTY, RETURNFXD ) )
wdtYr<-wdrate*x
balEq<-alloceq*x
for ( i in 2:length(x)) {
x[i] = alloceq*x[i-1]*(1+rtnEqty) + (1-alloceq)*x[i-1]*(1+rtnFxd)
wdtYr[i] = x[i] * wdrate
balEq[i] = alloceq * ( x[i] - wdtYr[i]) 
x[i] = x[i] - wdtYr[i]
}
return (list(x, wdtYr, balEq))

}


retList <- lapply(case1(INITBAL, alloceq=ALLOCEQ, rtnEqty=0.075, wdrate=0.06), function(x) round(x,2) )

yrBal <- retList[[1]]
wdtYr <- retList[[2]]
balEq <- retList[[3]]


###### To see effect of different withdrawal rates
###### function will compute year-by-year for each different withdrwal rate given
###### we can vary other parms as well but simple for now
###########################################################################################
runWD <- function (x) {
retList <- lapply(case1(INITBAL, rtnEqty=RETURNEQTY, wdrate=x), function(y) round(y,2) )
return(retList[[1]])
}

## vary withdrawal rates from 4% to 8% in increments of .4%
parmWD<-seq(.04, .08, by=.004)
rangeYrBal<-lapply(parmWD, function(x) runWD(x))

xrange <- range (seq(1:length(INITBAL)) )
yrange <- range (c(0, INITBAL * 1.75) )


colors <- rainbow(length(parmWD))
lntype <- c(1:length(parmWD))
pchar <- seq(12, 12+length(parmWD), 1)
##
## begin plot of data
plot (xrange, yrange, type="n", xlab="Year", ylab="YearEnd Balance")
for ( i in 1:length(parmWD)) {
print (sprintf (" Plotting Withdrawal Case =%f" , parmWD[i] ) ) # just showing message
lines( seq(1:length(rangeYrBal[[i]])), rangeYrBal[[i]], type="b",
lwd=2, lty=lntype[i], col=colors[i], pch=pchar[i] )
}

txt = paste("Varying WD% Rates from ", 100*parmWD[1], " to ", 100*parmWD[length(parmWD)], "\n Eqty Alloc ", 100*ALLOCEQ ,
"| Ret% Eq ", RETURNEQTY*100, " Fixed ", RETURNFXD*100, sep="" )

title(txt, "Nominal Balances")
#legend("topright", <<< needs legend keyword?
# the xrange and y range can be controlled to properly place it, i.e., 
legend(xrange[1], 65000,
parmWD,
cex=0.8,
col=colors,
pch=pchar,
lty=lntype,
title="WD Rate" )

####
#### Now lets adjust for inflation
#### factor compounded for years out

factorCPI = cumprod(c(1, rep(1 + CPIRATE,29)))

plot (xrange, yrange, type="n", xlab="Year", ylab="Inflation Adjusted YearEnd Balance")

## inflation factor applied to nominal year end balances 
for ( i in 1:length(parmWD)) {
lines( seq(1:length(rangeYrBal[[i]])), rangeYrBal[[i]]/factorCPI, type="b", cex=0.4,
lwd=2, lty=1, col=colors[i], pch=pchar[i] )
}

title(txt, "Inflation Adjusted Balances")
legend(0, 90000,
#legend(xrange[1], yrange[1],
parmWD,
cex=0.65,
col=colors,
pch=pchar,
lty=lntype,
title="WD Rate" )

t showing nominal balances

 

Plot showing Inflation Adjusted Balances

Thanx for reading