Advertisement
vaboro

ps4.py

Jun 2nd, 2011
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.39 KB | None | 0 0
  1. # Problem Set 4
  2. # Name: Vaboro
  3. # Collaborators: None
  4. # Time: N/A
  5.  
  6. #
  7. # Problem 1
  8. #
  9.  
  10. def nestEggFixed(salary, save, growthRate, years):
  11.     """
  12.    - salary: the amount of money you make each year.
  13.    - save: the percent of your salary to save in the investment account each
  14.      year (an integer between 0 and 100).
  15.    - growthRate: the annual percent increase in your investment account (an
  16.      integer between 0 and 100).
  17.    - years: the number of years to work.
  18.    - return: a list whose values are the size of your retirement account at
  19.      the end of each year.
  20.    """
  21.     # TODO: Your code here.
  22.  
  23.     retirement_account = [salary * save * 0.01,]
  24.  
  25.     for i in range(years-1):
  26.        
  27.         retirement_account.append(retirement_account[i] * (1 + growthRate * 0.01) + salary * save * 0.01)
  28.    
  29.     return retirement_account
  30.  
  31.    
  32.  
  33. def testNestEggFixed():
  34.     salary     = 10000
  35.     save       = 10
  36.     growthRate = 15
  37.     years      = 5
  38.     savingsRecord = nestEggFixed(salary, save, growthRate, years)
  39.     print savingsRecord
  40.     # Output should have values close to:
  41.     # [1000.0, 2150.0, 3472.5, 4993.375, 6742.3812499999995]
  42.  
  43.     # TODO: Add more test cases here.
  44.  
  45.     salary     = 20000
  46.     save       = 10
  47.     grothRate  = 15
  48.     years      = 5
  49.     savingsRecord = nestEggFixed(salary, save, growthRate, years)
  50.     print savingsRecord
  51.  
  52.     salary     = 10000
  53.     save       = 10
  54.     grothRate  = 15
  55.     years      = 10
  56.     savingsRecord = nestEggFixed(salary, save, growthRate, years)
  57.     print savingsRecord
  58.    
  59.  
  60. #
  61. # Problem 2
  62. #
  63.  
  64. def nestEggVariable(salary, save, growthRates):
  65.     # TODO: Your code here.
  66.     """
  67.    - salary: the amount of money you make each year.
  68.    - save: the percent of your salary to save in the investment account each
  69.      year (an integer between 0 and 100).
  70.    - growthRates: a list of the annual percent increases in your investment
  71.      account (integers between 0 and 100).
  72.    - return: a list of your retirement account value at the end of each year.
  73.    """
  74.  
  75.     retirement_account = [salary * save * 0.01,]
  76.  
  77.     for i in range(len(growthRates)-1):
  78.  
  79.         retirement_account.append(retirement_account[i] * (1 + growthRates[i+1] * 0.01) + salary * save * 0.01)
  80.  
  81.     return retirement_account
  82.    
  83. def testNestEggVariable():
  84.     salary      = 10000
  85.     save        = 10
  86.     growthRates = [3, 4, 5, 0, 3]
  87.     savingsRecord = nestEggVariable(salary, save, growthRates)
  88.     print savingsRecord
  89.     # Output should have values close to:
  90.     # [1000.0, 2040.0, 3142.0, 4142.0, 5266.2600000000002]
  91.  
  92.     # TODO: Add more test cases here.
  93.  
  94.     salary      = 10000
  95.     save        = 10
  96.     growthRates = [3, 4, 5, 0, 3, 5, 2, 7, 9]
  97.     savingsRecord = nestEggVariable(salary, save, growthRates)
  98.     print savingsRecord
  99.  
  100.     salary      = 20000
  101.     save        = 5
  102.     growthRates = [3, 4, 5, 0, 3, 2, 3, 1, 5, 7]
  103.     savingsRecord = nestEggVariable(salary, save, growthRates)
  104.     print savingsRecord
  105.  
  106. #
  107. # Problem 3
  108. #
  109.  
  110. def postRetirement(savings, growthRates, expenses):
  111.     """
  112.    - savings: the initial amount of money in your savings account.
  113.    - growthRate: a list of the annual percent increases in your investment
  114.      account (an integer between 0 and 100).
  115.    - expenses: the amount of money you plan to spend each year during
  116.      retirement.
  117.    - return: a list of your retirement account value at the end of each year.
  118.    """
  119.     # TODO: Your code here.
  120.  
  121.     fund_size = [savings * (1 + 0.01 * growthRates[0]) - expenses,]
  122.  
  123.     for i in range(len(growthRates)-1):
  124.  
  125.         fund_size.append(fund_size[i] * (1 + 0.01 * growthRates[i+1]) - expenses)
  126.  
  127.     return fund_size
  128.  
  129. def testPostRetirement():
  130.     savings     = 100000
  131.     growthRates = [10, 5, 0, 5, 1]
  132.     expenses    = 30000
  133.     savingsRecord = postRetirement(savings, growthRates, expenses)
  134.     print savingsRecord
  135.     # Output should have values close to:
  136.     # [80000.000000000015, 54000.000000000015, 24000.000000000015,
  137.     # -4799.9999999999854, -34847.999999999985]
  138.  
  139.     # TODO: Add more test cases here.
  140.  
  141.     savings     = 200000
  142.     growthRates = [10, 5, 0, 5, 1,0,3,2]
  143.     expenses    = 10000
  144.     savingsRecord = postRetirement(savings, growthRates, expenses)
  145.     print savingsRecord
  146.  
  147.     savings     = 80000
  148.     growthRates = [10, 5, 0, 5, 1,4,6,8]
  149.     expenses    = 20000
  150.     savingsRecord = postRetirement(savings, growthRates, expenses)
  151.     print savingsRecord
  152.  
  153. #
  154. # Problem 4
  155. #
  156.  
  157. def findMaxExpenses(salary, save, preRetireGrowthRates, postRetireGrowthRates,
  158.                     epsilon):
  159.     """
  160.    - salary: the amount of money you make each year.
  161.    - save: the percent of your salary to save in the investment account each
  162.      year (an integer between 0 and 100).
  163.    - preRetireGrowthRates: a list of annual growth percentages on investments
  164.      while you are still working.
  165.    - postRetireGrowthRates: a list of annual growth percentages on investments
  166.      while you are retired.
  167.    - epsilon: an upper bound on the absolute value of the amount remaining in
  168.      the investment fund at the end of retirement.
  169.    """
  170.     # TODO: Your code here.
  171.  
  172.     retirement_account = nestEggVariable(salary,save,preRetireGrowthRates)
  173.     retirement_fund = retirement_account[len(retirement_account)-1]
  174.     expenses_low = 0
  175.     expenses_high = retirement_fund
  176.     expenses = (expenses_high + expenses_low) / 2
  177.     savings_account = postRetirement(retirement_fund, postRetireGrowthRates, expenses)
  178.     savings_left = savings_account[len(savings_account)-1]
  179.  
  180.     while abs(savings_left) > epsilon:
  181.         print "Current expenses estimate =", expenses, "Savings left =", savings_left,
  182.         if savings_left < 0:
  183.             expenses_high = expenses
  184.             print "Previous expenses low =", expenses_low, "New expenses high =", expenses_high
  185.         else:
  186.             expenses_low = expenses
  187.             print "New expenses low =", expenses_low, "Previous expenses high =", expenses_high
  188.         expenses = (expenses_high + expenses_low) / 2
  189.         savings_account = postRetirement(retirement_fund, postRetireGrowthRates, expenses)
  190.         savings_left = savings_account[len(savings_account)-1]
  191.        
  192.     return expenses
  193.  
  194.  
  195. def testFindMaxExpenses():
  196.     salary                = 10000
  197.     save                  = 10
  198.     preRetireGrowthRates  = [3, 4, 5, 0, 3]
  199.     postRetireGrowthRates = [10, 5, 0, 5, 1]
  200.     epsilon               = 0.01
  201.     expenses = findMaxExpenses(salary, save, preRetireGrowthRates,
  202.                                postRetireGrowthRates, epsilon)
  203.     print expenses
  204.     # Output should have a value close to:
  205.     # 1229.95548986
  206.  
  207.     # TODO: Add more test cases here.
  208.    
  209.     salary                = 20000
  210.     save                  = 15
  211.     preRetireGrowthRates  = [3, 4, 5, 0, 3, 4, 8, 1]
  212.     postRetireGrowthRates = [10, 5, 0, 5, 1]
  213.     epsilon               = 0.01
  214.     expenses = findMaxExpenses(salary, save, preRetireGrowthRates,
  215.                                postRetireGrowthRates, epsilon)
  216.     print expenses
  217.  
  218.     salary                = 50000
  219.     save                  = 10
  220.     preRetireGrowthRates  = [3, 4, 5, 0, 3, 4, 6, 7, 9, 1, 2]
  221.     postRetireGrowthRates = [10, 5, 0, 5, 1]
  222.     epsilon               = 0.01
  223.     expenses = findMaxExpenses(salary, save, preRetireGrowthRates,
  224.                                postRetireGrowthRates, epsilon)
  225.     print expenses
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement