Skip to content

J-ottings 10: Interest Calculations

Vector 13:1, page 29

mathematics and statistics education

Transcribed from the printed issue; not yet reviewed. Read it in the PDF of the issue, page 29

The last article in this series was very powerful; this one, for reasons which will soon be apparent, is going to be full of interest.

First, I want to develop the theme of "little things that do so much". Take the hook

   +*:

which realises in J the phrase in algebra \(k(k+1)\).

In mathematics it is often desirable to sum series of such terms from 1 to \(n\), say, so define an adverb

   upto=.(&>:)@i.

and a verb of the sort which seems to make things more acceptable to general readers and sceptical colleagues:

   sum=:+/

so that

   sum (+*:) upto 5
70

gives the sum of the series 1.2 + 2.3 + 3.4 + 4.5 + 5.6.

You can now slot between the parentheses a description of an individual term of a series of your choice. For example

   sum (^&4) upto 5     NB. means sum the fourth powers of 1,..5
979
   sum (2&^) upto 5     NB. means sum the series 2 to the powers 1,..5
62

You can have endless fun with hooks, but be careful of the differences between composition and hook. For example, sum (%>:).. means sum the series \(k/k+1\), while sum (%@>:).. means sum the series \(1/k+1\).

As another illustration %^ and %@^ define values according to the following table:

monadic dyadic
%^ y.e−y. x.e−y.
%@^ e−y. x.−y.

Parentheses can also make subtle differences:

   sum (+*:^:2) upto 5      NB. sum n+ n^4
994
   sum ((+*:)^:2) upto 5    NB. sum ((n(n+1))((n(n+1))+1) = n(n+1)(n^2 +n+1)
1554

Standard mathematical series fit within this framework, for example:

   sum (%!) upto 5            NB. series for e = 1 + 1/1! + /2! + ..
2.70833
   (sum% upto 9)-0.57721      NB. sum of recip's->ln(n)+Euler's const
2.25176
   ^2.25176                   NB. e to power 2.25176 is approx 9
9.50445

Or take a well known series approximation to pi using the sums of the reciprocals of the integers:

   %:6*sum ^&_2 upto 100
3.13208

While summing series with a computer is no substitute for analysis, it can become a very supportive, as well as a compulsive, activity.

Now let’s come to earth and do the interesting bit! Suppose that the series to be summed calculates the present worth of future payments. For example, if the annuity rate is 8% what is the present value of an income of £1 per annum for the next ten years? This requires the summation of the series (1/(1+i))^k for \(k=1,..10\). Hence define the 1/1+i verb described above

   g=.%@^

which anticipates inputs of, for example 1.08 and 10, and follow the pattern established above:

   sum (1.08&g) upto 10
6.71008

Now suppose that the income is to be paid more frequently. The appropriate root of the interest rate 1+i has to be taken, and the premium divided by the frequency of the payments.

For example if income is paid monthly:

   12%~ sum (12%:1.08)&g upto 120
6.9527

or for quarterly income:

   4%~ sum (4%:1.08)&g upto 40
6.90816

APR (Annual Percentage Return) calculations can be performed in the same fashion. For example borrowing £1 at an APR of 20% with monthly repayments results in a total repayment of

   sum 12%~12%:1.2&^ upto 12
1.10532

showing that a nominal rate of 10.53% is equivalent to an APR of 20%.

Obtaining APRs from nominal rates requires the solution of polynomial equations. Writing a verb to perform Newton-Raphson root finding is greatly aided by the derivative adverb D. . A root-finding adverb is discussed in detail in [1], and is updated here to run in J for Windows:

   newton=.1 : ']-x.%x.D.1' (^:_)

The 1 : indicates that the string following defines an adverb, whose relation to the formula for Newton-Raphson iteration will be immediately clear to anyone who is familiar with this topic. The final "power to infinity" adverb in parentheses means that iteration is to be continued until convergence.

A verb which models a polymonial uses #., for example

   ff=.#.&1 _1 _2

where 1 _1 _2 are the coefficients of powers in descending order, so that the polynomial represented above is x^2 -x-2.

Everything is now in place to calculate APRs from nominal rates of interest. The polynomial to be solved is

   x^(n+1) - (k+1)x +k = 0

where k is n times 1+i and 100*i is the nominal rate of interest. Taking the case which was solved "in reverse" above, k=12*1.10532 = 13.2636, and so the appropriate polynomial and its solution is:

   f1=.#.&1 0 0 0 0 0 0 0 0 0 0 0 _14.2636 13.2636
   (f1 newton 1.05)^12
1.19997

To take another example, the APR for monthly payments and a nominal rate of 10% is given by

   f2=.#.&1 0 0 0 0 0 0 0 0 0 0 0 _14.2 13.2
   f2 newton 1.05^12
1.01458
   1.01458^12
1.18969

that is, the APR is 18.97%.

Turning next to the question of loan repayments, the formula concerned is a little different, namely

(x-1).x^n
---------
1- x^n

Given that \(x=1+i\) with \(i\) defined as above, this predicts the annual repayments for a loan of £1 spread over n years. In J we have

   repay=.((<:@[)*^) % (<:@^)

None of the parentheses in the above verb definition are necessary, but they help clarify its relation to the formula immediately preceding it. An example of use is

   1.09 repay 25
0.1018063

that is, the annual repayments on a loan of £1000 for 25 years are £101.81. Since interest on mortgage repayments is normally applied on an annual basis, this formula can be used to predict monthly mortgage repayments by simply dividing the result by 12. Thus the monthly repayment on a 9% mortgage of £10,000 for 25 years is £84.84.

You were warned that this article was going to be interesting ...

Reference

[1] Applying Matrix Divide in APL and J, Norman Thomson, Conference Proceedings, APL94, Quote Quad Vol. 25 No.1, Sept. 1994