Post Reply 
Some TVM fun
01-01-2024, 10:12 PM
Post: #1
Some TVM fun
I recorded myself playing around with a few TVM problems of varying difficulty on the HP-12c, RPN83P, Plus42 and the DM42. I hope some of you might enjoy it! It's been fun making a TVM program - tickles my maths, programming, solving and UI bits of my head.

Part I
Part II
Part III
Part IV
Part V
Part VI
Part VII

I've just realised that I forgot I had a 5th TVM solver - the one on the TI-83plus. Maybe I'll try that one another time. Also, I've been so impressed with RPN83P - clearly so much work and love has gone into that app, and in more than one way, it can exceed the capability of our much beloved HPs, and in a similar vein, Plus42 - the effort that has gone into honing that TVM solver by Thomas and Albert Chan is mighty impressive - and all of this is relatively recent progress too, which is nice to know that despite using calculators that are 40+ years old, they can still be modern.
Find all posts by this user
Quote this message in a reply
01-02-2024, 12:51 AM (This post was last modified: 01-02-2024 01:58 AM by Albert Chan.)
Post: #2
RE: Some TVM fun
Compounding factor C = x*n/(1-(1+x)^-n) ≈ 1 + (n+1)/2*x + (n^2-1)/12*x^2 + ...

Part VII, solving for a very tiny interest rate x, we can ignore x^(2 or higher)

NPMT = C*PV + n*PMT + C(n=-n)*FV = 0

(1+(n+1)/2*x)*PV + n*PMT + (1+(-n+1)/2*x)*FV ≈ 0

(PV*(n+1)-FV*(n-1))/2 * x ≈ -(n*PMT+PV+FV)

Part VII, n=10, PV=-100, PMT=10, FV=1e-10, solve for x:

(-100*11 - 1e-10*9)/2 * x ≈ -1e-10
x ≈ 1e-10 / 550

P/YR=12 --> I%YR = 1200x ≈ 1e-10 * 24/11 ≈ 2.18181818e-10 %
Find all posts by this user
Quote this message in a reply
01-02-2024, 01:47 PM (This post was last modified: 01-02-2024 01:52 PM by Albert Chan.)
Post: #3
RE: Some TVM fun
(01-02-2024 12:51 AM)Albert Chan Wrote:  (PV*(n+1)-FV*(n-1))/2 * x ≈ -(n*PMT+PV+FV)

If n≫1, and we solve for PMT instead of x, we have:

PMT ≈ (-PV - FV)/n + (-PV + FV) * x/2

Converted to loan convention (PV sign flipped), we have car lease formula
Quote:PMT = (CAP – RES) ÷ TRM + (CAP + RES) x MF
Find all posts by this user
Quote this message in a reply
01-02-2024, 07:37 PM
Post: #4
RE: Some TVM fun
(01-02-2024 12:51 AM)Albert Chan Wrote:  Part VII, solving for a very tiny interest rate x, we can ignore x^(2 or higher)

Ah that makes sense!

Do you have a good TVM torture test that can trigger that issue with the solving when i is close to zero? Also I'm curious if you know why RPN83P and Plus42 do so well with finding zero on problem IV?
Find all posts by this user
Quote this message in a reply
01-02-2024, 08:16 PM
Post: #5
RE: Some TVM fun
Hi, dm319

Plus42 does not evaluate f(ε), or its derivatives. Instead, it use taylor series:

f(ε) ≈ f(0) + f'(0)*ε
f'(ε) ≈ f'(0) + f''(0)*ε

RHS is easy to evaluate accurately, LHS may not, due to massive cancellations.

core_commands9.cc
Code:
    if (1 + i * i == 1) {
        phloat a = (pv + fv) / n;
        phloat b = pv - fv;
        phloat fp = (a + b) / 2;
        phloat fpp = (n * n - 1) * a / 6;
        f = a + pmt + fp * i;
        fp += fpp * i;
        i += (eps = -f / fp);
    } else {
    ...
Find all posts by this user
Quote this message in a reply
01-03-2024, 01:08 PM
Post: #6
RE: Some TVM fun
Thanks Albert,

So I think you're saying that I'm using an iterative method to solve for I%, by using the program solver.

But you are using a Taylor series to computer I%? That's very intriguing. Would it be worth switching to using this method for my DM42 solver?
Find all posts by this user
Quote this message in a reply
01-03-2024, 05:14 PM (This post was last modified: 01-03-2024 05:36 PM by bxparks.)
Post: #7
RE: Some TVM fun
I had a quick peak into Part IV, where I%YR is 0 or slightly negative. Two comments about how RPN83P handles that:

1) Negative I%YR: I set the 2 default initial guesses in the TVM Solver to be 0% and 100%, assuming that real life problems have positive interest rates, instead of deflationary negative interest rates. But I hadn't considered rounding or truncation errors which may push the solution to be slightly negative. So maybe I should set the guesses to be -1% and 100%? (Edit: The reason it fails to find a solution is because I coded the solver to be conservative, and bail out immediately if it does not detect a sign change using the initial root guesses.) I have tried hard to avoid dealing with negative interest rates because many expressions in the TVM equations blow up at -100%, and I don't have a full understanding of the mathematical and numerical properties of those expressions near -100%.

2) Accuracy When I%YR is Close to Zero: I have doubts about the numerical accuracy of the RPN83P TVM Solver when I%YR is close to 0%. Mostly because I'm not sure what the termination condition should be. Normally the root solver (I use the Secant Method) terminates when the relative difference between 2 successive candidate roots is less than some tolerance, specifically, abs((i2-i1)/i2) < 1e-8. But when the actual solution is 0 or close to it, we can suffer from a division by zero error. My current hack is to use an absolute error when i2==0, i.e. use abs(i2-i1) < 1e-8. But that becomes progressively less accurate when the actual root is less than around 1e-8 (Edit: Or does it only start becoming less accurate when i1 or i2 is < 1e-49 or 1e-99? Anyway, it's not ideal.)

Albert's post about switching to Taylor series expansion when (1+i^2 == 1) seems like the appropriate solution when i becomes close to zero. I don't have time to look at this now, as I'm spending all my free time towards a new release of RPN83P with complex numbers. I may get around to it afterwards. (On the other hand, coding complicated TVM equations in assembly language and doing manual stack juggling is so painful, I may not have the energy to come back to this until much much later.)
Find all posts by this user
Quote this message in a reply
01-03-2024, 05:21 PM
Post: #8
RE: Some TVM fun
(01-03-2024 01:08 PM)dm319 Wrote:  So I think you're saying that I'm using an iterative method to solve for I%, by using the program solver.

I think all TVM solver use iterative method to solve I%. (there is no closed-form I am aware of)

Quote:But you are using a Taylor series to computer I%?

Perhaps it is better if we see else clause too
Code:
    } else {
        phloat x = i / expm1(n * log1p(i));
        phloat k = (pv + fv) * x;
        phloat y = n * x - 1;
        f = k + pv * i + pmt;
        phloat num = y + (n - 1) * i;
        phloat den = i + i * i;
        x = f * den / (k * num - pv * den);
        i = i + x; // Newton's method
        eps = x;
    }

if i is tiny, x ≈ i/(n*i) ≈ 1/n --> y = n*x - 1 ≈ 1 - 1, generated massive cancellations.
And, if i = 0, we have x = 0/0

But code assumed one-sided convergence. Over-shoot is used to detect no solution.
Accurate Newton correction is crucial to avoid getting false positive.

That's why Plus42 need if clause to handle tiny i, else clause for general case.

Note: Plus42 start with rate guess from the "edge".

[Image: newton_graph.jpg]

It intentionally not use good guess, so that one-sided convergence is guaranteed.
It is possible to hit tiny i as one of its iterations, even if true rate is not close to 0%
Find all posts by this user
Quote this message in a reply
01-03-2024, 08:04 PM
Post: #9
RE: Some TVM fun
So I think you're saying that plus42 checks for tiny i at each iteration? And if it hits that, it gets into that taylor series? And if you are 'coming from the left' there is an outside chance you land in that condition and get stuck in a taylor series?
Find all posts by this user
Quote this message in a reply
01-03-2024, 08:10 PM
Post: #10
RE: Some TVM fun
And in part VI Plus42 is the only one that finds the negative root first. Is that because it is only going left?

One day I will understand some of this.
Find all posts by this user
Quote this message in a reply
01-03-2024, 08:28 PM
Post: #11
RE: Some TVM fun
(01-03-2024 08:04 PM)dm319 Wrote:  So I think you're saying that plus42 checks for tiny i at each iteration? And if it hits that, it gets into that taylor series?

Yes. But, think of taylor series is just a tool to get better Newton's correction, for tiny i.

Quote:And if you are 'coming from the left' there is an outside chance you land in that condition and get stuck in a taylor series?

It will never get stuck.

Because of one-sided convergence, next iteration is an improvement over previously.
If it over-shooted, it means either no solution, or rate iterations numerically converged.

core_commands9.cc int do_i_pct_yr() test
Code:
    if (f == 0 || (f > 0) != (f0 > 0))
        break;
    if (fabs(f) >= fabs(f0)) {
        if (i == i - eps / 1000) {
            i -= eps / 2;
            break;
        } else
            return ERR_NO_SOLUTION_FOUND;
    }
Find all posts by this user
Quote this message in a reply
01-03-2024, 09:36 PM (This post was last modified: 01-03-2024 11:15 PM by Albert Chan.)
Post: #12
RE: Some TVM fun
(01-03-2024 08:10 PM)dm319 Wrote:  And in part VI Plus42 is the only one that finds the negative root first. Is that because it is only going left?

TVM problems with 2 sign changes, we may have 2 rate solutions, from rate edges [PMT/-PV, PMT/FV]
Thomas wanted solver to always give one rate, and decided smallest absolute edge for rate guess.

Think of a huge loan, after some payments, with a small left-over (future value sign unknown)
It is reasonable to assume user wanted the rate close to future value = 0, and not the other.

Here is Part VI 2 rate roots, solved with Plus42 equations.
I keep this around, just in case I need the other rates, or both.

TVM:0=(1/EXPM1(N*LNP1(I))*(PV+0*PMT+FV)+PV+BEGIN*PMT)*I+PMT

XEQ "EQN", pick above. Set N=10, PV=50, PMT=-30, FV=80, BEGIN=0

If guess I = PMT/FV = -0.375, we get I ≈ −0.3689
If guess I = PMT/-PV = 0.600, we get I ≈ +0.5846      (*)

Note that solved rates are always inside rate edges.

(*) solver actually take 2 guesses. One stored previously, the other just entered.
To make sure only edge guess used, enter it twice! 0.6 [I] [ENTER] [I] [I] to solve
Find all posts by this user
Quote this message in a reply
Post Reply 




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