Skip to content

Instantly share code, notes, and snippets.

@mitbailey
Last active November 12, 2022 19:14
Show Gist options
  • Save mitbailey/c43a2846cb2e2c4791003e799ccff589 to your computer and use it in GitHub Desktop.
Save mitbailey/c43a2846cb2e2c4791003e799ccff589 to your computer and use it in GitHub Desktop.
import matplotlib.pyplot as plt
START_YEAR = 1
LAST_YEAR = 20
PERCENT = '%'
initial_investment: float = float(input('Initial investment ($): '))
yearly_investment: float = float(input('Yearly contribution ($): '))
yearly_investment_yearly_increase = float(input('Yearly contribution change ($): '))
yoy_ir: float = float(input('Estimated year-over-year interest rate (0.0 - 1.0): '))
inflation: float = float(input('Estimated inflation each year (0.0 - 1.0): '))
LAST_YEAR = int(input('Number of years to simulate: '))
# initial_investment: float = 50000.0
# yearly_investment: float = 10000.0
# yearly_investment_yearly_increase: float = 500.0
# yoy_ir: float = 0.10
# inflation: float = 0.03
fig = plt.figure(figsize=(11,11))
ax = fig.add_subplot(1,1,1)
lw = '2'
ax.set_title('Value of Account with Starting Value \$%.02f with \$%.02f Yearly Contributions Changing by \$%.02f Each Year'%(initial_investment, yearly_investment, yearly_investment_yearly_increase))
current_total_cash: float = initial_investment # Cash w/o inflation.
current_total_cash_inf: float = initial_investment # Stored in cash w/ inflation.
current_total_sm: float = initial_investment # YOYIR w/o inflation.
current_total_sm_inf: float = initial_investment # YOYIR w/o inflation.
current_total_sm_inf_dec: float = initial_investment # YOYIR w/o inflation.
total_cash_list = [] # Cash w/o inflation.
total_cash_inf_list = [] # Stored in cash w/ inflation.
total_sm_list = [] # YOYIR w/o inflation.
total_sm_inf_list = [] # YOYIR w/o inflation.
total_cash_inf_increase_list = []
total_sm_inf_increase_list = []
yearly_contributions = []
years = range(START_YEAR,LAST_YEAR+1,1)
for year in years:
total_cash_inf_increase_list.append((current_total_cash_inf * (1.00 - inflation)) - current_total_cash_inf)
total_sm_inf_increase_list.append((current_total_sm_inf * (1.00 + yoy_ir - inflation)) - current_total_sm_inf)
current_total_cash_inf *= (1.00 - inflation)
current_total_sm *= (1.00 + yoy_ir)
current_total_sm_inf *= (1.00 + yoy_ir - inflation)
total_cash_list.append(current_total_cash) # Cash w/o inflation.
total_cash_inf_list.append(current_total_cash_inf) # Stored in cash w/ inflation.
total_sm_list.append(current_total_sm) # YOYIR w/o inflation.
total_sm_inf_list.append(current_total_sm_inf) # YOYIR w/o inflation.
current_total_cash += yearly_investment
current_total_cash_inf += yearly_investment
current_total_sm += yearly_investment
current_total_sm_inf += yearly_investment
yearly_contributions.append(yearly_investment)
if yearly_investment > 0:
yearly_investment += yearly_investment_yearly_increase
if yearly_investment < 0:
yearly_investment = 0
ax2 = ax.twinx()
ax2.set_ylabel('Account Increase ($) [BARS]')
ax2.bar([x for x in years], total_cash_inf_increase_list, linewidth=lw, linestyle='solid', color='tab:green', alpha=0.35, label='Cash Increase (%.02f%s Inflation)'%(inflation*100, PERCENT))
ax2.bar([x for x in years], total_sm_inf_increase_list, linewidth=lw, linestyle='solid', color='tab:blue', alpha=0.35, label='%.02f%s IR Account Increase (%.02f%s Inflation)'%(yoy_ir*100, PERCENT, inflation*100, PERCENT))
ax2.bar([x for x in years], yearly_contributions, width=0.5, linestyle='solid', color='tab:red', alpha=0.35, label='Contribution (Increasing by \$%.02f each year)'%(yearly_investment_yearly_increase))
ax2.legend()
ax.plot([x for x in years], total_cash_list, linewidth=lw, linestyle='dashed', color='tab:green', label='Cash (No Inflation)')
ax.plot([x for x in years], total_cash_inf_list, linewidth=lw, linestyle='solid', color='tab:green', label='Cash (%.02f%s Inflation)'%(inflation*100, PERCENT))
ax.plot([x for x in years], total_sm_list, linewidth=lw, linestyle='dashed', color='tab:blue', label='%.02f%s IR Account (No Inflation)'%(yoy_ir*100, PERCENT))
ax.plot([x for x in years], total_sm_inf_list, linewidth=lw, linestyle='solid', color='tab:blue', label='%.02f%s IR Account (%.02f%s Inflation)'%(yoy_ir*100, PERCENT, inflation*100, PERCENT))
ax.set_ylabel('Account Value ($) [LINES]')
plt.ticklabel_format(style='plain')
ax.grid(True, which='both')
ax.legend()
_, top = ax.get_ylim()
bottom, _ = ax2.get_ylim()
ax.set_ylim(top=top, bottom=bottom*10)
ax2.set_ylim(top=top/10, bottom=bottom)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment