Formula de la Tasa interna de retorno

445 views
Skip to first unread message

Alejandro Garcia G.

unread,
Sep 12, 2014, 12:59:42 PM9/12/14
to publice...@googlegroups.com
¿Alguno de ustedes tiene una funcion o procedimiento para calcular la Tasa interna de retorno?

Gracias.

Fernando D. Bozzo

unread,
Sep 12, 2014, 2:57:32 PM9/12/14
to publice...@googlegroups.com
Hola Alejandro:

Por las dudas nadie lo tenga, encontré esto que puede servirte:

http://programacion.net/foros/oracle/calculo_tir_20821

https://www.youtube.com/watch?v=_B4Wl23lO_Q


Saludos.-

Alejandro Garcia G.

unread,
Sep 12, 2014, 3:13:17 PM9/12/14
to publice...@googlegroups.com
Gracias Fernando, esa tambien la encontre y estoy jugando con esa formulacion y otras mas, pero aun estoy tratando de depejar de manera optima.

Víctor Hugo Espínola Domínguez

unread,
Sep 12, 2014, 4:13:17 PM9/12/14
to publice...@googlegroups.com
Hola Alejandro

El siguiente código calcula la tasa de interés, sistema francés, de un préstamo dando como parámetros el monto del préstamo, el importe de la cuota y la cantidad de cuotas. El resultado es el mismo que arroja la función RATE de Excel.

Local lnCuota, lnPlazo, lnPrestamo, lnTasAnual, lnTasMensual

lnPrestamo =  6400000
lnCuota       =   409000
lnPlazo       =       24

lnTasMensual = InteresFrances(m.lnPrestamo, m.lnCuota, m.lnPlazo)
lnTasAnual     = m.lnTasMensual * 12

Messagebox(Transform(m.lnTasAnual) + " - " + Transform(m.lnTasAnual / 12))
*
*-------------------------------------------------------------------------------------------
*
Function InteresFrances
Lparameters tnPrestamo, tnCuota, tnCntCuotas

Local lcSetDecimal, lnDifPrestamo, lnInteresMax, lnInteresMin, lnInteresPeriodo, lnPrecision
Local lnPrestamoAux

lcSetDecimal = Set("Decimals")
Set Decimals To 18

lnInteresMax = + 100
lnInteresMin  = -101

lnPrecision    = 0.000001
Do While .T.
    lnInteresPeriodo = ( m.lnInteresMax + m.lnInteresMin ) / 2
    lnPrestamoAux     = Pv( m.tnCuota, m.lnInteresPeriodo / 100, m.tnCntCuotas )
    lnDifPrestamo     = m.tnPrestamo - m.lnPrestamoAux

    If Abs( m.lnDifPrestamo ) <= m.lnPrecision
        Exit
    Endif
    If m.lnDifPrestamo < 0
        lnInteresMin = m.lnInteresPeriodo
    Else
        lnInteresMax = m.lnInteresPeriodo
    Endif
Enddo

Set Decimals To (m.lcSetDecimal)

Return (m.lnInteresPeriodo)


Saludos,
Víctor.
Lambaré - Paraguay.

Alejandro Garcia G.

unread,
Sep 13, 2014, 9:36:28 AM9/13/14
to publice...@googlegroups.com
Victor, saludos y gracias por tu respuesta.

Te comento que tengo una rutina similar para ese calculo,  aca no tengo inconvenientes, lo que estoy requiriendo es un proceso para el calculo de la Tasa Interna de Retorno o TIR, econtre este ejemplo de la formula (tambien me lo recomendo Fernando) pero no he logrado despejara, no recuerdo bien metodos numericos, algebra, jejeje. Lo coloco para ver si alguien me colabora a despejarlo.

TIR, TASA INTERNA DE RETORNO
0=sumatoria de (Pi/(1+tasa)^((di-d1)/365)
donde:

di = es la iésima o última fecha de pago.
d1 = es la fecha de pago 0.
Pi = es el iésimo o último pago.
tasa: es el valor que se debe encontrar, el cual es la TIR

Lo que interesa encontrar en esta formula es la tasa (que es la TIR).

Gracias a todos.

Víctor Hugo Espínola Domínguez

unread,
Sep 13, 2014, 12:25:23 PM9/13/14
to publice...@googlegroups.com
Hola Alejandro

El ejemplo que te envié es para que veas el truco de hacer las aproximaciones en un ciclo para hallar un valor suficientemente próximo al valor de de la cuota en dicho ejemplo.

Es imposible despejar una fórmula para las tasas de interés, se calculan mediante aproximaciones, envíanos una muestra de los datos que tienes, imagino que están formados por un conjunto de montos y fechas y un monto de inversión inicial.

Saludos,
Víctor.
Lambaré - Paraguay.
P.D.: Si Excel lo puede calcular, VFP también ;-)




Alejandro Garcia G.

unread,
Sep 13, 2014, 1:32:18 PM9/13/14
to publice...@googlegroups.com
Saludos a todos, y como siempre gracias por sus respuestas.

Bueno, he estado buscando ya que realmente no logre despejar la formula y encontre esta rutina que hasta donde he visto (probado) me da los resultados comparados con unos ejemplo en Excel que tenia. Les coloco aca la función y la pagina en donde la encontre.

Internal Rate Of Return

Function irr(cashflowA)
  * IRR by Ben Creighton
  * Version 1.0, 9/19/2005
  *
  * Returns the internal rate of return for a series of cash flows
  * passed to IRR in an array.
  *
  * Requirements of array cashflowA
  * 1.  There must be at least two cash flows.
  * 2.  There must be at least one positive cash flow.
  * 3.  There must be at least one negative cash flow.
  * 4.  The array must be one dimensional
  *
  * Usage Example:
  *   Dimension cf(3)
  *   cf(1)=-100
  *   cf(2)=50
  *   cf(3)=80
  *   ? irr(@cf)
  *
  Private All
  #Define precisionNC .000000001  && Precision of Result
  #Define maxloopsNC 50           && Maximum Iterations

  * Count Periods, Total Negative Values and Total Positive Values
  Store 0 To totalPosN, totalNegN, periodsN, iterationN
  For xx=1 To Alen(cashflowA)
    totalPosN = m.totalPosN + Iif(cashflowA[m.xx]>0, cashflowA[m.xx], 0)
    totalNegN = m.totalNegN + Iif(cashflowA[m.xx]<0, -cashflowA[m.xx], 0)
    periodsN = Iif(cashflowA[m.xx]=0, m.periodsN, m.xx)
  Endfor

  Do Case
    Case m.totalNegN=0 Or m.totalPosN=0 Or m.periodsN<2
      Error "IRR: Invalid parameters in array cashflowA"

    Case m.periodsN=2
      * Two Period Interest Rate
      irrN = (m.totalPosN / m.totalNegN - 1)

    Otherwise
      * Use Newton-Raphson Algorithm to Converge on irrN
      Try
        irrN = (m.totalPosN / m.totalNegN) ^ (1 / m.periodsN) - 1
        presentValN = 1
        iterationN = 0
        Do While Abs(m.presentValN) > precisionNC And m.iterationN < maxloopsNC
          iterationN = m.iterationN + 1
          Store 0 To slopeN, presentValN
          For xx = 1 To m.periodsN
            presentValN = m.presentValN + cashflowA[m.xx] / (1 + m.irrN) ^ (m.xx - 1)
            slopeN = m.slopeN - cashflowA[m.xx] * (m.xx - 1) / (1 + m.irrN) ^ m.xx
          Endfor
          irrN = m.irrN - m.presentValN / m.slopeN
        Enddo
      Catch
      Endtry
      If m.iterationN = maxloopsNC
        Error "IRR: Could not Converge to a Result"
      Endif

  Endcase
  Return m.irrN
Endfunc


Gracias señor Ben Creighton por su colaboración.

Alejandro Garcia G.

unread,
Sep 13, 2014, 1:40:11 PM9/13/14
to publice...@googlegroups.com
Aca encontre otra funcion que realiza el mismo calculo:

How to calculate Internal rates of returns (IRR) 
*----------------------------------------------------------
*-- IRR: Internal rate of return
*-- Interest rate where PV of future cash flows are equal to initial
*-- cash investment. This Interest rate is calculated using a trial
*-- and error iteration.
*--
*-- PARAMETERS
*-- laValues : An array of periodic cash flows organised chronologicaly
*--    lnStep   : Initial increment value (Optional)
*------------------------------------------------------------------------
*--
*-- RETURNS : IRR per period (depends on period used to construct laValue)
*--         Returns .NULL if user interrupts by pressing ESC
*-------------------------------------------------------------------------
*--
*-- Ex:  Calculate the IRR for an investment of 70 M$ generating an income
*-- stream of 5M$/year for the next 20 years
*--
*-- DIMENSION laCF(21)    && Build the cash flow array
*-- laCF = 5            && yearly income
*-- laCF[1] = -70        && Initial investment
*-- lnIRR = IRR(@laCF)  && Calculate IRR by passing Array by reference
*-- ?lnIRR                && Prints 3.667%
*------------------------------------------------------------------------
FUNCTION IRR
LPARAMETERS laValues, lnstep
LOCAL lnInt, lnStep, lnb, llAdd, lnPVSum, ln, lnPV


*-- Start testing interest rates at 10%
*---------------------------------------
lnInt = 0.10
*---------------------------------------

*-- Increment (decrement) of interest rate to apply
*-- on each successive iteration
*--------------------------------------------------
lnstep = IIF(VARTYPE(lnStep) # "N", 0.01, lnStep)
*---------------------------------------------------

lnb     = ALEN(laValues)        && Lenght of cash flow schedule
llAdd    = .NULL.                && Initial direction of rate testing

*-- Rate testing loop
DO WHILE .t.
    lnPVSum = 0
    *-- Add Present value of all future cash flows
    FOR ln = 2 TO lnb
        lnPVSum = lnPVSum + (laValues[ln]/ (1+lnInt)^(ln-1))
    NEXT

    *-- Assess results
    *----------------------------------------------
    lnPV = laValues[1] + lnPVSum
    *-----------------------------------------------
    
    *-- We need to establish direction of search on the first pass
    *--------------------------------------------------------------
    IF ISNULL(llAdd)
        llAdd = ( lnPV > 0)
    ENDIF        
    *-------------------------------------------------
    
    DO CASE
        CASE lnPV = 0
            *-- This is the rate we are looking for
            EXIT
            
        CASE lnStep < 0.000001
            *-- Precision limit reached. Return current rate
            EXIT
            
        CASE LASTKEY() = 27
            *-- Allow escaping by pressing ESC
            lnInt = .NULL.
            EXIT
        
        
        CASE llADD .and. (lnPV < 0)
            *-- We when to far need to narrow search
            llAdd = !llAdd     && Reverse direction of search
            lnStep = lnStep / 10
            
        CASE !llADD .and. (lnPV > 0)
            *-- We when to far need to narrow search
            llAdd = !llAdd     && Reverse direction of search
            lnStep = lnStep / 10
        
        OTHERWISE
            *-- Get new rate for testing
            lnInt = lnInt + (IIF(llAdd, 1, -1) * lnStep)    
    ENDCASE
ENDDO            
RETURN IIF(ISNULL(lnInt), .NULL., lnInt*100)



Reply all
Reply to author
Forward
0 new messages