Velocity Reviews > bisection method: Simulating a retirement fund

# bisection method: Simulating a retirement fund

Baba
Guest
Posts: n/a

 09-02-2010
level: beginner

exercise source:
http://ocw.mit.edu/courses/electrica...ents/pset4.pdf

Problem 4

Can my code be optimised?
I think my approach is correct but i am hesitant about the initial max
value.

def nestEgVariable(salary, save, preRetireGrowthRates):
SavingsRecord = []
fund = 0
depositPerYear = salary * save * 0.01
for i in preRetireGrowthRates:
fund = fund * (1 + 0.01 * i) + depositPerYear
SavingsRecord += [fund,]
startingPot = SavingsRecord[-1]
return startingPot

def
expenseCalculator(postRetireGrowthRates,fundsize,e psilon,yearsOfretirement ):
low = 0
high = fundsize
guess = (low + high) /2
while abs(guess * yearsOfretirement - fundsize) > epsilon:
if guess * yearsOfretirement > fundsize :
high = guess
else: low = guess
guess = (low + high) /2
return guess

def findMaxExpenses(salary,save,preRetireGrowthRates,
postRetireGrowthRates,epsilon):
yearsOfretirement = len(postRetireGrowthRates)
fundsize = nestEgVariable(salary, save, preRetireGrowthRates)
for growthRate in postRetireGrowthRates:
expenses = expenseCalculator(postRetireGrowthRates,

fundsize ,epsilon,yearsOfretirement )
fundsize = (fundsize * (1 + 0.01 * growthRate)) - expenses
print 'fundsize', fundsize
print 'expenses', expenses
yearsOfretirement -=1
return fundsize

findMaxExpenses(10000,10,[3, 4, 5, 0, 3],[10, 5, 0, 5, 1],0.01)

thanks
Baba

MRAB
Guest
Posts: n/a

 09-02-2010
On 02/09/2010 21:37, Baba wrote:
> level: beginner
>
> exercise source:
> http://ocw.mit.edu/courses/electrica...ents/pset4.pdf
>
> Problem 4
>
> Can my code be optimised?
> I think my approach is correct but i am hesitant about the initial max
> value.
>
> def nestEgVariable(salary, save, preRetireGrowthRates):
> SavingsRecord = []
> fund = 0
> depositPerYear = salary * save * 0.01
> for i in preRetireGrowthRates:
> fund = fund * (1 + 0.01 * i) + depositPerYear
> SavingsRecord += [fund,]
> startingPot = SavingsRecord[-1]
> return startingPot
>

Why are you saving 'fund' in SavingsRecord if you're returning just the
last and discarding others? Basically you're returning the final value
of fund.
>
> def
> expenseCalculator(postRetireGrowthRates,fundsize,e psilon,yearsOfretirement ):
> low = 0
> high = fundsize
> guess = (low + high) /2
> while abs(guess * yearsOfretirement - fundsize)> epsilon:
> if guess * yearsOfretirement> fundsize :
> high = guess
> else: low = guess
> guess = (low + high) /2
> return guess
>

When performing this type of 'search' make sure that the interval (high
- low) reduces at every step. If, for example:

low + 1 == high

then:

guess == (low + (low + 1)) / 2 == (low * 2 + 1) / 2 == low

(integer division) and if the 'if' condition happens to be false then
the value of 'low' won't change for the next iteration, leading to an
infinite loop.
>
> def findMaxExpenses(salary,save,preRetireGrowthRates,
> postRetireGrowthRates,epsilon):
> yearsOfretirement = len(postRetireGrowthRates)
> fundsize = nestEgVariable(salary, save, preRetireGrowthRates)
> for growthRate in postRetireGrowthRates:
> expenses = expenseCalculator(postRetireGrowthRates,
>
> fundsize ,epsilon,yearsOfretirement )
> fundsize = (fundsize * (1 + 0.01 * growthRate)) - expenses
> print 'fundsize', fundsize
> print 'expenses', expenses
> yearsOfretirement -=1
> return fundsize
>
>
>
> findMaxExpenses(10000,10,[3, 4, 5, 0, 3],[10, 5, 0, 5, 1],0.01)
>

Baba
Guest
Posts: n/a

 09-03-2010
On Sep 2, 11:10*pm, MRAB <(E-Mail Removed)> wrote:
>
> Why are you saving 'fund' in SavingsRecord if you're returning just the
> last and discarding others? Basically you're returning the final value
> of fund.

Hi MRAB

ok i agree that this is not be ideal. I should shorten this to ONLY
return SavingsRecord[-1]

> When performing this type of 'search' make sure that the interval (high
> - low) reduces at every step. > (integer division) and if the 'if' condition happens to be false
> then the value of 'low' won't change for the next iteration, leading to an infinite loop.

If you look at the output you will see that the interval DOES seem to
reduce at each interval as expenses and fundsize reduce gradually. The
computation does not lead to an infinite loop.

tnx
Baba

MRAB
Guest
Posts: n/a

 09-03-2010
On 03/09/2010 09:06, Baba wrote:
> On Sep 2, 11:10 pm, MRAB<(E-Mail Removed)> wrote:
>>
>> Why are you saving 'fund' in SavingsRecord if you're returning just the
>> last and discarding others? Basically you're returning the final value
>> of fund.

>
> Hi MRAB
>
> ok i agree that this is not be ideal. I should shorten this to ONLY
> return SavingsRecord[-1]
>
>
>> When performing this type of 'search' make sure that the interval (high
>> - low) reduces at every step.> (integer division) and if the 'if' condition happens to be false
>> then the value of 'low' won't change for the next iteration, leading to an infinite loop.

>
>
> If you look at the output you will see that the interval DOES seem to
> reduce at each interval as expenses and fundsize reduce gradually. The
> computation does not lead to an infinite loop.
>

It doesn't in that particular case, but it might in some other cases.

Carl Banks
Guest
Posts: n/a

 09-03-2010
On Sep 2, 1:37*pm, Baba <(E-Mail Removed)> wrote:
> level: beginner

In this economy, simulating the value of retirement funds with
bisection is easy. Look:

def retirement_fund_value(n_years,initial_value):
for i in xrange(n_years):
value /= 2 # <- bisect value of fund
return value

Carl Banks