# The math behind the calculator

Networthify.com calculates how many years you have to retirement. Lots of
people have been curious about how it arrives at the magic number. The answer
to this question is: *Math!*

More specifically the equation which drives the calculator is derived from two common formulas used for dealing with compound interest. Many people must have done this before me but I couldn't find an example online and I enjoyed doing the math anyway.

Before we start with the math, we have to define retirement or else we can't calculate how long it takes to get there. This is a bit of a problem because people will define it differently. But I'll just go ahead do it anyway.

First, I have no idea what my expenses after retirement are going to be but I decided it can't be that different from my current expenses. After all I will still need to pay for food, housing, and entertainment.

Also, I don't want to ever draw down my retirement nest egg. I want my wealth to grow, not shrink. I'm being overly safe, but I don't want to have some bad luck or unexpectedly live to 120 years old and have to lower my standard of living. I want my standard of living to go up because thats way more fun.

So this is how I defined retirement:

\[ \begin{aligned} currentExpenses & = annualIncome - annualSavings \\ futureExpenses & = withdrawalRate \times futureValue \\ currentExpenses & = futureExpenses \\ \end{aligned} \]

where *futureValue* is my total savings after retirement and *withdrawalRate*
is slightly less than my average annual return on investment. What we want to
know is how long will it take to save a pile of money equal in size to
*futureValue*.

Notice there are 2 basic ways to build a pile of money over time:

- Earn interest earned on our existing and growing pile of saved up money.
- Contribute savings from our annual income.

Happily there are formulas for calculating this stuff. Lets take a look at them.

\[ futureValueA = initialValue \times (1 + interestRate)^n \]

Lets say you have an initial pile of money. And someone is willing to pay you some interest for it. This formula calculates how big your pile of money will be in the future.

More precisely: it calculates how much money you will have in the future given an initial balance which earns interest each time period for n time periods. The time period I use is one month.

\[ futureValueB = payment \times \frac{ (1 + interestRate)^n - 1 }{ interestRate } \]

This formula also calculates how much money you will have in the future. But this time you start with no money and contribute a payment each time period for n time periods while earning interest on your growing pile of money.

Again I want to use a time period of one month. I'll make a small change and now this formula calculates what happens when you put some of your salary into your savings account each month:

\[ futureValueB = \frac{annualIncome}{12} \times \frac{ (1 + interestRate)^n - 1 }{ interestRate } \]

But what we really want is a single formula we can solve for n to figure out how long it will take to reach retirement. So lets start doing some substitutions:

\[ \begin{aligned} currentExpenses & = annualIncome - annualSavings \\ futureExpenses & = withdrawalRate \times futureValue \\ currentExpenses & = futureExpenses \\ \\ futureValue & = futureValueA + futureValueB \\ \\ currentExpenses & = withdrawalRate \times (futureValueA + futureValueB) \\ annualIncome - annualSavings & = withdrawalRate \times (futureValueA + futureValueB) \\ \end{aligned} \]

Awesome. So now its a "simple" matter of solving for n. So after doing substitutions for *futureValueA* and *futureValueB*,
doing natural log stuff, *handwaving*, *handwaving* -- I arrived at the following:

\[ n = \frac{ln(interestRate \times \frac{\frac{1 - savingsRate}{savingsRate} + withdrawalRate \times initialValue}{withdrawalRate \times initialValue})}{ln(1 + interestRate)} \]

And thats the math behind the calculator. Here is more reading for the super duper interested: