My objective function:
helper.post<-function(monthly.mean.return,
start.capital, #initial nest egg
target.legacy,
monthly.inflation.post,
monthly.withdrawals,
n.obs){
req = matrix(start.capital, n.obs+1, 1) #matrix for storing target weight
for (a in 1:n.obs) {
#cat("a: ",a,"\n")
req[a + 1, ] = req[a, ] * (1 + monthly.mean.return - monthly.inflation.post) - monthly.withdrawals[a,]
}
ending.value=req[nrow(req),]
#ending.value
value=target.legacy - ending.value
return(abs(value))
}
With the following Optimization structure, changing the n.obs between the two values give the same output:
ie if n.obs = 288 or n.obs = 336, it gives the same values.
optimize(f=helper.post,
start.capital = 1000000,
target.legacy = 1000000,
monthly.inflation.post=0.002083333,
monthly.withdrawals = matrix(rep(10000,n.obs)),
n.obs = n.obs,
lower = 0,
upper = 1,
tol = 0.00000000000000000000000000000000001)$minimum
The value is correct seems to be a estimation as oppose to the correct value. Any idea what I may be doing incorrectly? Would a different optimization tool work better for such precise optimization efforts? I tried uni-root, but it doesn't sem to work as the end points are not opposite signs..
uniroot( helper.post,
c(0, 1),
start.capital = start.capital,
target.legacy = target.legacy,
monthly.inflation.post=monthly.inflation.post,
monthly.withdrawals = monthly.withdrawals,
n.obs = n.obs)$root