bisection method: Simulating a retirement fund

B

Baba

level: beginner

exercise source:
http://ocw.mit.edu/courses/electric...d-programming-fall-2008/assignments/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,epsilon,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
 
M

MRAB

level: beginner

exercise source:
http://ocw.mit.edu/courses/electric...d-programming-fall-2008/assignments/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,epsilon,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)
 
B

Baba

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
 
M

MRAB

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.
 
C

Carl Banks

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top