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));
},
0
);
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 $({t}_{i},{x}_{i})$: at time ${t}_{i}$ we changed our balance by ${x}_{i}$ (this is a deposit if positive, otherwise a withdrawal),

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

$\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))))`

.

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 $P{e}^{rt}$.

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

- at each time ${t}_{i}$, when there’s a deposit or withdrawal ${x}_{i}$, compute the new balance ${B}_{i}$, and
- accrue interest on ${B}_{i}$ in each intervening interval ${t}_{i}$ to ${t}_{i+1}$.

This leads us to write down this recurrence relation:

$B_{i+1} = B_i e^{r(t_{i+1}-t_{i})} + x_{i+1}.$

Starting with ${B}_{0}=0$, we quickly find

$\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

$${B}_{n}=\sum _{i\le n}{x}_{i}{e}^{r({t}_{n}-{t}_{i})}$$

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.

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, $\text{balance}=P{e}^{rt}$, 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 $(\text{time},\text{amount})$ tuples, and it’s not as clear. Taking addition in the domain to be concatenation of the lists, we can show linearity with:

$\begin{split} \textrm{balance}\left(t, \left{(x_i, t_i)\right}$

Also note that the differential equation $\frac{dB}{dt}=rB$ is linear in $B$ and that the linear combination $B(t)={\sum}_{i}{x}_{i}{e}^{r(t-{t}_{i})}$ 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\mathrm{/}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 (${x}_{i}<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: $P{e}^{rt}+P{e}^{r{t}^{\mathrm{\prime}}}\mathrm{\ne}P{e}^{r(t+{t}^{\mathrm{\prime}})}$. 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.