[WP34s] New TVM-solver version



#2

Hi,

I've made a new version of my TVM-solver:

http://www.hpmuseum.org/guest/fhub/tvm.zip

This new version is now also able to find both results when calculating the interest rate 'I' (if 2 solutions exist).

Although such double-solutions are not usual for real problems, but anyway - they're found now!

The costs of it were about 100 steps more ... :-(

Let me know if you discover any problems,

Franz

PS: Here's the thread where Dieter reported this problem (with 2 solutions for the interest rate) and posted a few examples:

http://www.hpmuseum.org/cgi-sys/cgiwrap/hpmuseum/archv021.cgi?read=234439#234439


Edited: 16 Dec 2012, 11:43 a.m.


#3

Is it still okay to include this new version as part of the 34S library in firmware? If so, I'll add it in.


- Pauli


#4

Quote:
Is it still okay to include this new version as part of the 34S library in firmware? If so, I'll add it in.

Yes of course - if you don't worry about the 100 steps more. ;-)

BTW, this new version has a bit different handling as the one currently included in the WP34s library. I made it from my later version which uses [A] as prefix key and then the 5 top-row keys [A]..[->] for the 5 financial variables. That's the way all usual financial calculators do it, and it's definitely more comfortable (and it displays the 5 keys in the alpha display).

Franz

Edited: 16 Dec 2012, 12:07 p.m.


#5

Quote:
BTW, this new version has a bit different handling as the one currently included in the WP34s library. I made it from my later version which uses [A] as prefix key and then the 5 top-row keys [A]..[->] for the 5 financial variables. That's the way all usual financial calculators do it, and it's definitely more comfortable (and it displays the 5 keys in the alpha display).

Hope you documented it properly.

d;-)


#6

Quote:
Hope you documented it properly.

Yes, as usual the doc is commented at the beginning of the file.

Franz


#7

Good boy!

d:-)


#8

Quote:
Good boy!

Well, to be really a 'good boy' I would have to write at least a 100+ pages PDF documentation (like your WP34s manual).

But I'm just too lazy for it. ;-)

Franz

#9

I forgot something:

Since I have only the emulator of the WP34s I would be curious how long such a interest rate calculation for 2 solutions need on a real WP34s.

If anyone has loaded the new TVM version above in a real WP34s, then it would be nice to execute the following calculation:

  N=10
PV=50
PMT=-30
FV=100
Then solve for 'I' and stop the time until the solutions are displayed.

Thanks,

Franz


#10

I have yet to try your application on my 34S. My 27S fails on your example ("many or no solutions").


#11

Quote:
I have yet to try your application on my 34S. My 27S fails on your example ("many or no solutions").

Yes Marcus, I've tried this on every existing (financial) HP calculator (or rather on their emulations), and all of them returned this error message as result.

But since Dieter said that such a behaviour won't be acceptable for a high-end calculator like the WP34s, I decided to finally solve this problem (although it never happens in reality). ;-)

Franz

#12

Quote:
My 27S fails on your example ("many or no solutions").

Yes, that's the easy way to, err... "solve" this problem. #-)

IMHO a decent solver should find out 1. whether no or two solutions exist, and 2. if there are two, what are their values.

Dieter

#13

What a coincidence. Over the last days I tried the one or other alternative approach to the TVM problem, and basically it boils down to the same ideas. ;-) While I did not transfer your program to a physical 34s (honestly, I do not know how to do that, i.e. move a .wp34s file to a physical 34s' RAM), I can provide some results of two similar solutions.

  • The previous TVM solver (in version 3.1 3325) throws an error after about one second. Usually the iteration for less critical cases takes about 2 - 5 seconds.
  • My first own attempt was a program that relies on the internal solver. For cases like the one above it determines the minimum/maximum m of the TVM function (by using the solver with the first derivative) and then finds the two roots on the interval ]-100%; m] resp. [m; 1000%]. In ALL mode, finding the extremum takes about four seconds, and an additional 2...3 s for each of the two solutions. This is what I also expect for your new version.
  • My second attempt employs a dedicated Newton-style solver. Depending on the four given values, it makes a kind of "smart guess" and then solves for i. This works much faster than the built-in SLV function. In this case, the first solution is displayed after one second, and the other one after another. Actually, most cases require not much more than a second before the interest rate is returned.
However, my current version still requires some additional work - it currently does not abort the iteration if no root is found (which on the other hand should not happen too often since most cases with no solution are sorted out before).

BTW - did you also try some cases with 0 < n < 1? On this interval things are a bit different.

Dieter


Edited: 16 Dec 2012, 5:18 p.m.


#14

Quote:
What a coincidence. Over the last days I tried the one or other alternative approach to the TVM problem, and basically it boils down to the same ideas. ;-)

Well, no coincidence at all - of course I've improved my program only because you complained about not finding such double-solutions. And of course splitting the interval into 2 parts by searching for the minimum/maximum is the logical way to do it. ;-)

Your method of using an own Newton-solver is an alternative (and maybe indeed faster) method than calling the integrated rootsolver, but IMO it's not worth the troubles (i.e. the effort to write a well working code and the quite big size of such a code). Such problems with 2 solutions occur so rarely that I won't worry about waiting a few seconds for the results.

Quote:
BTW - did you also try some cases with 0 < n < 1? On this interval things are a bit different.

Yes I know (I've even tried a few graphs with n<0), but I don't see any sense in decimal number for n at all, at least not when there are periodical payments (i.e. if PMT!=0).

Take the following example:

You pay 1000,-$ at the beginning and then every month (at the end) additional 100,-$. Now what meaning should the TVM value have for n=0.7 for example???

Since your payments are at the end of each period, you don't even have made a single 100,-$ payment at the time n=0.7 - so what?

What 'realistic' value does this TVM formula have for n=0.7 (or any other non-integer value)? Of course it's mathematically possible to use this TVM formula for such values of n, but it's of no real use.

And even if PMT=0 (i.e. you're only compounding a fixed PV), then the calculated value for n=1.7 (for example) has nothing to do with the reality, because for such fractional periods banks are using the so-called 'mixed' compounding (at least in Europe): exponential for full years, but linear for fractional years (at the end).

So to handle also this case the TVM solver would need a HUGE enhancement, and that's something I would consider for a PC program but not for a tiny calculator like the WP34s.

Ok, in the meantime I already know you a bit, so I'm quite sure that you'll certainly disagree with me again ... ;-)

Franz


Edited: 16 Dec 2012, 5:40 p.m.


#15

It's late, so let me add just two remarks:

Quote:
Your method of using an own Newton-solver is an alternative (and maybe indeed faster) method than calling the integrated rootsolver, but IMO it's not worth the troubles

It's actually less effort since this method does not rely on the position of an extremum - the whole process of finding its location can be omitted. Simply take two initial guesses derived from -1 and +infinity. In this case that's -30/100 and 30/50, i.e. -30 and +60%. Which is quite close to the actual solutions -28,4% and 58,2%.

Quote:
Ok, in the meantime I already know you a bit, so I'm quite sure that you'll certainly disagree with me again ... ;-)

I agree or disagree with opinions, not with persons. So don't take things too personally - it's just simple mathematics. ;-)

Dieter


#16

Quote:
It's actually less effort since this method does not rely on the position of an extremum - the whole process of finding its location can be omitted. Simply take two initial guesses derived from -1 and +infinity.

Unfortunately not in all cases. For some values the special form of the TVM graph (at least for the TVM formula that I use) can put the 2nd guess far away from your initial value (on the opposite side of the min/max), so also your method is not foolproof.

And if you check for such conditions, then it's not only a thing of coding the Newton formula.

Franz


Edited: 17 Dec 2012, 6:05 a.m.


#17

Quote:
Unfortunately not in all cases. For some values the special form of the TVM graph (at least for the TVM formula that I use) can put the 2nd guess far away from your initial value (on the opposite side of the min/max).

Please give an example.
Quote:
so also your method is not foolproof

Things only have to be foolproof if used by fools. 8-)

Dieter

#18

Quote:
it's just simple mathematics. ;-)

I wouldn't quite call it that or anyone would be writing robust TVM solvers easily.

The fact that Professor Willian Kahan's carefully constructed algorithms for the 12C fail in some cases, is to me at least, an indication that this is not even a remotely simple problem to solve correctly. I would even go as far as to put a part of the 12c amazing longevity down to the simple fact that it was producing better answers than the competition when introduced (and still today I suspect).


- Pauli

#19

Quote:
I did not transfer your program to a physical 34s (honestly, I do not know how to do that, i.e. move a .wp34s file to a physical 34s' RAM)

Here it goes:

  1. Create a simple text file in the emulator's default directory with the name of the COM port in it.
  2. On the emulator use CAT to navigate to the TVM application in the library and press ENTER.
  3. Connect the PC and your WP 34S with the programming cable.
  4. On the calculator execute RECV (in P.FCN).
  5. On the emulator execute PSEND (in P.FCN). The calculator should show "Program" in its display.
  6. Execute PSTO (in P.FCN) to add the program to the end of the library. The old copy will be deleted automatically.

#20

Two remarks AFAIK:

0. Make sure the program you want to transfer is part of the library on the emulator. See the assembler manual for this step.

3. should read: Connect the computer and your WP 34S with a proper cable (see p.147).

d:-)

#21

Quote:
1. Create a simple text file in the emulator's default directory with the name of the COM port in it.

What's the name of that file?
Quote:
2. On the emulator use CAT to navigate to the TVM application in the library and press ENTER.

3. Connect the PC and your WP 34S with the programming cable.
4. On the calculator execute RECV (in P.FCN).

5. On the emulator execute PSEND (in P.FCN). The calculator should show "Program" in its display.

6. Execute PSTO (in P.FCN) to add the program to the end of the library. The old copy will be deleted automatically.


I assume these are the steps to get a program from the emulator to a physical 34s. Now, all I have is a file named tvm.wp34s - how do I get that to a (physical) 34s? I am able to assemble this file to a .dat file, but I am not sure about how to move this to the emulator (library? RAM?) or "the real thing".

Dieter


#22

Quote:
Quote:
1. Create a simple text file in the emulator's default directory with the name of the COM port in it.

What's the name of that file?

See p. 166 of the v3.1 manual.

d:-)

#23

wp34s.ini. Sorry for not mentioning it.

If you look at the SVN repository, you'll see a .cmd file in the top directory that I'm using to build the default library. Pauli has already done that so you can just take the file wp34s.lib in the library directory (latest build in the old repository).


#24

Quote:
(latest build in the old repository).

Old repository? Is there a new one? Could we have the link? ;-)

Franz


#25

Main source forge page, click on Code tab. Instructions follow.

This isn't something we really wanted to do, but SF is forcing a change over in the coming months so I got in early to give us time to iron out issues.


- Pauli


#26

Quote:
Main source forge page, click on Code tab. Instructions follow.

This isn't something we really wanted to do, but SF is forcing a change over in the coming months so I got in early to give us time to iron out issues.


Thanks, found it. You're right, looks a bit confusing when used to the old SVN.

But I even found your new WP43s site, although it's still in a prenatal state ... ;-)

Franz

#27

Quote:
Pauli has already done that so you can just take the file wp34s.lib in the library directory ...

Almost. The file is called
wp34s-lib.dat and ist found using that link.

d:-)


#28

Quote:
Almost. The file is called wp34s-lib.dat and ist found using that link.

Sorry for mixing it up again. I must have been a bit overworked when I tried to answer the question.

#29

Quote:
Then solve for 'I' and stop the time until the solutions are displayed.

The new version runs for about ten seconds before both solutions are returned.

However, I also observed a problem that may not be related to your code (see my other post a few minutes earlier). In this case the calculation starts with the RCL indicator continuously on, after about eight seconds RESET appears, and after that [R/S] returns the two solutions in another eight or nine seconds.

Dieter

#30

Built in the old respoistory as version 3335.

The new respository (which we're being forced into using anyway soon) doesn't have build machine support yet.


- Pauli


#31

Quote:
Built in the old respoistory as version 3335.

A small problem: you've put the old manual (v2.2) into the current version.

Franz


#32

Nice catch. Looks like a problem with the build process and recent changes. Or something I did that was silly -- insomniac commits before 4am aren't sensible.


- Pauli


#33

There shouldn't be a manual in the bin directory in the repository. The current manual can be copied there locally under the name wp34s_manual.pdf to support the help menu of the emulator but the file shouldn't be committed.

I've deleted the stray copy in the repository.


#34

I think Walter added it a short while back.
I didn't realise it would cause hassles and you mustn't have noticed.

- Pauli


#35

Agree (except on "mustn't").

d;-)

#36

New version 3 of the TVM solver:

http://www.hpmuseum.org/guest/fhub/tvm.zip

Now solving for 'I' also works for negative periods N<0 (whatever this is good for ;-)).

Nevertheless the program is a few steps shorter now (and a bit faster when solving for 'I') due to a few code optimizations.

The handling did not change at all.

Franz

Edited: 26 Dec 2012, 5:48 a.m. after one or more responses were posted


#37

I've committed the new code but haven't built anything.


- Pauli


#38

Isn't it already Dec 22 in Australia?

And Australia still exists??? ;-)

Franz


#39

It is 23:18 here on the 21st. Other parts of Australia have reached the 22nd and some parts are further behind still.


- Pauli


#40

Quote:
It is 23:18 here on the 21st.

Aaah, then enjoy your last minutes! :-)

Franz


#41

Franz, you mixed it up: that was a prophecy for Austria, not Australia (to our friends in GOC: yes, there is a difference, even a pretty large one)!

d:-)

#42

I the meantime I managed to transfer the new TVM solver to my hardware 34s where it now replaces the former version in the library. So I tried the following case:

  n   =   5
PV = 40
PMT = -30
FV = 100
The 34s requires nine seconds to return the two solutions:
  I1 = -9,321
I2 = 46,892
I observed two problems:

First, the iteration sometimes does not seem to terminate. This possibly is not a problem in the TVM routine but either in the internal solver or maybe even the 34s firmware itself. After starting the calculation with [A] [B] the RCL indicator in the display starts flashing very slowly in irregular intervals: the indicator may come on for a second or two, then it disappears for another two seconds, comes back again, etc. In many cases this evetually led to a RESET message. After R/S the program then usually finishes within a few seconds (with the RCL indicator flashing as usual at about 1 Hz), as if nothing had happened at all. Pauli, Marcus?

And finally, after pressing [B] the iteration starts, but the display continues to show its N I PV PMT FV message. Except the flashing RCL indicator there is no visible feedback to the user, telling him that a calculation is in progress. I would like to see something like "Solving..." while the interest rate is being calculated.

BTW: In the current version, the [A] key is used to enter values as well as to start their calculation. What about two keys, say [A] and [B] - one for storing values and the other to have them calculated? Like a [SET] and [COMPUTE] button, similar to what is found on other brand calculators. This way we would also get rid of the cumbersome ENTRY? test and the one or other additional [R/S] to clear the entry flag.

Dieter


#43

I fear I've broken your hardware. :-( I'll have to test with one of my devices if the "reset" phenomenon is the same.

#44

Hi Dieter!

Quote:
The 34s requires nine seconds to return the two solutions:

Ooops, that's indeed a bit long! :-(

But this only if there are 2 solutions, for usual TVM problems with 1 solution the time should be less than 3 seconds, because only 1 instead of 3 SLV calls are needed, and I would say that this is acceptable.
Quote:
I observed two problems:

Well, I'm absolutely sure that it's not the fault of my program and I'm quite sure that it's also not the SLV routine. I'm calling SLV only when the interval of the 2 initial guesses has different signs on both ends, so the solver should always find a solution. And in fact in 99.9% of all examples I've tried the solver returns this solution - only in very rare cases (e.g. for interest rates < -99.9%) sometimes it doesn't find a solution. The reason is that for such unrealistic interest values the TVM expression may have really huge values (e.g. 10^30), and then SLV fails. But such I-values are clearly out of the valid range.

I rather guess there's a problem with your firmware build, but let's wait what Marcus gets on his WP34s.
Quote:
And finally, after pressing [B] the iteration starts, but the display continues to show its N I PV PMT FV message. Except the flashing RCL indicator there is no visible feedback to the user, telling him that a calculation is in progress. I would like to see something like "Solving..." while the interest rate is being calculated.

Yes, that may indeed be a good idea. The problem is that I only have the emulator, so solving for 'I' is done in a fraction of a second and for me this message disappears immediately

What would you prefer, just delete this 'N I PV PMT FV' display or really showing 'Solving..' (which would require more bytes of course)?

Quote:
BTW: In the current version, the [A] key is used to enter values as well as to start their calculation. What about two keys, say [A] and [B] - one for storing values and the other to have them calculated? Like a [SET] and [COMPUTE] button, similar to what is found on other brand calculators. This way we would also get rid of the cumbersome ENTRY? test and the one or other additional [R/S] to clear the entry flag.

That's an idea which I won't like very much. AFAIK all financial HP calculators work exactly this way, i.e. if you've entered a value before pressing those keys the value is stored, else it is calculated. I would say that's the most simple method, and shouldn't the WP34s be as compatible as possible to all other HP calculators?

BTW, if you've entered anything previously and then want to calculate any of the 5 TVM values, there's no need to press [R/S] before - you can just press [A][?] twice: the 1st press stores the value, but the 2nd press calculates it.

Franz


Edited: 25 Dec 2012, 4:17 p.m.


#45

I've now implemented this 'Solving...' message when the SLV iteration is used to calculate the interest rate 'I'.

Since this is only necessary on a 'real' WP34s (on the emulator this iteration is too fast for such a message!), I've now included 2 versions of the TVM-solver:

TVM.wp34s is for the real calc (WITH this 'Solving...' message)

TVM_emu.wp34s is for the emulator (no such message).

Additionally I've found a small error in one of my formulas (derivative of TVM for i=0), so of course also this bug (although it would probably never occur) is fixed in the new version:

http://www.hpmuseum.org/guest/fhub/tvm.zip

Franz


Possibly Related Threads...
Thread Author Replies Views Last Post
  hp-prime solver and variable name fabrice48 22 1,745 12-10-2013, 03:25 AM
Last Post: fabrice48
  HP Prime Triangle solver BruceH 29 1,810 11-28-2013, 12:03 AM
Last Post: Dale Reed
  Using units in Numeric Solver Harold A Climer 1 249 10-13-2013, 10:44 AM
Last Post: Tim Wessman
  Does Prime Have a Multiple Equation Solver? Norman Dziedzic 2 277 09-20-2013, 09:43 AM
Last Post: Norman Dziedzic
  TVM again ;-) fhub 17 1,071 09-02-2013, 11:03 AM
Last Post: fhub
  New version of WP34s iOS emulator pascal_meheut 4 452 07-22-2013, 03:55 PM
Last Post: Matt Agajanian
  Just a lazy solver algortihm PGILLET 1 260 06-28-2013, 11:47 PM
Last Post: Namir
  TVM WP34s trouble Jim P 4 451 06-28-2013, 07:31 AM
Last Post: fhub
  New version of the WP34s iOS emulator pascal_meheut 15 960 04-23-2013, 01:58 AM
Last Post: Walter B
  [43s] : How the solver will be implemented Miguel Toro 3 367 03-14-2013, 06:09 PM
Last Post: Walter B

Forum Jump: