How to calculate Internal Rate of Return (IRR) and Payback Period with Python for Business and Finance analysis.


The IRR is a useful metric in financial analysis to evaluate the profitability of potential investments. It is actuallt a discount rate that makes the net present value (NPV) of all cash flows equal to zero.

Simple Decision Rule:
• Accept the Project if IRR > Required Rate of Return
• Reject the Project if IRR < Required Rate of Return

Interpretation of IRR:
• (Hypothetical) Rate of Return where NPV = 0
• Leads to exactly the same decisions as NPV Decision Rule




Internal Rate of Return (IRR):


$$NPV = 𝐼𝑜 + \sum_{t = 1}^n \frac { CFt }{ (1 + IRR)^t } = 0$$

Where:

NPV: Net Present Value
Io: Initial Investment (negative)
CFt: cashflow @ timestamp t
N: Total number of periods
IRR: Internal Rate of Return (NPV = 0)
t = timestamp (0, 1, …, N)


cf = [-200, 20, 50, 70, 100, 50]   
guess = 0.06
step = 0.0000001
target_npv = 0
tolerance = 0.001 

while True:
    f = 1 + guess
    NPV = 0
    for i in range(len(cf)):
        NPV += cf[i] / f**(i)
    #print(NPV, guess)
    diff = NPV - target_npv
    
    if abs(diff) > tolerance:
        if diff < 0:
            guess -= step
        elif diff > 0:
            guess += step
    else:
        break
        
print(NPV, guess)        

You can also calculate IRR using NUMPY library which is much more simple way to do it.


import numpy as np
import numpy_financial as npf
cf = [-200, 20, 50, 70, 100, 50] 
npf.irr(cf)

Below is an example of calculating projects Payback Period - Time until initial Investment is recovered.



cf = [-200, -150, 50, 70, 100, 50]
cum_cf = 0
for i in range(len(cf)):
    cum_cf += cf[i]
    #print(cum_cf)
    if cum_cf > 0:
        print("The Project´s Payback Period is {} Years!".format(i) )
        break
    elif cum_cf <= 0 and i == len(cf)-1:
        print("The Project does not break even!")        




See also related topics: