Monte Carlo simulations are an important part of the Data Science toolkit. They help us to model things that could have a variety of different possible outcomes by running the same method many times. This more accurately shows the possible outcomes given many random events, like the performance of the stock market or other investments over a number of years. In this article, I show you how I have used the Monte Carlo Simulation to model the possible outcomes of retirement accounts.
In order to get different outcomes each time, I use a random number generator. Each time the model is executed, the results of the random numbers is different. When you plot the outcomes together you can clearly see the range of possible values.
To do this in two object oriented programs, I declared a class called Finance, from which I could declare the variables that I will need like the age of the user, the interest rate, the amount that he or she wants to save, the user’s salary among others. I called the file that contains this class “” with a lower case to differentiate it from the class its self.
To call this class I created another program called retirement, and imported the finance.Finance class. I imported it and initialized all the needed variable.
Then I developed a model to cacluate the amounts of money stored in two types of retirement accounts, the IRA and the 401k/403b plans. The IRA works like this: you put in a certain amount of money in each year. According to Jean Folger at Investopedia in an article that can be found at, in 2021 the contribution limit is $6,000 if you are under 50 and $7,000 if you are aged 50 or over. The extra $1,000 is called a catch up contribution. These contribution limits are also indexed to inflation, so when inflation has compounded to make the original $6,000 less valuable, and when $6,500 will buy what $6,000 would buy now the contribution limit rises to $6,500.
Another type of retirement account is the 401k/403b plan. They are both very similar, but there are some differences so for this example we will just look at the 401k. The 401k allows you to contribute a portion of your pay each year toward your retirement. According to the IRS at the following website: ( the 401k contributions are capped at $19,500 if you are under 50. For people over 50 there is a catch up provision just like in the IRA where you can contribute an extra $6,500 for 2021. This brings the total 401k contribution to $26,000 for 2021. It is also indexed to inflation.
Here I have modeled the 401k in python that automatically increases the contribution maximums as the participant ages, just as I did in the IRA example above.
Once I had these methods in place in the Finance class within the file, I could then try running them from the retirement program.
This only shows the end results, but some people might want to see how we arrive at these numbers. We keep the data for each year in the program so lets plot it. In the Finance class in the file we create the following function.
We access this function with this code in
Next is where the Monty Carlo comes in. Here I run the “IRA” and “four01k” methods 20 times and plot each of the trials using this new method in the Finance class in
Then I need to call this from
Then I could plot both the range that I get from running this simulation and plot it in the graph using matplotlib.pyplot’s fill_between method in the following code:
This is the same graph as above with the area shaded using the fill_between method in Matplotlib.pyplot:
To emphasize the range and the maximum, minimum and average for every year, I used this code in the Finance class to create a plot that just plots the maximum, minimum and average values of the 401k and IRA at each year and plots them.
This gives us a clear image of what the range of retirement nest eggs we can accumulate during a working life given certain savings habits.
For future work, I realize that there are limits to the level of income that someone can reach when considering how much to contribute to both the IRA and the 401k. Therefore to give an even more realistic picture of one’s retirement portfolio size, I might want to add this functionality.