Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Problem Set 4
- # Name: Vaboro
- # Collaborators: None
- # Time: N/A
- #
- # Problem 1
- #
- def nestEggFixed(salary, save, growthRate, years):
- """
- - salary: the amount of money you make each year.
- - save: the percent of your salary to save in the investment account each
- year (an integer between 0 and 100).
- - growthRate: the annual percent increase in your investment account (an
- integer between 0 and 100).
- - years: the number of years to work.
- - return: a list whose values are the size of your retirement account at
- the end of each year.
- """
- # TODO: Your code here.
- retirement_account = [salary * save * 0.01,]
- for i in range(years-1):
- retirement_account.append(retirement_account[i] * (1 + growthRate * 0.01) + salary * save * 0.01)
- return retirement_account
- def testNestEggFixed():
- salary = 10000
- save = 10
- growthRate = 15
- years = 5
- savingsRecord = nestEggFixed(salary, save, growthRate, years)
- print savingsRecord
- # Output should have values close to:
- # [1000.0, 2150.0, 3472.5, 4993.375, 6742.3812499999995]
- # TODO: Add more test cases here.
- salary = 20000
- save = 10
- grothRate = 15
- years = 5
- savingsRecord = nestEggFixed(salary, save, growthRate, years)
- print savingsRecord
- salary = 10000
- save = 10
- grothRate = 15
- years = 10
- savingsRecord = nestEggFixed(salary, save, growthRate, years)
- print savingsRecord
- #
- # Problem 2
- #
- def nestEggVariable(salary, save, growthRates):
- # TODO: Your code here.
- """
- - salary: the amount of money you make each year.
- - save: the percent of your salary to save in the investment account each
- year (an integer between 0 and 100).
- - growthRates: a list of the annual percent increases in your investment
- account (integers between 0 and 100).
- - return: a list of your retirement account value at the end of each year.
- """
- retirement_account = [salary * save * 0.01,]
- for i in range(len(growthRates)-1):
- retirement_account.append(retirement_account[i] * (1 + growthRates[i+1] * 0.01) + salary * save * 0.01)
- return retirement_account
- def testNestEggVariable():
- salary = 10000
- save = 10
- growthRates = [3, 4, 5, 0, 3]
- savingsRecord = nestEggVariable(salary, save, growthRates)
- print savingsRecord
- # Output should have values close to:
- # [1000.0, 2040.0, 3142.0, 4142.0, 5266.2600000000002]
- # TODO: Add more test cases here.
- salary = 10000
- save = 10
- growthRates = [3, 4, 5, 0, 3, 5, 2, 7, 9]
- savingsRecord = nestEggVariable(salary, save, growthRates)
- print savingsRecord
- salary = 20000
- save = 5
- growthRates = [3, 4, 5, 0, 3, 2, 3, 1, 5, 7]
- savingsRecord = nestEggVariable(salary, save, growthRates)
- print savingsRecord
- #
- # Problem 3
- #
- def postRetirement(savings, growthRates, expenses):
- """
- - savings: the initial amount of money in your savings account.
- - growthRate: a list of the annual percent increases in your investment
- account (an integer between 0 and 100).
- - expenses: the amount of money you plan to spend each year during
- retirement.
- - return: a list of your retirement account value at the end of each year.
- """
- # TODO: Your code here.
- fund_size = [savings * (1 + 0.01 * growthRates[0]) - expenses,]
- for i in range(len(growthRates)-1):
- fund_size.append(fund_size[i] * (1 + 0.01 * growthRates[i+1]) - expenses)
- return fund_size
- def testPostRetirement():
- savings = 100000
- growthRates = [10, 5, 0, 5, 1]
- expenses = 30000
- savingsRecord = postRetirement(savings, growthRates, expenses)
- print savingsRecord
- # Output should have values close to:
- # [80000.000000000015, 54000.000000000015, 24000.000000000015,
- # -4799.9999999999854, -34847.999999999985]
- # TODO: Add more test cases here.
- savings = 200000
- growthRates = [10, 5, 0, 5, 1,0,3,2]
- expenses = 10000
- savingsRecord = postRetirement(savings, growthRates, expenses)
- print savingsRecord
- savings = 80000
- growthRates = [10, 5, 0, 5, 1,4,6,8]
- expenses = 20000
- savingsRecord = postRetirement(savings, growthRates, expenses)
- print savingsRecord
- #
- # Problem 4
- #
- def findMaxExpenses(salary, save, preRetireGrowthRates, postRetireGrowthRates,
- epsilon):
- """
- - salary: the amount of money you make each year.
- - save: the percent of your salary to save in the investment account each
- year (an integer between 0 and 100).
- - preRetireGrowthRates: a list of annual growth percentages on investments
- while you are still working.
- - postRetireGrowthRates: a list of annual growth percentages on investments
- while you are retired.
- - epsilon: an upper bound on the absolute value of the amount remaining in
- the investment fund at the end of retirement.
- """
- # TODO: Your code here.
- retirement_account = nestEggVariable(salary,save,preRetireGrowthRates)
- retirement_fund = retirement_account[len(retirement_account)-1]
- expenses_low = 0
- expenses_high = retirement_fund
- expenses = (expenses_high + expenses_low) / 2
- savings_account = postRetirement(retirement_fund, postRetireGrowthRates, expenses)
- savings_left = savings_account[len(savings_account)-1]
- while abs(savings_left) > epsilon:
- print "Current expenses estimate =", expenses, "Savings left =", savings_left,
- if savings_left < 0:
- expenses_high = expenses
- print "Previous expenses low =", expenses_low, "New expenses high =", expenses_high
- else:
- expenses_low = expenses
- print "New expenses low =", expenses_low, "Previous expenses high =", expenses_high
- expenses = (expenses_high + expenses_low) / 2
- savings_account = postRetirement(retirement_fund, postRetireGrowthRates, expenses)
- savings_left = savings_account[len(savings_account)-1]
- return expenses
- def testFindMaxExpenses():
- salary = 10000
- save = 10
- preRetireGrowthRates = [3, 4, 5, 0, 3]
- postRetireGrowthRates = [10, 5, 0, 5, 1]
- epsilon = 0.01
- expenses = findMaxExpenses(salary, save, preRetireGrowthRates,
- postRetireGrowthRates, epsilon)
- print expenses
- # Output should have a value close to:
- # 1229.95548986
- # TODO: Add more test cases here.
- salary = 20000
- save = 15
- preRetireGrowthRates = [3, 4, 5, 0, 3, 4, 8, 1]
- postRetireGrowthRates = [10, 5, 0, 5, 1]
- epsilon = 0.01
- expenses = findMaxExpenses(salary, save, preRetireGrowthRates,
- postRetireGrowthRates, epsilon)
- print expenses
- salary = 50000
- save = 10
- preRetireGrowthRates = [3, 4, 5, 0, 3, 4, 6, 7, 9, 1, 2]
- postRetireGrowthRates = [10, 5, 0, 5, 1]
- epsilon = 0.01
- expenses = findMaxExpenses(salary, save, preRetireGrowthRates,
- postRetireGrowthRates, epsilon)
- print expenses
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement