Advertisement
Guest User

Untitled

a guest
Sep 29th, 2017
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.41 KB | None | 0 0
  1. import argparse
  2. import time
  3.  
  4. def start_simulation(initial_portfolio, target_portfolio, growth_rate, investment_horizon, tolerance):
  5.     # get a target monthly investment
  6.     min_val = 0
  7.     mid_val = 60000
  8.     max_val = 240000
  9.  
  10.     while (min != max):
  11.         balance = simulate(initial_portfolio, target_portfolio, growth_rate, investment_horizon, mid_val)
  12.         if abs(target_portfolio - balance) < (tolerance * target_portfolio):
  13.             return mid_val
  14.         else:
  15.            if balance < target_portfolio:
  16.                min_val = mid_val
  17.                mid_val = (max_val + min_val) / 2.0
  18.            else:
  19.                max_val = mid_val
  20.                mid_val = (max_val + min_val) / 2.0
  21.  
  22. def simulate(initial, target, rate, time, contribution):
  23.     portfolio = initial
  24.     for year in xrange(time):
  25.         portfolio += contribution
  26.         portfolio *= rate
  27.     return portfolio
  28.  
  29. if __name__ == '__main__':
  30.     parser = argparse.ArgumentParser()
  31.  
  32.     parser.add_argument('-t', help='investment horizon', type=int)
  33.     parser.add_argument('-r', help='growth rate (default=1.07)', type=float, default=1.07)
  34.     parser.add_argument('-i', help='initial portfolio balance', type=int, default=0)
  35.     parser.add_argument('-f', help='final target portfolio balance', type=int)
  36.     parser.add_argument('--tolerance', help='difference between simulation goal and target portfolio balance', type=float, default=0.01)
  37.     args = parser.parse_args()
  38.  
  39.     vals = [500000, 750000, 1000000, 1250000, 1500000, 1750000, 2000000, 2500000, 3000000]
  40.     timeframes = [10, 15, 20, 25, 30, 35, 40]
  41.  
  42.     # uncomment these lines, comment the following set of lines to enter your specific info on the command line
  43.     # start the program, calculate what our recurring monthly deposit should be
  44.     # result = execute_test(args.i, args.f, args.r, args.t, args.tolerance)
  45.     # output the recurring monthly deposit
  46.     # print 'To save ${0:,.2f} in {1} years, invest ${2:,.2f} every month'.format(args.f, args.t, result / 12.0)
  47.  
  48.     # comment the following lines, uncomment the previous lines to enter your specific information instead
  49.     print '-' * 80
  50.     for val in vals:
  51.         for time in timeframes:
  52.             result = start_simulation(args.i, val, args.r, time, args.tolerance)
  53.             print 'To save ${0:,.2f} in {1} years, invest ${2:,.2f} every month'.format(val, time, result / 12.0)
  54.         print '-' * 80
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement