Python-Powered Internal Rate of Return (IRR) Investments Analysis.

IRR for Finance Analysis.

The Internal Rate of Return is a crucial financial metric used to evaluate the profitability of an investment. In this guide, we will take you step-by-step through the process of calculating IRR with Python, providing clear explanations, practical code examples, and valuable insights along the way. By the end, you'll have the confidence to leverage Python's powerful capabilities to analyze and optimize your investment decisions through precise IRR calculations. Let's dive in and unlock the power of Internal Rate of Return with Python.

Internal Rate of Return (IRR) and Payback Period with Python.

Python programming language and its libraries combined together form a powerful tool for solving Regression analysis tasks.

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$$


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
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] 

Payback Period in Finance.

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]
    if cum_cf > 0:
        print("The Project´s Payback Period is {} Years!".format(i) )
    elif cum_cf <= 0 and i == len(cf)-1:
        print("The Project does not break even!")        

IRR real life use cases.

Investment evaluation: IRR is commonly used to assess the profitability of potential investments. It helps investors determine whether a project or investment is financially viable by comparing the expected returns with the cost of capital.

Capital budgeting: IRR is used in capital budgeting decisions to evaluate different investment options. By calculating the IRR for each project, companies can prioritize investments based on their potential returns and select the most profitable ones.

Project valuation: IRR is used to estimate the value of a project or business. By discounting future cash flows at the IRR, companies can determine the present value of the project and make informed decisions about its feasibility and profitability.

Performance measurement: IRR is used to measure the performance of investment portfolios or funds. It helps investors assess the effectiveness of their investment strategies by comparing the actual returns achieved with the expected returns based on the IRR.

Cost of capital determination: IRR is used to determine the cost of capital for a company. By calculating the IRR, companies can identify the rate of return that equates the present value of cash inflows with the present value of cash outflows, which represents the cost of capital.


Internal Rate of Return (IRR) analysis with Python is a vital financial evaluation technique that allows you to assess the profitability of investments. By leveraging the programming capabilities of Python, you can efficiently calculate the IRR and make well-informed investment decisions. Python's libraries and functions simplify the IRR calculation process, enabling financial professionals and data enthusiasts to utilize this powerful tool. With Python, you can input cash flow values, apply mathematical operations, and utilize efficient algorithms to determine the IRR. Python's flexibility extends to handling complex scenarios and performing sensitivity analysis by modifying cash flow assumptions. Moreover, Python's integration with other financial and statistical libraries enhances the accuracy and versatility of IRR analysis, enabling you to consider factors like risk, inflation, and compare different investment opportunities. In summary, Internal Rate of Return with Python empowers you to evaluate investment projects, assess potential returns, and make data-driven decisions, making it an invaluable resource for maximizing profitability.

See also related topics: