DEV Community

Cover image for Use Python to figure out how to be richer in your retirement
Mich Rodz
Mich Rodz

Posted on • Updated on • Originally published at Medium

Use Python to figure out how to be richer in your retirement

I used Python to build a dynamic plot to understand how to maximize my retirement savings. You can, too.

Like Einstein is said to have said, “compound interest is the most powerful force in the universe.” And I’m not disagreeing with that claim.

Einstein thinking about compound interest

Think, think.

There are many 401(k) calculators online. None were as interactive as I wanted. So, I decided to do a simple Python script to scratch my itch.

For now, this is the result. Below the code to generate the plot.

Compound interest grows exponentially

Compound interest grows exponentially (a.k.a. fast!)

You can use the sliders to adjust different parameters, like market return and the number of years you’ll be working.

In the plot's title, you can see what would your final balance in your retirement 401(k) account would be.

In fact, I realized that the only parameters that really count are the “total number of years investing” and the “market return”.

If you max-out your 401k year after year, your final balance doesn’t change with your salary (obviously). The maximum contribution as of 2020 is $19,500 per year.

So the factors to control are most certainly the number of years you’re chipping in into your 401(k), and the selection of stocks you make to invest.

The more time you wait before retiring, the more your fund grows. This is the power of compound interest.

Another interesting factor, as many, many, many people have realized, is to invest in an index fund. If you don't know what an "index fund" is, please do yourself a favor and google it immediately.

Managed funds charge fees. These can hinder the growth of your 401(k) by A LOT. And worse, it is well known that they don't fare better than the market.

There is a famous bet Warren Buffet won about it:

In 2007, Warren Buffett bet a million dollars that an index fund would outperform a collection of hedge funds over the course of 10 years.

Index funds with a low maintenance fee (formally called "expense ratio", typically 0.03%, like Vanguard Vanguard S&P 500 ETF (VOO) which follows the S&P500).

Historically, the S&P 500 has yielded an approximate 7% yearly. So that’s why, in the default parameters for the plot, I set the default value to that number.

Being said that there is a cap on your 401(k) tax-free savings, you should strive to invest more. Be it with a capped ROTH IRA ($6K tops in 2020) or straight in the stock market.

Currently most serious investing brokers/banks don’t charge any commission to buy/sell positions. Anyway, the best strategy if you are not an active investor, is to buy an ETF and look at it years later. Ride the market. Don't try to time the market.

Here’s the script that makes an interactive plot using matplotlib.pyplot to see how a 401(k) retirement fund changes while varying some parameters:

Although this is a simple script, I had to iron out some kinks:

This is my favorite 401(k) calculator, since it includes inflation.


Inflation is a very important factor to consider when calculatin the buyin-power of your money in the future. 

Currently, the inflation is around 2%.

As seen below, compounding a 2% inflation rate for 20 years, what today costs $100 will cost $148. 

Equivalently, if you manage to become a 401(k) millionaire (and have a $1M), the buying power is equivalent to $675,675.

Accumulated inflation factor in 20y:
1.02 x 1.02 x ...<20 times> ... = 1.48

$1M equivalent today:
$1,000,000 / 1.48 = $675,675

In the previous script I didn’t include inflation in my script just to keep it simple. It can be simply added as a toggle button to turn it on/off, and in the get_balance() function in the code.

Hope you have fun customizing the plot!

Discussion (1)

samueljason13 profile image
samueljason13 • Edited

Nice! I found this text interesting. I started to search for the best tool for retirement planning, but I am too dumb to take care of my financial situation. I tried the website Personal Capital, but somehow it was too complicated for me to follow. My friend suggested I try because they have the best services. What I like about them is that I do not have to learn how to do it, they do the plan for me, and I follow it. With this company, my future is insured.