From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,291a053a531b5b8f X-Google-Attributes: gid103376,public From: Al Christians Subject: Re: Ada routine to compute Internal Rate Of Return Date: 1998/12/12 Message-ID: <3672DFCE.2652ED45@easystreet.com>#1/1 X-Deja-AN: 421615999 Content-Transfer-Encoding: 7bit References: <366EC73C.1BF26A8E@pwfl.com> Content-Type: text/plain; charset=us-ascii X-Trace: news6.ispnews.com 913497422 206.103.58.41 (Sat, 12 Dec 1998 16:17:02 EDT) Organization: Trillium Resources Corporation MIME-Version: 1.0 NNTP-Posting-Date: Sat, 12 Dec 1998 16:17:02 EDT Newsgroups: comp.lang.ada Date: 1998-12-12T00:00:00+00:00 List-Id: Marin David Condic wrote: > > Does anybody have an Ada function to compute Internal Rate of Return > (IRR)? I've got a Net Present Value (NPV) function that takes an array > of cash flows & and interest rate. I could build something that tries to > converge this on zero, which would result in IRR, but I'm not sure about > developing an algorithm that will converge quickly. If someone has > solved this problem in Ada, I'd appreciate seeing some source code. > Thanks. > Here's my code to do the inverse interpolation: ------------------------------------------------------------------- function Interpolate_R_of_R( Profit: Profit_Types.Profit_Element_Vector; -- rr0 & rr1 are 1st 2 guesses at -- rate of return rr0, rr1: Long_Float; last_item: Natural := 0 ) return Long_Float is pv0, pv1, pv2, r2: Long_Float; r0: Long_Float := rr0; r1: Long_Float := rr1; begin -- calculate present values. pv0 := pv( Profit, r0, last_item ); pv1 := pv( Profit, r1, last_item ); for i in 1 .. 100 loop exit when abs ( r0 - r1 ) < 0.000000001; if pv0 * pv1 >= 0.0 then Ada.Text_IO.Put_Line( "Error -- Rate of Return not Found" ); exit; end if; r2 := ( r0 * pv1 - r1 * pv0 ) / ( pv1 - pv0 ); pv2 := pv( Profit, r2, last_item ); if pv0 * pv2 > 0.0 then pv0 := pv2; r0 := r2; else pv1 := pv2; r1 := r2; end if; end loop; if abs pv0 < abs pv1 then return r0; else return r1; end if; exception when others => Ada.Text_IO.Put_Line( "Error Interpolating R of R" ); raise; end; --------------------------------------------------------------------- The code not shown, for example determining if there are any real roots and finding the initial range may be harder. This algorithm will usually converge quickly. For something more advanced, see Brent's and Muller's methods in the Numerical Recipes book. Al