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

Published Dec. 31, 2021, 9:34 p.m.

Welcome to the 8th tutorial in the series!  Now we are going to venture into the multiverse!  We will simulate as many histories as there are batman or spiderman movies... if only numbers went that high.

Doing so will be ridiculously-- almost embarrassingly easy.  First we will just assign a number of simulations, start a "for" loop and put our code inside it.

nsims = 10

for sim in range(nsims):
    todays_date = date.today()
    retirement_date = date(2030, 3, 31)
    retirement = amount_already_in_retirement

    while todays_date < retirement_date:
        i+=1
        retirement += add_to_retirement
        ...

In the video I say "set nsims to 1000" but we should start out with 10 (and I correct myself in the video anyway).  You will notice that I put a few items in our for loop ahead of our while loop.  That is because we need to be sure to reset today's date and the amount in retirement at the begining of each simulation or else we will not get proper results.  (We don't really need to reset the retirement date but it doesn't hurt anything at the moment).

Now if you run this you will notice that it runs but it doesn't behave quite like we would like.  The reason for that is that we need to reset our money and dates variables in each simulation in order to get a clean rendering of plots.  To do that we just need to copy our declarations of empty lists and place them inside our for loop.

dates = []
money = []
i=0

nsims = 10

for sim in range(nsims):
    todays_date = date.today()
    retirement_date = date(2030, 3, 31)
    retirement = amount_already_in_retirement
    dates = []
    money = []
    while todays_date < retirement_date:
        i+=1
        retirement += add_to_retirement
       

Now if you run the code we see 10 plots quickly plotted to the screen.  Progress!  Now we would like to run more simulations and display the results even faster.

Near the bottom of our while loop where we were plotting last time we will take our code from the previous tutorial and shift+tab (or backwards tab) it out of our 'while' loop.  We will also change the variable 'i' to 'sim' because we want to see a subset of our simulations.  Finally I changed the freqency to 10 (rather than 25) so that the code looks like:

nsims = 1000
for sim in range(nsims):
    todays_date = date.today()
    retirement_date = date(2030, 5, 30)
    retirement = add_to_retirement
    money= []
    dates = []

    while todays_date < retirement_date:
        i+=1
        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.choice(x_dist)
            paycheck_interest_rate = get_paycheck_interest_rate(annual_interst_rate, number_of_paychecks_per_year)
     
        money.append(retirement)
        dates.append(todays_date)
        year_check = todays_date.year
    if sim%10==0:
        axs[0,1].plot(dates, money)
        plt.pause(0.001)

This should run relatively quickly and display 100 plots (out of 1000).  Now we can easily see that some scenarios have amazingly good and amazingly bad outcomes.  On the road to understanding we are still at the crossroads of confusion.  However, we will have some strategies that will makes sense of these results soon.  We will start looking at the distribution of final outcomes as well as some average behavior.  Stick around!

skip_nextRetirement Simulator #9: Track averages and final outcomes
  • 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!

  • 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
    (currently viewing)
  • Retirement Simulator #9: Track averages and final outcomes

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