Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import argparse
- import time
- def start_simulation(initial_portfolio, target_portfolio, growth_rate, investment_horizon, tolerance):
- # get a target monthly investment
- min_val = 0
- mid_val = 60000
- max_val = 240000
- while (min != max):
- balance = simulate(initial_portfolio, target_portfolio, growth_rate, investment_horizon, mid_val)
- if abs(target_portfolio - balance) < (tolerance * target_portfolio):
- return mid_val
- else:
- if balance < target_portfolio:
- min_val = mid_val
- mid_val = (max_val + min_val) / 2.0
- else:
- max_val = mid_val
- mid_val = (max_val + min_val) / 2.0
- def simulate(initial, target, rate, time, contribution):
- portfolio = initial
- for year in xrange(time):
- portfolio += contribution
- portfolio *= rate
- return portfolio
- if __name__ == '__main__':
- parser = argparse.ArgumentParser()
- parser.add_argument('-t', help='investment horizon', type=int)
- parser.add_argument('-r', help='growth rate (default=1.07)', type=float, default=1.07)
- parser.add_argument('-i', help='initial portfolio balance', type=int, default=0)
- parser.add_argument('-f', help='final target portfolio balance', type=int)
- parser.add_argument('--tolerance', help='difference between simulation goal and target portfolio balance', type=float, default=0.01)
- args = parser.parse_args()
- vals = [500000, 750000, 1000000, 1250000, 1500000, 1750000, 2000000, 2500000, 3000000]
- timeframes = [10, 15, 20, 25, 30, 35, 40]
- # uncomment these lines, comment the following set of lines to enter your specific info on the command line
- # start the program, calculate what our recurring monthly deposit should be
- # result = execute_test(args.i, args.f, args.r, args.t, args.tolerance)
- # output the recurring monthly deposit
- # print 'To save ${0:,.2f} in {1} years, invest ${2:,.2f} every month'.format(args.f, args.t, result / 12.0)
- # comment the following lines, uncomment the previous lines to enter your specific information instead
- print '-' * 80
- for val in vals:
- for time in timeframes:
- result = start_simulation(args.i, val, args.r, time, args.tolerance)
- print 'To save ${0:,.2f} in {1} years, invest ${2:,.2f} every month'.format(val, time, result / 12.0)
- print '-' * 80
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement