Compound Interest

2024-03-08 math kids money

My wife and I recently started paying two of our kids allowance. One of them likes to spend it, while the other likes to save it. I looked into opening a bank account so our saver could earn a return on the money, but it was too complicated. We don’t have any brick-and-mortar banks in walking distance from our house, and an online bank may make deposits and withdrawals tricky. There’s also questions of joint ownership or custodianship. Too serious for this age and amount of money.

I decided instead that we parents could act as the bank. Actually, I had toyed with this idea a few years ago and even made a Google Sheet for it. Looking at the spreadsheet, I saw that there was a formula implemented in “Apps Script” for computing the balance at a date:

function balance_at_date(date, compounding_rate, transaction_list) {
  const timestamp = (new Date(date)).getTime();
  const relevant_transactions = transaction_list.filter(
    function(transaction) {
      const transaction_timestamp = (new Date(transaction[0])).getTime();
      return transaction_timestamp && transaction_timestamp <= timestamp;

  const balance = relevant_transactions.reduce(
    function(accumulator, transaction) {
      const transaction_timestamp = (new Date(transaction[0])).getTime();
      return accumulator + transaction[1] * Math.exp(compounding_rate * (timestamp - transaction_timestamp));

  return balance;

Apparently I had written it a few years ago (the idiosyncratic JavaScript style is undeniably mine 😁), but neglected to write comments, so now I was puzzled by it [1]. It boils down to this:

  • we’re doing continuous compounding with some rate r, and
  • we have a list of transactions (ti,xi): at time ti we changed our balance by xi (this is a deposit if positive, otherwise a withdrawal),

and the code claims that the balance at time t should be given by

balance(t)=i,ti<txier(tti) \textrm{balance}(t) = \sum_{i, t_i<t}x_i e^{r(t-t_i)}

It’s alluringly simple: each deposit/withdrawal accrues interest independent of the others. Can that be right? Why is it right?

[1] Aside: it’s unnecessary to use Apps Script for this. I’ve since replaced it with a “named function” (I think this is a new-ish feature) which looks like =ARRAYFORMULA(SUM(transaction_amounts * EXP(LN(1+rate)/365.25*(date-transaction_dates)))).

Another perspective

In a sense, it’s right simply because the choice of how my bank pays out interest is arbitrary. But is it compatible with my understanding of continuous compound interest? Namely, if some principal P is invested with a rate r, then after a time t its value is Pert.

With that in mind, the thing that seems “clearly right” to me is to compute the balance according to:

  • at each time ti, when there’s a deposit or withdrawal xi, compute the new balance Bi, and
  • accrue interest on Bi in each intervening interval ti to ti+1.

This leads us to write down this recurrence relation:

Bi+1=Bier(ti+1ti)+xi+1. B_{i+1} = B_i e^{r(t_{i+1}-t_{i})} + x_{i+1}.

Starting with B0=0, we quickly find

B1=x1B2=x1er(t2t1)+x2B3=x1er(t3t2+t2t1)+x2er(t3t2)+x3=x1er(t3t1)+x2er(t3t2)+x3\begin{aligned} B_1&=x_1 \ B_2&=x_1 e^{r(t_2-t_1)}+x_2 \ B_3&=x_1 e^{r(t_3-t_2+t_2-t_1)}+x_2 e^{r(t_3-t_2)}+x_3=x_1 e^{r(t_3 - t_1)} + x_2 e^{r(t_3-t_2)} + x_3 \end{aligned}

and by induction we have


which is essentially the same as the first formula (if we’re interested in an arbitrary time t, consider a final deposit of 0 at t). So, yes, these two perspectives are compatible: (1) adjust the balance after each deposit/withdrawal and then accrue interest on that balance, and (2) every deposit/withdrawal accrues interest independently.

Other thoughts

We can see that the balance is “linear” in the deposits, in the sense that a function f is linear in its argument if f(x+y)=f(x)+f(y). Going back to the textbook formula, balance=Pert, it’s pretty clearly linear in P. But in the current context, we’re dealing with a balance function whose input is a list of (time,amount) tuples, and it’s not as clear. Taking addition in the domain to be concatenation of the lists, we can show linearity with:

balance(t,{(xi,ti)}i=1N)+balance(t,{(xi,ti)}i=N+1M)=i=1Nxier(tti)+i=N+1Mxier(tti)=i=1Mxier(tti)=balance(t,{(xi,ti)}i=1M)\begin{split} \textrm{balance}\left(t, \left{(x_i, t_i)\right}{i=1\ldots N}\right)&+\textrm{balance}\left(t, \left{(x_i,t_i)\right}{i=N+1\ldots M}\right) \ &=\sum_{i=1}^N x_i e^{r(t-t_i)}+\sum_{i=N+1}^M x_i e^{r(t-t_i)} \ &=\sum_{i=1}^M x_i e^{r(t-t_i)} \ &= \textrm{balance}\left(t, \left{(x_i, t_i)\right}_{i=1\ldots M}\right) \end{split}

Also note that the differential equation dBdt=rB is linear in B and that the linear combination B(t)=ixier(tti) is a solution. I don’t think that implies that it must be the solution we were looking for, but maybe there’s some way to argue that.

I still find that linearity a bit surprising (so maybe I’m missing a key insight). But if I made two simultaneous deposits of x/2 instead of one deposit of x, it’s not too hard to believe that they’d earn the same interest. It’s less obvious to me that deposits made at different times should accrue interest independently: shouldn’t the second deposit combine with the existing balance to enhance the earnings? Yes, but exactly as if the first continued to accrue interest independently, and the second accrued interest independently.

What does the term mean for a withdrawal (xi<0), though? I think this can be seen as an opportunity cost. Had it been left in the account instead of withdrawn, it would have earned that interest. In the formula for the balance, this opportunity cost is there in order to offset the interest that continues being accrued by that same money prior to its withdrawal. I suppose this is why a mortgage pre-payment should be considered like an investment at the mortgage interest rate: the future principal is reduced as if the extra payment were accruing that interest.

One last obvious thing to point out is that the balance is not linear in its time argument: Pert+PertPer(t+t). Instead, the dependence on time is exponential, which is what makes compound interest so interesting and “the most powerful force in the universe”, according to Einstein somebody.

comments powered by Disqus