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:
and the code claims that the balance at time should be given by
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 is invested with a rate , then after a time its value is .
With that in mind, the thing that seems βclearly rightβ to me is to compute the balance according to:
This leads us to write down this recurrence relation:
Starting with , we quickly find
and by induction we have
which is essentially the same as the first formula (if weβre interested in an arbitrary time , consider a final deposit of at ). 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 is linear in its argument if . Going back to the textbook formula, , itβs pretty clearly linear in . But in the current context, weβre dealing with a balance function whose input is a list of tuples, and itβs not as clear. Taking addition in the domain to be concatenation of the lists, we can show linearity with:
{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 is linear in and that the linear combination 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 instead of one deposit of , 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 (), 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: . 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.