Jun. 3rd, 2010

[personal profile] aranthe

Sorry these are late! I completely forgot that it was due this week.

Note: To simply readability, I've removed all of the triple-quoted comments found in the template and will post problem 4 separately because it's longer.

PS4 » Problem 1

def nestEggFixed(salary, save, growthRate, years):

    assert salary > 0, 'Salary must be greater than 0:' + str(salary)
    assert 0 < save < 100, 'Savings percentage must be between 0 and 100:' + str(savePct)
    assert 0 < growthRate < 100, 'Growth rate must be greater than 0:' + str(growthPct)
    assert years > 0, 'Years must be greater than 0:' + str(years)

    savingsRecord = [] # Initialize savings list.

    # Add one to years to capture last year.
    for year in range( 1, years+1 ):

        # First year is different.
        if( year == 1 ):
            savingsRecord.append( salary * save * 0.01 )
        else:
            # savingsRecord[-1] is the savings at the end of the last year.
            savingsRecord.append( savingsRecord[-1] * (1 + 0.01 * growthRate) + salary * save * 0.01 )
    return savingsRecord

PS4 » Problem 2

def nestEggVariable(salary, save, growthRates):

    assert salary > 0, 'Salary must be greater than 0:' + str(salary)
    assert 0 < save < 100, 'Savings percentage must be between 0 and 100:' + str(savePct)

    savingsRecord = [] # Initialize savings list.
    years = len(growthRates) # Compute number of years from input variable.
    
    # Add one to years to capture last year.
    for year in range( 1, years+1 ):
        
        # First year is different.
        if( year == 1 ):
            savingsRecord.append( salary * save * 0.01 )
        else:
            # savingsRecord[-1] is the savings at the end of the last year.
            savingsRecord.append( savingsRecord[-1] * (1 + 0.01 * growthRates[year-1]) + salary * save * 0.01 )
    return savingsRecord

PS4 » Problem 3

def postRetirement(savings, growthRates, expenses):

    assert savings > 0, 'Savings must be greater than 0:' + str(savings)

    fundBalances = [] # Initialize fund list.
    years = len(growthRates) # Compute number of years from input variable.
    
    # Add one to years to capture last year.
    for year in range( 1, years+1 ):
        
        # First year is different.
        if( year == 1 ):
            fundBalances.append( savings * (1 + 0.01 * growthRates[year-1]) - expenses )
        else:
            # fundBalances[-1] is the fund at the end of the last year.
            fundBalances.append( fundBalances[-1] * (1 + 0.01 * growthRates[year-1]) - expenses )
    return fundBalances
[personal profile] aranthe

PS4 » Problem 4

def findMaxExpenses(salary, save, preRetireGrowthRates, postRetireGrowthRates,
                    epsilon):

    assert salary > 0, 'Salary must be greater than 0:' + str(salary)
    assert 0 < save < 100, 'Savings percentage must be between 0 and 100:' + str(savePct)
    assert 0 < epsilon < 1 , 'Upper boundary must be between 0 and 1:' + str(epsilon)

    # Generate the savings record.
    savingsRecord = nestEggVariable(salary, save, preRetireGrowthRates)

    # Savings at retirement will be the last entry;
    # it is also the upper limit on possible expenses.
    savingsAtRetirement = savingsRecord[-1]
    high = savingsAtRetirement
    guess = high # Seed initial guess.
    low = 0.0 # Seed lower guessing limit.
    found = False # Initialize search check.
    count = 1 # Initialize a counter, so search doesn't get out of hand.

    # Upper limit on count protects against process overruns.
    while not found and count < 100:

        # Calculate the fund balance with the current guess.
        fundBalances = postRetirement(savingsAtRetirement, postRetireGrowthRates, guess)
        endingBalance = fundBalances[-1] # Obtain the ending balance for testing.

        # Check to see if it is within episilon on either side of 0.
        if abs(endingBalance) < epsilon:
            found = True
        else:
            # If not, check to see if it's too high or too low.
            if endingBalance < 0:
                # Guess was too high. I need a new guess halfway between
                # the old one and the low.
                high = guess
                guess = (low + high)/2
            else:
                # Guess was too low. I need a new guess halfway between
                # the old guess and the last high.
                low = guess
                guess = (low + high)/2
            count = count + 1
    return guess

Profile

Introduction to Computer Science

July 2010

S M T W T F S
    123
45678910
11121314151617
18192021222324
2526272829 3031

Style Credit

Expand Cut Tags

No cut tags
Page generated Sep. 25th, 2017 06:19 am
Powered by Dreamwidth Studios