Loans and schedules
A loan's schedule is a table of real payments in whole cents. Each row is rounded by a rule, the rows are consistent with one another, and the last payment is whatever clears the balance.
const l = loan({ principal: 250000, rate: "6%", periods: 360 });
l.payment; // 1498.88
l.exactPayment; // 1498.876313…, before rounding
l.schedule[0]; // { period: 1, rate, payment, interest, principal, extra, balance }
l.finalPayment; // 1495.22
l.totals; // { payments, interest, principal, extra }
loan({ principal: 250000, rate: "6%", periods: 360, extra: 200 }).periods; // 267
refinance({ balance: 200000, rate: "7%", periods: 300 }, { rate: "5.5%", periods: 300 }, { costs: 4000 }).breakEven; // 22
How a schedule is made
The level payment that repays a balance B in n periods at rate i per period comes from the annuity formula, . It is almost never a whole number of cents, so it is rounded, and that is where a spreadsheet and a bank begin to differ. Then for each period: interest is the balance times the rate, rounded; the rest of the payment reduces the balance. Because the payment was rounded, the balance after n − 1 payments is not quite what one more level payment would clear, so the last payment is the balance plus its interest. Here that payment is shown (finalPayment), and the principal parts add to the amount borrowed exactly.
rate is the nominal annual rate and the rate per period is rate/periodsPerYear, the usual convention. For a rate quoted another way, convert it first with equivalentRate. With rounding: false nothing is rounded and every payment is equal.
Variations
| option | effect |
|---|---|
| extra | more principal each period: an amount, { period: amount }, or (period, balance) ⇒ amount. The loan ends early. |
| interestOnly: k | the first k payments are interest alone; the payment is then set to repay over what remains |
| balloon | an amount left owing, paid with the last payment |
| rateChanges: [{ from, rate }] | a new rate from a period on; the payment is worked out again for the balance and periods left |
| payment | fix the payment yourself; then periods may be omitted and the loan runs until it is repaid. A payment that does not cover the interest is refused. |
Comparing and refinancing
compareLoans(a, b) puts two loans side by side. refinance(current, proposed, { costs, rollIn }) answers the usual question: the new loan has a lower payment but costs something to set up, so when do the payments saved add up to the cost (breakEven), and what is saved over the whole life (totalSaved)? A lower payment over a longer term can cost more in total, and the numbers say so.
loanAPR finds the rate a borrower really pays when fees are taken from the amount advanced: the single rate at which the payments are worth the money received.
Revolving credit
A card charges interest on the balance and asks for a payment that is a percentage of it, with a floor. Since the payment shrinks with the balance the debt lasts a long time. revolving runs it out and totals the interest; a fixed payment that does not cover the interest is refused, since the balance would never fall.
| call | returns |
|---|---|
| loan({ principal, rate, periods, periodsPerYear = 12, … }) | { payment, exactPayment, schedule, periods, finalPayment, balloon, totals } |
| levelPayment(balance, periodicRate, periods, balloon) | Rational, unrounded |
| compareLoans(a, b) | { a, b, difference } |
| refinance(current, proposed, { costs, rollIn }) | { current, proposed, paymentChange, interestSaved, totalSaved, breakEven, worthIt } |
| loanAPR({ principal, fees, payment, periods }) | { periodic, nominal, effective } |
| revolving({ balance, rate, payment, purchases }) | { schedule, periods, totals } |
@ratmath/finance and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.