Post Reply 
Request TVM formula guidance
12-11-2014, 10:02 AM
Post: #5
RE: Request TVM formula guidance
(12-03-2014 09:17 PM)Dieter Wrote:  [quote='iMatt04bit' pid='22217' dateline='1417498436']
The results appear precise and a match to a variety of real and simulated calculators.

Quote:Even with very small values of i ?-)

It works with an interest value entry of 0.005/12, and up to 999/12.
The very small value is not solving as well as I want, though still accurate, but normal values do solve really well. I still get results that match the hp12c and hp20b for all values, so I feel okay with the code so far. It should solve to the maximum of display characters as input - still have to test it out to see how well that works.

I still have more scenarios to run through the code to be certain that it is covering all the conditions. You should see the spreadsheet that I have with all the TVM scenarios and much of where I worked out the i guesser code formulas.

Quote:Some HP calculators use special functions like ln(1+x) or exp(x)-1 to ensure exact results even under these circumstances. Have you implemented something similar in Java? What's the working precision? Standard double precision, i.e. approx. 15 decimal digits?


The precision is exact in all tested cases to the hp12c emulator I am using, and 1e-10 almost always vs the hp20b emulator, except a rare occasional 1e-9 difference.
Java seems to be producing 1e-15 precision, but I really don't have a method to test it fully. I do have logarithms, Eulers, all kinds of good intrinsic math formulas in Java. I am very interested in applying the two functions you mention.

(12-02-2014 05:33 AM)iMatt04bit Wrote:  I just need to know if I have a correct derivative. ...if only I had taken Calculus...

Quote:If you didn't there is always Wolfram Alpha. ;-)

I checked that out (thanks!), its really nice site.
I also found one that solves and gives a tutorial at calculus-calculator.com

(12-02-2014 05:33 AM)iMatt04bit Wrote:  // formula from the hp12c manual, solves to 0 for matched I converted to R, used below
f(x) = PV + (1+R*K) * PMT * ((1-(1+R)^-N)/R) + FV * (1+R)^-N

Quote:I assume you mean f(R), not f(x).

Yes, as in R being the mutating value of i/100, fed into the formulas for fx and fx'

(12-02-2014 05:33 AM)iMatt04bit Wrote:  // the derivative as best I know
f'(x) = PV + (1+R*K) * PMT * [1/N * (1-(1+R)^-(N-1)) / (1/R)] + FV * [1/N * (1+R)^-(N-1)]

Hmmm... not quite. ;-)

I found the real one and its working well.
Its really odd how that version and similar others were producing reasonable results.
At times I think it had better results than the real thing.
But it seemed to be confined to particular ranges and I wanted more.

(12-02-2014 05:33 AM)iMatt04bit Wrote:  // compute for +I
I = I - f(x)/f'(x)

// or for -I
I = I + f(x)/f'(x)

Quote:Could you explain this a bit more in detail?
And what about cases with, say, two different positive solutions?
What is the initial guess for i, what value does the iteration start with?
How do you handle cases where no solution exists?

I have since moved to just using one of those formulas.
I was having a bad sign day...

These functions are inside a loop to form the complete function of the Newton method.

The fx function is the HP TVM formula that solves to zero, is in a variable named fxA.
The fx' function which has this massively long derivative formula, is in a variable named fxB.

Both functions receive the input of R, which at first is the seed value (aka guess of i), in the form R = x/100, where x is the i value in the loop.

Next these function results are computed into the evolving version of i, in variable x.
So next: x = x - fxA/fxB;

When the loop cycles again the x value feeds into R, which in turn feeds into fxA,fxB.
As in: R = x/100;

I really hope this is the right way, it seemed so from examples I have reviewed.
I still think it takes far too many iterations to solve.
The partial derivatives that I was using solved much faster.
But still the same format of x = x - fxA/fxB was in use.
Do you know how fast this should solve?
A rough estimate of expected iterations?

For an initial value/guess of i, there is a set of conditional code statements which calculates an estimate depending on variations of the TVM inputs of PV,PMT,FV and if all are non-zero, or if one of them is zero, and also per signs. The estimates are within 0.5 of the solution at times, and aimed slightly above the expected value of i.
I also use a rough estimater that simply walks up the powers of 10 from 1 to 9e+10 and tests for a good match using the formula assigned to variable fxA. This involves multiple stepped loops which do not iterate through every digit until refining the value.

I am solving both pos/neg double roots - found an example in the WP34S archives.
I look for a condition where a double root is expected, like (PV > 0.0) && (PMT < 0.0) && (FV > 0.0), and then I compare multiple guess values, where if there are different signs, then both pos/neg values are queued for the Newton solver. It also catches conditions where a lower value of i will also solve. So both types of roots are covered.

When no solution is found, a value of zero is returned.
Best idea for an error code that I have at the moment since i != 0 is expected.

(12-02-2014 05:33 AM)iMatt04bit Wrote:  // breakpoint on difference of prior computed I and current I is <= 5e-12 for fractional I | I < 1 percent
// or computed I <= 5e-15 for I | I >= 1 percent
Quote:Sorry, I do not quite understand what you want to say here.

I save the prior value of computed interest, payment, and the result of the TVM formula in fxA. The differences of these values are compared on each iteration. When little or no difference it is time to break out of the loop.

I print every iteration to get a visual on how well the solver is working.
The values adf, idf, pdf : are differences of prior and current values.
adf : prior fxA and current fxA
idf : prior/cur interest
pdf : prior/cur pmt
dfPMT : computed pmt vs actual PMT
x : permutating interest
fxA : TVM formula which should resolve to 0.00 under ideal conditions
ct : certain near exit conditions engage a counter as a backup loop exit method



Code:

Using: N: 10  PV: 50.00  PMT: -30.00  FV: 400.00
Using i guess of: 60.200000000000000
   1)  fxA: 42.065379721231366  fxB: 14.581248976719598  x: 57.315104488758620  pmt: -32.555287548788040  PMT: -30.00
         ct:  0   adf: 55.796304578028526  idf: 2.884895511241382  pdf: 32.555287548788040  dfPMT: 2.555287548788037
   2)  fxA: 25.304006313140775  fxB: 14.691976764111903  x: 55.592803486013310  pmt: -31.466094515010040  PMT: -30.00
         ct:  0   adf: 16.761373408090590  idf: 1.722301002745311  pdf: 1.089193033777995  dfPMT: 1.466094515010042
   3)  fxA: 14.953452283311517  fxB: 14.723078734743643  x: 54.577156381714110  pmt: -30.841422907263926  PMT: -30.00
         ct:  0   adf: 10.350554029829258  idf: 1.015647104299198  pdf: 0.624671607746116  dfPMT: 0.841422907263926
// snipped
  56)  fxA: -0.000000000001847  fxB: -41.025090567119340  x: 14.435871328079948  pmt: -29.999999999999964  PMT: -30.00
         ct:  5   adf: 0.000000000002274  idf: 0.000000000000044  pdf: 0.000000000000050  dfPMT: 0.000000000000036
  57)  fxA: -0.000000000000426  fxB: -41.025090567119560  x: 14.435871328079937  pmt: -29.999999999999993  PMT: -30.00
         ct:  5   adf: 0.000000000001421  idf: 0.000000000000011  pdf: 0.000000000000028  dfPMT: 0.000000000000007
Break countdown (5) on abs(fxA) < 1e-12 : -0.000000000000426
I is: 53.172213268384730,14.435871328079937

Quote:The usual exit condition for the iteration loop is a relative (!) error near the working precision, i.e. here 1E-15. Due to numeric pitfalls this may not occur under some circumstances, so I often use another idea: Since approach Newton's method converges roughly quadratically, the idea is to quit as soon as a relative error as small as, say, 1E-10 is reached. Assuming quadratic convergence, this means that the next iteration would have an error as low as 1E-20 and thus the approximation of i would not change — so you're done and the result is fine. Just an idea...

Quote:Finally, here are two links that may be helpful:

I have a set of variables where the difference of the prior value and current value is compared. This is done for computed values of pmt, i, fxA, and a difference of pmt vs PMT, where pmt is computed in the loop using the permutating i, and PMT is the TVM value received by the solver function. I have been able to use fairly extreme cutoffs of 1e-15 for many, but have some backup solutions for where they may fail. Still testing things, but I can break out on the solve of 0.005/12, so they seem to be ok.

Quote:First you may take a look at the way the TVM equation is solved in the HP41 standard application pac. The code can be found in the HP41 software section. Hint: R07 holds 1+i%/100 and R09 stores i%/100.

I'll have to look at that to see how it is used, it looks like good reading.

Quote:And finally, as you might have expected, the TVM issue has been discussed numerous time before. Maybe a look at this thread in the old forum may provide some useful information.

I think I have been everywhere and now have a nice collection of documents and manuals. Did a lot of reading online about algorithms for solving for root. There is not enough info regarding 5th degree polynomials though. ; (

Quote:Dieter

Thanks for the info, much appreciated, Matt
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
Request TVM formula guidance - iMatt04bit - 12-02-2014, 05:33 AM
RE: Request TVM formula guidance - Dieter - 12-03-2014, 09:17 PM
RE: Request TVM formula guidance - iMatt04bit - 12-11-2014 10:02 AM
RE: Request TVM formula guidance - Dieter - 12-11-2014, 06:36 PM
RE: Request TVM formula guidance - Dieter - 12-14-2014, 04:24 PM
RE: Request TVM formula guidance - Dieter - 12-17-2014, 01:21 PM
RE: Request TVM formula guidance - Dieter - 12-21-2014, 06:14 PM
RE: Request TVM formula guidance - Dieter - 12-25-2014, 12:42 PM
RE: Request TVM formula guidance - Dieter - 12-29-2014, 07:47 PM



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