Retirement Simulator #3+: Verify our Gaussian distribution!

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

Welcome to the appendix to the 3rd tutorial!  Working from what we did in the last tutorial we will now attempt to verfiy what we did (or thought we did) last time.

Last time we left with something like this at the end of our code:

year_check = todays_date.year
while todays_date < retirement_date:
    retirement += add_to_retirement
    retirement *= (1+paycheck_interest_rate)
    todays_date += pay_frequency
    this_year = todays_date.year
    if this_year > year_check:
        annual_interst_rate  = random.gauss(mean, sigma)
        paycheck_interest_rate = get_paycheck_interest_rate(annual_interst_rate, paychecks_per_year)
        print(this_year, annual_interst_rate)
    year_check = todays_date.year

Now we want to "see" that we did what we expected to do.  So let's make another test.  Technically we can import a module anywhere so lets just import matplotlib.pyplot as plt right here.  We'll print the word "TEST" so we can see when the test begins.  Then we will make an empty list that we call list_of_annual_interest_rates.  (I wonder what we will fill it with?) 

To fill our list let us make a for loop that does 1000 iterations.  Each time we will fill the list with a number chosen by the random.gauss(mean,sigma) function.

Once finished with the loop we will plot a histogram of our list_of_annual_interest_rate using plt.hist(list_of_annual_interest_rate ).  To display the plot we invoke plt.show().

### TEST: ###
import matplotlib.pyplot as plt
print("TEST")
list_of_annual_interest_rates = []
for i in range(1000):
    list_of_annual_interest_rates.append( random.gauss(mean, sigma))

plt.hist(list_of_annual_interest_rates)
plt.show()

If you are using the Spyder IDE then you may have to click on a 'Plots' tab to see what we have plotted.

The automatic formatting of plt.hist should show a decent looking histogram.  It should be a mountain shape that centers on 0.07 or whatever you have selected for your 'mean' value.

This plot is our verification that we are sampling from a list that has a gaussian distribution.  In the next tutorial we will find some real historic market data to use in our simulation.  Thanks for watching.  Hope you stick around.

skip_nextRetirement Simulator #4: Make a US market rates distribution
  • Retirement Simulator Introduction and Overview

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

  • 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!
    (currently viewing)
  • 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!)