# Calculating compound interest

One day you wake up and you are old. At least that's how I felt the other day when I got a letter from one of my pension providers. The letter reminded me that my designated retirement age of 60 was fast approaching, and was I ready for it? Thanks for the reminder. The truth is that came as a bit of a shock. It seems like only a few years ago when I was starting out in industry - and yet - it was actually 1984 and the Z80 CPU, clocking in at a mighty 4MHz, was considered as good as it got. Where did all those years go?

The truth is I have had a wonderful career and I have been very fortunate to have worked with some superb companies, and, well, some not so good ones too (learning experiences!).

So, on the whole it's has been an amazing career - but could I actually afford to retire was the current question? Was I financially ready?

It was time to crunch some numbers...

Yes, there are many financial calculators available on the net that you can use - but where's the fun in that I ask you! So I decided to reach for Emacs and whip up some of my own financial calculators in Python. I will be looking at two formulae in this article:

- Present/future value
- Compound interest

In future articles I will look at additional calculations.

## Present/future value

The first formula to look at is simple present/future value. This looks at a principle lump sum, and then calculates what it will be worth in the future given a certain interest rate over a certain number of years. Let's get straight to it and state the formula:

```
fv = pv * (1+r)**t
```

Note that `**`

is Python's way of saying "raise to the power of". In the formula `fv`

is future value, `pv`

is present value, `t`

is number of years, and `r`

is the interest rate.

Note the interest rate has to be specified in decimal format, so a 7% interest rate would be written in as 0.07.

So, for the sake of example, let's say our pension pot is 200,000 and we want to know what it will be worth in 20 years (well into retirement), given a 7% return. In Python this would be:

```
t = 20
r = 0.07
pv = 200000.00 # Two decimal places
fv = pv * (1+r)**t
print("Pension of %.2f will be worth %.2f in %d years" % (pv, fv, t))
```

Running this will give us the following good news:

```
Pension of 200000.00 will be worth 773936.89 in 20 years
```

That's the power of compounding - the effect by which gains build on each other over the years. Albert Einstein called compound interest the "eighth wonder of the world", adding: "He who understands it earns it; he who doesn't pays it." Well said Albert!

Anyway, that brings me on to another useful formula - the compound interest formula.

## Compound interest

The compound interest formula is very closely related to the previous formula - they are in fact more or less the same thing. The difference is this one takes into account the number of times the interest is added to your accumulating sum.

In most cases that interest is added annually, and you would calculate the gains over a number of years. However, some accounts can add interest monthly. In this case, if you were calculating gains over a number of years, interest would be added twelve times per year to your accumulating sum.

The formula is:

```
fv = pv (1 + r/n)**(n*t)
```

You can see this is nearly identical to the previous formula. In this case the only new item is `n`

, which is the number of times interest is added per year.

Notice we are using as our timebase "years". It could be some other timebase such as "months", but for most financial calculations years make the most sense. What's this means is that really `n`

is the number of compounding operations per unit of time or timebase. In my examples this is always one compounding instance per year.

So, here is the same situation again, but we will print out some results to see how our lump sum grows over the years:

```
pv = 200000.00
r = 0.07
t = 20
n = 1
fv = pv * (1 + r/n)**(n*t)
print ("First formula calculates fv to : %.2f" % fv)
fv = pv * (1 + r/n)**(n*1) # Year 1 only
print("Year %d : %.2f" % (1, fv))
for i in range (2, t+1):
fv = fv * (1 + r/n)**(n*1) # Calculate one year at a time
print("Year %d : %.2f" % (i, fv))
```

We just keep annually figuring out what we would have at the end of each year. You should also see that the first number calculate using the first formula, and the final sum generated by the second formula are exactly the same.

Note, these calculations assume no withdrawals. In that case we'd have to recalculate things. I will look at this again in a future article.

Have a play with the formulas. You could for example compare accounts that have slightly different interest rates, but one compounds monthly rather than annually. You should be able to calculate which one will give you the greatest return.

## A quick introduction to the 4% rule

One rule of thumb you might run into when you look into retirement lump sums is the famous "4% rule". This was made famous by Mr. Money Mustache. This basically says that given a retirement lump sum you can, on average, draw down 4% of the lump sum per year, without ever depleting your original lump sum. Thus in effect you have an income for life.

Example, if you had 100,000 you'd be able to withdraw 4,000 per year without depleting your 100,000 *in theory*. Of course this is just a rule of thumb. The reason I mention it is that the 4% rule is premised on average stock market returns of 7%, which is why I used 0.07 in my calculations above.

I will be coming back to this topic in future articles, and also looking at mortgages and other loan calculations. You could then figure out whether you are better off paying off mortgage debt or investing those mortgage overpayments in the stock market.

## Summary

In this article you've seen how to calculate compound interest. You've also seen that it's really important to start saving when you are young, so you can let compound interest work its magic! I also briefly mentioned the 4% rule, which is predicated on average stock market returns of 7% annually.