Skip to content

Instantly share code, notes, and snippets.

@jiaaro
Last active December 3, 2022 18:51
Show Gist options
  • Save jiaaro/6780635 to your computer and use it in GitHub Desktop.
Save jiaaro/6780635 to your computer and use it in GitHub Desktop.
A Simple, Easy-to-edit Retirement Saving Calculator (in python)

Retirement Calculator

  • ages are in years
  • contribution, and savings are in dollars
  • avg_annual_return is a ratio, so 1.07 is a 7% annual return

let's say I'm 25 years old, I am going to contribute $2000/yr in bonds (~5% return), and I've already invested $5700 in bonds

>>> retirement_calculator(25, 2000, 5700, avg_annual_return=1.05)
281727.48414409667

So I could expect to have $281,727 saved when I am 65 years old... uh oh ;).

def retirement_calculator(current_age, yearly_contribution=0, current_savings=0,
retirement_age=65, avg_annual_return=1.07):
years_until_retirement = retirement_age - current_age
savings = current_savings * (avg_annual_return ** years_until_retirement)
while years_until_retirement > 0:
years_until_retirement -= 1
savings += yearly_contribution * (avg_annual_return ** years_until_retirement)
return savings
@DavidHason
Copy link

Could you please explain more about avg_annual_return. Thanks

@DavidHason
Copy link

My project is very similar to this retirement calculator.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment