Financial Calculations in Emacs
I may or may not have been involved in a large home purchase, and I may or may not have run a lot of financial calculations the past few days. For financial calculations, nothing beats the hp-12c. But mine is at home, and when I’m away I might only have access to the Emacs Calc calculator.1 This is a lie. I have Touch rpn on my Android, which simulates a portrait-orientation hp-12c. Brief review: it is a hp-12c, but it is clearly prioritising faithfulness to the original over touch screen ergonomics. Thus, my favourite pocket calculator remains RealCalc for Android. Great rpn support, but lacks functions for solving problems with compound interest. I’ll have to use both apps going forward. It does have decent support for financial calculations, but it’s not as easy as on the hp-12c. I wanted to try anyway.
In the end, these things didn’t stick for me. When I need to do financial calculations, I still bring up the hp-12c. If financial calculations is your interest, you might want to skip this article and instead stay tuned for a future tutorial on the hp-12c. This article is about doing these things in Emacs – I would argue a worse choice for the problem, but maybe sometimes the only one we have.
Calc is primarily a postfix or rpn calculator, meaning to calculate \((5 + 3)
\times 9\) we type in 5 ␍ 3 + 9 *
, where the ␍
symbol means pressing enter or
return.2 Calc uses return to confirm input. Return and space can be used
interchangeably to push the current input onto the top of the stack. It also
supports algebraic input using a leading apostrophe, so the same thing could
be typed in as '(5 + 3) * 9 ␍
. We prefer the rpn style because it’s fewer
keypresses: six versus eight. I have also found that once I got used to rpn
input, it became the more natural way to do things.3 Hence RealCalc on
Android: it also supports rpn input.
The financial functions of Calc are not very well known, so I thought I might show them. For lack of imagination, we’ll present them using the opening examples of the hp-12c manual, translated to Emacs.
Suppose you want to ensure that you can finance your child’s college education 14 years from today. You expect that the cost will be about $6,000 a year ($500 a month) for 4 years. Assume your child will withdraw $500 at the beginning of each month from a savings account. How much would you have to deposit into the account when they enter college if the account pays 6 % annual interest compounded monthly?
The first thing we need to know is how much needs to be in the account at the start of the college period. This is a present value calculation4 Somewhat confusingly, because we’re talking about what the present value will be 14 years from now!: what is the present value of all the college withdrawals, assuming that 6 % interest rate?5 You can tell this is a manual from the ’80s, because it talks about a savings account that pays 6 % interest.
0.06 ␍ 12 / 4 ␍ 12 * 500 IbP
First, we specify the monthly rate as a twelvth of the annual rate. Then we
specify the number of periods as twelve months times four years. Finally, the
payment size is 500. We then compute the present value (bP
), assuming withdrawals
happen at the start of each period (that’s the I
prefix).
We find out we need $21,396.61 at the start of the college attendance. This is also what the hp-12c manual says, so we’re good so far.
We now need to determine how to accumulate the required deposits by the time your child enters college 14 years from now. Let’s say they have a paid-up $5,000 insurance policy that pays 5.35 % annually, compounded semiannually. How much would it be worth by the time they enter college?
This is a future value calculation:
0.0535 ␍ 2 / 14 ␍ 2 * 5000 HbF
Again, we specify the per-period rate, followed by the number of periods. Then
we have a fixed lump sum investment of $5,000 which we want the future value
(bF
) of. The H
prefix is what makes it treat it a lump sum rather than a
recurring payment.
This is revealed to be 10470.85, meaning we still need to find a way to collect the remaining
21396.61 ␍ 10470.85 -
i.e. $10,925.76. This is an inverse future value problem – how much must we make in recurring payments to get that number?
Suppose you make monthly payments, beginning at the end of next month, into an account that pays 6 % annually, compounded monthly. What payment amount would be required in order to accumulate $10,925.76 in the 14 years remaining?
There is no convenient hotkey to do this in Emacs, so we enter the equation
equation algebraically (by leading with a single apostrophe). Then we ask it to
solve analytically for x, by entering the command aS
and specifying that we
want to solve for x:
'fv(0.06/12, 14*12, x) = 10925.76 ␍ aS x ␍
This tells us we need to deposit $41.66 per month.
Suppose you cannot find a bank that currently offers an account with 6 % annual interest compounded monthly, but you can afford to make $45 monthly payments. What is the minimum interest rate that will enable you to accumulate the required amount?
We want the rate which, after some periods with recurring deposits, results in a
particular future value. This, unfortunately, is not something Emacs offers
support for, and nor is it able to solve the corresponding equation
algebraically. Fortunately for us, it does solve equations iteratively. We
feed in the equation we need solved, and then a guess (6 % ought to be close),
and then kick off numeric root finding with aR
.
'fv(x/12, 14*12, 45) = 10925.76 ␍ 0.06 aR x ␍
This comes back with 5.01 %, which is the correct answer!
The language of Calc reminds me a lot of the language of Vim, which I suppose is one of the reasons I enjoy it so much. It’s a little annoying that it doesn’t make financial calculations in all directions as convenient and intuitive as the hp-12c. Then again, few things do.