# Financial modules for Python

Paul Rubin phr-n2001d at nightsong.com
Tue Oct 9 10:37:15 CEST 2001

```jorjun <jorjun at mac.com> writes:
> I'm looking for tools to help me make mortgage repayment decisions.
> Couldn't get a working URL for pyfi which contains loan amortization
> stuff, I think.
> Can someone help?

The basic notion is that the loan payoff is a truncated geometric series.
Let z=1/(1+i) where i is the interest rate per payment period.  So if
it's a monthly payment (12 periods/year) and 6%/year, i=.06/12 = .005
so z is approx. 0.995.

What's the present value of a payment stream of \$5 per month for the
next 5 years (60 months), starting immediately?

The payment this month is obviously worth \$5.  The payment next month
is \$5 discounted by z.  The payment 2 months from now is discounted by z**2,
and so forth.  So the value of the whole stream is

\$5 * (1 + z + z**2 + z**3 + ... + z**59).

How do you sum this series?  Remember from calculus that for |z|<1,
the infinite series 1+z+z**2+... (going on to infinity) = 1/(1-z).
To see why that's so, let X=1+z+z**2+...  Then zX=z+z**2+z**3+...
which means that X-zX = X(1-z)=1.  So X=1/(1-z).

Now what about the truncated series?  Well, 1+z+z**2+...+z**59 is
equal to X - (z**60)*X, where X is the infinite sum above.  So that
payment stream is worth \$5*[1/(1-z) - z**60/(1-z)] = \$5*((1-z**60)/(1-z)).
For i=6%, substituting back, z**60 is about .74, and the total is

That shows you how to compute present value from payment amount,
interest rate, and number of periods.  You can juggle the formulas
around, or in some cases you may need to use a numerical rootfinder,
should be enough to get you started writing your own functions.  It's
not something you need a fancy package for--just a few lines of code
is enough.

True story: when I got my car loan, being a total nerd I brought a
handheld computer and checked the loan arithmetic with the above
formula.  The payment they had quoted me turned out to be \$4.00 too
high for the loan amount, a small enough difference that I was going
to shrug it off, but then realized that over the 4-year loan, it meant
\$200 extra for the dealer.  So I asked why the numbers didn't check,
the salesman took the printout back to the finance office, and he came
back with a new printout whose numbers were closer to mine and I saved
the \$200.  He explained the reason for the discrepancy: "the two
printouts were done with different versions of the software".  Yeah,
right.  So, it pays to have some understanding of how these things are
done, rather than just running some package as a black box.

```