Retirement Simulator #1: March through time with datetime and put money under the mattress!

Published Dec. 31, 2021, 4:31 p.m.

 

Welcome to the first tutorial in this python millionaire retirement simulator series. Truth be told this is not going to be super glamorous however we're going to create a simulator that marches through time and predicts how much money you could save for retirement. None of this counts as advice. It is just here to show how to build a simulator that can march through time and multiply and add numbers together because that's just about how simple it is. At the end we should be able to make a customizable simulator that can predict how much money you might save for retirement given a typical savings scenarios.

With all with that said i would i'll bring your attention over here and say that normally in this kind of thing i would use a windows terminal and i would use a notepad editor i'm going to start this series using the spyder editor.  It can be downloaded here: https://www.spyder-ide.org/.  Later on I will switch over to the notepad++ editor and run it from the terminal (but it will work the same no matter where you start or finish!).

When you save money for retirement you're going to put a little bit of money away every paycheck.  Our simulation is going to start with that basic fact and since we don't need to predict whatever happened in the past we're going to start with some parameters that tell us how things stand currently.  

First, no matter what editor you use, intentionally choose a location for your work to be done in.  This will be important later when we open and read data from some files that we create.  In the video, I show how to select your work area.  In a terminal, you need to change directories (cd) until you are in the desired work directory.

In the spyder terminal window you can verify that you are in the desired workspace with the following commands:

import os
os.getcwd() ## get current working directory

I have created a workspace I call "PythonWork" so the above command returns:

C:\\PythonWork

With all that sorted, we should start out with our imports:

import sys, os
from datetime import date, timedelta

Datetime is a python module for handling-- dates and times! Duh.  Here we are directly importing the 'date' and 'timedelta' functions from datetime.  This kind of import technique uses less memory, but more importantly for me, it means that when we want to call the function we don't have to use the  lengthy "datetime.date" or "datetime.timedelta"-- we can just use the singular name.

Let's grab today's date and then print it:

todays_date = date.today()
print(todays_date)

Now if we run our program (in spyder hit the green arrow) the console should run this and print out today's date.  I recorded this on 12/20/2021 in case you're watching this 100 years in the future. 

Next let's imagine that we get paid today and that we get paid with some regular frequency.

pay_frequency = timedelta(days=14)

 

Two weeks is a common pay frequency.  Yours can be whatever makes sense.  Here are a few ways to get something like monthly pay (since not all months are the same length):

two_weeks = timedelta(days=14)  # every two weeks.
one_year = timedelta(days=365.25)
one_month = one_year/12
half_month = one_month/2

and now we can change our pay frequency to our named timedelta:

pay_frequency = two_weeks

If we already have a retirement account we can account for that here:

amount_already_in_retirement = 10000

You'll notice that I like to use lengthy but descriptive variables.

Later on we will want to know how many paycheck we get in a year. 

paychecks_per_year = int(round(one_year/pay_frequency,0))

Finally, we need to establish how much money we put into the account each pay period.  We can do that a number of ways.  The simplest is to just fix a number:

add_to_retirement = 100

This means that we plan to add $100 to our retirement every paycheck.

Now we need to establish a retirement date:

retirement_date = date(2030, 3, 31)

The format of (year, month, day) matters and the date must be a real date! (duh?)

Finally the variable you've all been waiting for: retirement.

To start off our retirement will have only what is currently in our retirement account which we conveniently establish a few lines ago:

retirement = amount_already_in_retirement

So now we go into the interesting part of our bare-bones simulator:  the while loop.

The while loop continues as long as the argument is true.  We have established today's date and a retirement date (as well as a pay frequency) so we will set up our loop in the following way:

while todays_date < retirement_date:
    retirement += add_to_retirement
    todays_date += pay_frequency

We assume that we get paid today.  We add money to our account and we add our pay frequency (two weeks) to "today's date". Then the loop continues to do that until the date is a date later than our "retirement date".

You probably notice that we didn't add any interest to our money yet.  That will come in the next video.  Here we just want to establish that we properly step through time and add money to our account.  After the loop we can print a verification statement:

print(f"I have {retirement:.2f} in my retirement as of {todays_date}.")    

This is an 'f-string' where I have formatted the 'retirement' variable to only have 2 decimals following.

With my job, I elect to add a percentage of my paycheck to my retirement account.  If we go back to where we were determining the "add_to_retirement" amount we can make some alterations if desired:

...
paychecks_per_year = int(round(one_year/pay_frequency, 0))

Current_Salary = 50000
one_paycheck = Current_Salary/paychecks_per_year
add_to_retirement = 0.10*one_paycheck
# add_to_retirement = 100

...

When we run this it appears to work but it would be nice to have a verification.  We can build our own verification.  We will make a test where we clearly print "TEST" to the screen and then our test will follow.  I like simple round numbers so we will start with 100 dollars in our account and we will take as many steps in our loop as we get paychecks in a year.  At each step we will add $1 to our account.  If we use a 2 week pay frequency, we get 26 paychecks a year and we should add $26 total dollars to our account:

### TEST: ###
retirement = 100
# print("TEST")
for i in range(paychecks_per_year):
    retirement += 1
    print(f"I have {retirement:.2f} in my retirement after {i+1} paychecks.") 

This should print 26 times and the final statement should show 126 after 26 paycheck.

In the next tutorial we will apply some interest rate to our money so that we can see how it grows over time.  Thanks for watching and please go on to the next one!

skip_nextRetirement Simulator #2: Apply an interest rate and verify it!
  • Retirement Simulator Introduction and Overview

  • Retirement Simulator #1: March through time with datetime and put money under the mattress!
    (currently viewing)
  • Retirement Simulator #2: Apply an interest rate and verify it!

  • Retirement Simulator #3: Use a Gaussian distribution of rates!

  • Retirement Simulator #3+: Verify our Gaussian distribution!

  • Retirement Simulator #4: Make a US market rates distribution

  • Retirement Simulator #5: Fit the Market Data with scipy curve_fit

  • Retirement Simulator #6: Sampling from our custom market distribution

  • Retirement Simulator #7: Organize multiple plots with subplots! and learn about plt.pause()

  • Retirement Simulator #8: Multiverse Investing: Simulate 1000 random investors

  • Retirement Simulator #9: Track averages and final outcomes

  • Retirement Simulator #10: Test and Compare Different Retirement dates (And make it pretty!)