What is the numerical method used in this IRR implementation?

ActiveState Recipes has a function that implements the Internal Return Rate 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, ie, 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 the correct values ​​(at least for a couple of examples that I checked in Excel), but I would like to know why.

  • It is not an implementation of the Newton method (without derivative) or the Secant method (tracks only one iteration).
  • In particular, defining the investment variable as the first element of the cash flow (and subsequent use) confuses me.

Any ideas?

+6
source share
3 answers

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

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

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

rate will not change. This way, once you find the IRR, the iteration will not change it. A fixed-point hierarchy sometimes converges to the correct value, and sometimes not. Examples @Gareth and @unutbu show that here it does not always converge.

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

 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 happening.

You may wonder why iteration doesn't

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

without the strange variable investment . In fact, I also wondered; it will also be a fixed point method that converges to IRR if the derivative condition is met. I assume that the derivative condition is satisfied in some cases for the method that you gave, and not for the method without investment .

+4
source

It seems fake to me. Convergence is too slow for practical use.

 >>> irr([-100, 100]) # expecting answer 0 0.00990099009900991 
+3
source

It seems to me also a fake.

 >>> irr([-100,50],100000) # expecting answer -0.5 0.0 
+2
source

Source: https://habr.com/ru/post/893999/


All Articles