Calculation of loan repayments

F

Frank Millman

Hi all

This is fairly trivial, but I had to work it out for the application I
am writing, so I thought I would share it.

It is a function to calculate loan repayments in the same way that a
financial calculator does. You pass it the loan amount, the interest
rate, and the number of periods. It returns the finance charges, the
repayments required, and a list showing how to 'earn' the finance
charges over the period of the loan.

The magic is in line 4. Don't ask me to explain it - I copied it from
the user manual for my Sharp financial calculator. The rest should be
self explanatory.

Hope this is of interest to someone.

Frank Millman

---------------------------------------------------------------------

def calc(pv,i,n):
if i:
i /= 1200.0 # reduce to monthly rate
inst = pv / ((1-(1+i)**-n)/i) # instalment
else:
inst = pv / float(n)
tot = int(inst*n) # do not round up
fc = tot - pv # finance charges
pmt = int(round(inst)) # monthly payment
if pmt * n == tot:
pmts = [(n,pmt)]
else:
pmts = [(n-1,pmt),(1,tot-(pmt*(n-1)))]
fcs = [] # list of finance charges to earn per period
bal = pv
for x in xrange(n-1):
fcp = int(round(bal*i)) # fc for period
fcs.append(fcp)
bal = bal + fcp - pmt
fcs.append(fc - sum(fcs)) # force balance the last one
return fc,pmts,fcs

pv = 10000 # loan amount ($100.00)
i = 10 # interest rate
n = 12 # number of periods

print 'pv =',pv,' int =',i,' per =',n
fc,pmts,fcs = calc(pv,i,n)
print fc
print pmts
print fcs
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top