Post Reply 
(11C) TVM for HP-11C
12-07-2020, 06:55 PM (This post was last modified: 06-16-2021 01:44 PM by Albert Chan.)
Post: #15
RE: (11C) TVM for HP-11C
For getting interest rate for the general case, npmt() is very nice formula.

TVM equation, for "net payment", NPMT = NFV * i/(K-1):
Code:
require 'mathx'
expm1, log1p = mathx.expm1, mathx.log1p

function npmt(n,i,pv,pmt,fv)        -- npmt = (pv*K+fv) * i/(K-1) + pmt
    return ((fv+pv) / expm1(log1p(i)*n) + pv)*i + pmt
end

lua> n, pv, pmt, fv = 36, 30000, -550, -15000
lua> i1 = guess_i(n, pv, pmt, fv)
lua> p1 = npmt(n, i1, pv, pmt, fv)
lua> i1, p1
0.005804795434882038      -0.006442911074373114
lua> i2 = 1.001*i1                    -- p1<0, i1 is a bit low
lua> p2 = npmt(n, i2, pv, pmt, fv)
lua> i2, p2
0.005810600230316919      0.12838859778003098
lua> i1 - p1/(p2-p1) * (i2-i1)     -- secant's method
0.005805072816490459

For comparison, with same i1, i2, and secant's method, for other TVM formula:

0.005805072843740277           -- NPV formula
0.005805072788863714           -- NFV formula

Solve npmt()=0, using Newton's method, we have: (guess_i code here)
Code:
function loan_rate(n, pv, pmt, fv, i)
    i = i or guess_i(n, pv, pmt, fv)
    return function()
        local x = expm1(log1p(i)*n)
        local y = (fv+pv)/x
        local z = (1+1/x)*i/(1+i)*n - 1
        i = i - ((pv+y)*i+pmt) / (pv-y*z)   -- Newton's method
        return i
    end
end

lua> g = loan_rate(n, pv, pmt, fv)
lua> g(), g()
0.005805072819567153      0.005805072819420132

lua> g = loan_rate(-n, fv, -pmt, pv) -- time symmetric, as expected
lua> g(), g()
0.005805072819567159      0.005805072819420132

Update: numbers adjusted with updated guess_i()
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
(11C) TVM for HP-11C - Gamo - 05-09-2019, 01:15 AM
RE: (11C) TVM for HP-11C - Gamo - 12-03-2019, 10:12 AM
RE: (11C) TVM for HP-11C - Gamo - 02-13-2020, 06:14 AM
RE: (11C) TVM for HP-11C - bshoring - 12-02-2020, 09:02 PM
RE: (11C) TVM for HP-11C - Gamo - 12-03-2020, 08:23 AM
RE: (11C) TVM for HP-11C - Dave Britten - 12-03-2020, 01:48 PM
RE: (11C) TVM for HP-11C - bshoring - 12-03-2020, 05:53 PM
RE: (11C) TVM for HP-11C - Dave Britten - 12-03-2020, 06:08 PM
RE: (11C) TVM for HP-11C - Albert Chan - 12-03-2020, 08:53 PM
RE: (11C) TVM for HP-11C - Albert Chan - 12-04-2020, 08:01 PM
RE: (11C) TVM for HP-11C - Albert Chan - 12-05-2020, 01:05 AM
RE: (11C) TVM for HP-11C - Albert Chan - 12-05-2020, 03:46 AM
RE: (11C) TVM for HP-11C - Albert Chan - 05-10-2022, 09:35 PM
RE: (11C) TVM for HP-11C - Albert Chan - 05-11-2022, 01:07 PM
RE: (11C) TVM for HP-11C - Albert Chan - 12-06-2020, 02:32 PM
RE: (11C) TVM for HP-11C - Albert Chan - 12-06-2020, 04:41 PM
RE: (11C) TVM for HP-11C - Albert Chan - 12-07-2020 06:55 PM
RE: (11C) TVM for HP-11C - Albert Chan - 12-08-2020, 03:05 PM
RE: (11C) TVM for HP-11C - Albert Chan - 05-14-2022, 12:26 PM



User(s) browsing this thread: 6 Guest(s)