Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the numerical method used in this implementation of IRR?

The ActiveState Recipes site has a function implementing Internal Rate of Return in Python:

def irr(cashflows, iterations=100):
    """The IRR or Internal Rate of Return is the annualized effective 
       compounded return rate which can be earned on the invested 
       capital, i.e., the yield on the investment.

       >>> irr([-100.0, 60.0, 60.0, 60.0])
       0.36309653947517645
    """
    rate = 1.0
    investment = cashflows[0]
    for i in range(1, iterations+1):
        rate *= (1 - npv(rate, cashflows) / investment)
    return rate

This code returns correct values (at least for the couple of examples that I have checked against Excel), but I would like to know why.

  • It doesn't appear to be an implementation of Newton's Method (no derivative) or the Secant Method (only keeps track of one iteration).
  • In particular, the definition of the investment variable as the first cashflow element (and it's subsequent use) confuses me.

Any ideas?

like image 720
wk2752 Avatar asked Jul 31 '11 23:07

wk2752


1 Answers

The method is called fixed point iteration; see for instance the Wikipedia article http://en.wikipedia.org/wiki/Fixed_point_iteration.

The idea is that if rate contains the correct value (that is, the IRR), then the NPV is zero, so the statement

rate *= (1 - npv(rate, cashflows) / investment)

will not change rate. Thus, once you find the IRR, the iteration will not change it. Fixed point iteration sometimes converges to the correct value and sometimes it does not. The examples of @Gareth and @unutbu show that here it does not always converge.

The criterion for convergence is as follows. Write the update statement in the loop as

rate = rate * (1 - npv(rate, cashflows) / investment)

Now, if the derivative of the right-hand side with respect to rate is between 1 and -1, then the method converges. I cannot immediately see under what circumstances this is the case.

You may wonder why the iteration does not do

rate *= (1 - npv(rate, cashflows))

without the weird investment variable. Indeed, I wondered the same; that would also be a fixed point method that converges to the IRR if the derivative condition is satisfied. My guess is that the derivative condition is satisfied in some circumstances for the method that you gave, and not for the method without investment.

like image 128
Jitse Niesen Avatar answered Sep 29 '22 09:09

Jitse Niesen