Post Reply 
A formula for IRR
05-18-2015, 07:49 PM (This post was last modified: 05-18-2015 07:50 PM by salvomic.)
Post: #21
RE: A formula for IRR
abstract:

Home type update the flows list, but doesn't calculate some flows lists (i.e if they have two results, negative and positive)

Code:

EXPORT IRR()
// Thanks Cyrille, Akmon, Dale, salvomic
BEGIN
EDITLIST(flows);
RETURN fsolve(ΣLIST(MAKELIST((flows(I)/X^(I-1)),I,1,SIZE(flows))),X)-1;
END;

CAS type gives correct results with all lists (almost all), but it doesn't update the flows list

Code:

#cas
irr2():=
BEGIN
EDITLIST(flows);
fsolve(ΣLIST(MAKELIST((flows(I)/X^(I-1)),I,1,SIZE(flows))),X)-1;
END;
#end


In both of it we need to define first flows list, otherwise we get "Error: Bad argument type":
flows:={..., ..., ...}

I wish to have only one, *complete*, program, please, help! Smile

Salvo

∫aL√0mic (IT9CLU) :: HP Prime 50g 41CX 71b 42s 39s 35s 12C 15C - DM42, DM41X - WP34s Prime Soft. Lib
Visit this user's website Find all posts by this user
Quote this message in a reply
05-18-2015, 07:51 PM
Post: #22
RE: A formula for IRR
With this code:

Code:
EXPORT IRR()
BEGIN    
   return fsolve(ΣLIST(MAKELIST((L1(I)/X^(I-1)),I,1,SIZE(L1))),X)-1; //  (Original command line from Akmon)
END;

and your 10 numbers list,
I get 0,13917 in home and CAS mode, directly.
Find all posts by this user
Quote this message in a reply
05-18-2015, 07:55 PM
Post: #23
RE: A formula for IRR
(05-18-2015 07:51 PM)akmon Wrote:  With this code:

Code:
EXPORT IRR()
BEGIN    
   return fsolve(ΣLIST(MAKELIST((L1(I)/X^(I-1)),I,1,SIZE(L1))),X)-1; //  (Original command line from Akmon)
END;
and your 10 numbers list,
I get 0,13917 in home and CAS mode, directly.

should be 11: {-79000, 14000, 11000, 10000, 10000, 10000, 9100, 9000, 4500, 100000}
I get result only with #CAS version, not with the above code, but I'll try again...

∫aL√0mic (IT9CLU) :: HP Prime 50g 41CX 71b 42s 39s 35s 12C 15C - DM42, DM41X - WP34s Prime Soft. Lib
Visit this user's website Find all posts by this user
Quote this message in a reply
05-18-2015, 09:18 PM (This post was last modified: 05-18-2015 09:28 PM by salvomic.)
Post: #24
RE: A formula for IRR
a dirty solution toward the final version...

This program (Home HPPL) recall a #CAS function and present it in Terminal (so when there is a series of cash flows that the "Home" part doesn't handle, the result(s) appear(s) on terminal, while program gives error), and, at the same point, it's possible to edit the list (flows).

Welcome for better solutions!

Code:

EXPORT IRR()
// Thanks Cyrille, Akmon, Dale, salvomic
// cash flow in {flow} list
BEGIN
local mesg;
EDITLIST(flows);
mesg:= irr2();
PRINT(mesg);
RETURN fsolve(ΣLIST(MAKELIST((flows(I)/X^(I-1)),I,1,SIZE(flows))),X)-1;
END;

#cas
irr2():=
BEGIN
fsolve(ΣLIST(MAKELIST((flows(I)/X^(I-1)),I,1,SIZE(flows))),X)-1;
END;
#end

EDIT: If I use RETURN to return directly the result of #CAS function I get something like:
program({},{},[-1.0171... 0.138654...])
a very "ugly" output!

∫aL√0mic (IT9CLU) :: HP Prime 50g 41CX 71b 42s 39s 35s 12C 15C - DM42, DM41X - WP34s Prime Soft. Lib
Visit this user's website Find all posts by this user
Quote this message in a reply
05-18-2015, 10:15 PM (This post was last modified: 05-18-2015 10:33 PM by akmon.)
Post: #25
RE: A formula for IRR
(05-18-2015 07:55 PM)salvomic Wrote:  
(05-18-2015 07:51 PM)akmon Wrote:  With this code:

Code:
EXPORT IRR()
BEGIN    
   return fsolve(ΣLIST(MAKELIST((L1(I)/X^(I-1)),I,1,SIZE(L1))),X)-1; //  (Original command line from Akmon)
END;
and your 10 numbers list,
I get 0,13917 in home and CAS mode, directly.

should be 11: {-79000, 14000, 11000, 10000, 10000, 10000, 9100, 9000, 4500, 100000}
I get result only with #CAS version, not with the above code, but I'll try again...


I´ve discovered something interesting.
If the number of flows is par, the program gets the solution with no problem. however if the number of flows is impar it cannot solve. For that reason I can solve the first examples (4, 6 flows) but cannot solve 11, 13, or 9 flows. The question is: why?

EDIT: use solve instead fsolve. It works better, anyway the previous question is still curious.
Find all posts by this user
Quote this message in a reply
05-18-2015, 10:30 PM
Post: #26
RE: A formula for IRR
I found this page about IRR-- maybe helps?
http://www.thecalculatorstore.com/epages...D=25612040
Find all posts by this user
Quote this message in a reply
05-18-2015, 11:07 PM (This post was last modified: 05-18-2015 11:08 PM by akmon.)
Post: #27
RE: A formula for IRR
A prettier input:

Code:
EXPORT TIR()
BEGIN
sto(EDITLIST(L1,"TASA INTERNA DE RETORNO"),L1);
SOLVE(ΣLIST(MAKELIST((L1(I)/X^(I-1)),I,1,SIZE(L1))),X)-1;
END;
Find all posts by this user
Quote this message in a reply
05-19-2015, 08:58 AM (This post was last modified: 05-19-2015 10:27 AM by salvomic.)
Post: #28
RE: A formula for IRR
(05-18-2015 10:30 PM)Alfon Wrote:  I found this page about IRR-- maybe helps?
http://www.thecalculatorstore.com/epages...D=25612040

I'm seeing it, Alfon, but the formula in there seems to me a bit ...short :-), however I'll try it, this evening.
± is the same as Cyrille, akmon and me are discussion here...

(05-18-2015 11:07 PM)akmon Wrote:  A prettier input:

Code:
EXPORT TIR()
BEGIN
sto(EDITLIST(L1,"TASA INTERNA DE RETORNO"),L1);
SOLVE(ΣLIST(MAKELIST((L1(I)/X^(I-1)),I,1,SIZE(L1))),X)-1;
END;

I'll try it this evening too.
Do you already tested it also with 11 items list?
Our two problem are: solve (or fsolve) that work always in Home and CAS; editing list and create it if not exist (I'd use flows for this purpose)...

Could SOLVE resolve the issue of fsolve() (works well only in CAS)?

EDIT: for now I'm trying (as Cyrille suggests) with solve() and not fsolve() and it works better also with the "11 element list" (2 results, but the first, the negative one has -1 added...), however in CAS Terminal gives the message "Warning, argument is not an equation, solving ...", in Home no message.
Better, however!


Salvo

∫aL√0mic (IT9CLU) :: HP Prime 50g 41CX 71b 42s 39s 35s 12C 15C - DM42, DM41X - WP34s Prime Soft. Lib
Visit this user's website Find all posts by this user
Quote this message in a reply
05-19-2015, 10:32 AM
Post: #29
RE: A formula for IRR
(05-19-2015 08:58 AM)salvomic Wrote:  
(05-18-2015 10:30 PM)Alfon Wrote:  I found this page about IRR-- maybe helps?
http://www.thecalculatorstore.com/epages...D=25612040

I'm seeing it, Alfon, but the formula in there seems to me a bit ...short :-), however I'll try it, this evening.
± is the same as Cyrille, akmon and me are discussion here...

(05-18-2015 11:07 PM)akmon Wrote:  A prettier input:

Code:
EXPORT TIR()
BEGIN
sto(EDITLIST(L1,"TASA INTERNA DE RETORNO"),L1);
SOLVE(ΣLIST(MAKELIST((L1(I)/X^(I-1)),I,1,SIZE(L1))),X)-1;
END;

I'll try it this evening too.
Do you already tested it also with 11 items list?
Our two problem are: solve (or fsolve) that work always in Home and CAS; editing list and create it if not exist (I'd use flows for this purpose)...

Could SOLVE resolve the issue of fsolve() (works well only in CAS)?

EDIT: for now I'm trying (as Cyrille suggests) with solve() and not fsolve() and it works better also with the "11 element list" (2 results, but the first, the negative one has -1 added...), however in CAS Terminal gives the message "Warning, argument is not an equation, solving ...", in Home no message.
Better, however!


Salvo

Yes, I tested with 11, 17, 19 and even 31 flows. It works flawless. It works on Home or CAS mode.
Anyway it´s strange that on emulator the program give [2 results] and on my phisical calculator only one, the right one. The code is exactly the same and Home and CAS settings are exactly the same. How strange.
Find all posts by this user
Quote this message in a reply
05-19-2015, 11:28 AM
Post: #30
RE: A formula for IRR
The code list contains an upper case SOLVE(), instead of the lower case solve(), which results in a syntax error, as posted.
Find all posts by this user
Quote this message in a reply
05-19-2015, 11:43 AM
Post: #31
RE: A formula for IRR
(05-19-2015 11:28 AM)DrD Wrote:  The code list contains an upper case SOLVE(), instead of the lower case solve(), which results in a syntax error, as posted.

If you use Solve.SOLVE() as recommended by Cyrille here it works fine both in Home and CAS:

Code:
EXPORT IRR()
BEGIN
    sto(EDITLIST(L1,"Internal Rate of Return"),L1);
    Solve.SOLVE(ΣLIST(MAKELIST((L1(I)/X^(I-1)),I,1,SIZE(L1))),X)-1;
END;
Find all posts by this user
Quote this message in a reply
05-19-2015, 12:05 PM
Post: #32
RE: A formula for IRR
I'm not seeing the benefit of this:
sto(EDITLIST(L1,"Internal Rate of Return"),L1);
over just this:
EDITLIST(L1,"Internal Rate of Return");

EDITLIST(L1,"Internal Rate of Return"); which saves changes to L1, and sto(EDITLIST(L1,"Internal Rate of Return"),L1); just stores those same results again.

Am I missing something on that?
Find all posts by this user
Quote this message in a reply
05-19-2015, 12:10 PM
Post: #33
RE: A formula for IRR
(05-19-2015 11:43 AM)Didier Lachieze Wrote:  [quote='DrD' pid='35550' dateline='1432034883']
The code list contains an upper case SOLVE(), instead of the lower case solve(), which results in a syntax error, as posted.

If you use Solve.SOLVE() as recommended by Cyrille here it works fine both in Home and CAS:

solve.SOLVE() results in syntax error also. Compiler doesn't like the upper case SOLVE.
Find all posts by this user
Quote this message in a reply
05-19-2015, 12:27 PM
Post: #34
RE: A formula for IRR
(05-19-2015 12:05 PM)DrD Wrote:  I'm not seeing the benefit of this:
sto(EDITLIST(L1,"Internal Rate of Return"),L1);
over just this:
EDITLIST(L1,"Internal Rate of Return");

EDITLIST(L1,"Internal Rate of Return"); which saves changes to L1, and sto(EDITLIST(L1,"Internal Rate of Return"),L1); just stores those same results again.

Am I missing something on that?

Aaah, ok, I didn´t know EDITLIST autostores the list on the same variable when OK is pressed. Thank you for the info. Less code. ;-)
Find all posts by this user
Quote this message in a reply
05-19-2015, 12:33 PM (This post was last modified: 05-19-2015 12:36 PM by salvomic.)
Post: #35
RE: A formula for IRR
(05-19-2015 12:10 PM)DrD Wrote:  solve.SOLVE() results in syntax error also. Compiler doesn't like the upper case SOLVE.

hi Dale, I'm outside (but with Prime with me), I tried "at fly" this
solve(ΣLIST(MAKELIST((L1(I)/X^(I-1)),I,1,SIZE(L1))),X)-1;
solve() all lowercase.
It works fine in Home and in CAS (but here it gives a warning, see above), giving one or more results (IRR ca have more, at least one positive,...)
This evening I'll try better, but perhaps it works. Period :-)

(05-19-2015 12:27 PM)akmon Wrote:  EDITLIST(L1,"Internal Rate of Return"); which saves changes to L1, and sto(EDITLIST(L1,"Internal Rate of Return"),L1); just stores those same results again.
...

Aaah, ok, I didn´t know EDITLIST autostores the list on the same variable when OK is pressed. Thank you for the info. Less code. ;-)
[/quote]

yse, Editlist works, but only in Home (in CAS it is been opened but any changes will be lose, I don't know why, exactly...).

Evening, at home, I'll try writing a better version Smile

...but remain a problem: EDITLIST edits and save the list, but if the list doesn't already exists, the program gives error, so we need a control to see if the variable exists, like the "C" if (!flows) ...

Salvo

∫aL√0mic (IT9CLU) :: HP Prime 50g 41CX 71b 42s 39s 35s 12C 15C - DM42, DM41X - WP34s Prime Soft. Lib
Visit this user's website Find all posts by this user
Quote this message in a reply
05-19-2015, 12:45 PM
Post: #36
RE: A formula for IRR
I use SOLVE and no problem in CAS or HOME, with my physical calculator.
Also the list autostores the values in CAS and HOME without sto command.
And I don´t see any problem if the list doesn´t exists. L1 always exists, with 0 items but it exists. Then you enter the flows and that´s all. Try this again:

Code:
EXPORT TIR()
BEGIN
EDITLIST(L1,"TASA INTERNA DE RETORNO");
SOLVE(ΣLIST(MAKELIST((L1(I)/X^(I-1)),I,1,SIZE(L1))),X)-1;
END;

Erase all in L1 and start from de beginning. Tha program starts with EDITLIST menu, ready for input data.
Find all posts by this user
Quote this message in a reply
05-19-2015, 12:49 PM
Post: #37
RE: A formula for IRR
Since L1 is a reserved variable, it always exists. It may be an empty list, but it shouldn't give an error when referenced. For example, set L1 to an empty list, L1:={}; then run this program:

Code:

EXPORT IRR()
BEGIN
  EDITLIST(L1,"Internal Rate of Return");  // test vals: {-123400, 36200, 54800, 48100} 
  RETURN solve(ΣLIST(MAKELIST((L1(I)/X^(I-1)),I,1,SIZE(L1))),X)-1;
END;

It seems to run ok. EDITLIST() has optional parameters, which may be useful, but the above code is a very handy program for the purpose.
Find all posts by this user
Quote this message in a reply
05-19-2015, 01:02 PM
Post: #38
RE: A formula for IRR
(05-19-2015 12:49 PM)DrD Wrote:  Since L1 is a reserved variable, it always exists. It may be an empty list, but it shouldn't give an error when referenced. For example, set L1 to an empty list, L1:={}; then run this program:

Code:

EXPORT IRR()
BEGIN
  EDITLIST(L1,"Internal Rate of Return");  // test vals: {-123400, 36200, 54800, 48100} 
  RETURN solve(ΣLIST(MAKELIST((L1(I)/X^(I-1)),I,1,SIZE(L1))),X)-1;
END;

It seems to run ok. EDITLIST() has optional parameters, which may be useful, but the above code is a very handy program for the purpose.
It run ok from Home but not from CAS. If you replace "solve" by "Solve.SOLVE" (with an uppercase 'S') it works fine from both.
Find all posts by this user
Quote this message in a reply
05-19-2015, 01:22 PM (This post was last modified: 05-20-2015 09:26 PM by salvomic.)
Post: #39
RE: A formula for IRR
(05-19-2015 12:45 PM)akmon Wrote:  I use SOLVE and no problem in CAS or HOME, with my physical calculator.
Also the list autostores the values in CAS and HOME without sto command.
And I don´t see any problem if the list doesn´t exists. L1 always exists, with 0 items but it exists. Then you enter the flows and that´s all. Try this again:
...

yes, you're right. I'd prefer to use "flows" but L1 could be good also and it solves the problem of non existing list!

(05-19-2015 12:49 PM)DrD Wrote:  Since L1 is a reserved variable, it always exists. ...

Code:

EXPORT IRR()
...

It seems to run ok. EDITLIST() has optional parameters, which may be useful, but the above code is a very handy program for the purpose.

yes, EDITLIST is a good tool! What a pity it doesn't change anything in CAS, however...
I'll put also the export for variable, to export irr as variable also; it's useful to have it exported...

(05-19-2015 01:02 PM)Didier Lachieze Wrote:  ...
It run ok from Home but not from CAS. If you replace "solve" by "Solve.SOLVE" (with an uppercase 'S') it works fine from both.

it's right! I'm testing now.
But with the "11 element list" (it's not importante that is has 11 element or its length but that it IRR should have almost 2 results...) it gives only the positive result.

IRR may have (also in HP 12C) more results, if there is one positive it is unique, if they are negative they can be more, but one positive there is always (that says the HP 12C Guide)...
solve() gives two results, Solve.SOLVE only one, but it's the most important (solve two but only in CAS)...
In 12C other result can be achieved "guessing" an interest near the presumed IRR and recall NPV (with PSE key and so on)...
Normally in a "well post" problem there is only an IRR (or only an important IRR), however.

Salvo

P.S.
Finished with IRR we could also find a formula for ...MIRR (Modified Internal Rate of Return): see here in the 12C Guide (page 256).
IRR is very important but sometimes (if it has more results or period for cash flow are irregular or there could be fluctuation in the market...) some analysts prefer to use MIRR Smile

EDIT
***
Consider this 21 items list, and with the occasion another feature to add could be add the ability of set number of items (i.e. if for 9 months we have 10000$ income, we should avoid to input 9 times 10000: 12C use CFj and Nj for this use; NVP, the Eddis' program works with list -r, {flows}- or matrix -r,[[flow, freq], [flow, freq], ...]- this could be an idea here):
{-180000, 100000 (5 times), -100000 (5 times), 0 (9 times), 200000}
12C calculates 3 IRR (only with "guesses" (using i key), otherwise it gives "error 3": no IRR): 1.86, 14.35, 29.02.
With Solve.SOLVE() I get 0.018604...
With fsolve() (CAS) I get [-0.9625... 1.290215... 1.14348... 0.018604...]: 4 results?
These result should be erratic, however Solve.SOLVE() has one of the correct result (1.86%), fsolve() has 1.86%, the other two but with +100% (1.1435, 1.2902) plus another results not reported in 12C calcs (-96.25%)...

MIRR with the 12C formula (with a "secure rate" of 6% and a risk rate of 10% and n=20) I get (the example is in the Guide PDF) 0.81 (monthly) and 9.70% (yearly).
We need to investigate here!

EDIT 2
The best result are given by solve() (not fsolve() or Solve.SOLVE() ):
with the above "21 items list":
[-1.95257... 0.018604... 0.14348... 0.2902152...], the last three are the correct results (as 12C), the first not.
In Home no warnings, in CAS, however there is a warning: "Warning, argument is not an equation, solving -180000+100000/X^2+100000/X^3+...+200000/X^20=0)
I don't know if the warning could be avoided.

∫aL√0mic (IT9CLU) :: HP Prime 50g 41CX 71b 42s 39s 35s 12C 15C - DM42, DM41X - WP34s Prime Soft. Lib
Visit this user's website Find all posts by this user
Quote this message in a reply
05-20-2015, 09:25 PM
Post: #40
RE: A formula for IRR
I'm completing a program to calculate IRR, MIRR (modified IRR) and NPV for cash flows, both with list or matrix (editing them inside the program, L1 or M1), thanks to your help and good tips: stay tuned :-)

(I need only a reply about including some code of another user...)

I'm using solve() because it works better, also if in CAS it gives a warning in Terminal (for now I'm not be able to avoid that).

∫aL√0mic (IT9CLU) :: HP Prime 50g 41CX 71b 42s 39s 35s 12C 15C - DM42, DM41X - WP34s Prime Soft. Lib
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 




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