comp.lang.ada
 help / color / mirror / Atom feed
From: Al Christians <achrist@easystreet.com>
Subject: Re: Ada routine to compute Internal Rate Of Return
Date: 1998/12/12
Date: 1998-12-12T00:00:00+00:00	[thread overview]
Message-ID: <3672DFCE.2652ED45@easystreet.com> (raw)
In-Reply-To: 366EC73C.1BF26A8E@pwfl.com

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




      parent reply	other threads:[~1998-12-12  0:00 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1998-12-09  0:00 Ada routine to compute Internal Rate Of Return Marin David Condic
1998-12-09  0:00 ` Tom Moran
1998-12-10  0:00   ` Marin David Condic
1998-12-10  0:00     ` Tom Moran
1998-12-10  0:00 ` Michael F Brenner
1998-12-10  0:00   ` Marin David Condic
1998-12-11  0:00   ` dennison
1998-12-12  0:00 ` Al Christians [this message]
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox