bisection method: Simulating a retirement fund

Discussion in 'Python' started by Baba, Sep 2, 2010.

  1. Baba

    Baba Guest

    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
    Baba, Sep 2, 2010
    #1
    1. Advertising

  2. Baba

    MRAB Guest

    On 02/09/2010 21:37, Baba wrote:
    > 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)
    >
    MRAB, Sep 2, 2010
    #2
    1. Advertising

  3. Baba

    Baba Guest

    On Sep 2, 11:10 pm, MRAB <> 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
    Baba, Sep 3, 2010
    #3
  4. Baba

    MRAB Guest

    On 03/09/2010 09:06, Baba wrote:
    > On Sep 2, 11:10 pm, MRAB<> 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.
    MRAB, Sep 3, 2010
    #4
  5. Baba

    Carl Banks Guest

    On Sep 2, 1:37 pm, Baba <> 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
    Carl Banks, Sep 3, 2010
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Whitecrest
    Replies:
    0
    Views:
    356
    Whitecrest
    Jul 10, 2003
  2. rbt
    Replies:
    2
    Views:
    433
    Roel Schroeven
    Jun 1, 2005
  3. Replies:
    0
    Views:
    406
  4. anand
    Replies:
    18
    Views:
    454
    Ben Pfaff
    Feb 28, 2006
  5. Replies:
    14
    Views:
    80
    Gregory Ewing
    Jan 22, 2014
Loading...

Share This Page