Saturday, July 16, 2011

MIT 6.00 Problem Set 4 - Problems 1 - 4 Answers

White-headed Capuchin, Costa Rica

Python Code Answer:

# MIT 6.00 Problem Set 4 - Problems 1-4

def nestEggFixed(salary, save, growthRate, years):
    balance = []
    contrib = salary * save * 0.01
    growth = 1 + 0.01 * growthRate

    for y in range(years):
        if y == 0:
            balance.append(contrib)        
        else:
            balance.append(balance[y-1] * growth + contrib)
        
        print "y: %d balance[y]: $%0.2f " % (y, balance[y])

    return balance

print nestEggFixed(10000, 10, 15, 5)  
print

def nestEggVariable(salary, save, growthRates):
    balance = []
    contrib = salary * save * 0.01

    for y in range(len(growthRates)):
        growth = 1 + 0.01 * growthRates[y]

        if y == 0:
            balance.append(contrib)         
        else:
            balance.append(balance[y-1] * growth + contrib)
        
        print "y: %d balance[y]: $%0.2f (%2.2f%%)" % (y, balance[y], growthRates[y])

    return balance

rates = [15, 15, 15, 15, 15]
print nestEggVariable(10000, 10, rates)
print

rates = [1.34, 2.5, 3.23, 1.2, 0.93]
print nestEggVariable(10000, 10, rates)
print
    

def postRetirement(savings, growthRates, expenses):
    for y in range(len(growthRates)):
        growth = 1 + 0.01 * growthRates[y]
        savings = (savings * growth) - expenses
        
        #print "y: %d savings: $%10.2f (%2.2f%%)" % (y, savings, rates[y])

    return savings

rates = [0, 0, 0, 0, 0]
print postRetirement(100000, rates, 10000)
print

rates = [7, 7, 7, 7, 7]
print postRetirement(100000, rates, 20000)
print

rates = [-25.8, -10.3, 2.1, 1.1, 2.2]
print postRetirement(100000, rates, 25000)
print

rates = [10, 5, 0, 5, 1]
print postRetirement(100000, rates, 30000)
print


def findMaxExpenses(salary, save, preRetireGrowthRates, postRetireGrowthRates, epsilon): 
    nestEgg = nestEggVariable(salary, save, preRetireGrowthRates)
    print "\nnestEgg: %7.2f\n" % (nestEgg[-1])

    finalBalance = 2 * epsilon
    upper = nestEgg[-1]
    lower = 0
    midPoint = ((upper - lower) / 2.0) + lower
    count = 0

    while abs(finalBalance) > epsilon:
        finalBalance = postRetirement(nestEgg[-1], postRetireGrowthRates, midPoint)
        print "finalBalance: %7.2f  expenses: %7.2f" % (finalBalance, midPoint)

        if finalBalance > 0.0:
            lower = midPoint
        else:
            upper = midPoint

        midPoint = ((upper - lower) / 2.0) + lower
            
            
preRates = [3, 4, 5, 0, 3]
postRates = [10, 5, 0, 5, 1]
print
print findMaxExpenses(10000, 10, preRates, postRates, 0.01)

Output:

y: 0 balance[y]: $1000.00
y: 1 balance[y]: $2150.00
y: 2 balance[y]: $3472.50
y: 3 balance[y]: $4993.38
y: 4 balance[y]: $6742.38
[1000.0, 2150.0, 3472.5, 4993.375, 6742.381249999999]

y: 0 balance[y]: $1000.00 (15.00%)
y: 1 balance[y]: $2150.00 (15.00%)
y: 2 balance[y]: $3472.50 (15.00%)
y: 3 balance[y]: $4993.38 (15.00%)
y: 4 balance[y]: $6742.38 (15.00%)
[1000.0, 2150.0, 3472.5, 4993.375, 6742.381249999999]

y: 0 balance[y]: $1000.00 (1.34%)
y: 1 balance[y]: $2025.00 (2.50%)
y: 2 balance[y]: $3090.41 (3.23%)
y: 3 balance[y]: $4127.49 (1.20%)
y: 4 balance[y]: $5165.88 (0.93%)
[1000.0, 2025.0, 3090.4075, 4127.492389999999, 5165.878069227]

50000.0

25240.39287

-56197.5143751

-34848.0


y: 0 balance[y]: $1000.00 (3.00%)
y: 1 balance[y]: $2040.00 (4.00%)
y: 2 balance[y]: $3142.00 (5.00%)
y: 3 balance[y]: $4142.00 (0.00%)
y: 4 balance[y]: $5266.26 (3.00%)

nestEgg: 5266.26

finalBalance: -7358.99  expenses: 2633.13
finalBalance: -454.23  expenses: 1316.57
finalBalance: 2998.14  expenses:  658.28
finalBalance: 1271.95  expenses:  987.42
finalBalance:  408.86  expenses: 1151.99
finalBalance:  -22.69  expenses: 1234.28
finalBalance:  193.09  expenses: 1193.14
finalBalance:   85.20  expenses: 1213.71
finalBalance:   31.26  expenses: 1223.99
finalBalance:    4.28  expenses: 1229.14
finalBalance:   -9.20  expenses: 1231.71
finalBalance:   -2.46  expenses: 1230.42
finalBalance:    0.91  expenses: 1229.78
finalBalance:   -0.77  expenses: 1230.10
finalBalance:    0.07  expenses: 1229.94
finalBalance:   -0.35  expenses: 1230.02
finalBalance:   -0.14  expenses: 1229.98
finalBalance:   -0.04  expenses: 1229.96
finalBalance:    0.02  expenses: 1229.95
finalBalance:   -0.01  expenses: 1229.96

http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-00-introduction-to-computer-science-and-programming-fall-2008/assignments/pset4.pdf

1 comment:

  1. This comment has been removed by a blog administrator.

    ReplyDelete