↓Skip to main content

Regular Savings

·5 mins

Preamble #

Up until now, I have been authoring posts for this blog in Markdown. I often find myself writing code then blogging about it later, and for a while I’ve been thinking about combining the two. What if I wrote the blog post and code at the same time, using Emacs Org Mode?

I finally got around to reading Howard Abrams’s Introduction to Literate Programming and followed up with an article on Using Org Mode With Hugo by Kay Rhodes. The latter was very useful in getting me started with ox-hugo, an Org exporter backend that emits Hugo-compatible Markdown.

I’ve also been meaning for a while to check out the KaTeX support in the Congo Theme I’m using for the blog, so this post combines a little bit of mathematics with a little bit of scheme. I hope you enjoy it!

Calculating Interest on a Regular Savings Account #

My building society is advertising a regular saver account with an attractive interest rate of 5.45% APR that allows you to save up to £250 each month for a year. Interest is paid at the end of the one-year term. The question is: how much interest would you earn if you saved the maximum of £250 each month?

Compound Interest and Annual Percentage Rate #

Given an Annual Percentage Rate \(R = APR/100\) and initial sum invested \(P\), we can calculate how much interest we would earn in a year with the following formula:

\[ interest = P \times R \]

and our balance at the end of the year will be:

\[ closing\_balance = P + (P \times R) = P(1+R) \]

With a regular saver we are adding to the account each month: the balance changes from month to month, so we need to compute the interest on a monthly basis. Our first task, then, is to compute the monthly interest rate from the APR.

Given a monthly interest rate \(r\) and principal sum deposited \(P\), the interest earned in a month is \(P \times r\) so our balance at the end of the first month will be:

\[ P + (P \times r) = P(1+r) \]

After two months, it will be:

\[ P(1+r) + (P(1+r) \times r) = P(1+r)(1+r) = P(1+r)^2 \]

and after three months:

\[ P(1+r)^2 + (P(1+r)^2 \times r) = P(1+r)^2(1+r) = P(1+r)^3 \]

If we carry on in this manner, we see that after \(n\) months the balance will be:

\[ P(1+r)^n \]

When \(n = 12\) this must equal the closing balance we calculated from the annual rate:

\[ P(1+r)^{12} = P(1+R) \]

Dividing both sides by \(P\) give us:

\[ (1+r)^{12} = (1+R) \]

Taking twelfth roots and rearranging, we get the following formula:

\begin{equation} r = (1+R)^\frac{1}{12} - 1 \end{equation}

We implement a scheme function to compute this:

(define (annual-rate-to-monthly R)
  (- (expt (+ 1.0 R) (/ 1 12)) 1))

Plugging in the APR (remembering to divide by 100) give us:

(annual-rate-to-monthly (/ 5.45 100))
0.004432019218941319

Interest from Regular Saver #

We can now implement a function that, given a starting balance, monthly deposit amount, and monthly interest rate, gives us the balance at the end of the month:

(define (end-of-month-balance starting-balance monthly-deposit monthly-rate)
  (* (+ starting-balance monthly-deposit) (+ 1.0 monthly-rate)))

To calculate the end-of-year balance, we just need to take our initial balance of £0 and apply this function 12 times. We implement a function with a named let to compute this recursively:

(define (end-of-year-balance monthly-deposit monthly-rate)
  (let loop ((months-to-go 12) (balance 0))
    (if (zero? months-to-go)
        balance
        (loop (1- months-to-go) (end-of-month-balance balance monthly-deposit monthly-rate)))))

Use this to calculate our end-of-year balance:

(end-of-year-balance 250 (annual-rate-to-monthly (/ 5.45 100)))
3087.844520928601

This has too many decimal places, let’s implement a function to round it to the nearest penny:

(define (two-dp x)
  (/ (round (* 100 x)) 100))

This gives us:

(two-dp (end-of-year-balance 250 (annual-rate-to-monthly (/ 5.45 100))))
3087.84

So our closing balance would be £3087.84, and we would earn £87.84 in interest from this offer.

A Closed Formula #

If we don’t mind doing a bit more mathematics, we can derive a closed formula for the final balance rather than implementing a loop. Let \(M\) be the monthly deposit, \(r\) the monthly interest rate (as above), and \(b_n\) the balance after \(n\) months. Then we have:

\[ b_1 = M(1+r) \] \[ b_2 = (b_1 + M)(1+r) = (M(1+r) + M)(1+r) = M((1+r)^2 + (1+r))\] \[ b_3 = (b_2 + M)(1+r) = (M((1+r)^2 + (1+r)) + M)(1+r) = M((1+r)^3 + (1+r)^2 + (1+r))\]

and in general:

\[ b_n = M((1+r)^n + (1+r)^{n-1} + … + (1+r)) \]

Considering just the second term in this product:

\[ s_n = (1+r)^n + (1+r)^{n-1} + … + (1+r)^2 + (1+r) \]

so that:

\[ (1+r)s_n = (1+r)^{n+1} + (1+r)^n + (1+r)^{n-1} + … + (1+r)^2 \]

Observe that if we subtract \(s_n\) from \((1+r)s_n\) then most of the terms cancel out, leaving us with:

\[ (1+r)s_n - s_n = r s_n = (1+r)^{n+1} - (1+r) \]

So we have:

\[ s_n = \frac{(1+r)^{n+1} - (1+r)}{r} \]

and:

\[ b_n = \frac{M(1+r)\left((1+r)^n - 1\right)}{r} \]

Implementing this in scheme:

(define (balance-after-n-months n M r)
  (/ (* M (1+ r) (1- (expt (1+ r) n))) r))

Check that this is the same as the result we got from the recursive solution:

(two-dp (balance-after-n-months 12 250 (annual-rate-to-monthly (/ 5.45 100))))
3087.84

Good, it all works out!