Use Python to figure out how to be richer in your retirement
Tags: investing, money, retirement, python
Published: 2020-02-02 17:54:18

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](https://www.snopes.com/fact-check/compound-interest/), “compound interest is the most powerful force in the universe.” And I’m not disagreeing with that claim.

![Einstein thinking about compound interest](https://miro.medium.com/max/474/0*QBY77AvEJAba5rAM)
<figcaption>Think, think.</figcaption>

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](https://miro.medium.com/max/547/1*XO_p-fjKI6HBn3V_eQgHrg.png)
<figcaption>Compound interest grows exponentially (a.k.a. fast!)</figcaption>

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](https://medium.com/r/?url=https%3A%2F%2Ffortune.com%2F2017%2F12%2F30%2Fwarren-buffett-million-dollar-bet%2F) 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](https://www.investopedia.com/articles/trading/07/market_timing.asp).

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:

{% gist https://gist.github.com/siran/4b2a677d6cc682d677c98b2f0fafd596 %}

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

- [This](https://matplotlib.org/devdocs/gallery/widgets/slider_demo.html#sphx-glr-gallery-widgets-slider-demo-py) as a template for the script
- [This other link](https://preinventedwheel.com/matplotlib-thousands-separator-1-step-guide/) to know how to format the axis.
- [This other](https://stackoverflow.com/a/7066293) is for the title location in my plot.

This is [my favorite 401(k) calculator](https://www.calculator.net/401k-calculator.html), since it includes inflation. 

## 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.

<hr />

Hope you have fun customizing the plot!