Loan Amortization

May 12, 2009

Kate was struggling with her task, so I decided to help by showing her a complete version of the program. I wrote in awk; I didn’t want to give away the whole answer by writing in Java, but awk is similar enough to be genuinely helpful. Here is my solution:

awk -v bal=10000 -v rate=7 -v term=36 ' BEGIN {
    rate = rate / 12 / 100
    pmt = bal * rate / (1 - (1+rate) ^ -term)
    printf " N   PRIN    INT  BALANCE\n"
    printf "-- ------ ------ --------\n"
    printf "%2d               %8.2f\n",0,bal
    for (n=1; n<term; n++) {
        intr = round(rate * bal, 2)
        prin = pmt - intr
        bal = bal - prin
        printf "%2d %6.2f %6.2f %8.2f\n", n, prin, intr, bal }
    printf "%2d %6.2f %6.2f %8.2f\n", term, bal, pmt - bal, 0 }
    function round(x, n) { return int(x * 10^n + 0.5) / 10^n } '

The output looks like this:

 N   PRIN    INT  BALANCE
-- ------ ------ --------
 0               10000.00
 1 250.44  58.33  9749.56
 2 251.90  56.87  9497.66
 3 253.37  55.40  9244.29
 4 254.84  53.93  8989.45
 5 256.33  52.44  8733.12
 6 257.83  50.94  8475.28
 7 259.33  49.44  8215.95
 8 260.84  47.93  7955.11
 9 262.37  46.40  7692.74
10 263.90  44.87  7428.84
11 265.44  43.33  7163.40
12 266.98  41.79  6896.42
13 268.54  40.23  6627.88
14 270.11  38.66  6357.77
15 271.68  37.09  6086.09
16 273.27  35.50  5812.81
17 274.86  33.91  5537.95
18 276.47  32.30  5261.48
19 278.08  30.69  4983.40
20 279.70  29.07  4703.70
21 281.33  27.44  4422.37
22 282.97  25.80  4139.40
23 284.62  24.15  3854.78
24 286.28  22.49  3568.50
25 287.95  20.82  3280.55
26 289.63  19.14  2990.91
27 291.32  17.45  2699.59
28 293.02  15.75  2406.57
29 294.73  14.04  2111.84
30 296.45  12.32  1815.39
31 298.18  10.59  1517.21
32 299.92   8.85  1217.29
33 301.67   7.10   915.62
34 303.43   5.34   612.19
35 305.20   3.57   306.99
36 306.99   1.78     0.00

A similar loan calculator is available from Bret Whissel at http://www.bretwhissel.net/cgi-bin/amortize.

About these ads

Pages: 1 2

5 Responses to “Loan Amortization”

  1. [...] Praxis – Loan Amortization By Remco Niemeijer Today‚Äôs Programming Praxis problem is about loan amortization. For those of you who, like me, had no idea [...]

  2. Remco Niemeijer said

    My Haskell solution (see http://bonsaicode.wordpress.com/2009/05/12/programming-praxis-loan-amortization/ for a version with comments):

    import Text.Printf
    
    amortize :: Float -> Float -> Int -> [(Int, Float, Float, Float)]
    amortize _ _ 0 = []
    amortize b r l = (l - 1, prn, int, b') : amortize b' r (l - 1)
                     where int = b * r
                           prn = int / (1 - (1 + r) ^^ (-l)) - int
                           b' = b - prn
    
    amortization :: Float -> Float -> Int -> IO ()
    amortization b r l = do
        putStrLn "Left Principal Interest   Balance"
        mapM_ table $ (l, 0, 0, b) : amortize b (r / 12 / 100) l
        where table (m, p, i, t) = printf "%4d %9.2f %8.2f %9.2f\n" m p i t
    
    main :: IO ()
    main = amortization 10000 7 36
    
  3. grettke said

    A Solution in PLT Scheme.

    This is the first Scheme program I had ever wrote; and its present form is the result of many kind folks helping me out :)

    #lang scheme
    
    (define mamort
     (lambda (bal pcnt pay)
       (format "Payment #,Payment Amount,Interest Paid,Principle
    Paid,Balance,Total Interest~%")
       (amort bal pcnt pay 12)))
    
    (define amort
     (lambda (bal pcnt pay freq)
       (let loop ((bal bal) (tot-int 0) (pmt 1))
         (let* ((int (* pcnt bal (/ freq)))
                (prin (min (- pay int) bal))
                (new-bal (- bal prin))
                (new-int (+ int tot-int))
                (done (< (+ int prin) pay)))
           (if done
               (printf "~s,~s,~s,~s,~s,~s~n"
                       pmt (+ int prin) int prin new-bal new-int)
               (begin
                 (printf "~s,~s,~s,~s,~s,~s~n"
                         pmt pay int prin new-bal new-int)
                 (loop new-bal new-int (+ 1 pmt))))))))
    
    (mamort 10000 .12 350)
    
  4. Kathryn Bewig said

    This is my favorite posting yet, Dad. :)

  5. Hi, I do think this is a great web site. I stumbledupon it
    ;) I will come back yet again since i have book marked it.
    Money and freedom is the greatest way to change, may you be rich and
    continue to guide other people.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 600 other followers

%d bloggers like this: