irr is a guessing algorithm similiar to binary searching for bugs in a program. For a project with a positive IRR, then two **relatively prime** factors could be used
, one to increase the irr (multiple by factor > 1) if the npv is still positive and the other to decrease the irr when npv negative (divide by factor > 1), so that the tried values of the
irr are unlikely to cycle, due to the lack of common factors in multiply and divide.

Below is a python algorithm for irr calculation, which also deals with going to a negative IRR when the guessing shows the irr is approaching zero.

# copyright GNU license, sjt,2014

def irr(cashflows):

#print cashflows

r = 10.

lim =0.1

sign = 1

while True:

npv = reduce( lambda x,(i,cf): x + cf / pow(1 + r/100, i ) , \

enumerate(cashflows), 0.)

print npv

#cf= []

#for i,x in enumerate(cashflows):

# lr = pow((1 + r/100), i)

# y = x / lr

# print "lr", lr, "y", y

# cf.append(y)

#print reduce( lambda x, y: x + y, cf)

if abs(npv) < lim:

break

if npv * sign > 0:

r = r * 1.9

else:

r = r / 1.3

if sign > 0 and r < 0.0001:

r = - 20.

sign = -1

print "irr = ", r

cashflows=[-60000, 7000,9000,6000,5000,8000]

irr(cashflows)

This is a rather randomised algorithm; **Newton's method** has been mentioned as being used in open source spreadsheet programs.

Newton's method relies on the differentiation , or finding a derivative function to tell the slope of the graph of another function. The basic rule is that if there is a term which is
**ax ^{n}** then the derivative has a term ,

**n.ax**.

^{n-1}The npv function is a function of R the rate of return, such that npv is y, and R is x in the general function y = f(x) . Hence the derivative of the npv function is dy/dx = d(npv)/d(R) , which then requires finding the derivatives of all the terms in the npv function.

In Newton's method, an arbitrary R which is x, gives a NPV , which is a y value , when divided by the gradient which is delta-y/delta-x , gives delta-x , which is subtracted from R ( substitute x) to give another R' that is closer to the IRR, when NPV = 0 ( or the x value where y is zero, or where the function crosses the x-axis ). Using R' as the next R, this is repeated until a good approximation of IRR is achieved. The gradient is always recalculated at the start of each iteration , using the derivative function.

So if npv = CF_{0} / R^{0} + CF_{1} / R^{1} + .. + CF_{n} / R^{n},

or npv = CF_{0} + CF_{1} x R^{-1} + CF_{2} x R^{-2} + .. + CF_{n} x R^{-n},

then d(npv)/dR = 0 + - CF_{1}x R ^{-2} + - 2 CF_{2}x R ^{-3} ... + -n x CF_{n} x R^{-n - 1}.

See

- REDIRECT Computation instead of Table Lookup

- 1477 reads