< Return to Fixed Income

Yields: Internal Rate of Return (IRR)

Education Hero Image

Calculating Internal Rate of Return (IRR) in Python: A Practical Guide

Python, being a versatile programming language, allows us to calculate the Internal Rate of Return (IRR) with ease. In this practical guide, we'll walk through a simple example using Python to perform IRR calculations.

Example Scenario:

Consider an investment project with the following annual cash flows:

Year 1: $500 Year 2: $700 Year 3: $800 Year 4: $1,000 Year 5: $1,200

Steps to Calculate IRR in Python:

Step 1: Set Up Python Environment: Ensure you have Python installed on your computer. You can use a Python script or an interactive environment like Jupyter Notebook.

Step 2: Import Required Libraries: In your Python script or Jupyter Notebook, start by importing the numpy library, which includes the irr function for IRR calculations.

import numpy as np

Step 3: Enter Cash Flows: Define a list or array with the cash flows corresponding to each year.

cash_flows = [-500, 700, 800, 1000, 1200]

Here, the initial investment is represented as a negative value, and subsequent returns are positive.

Step 4: Calculate IRR: Use the irr function from numpy to calculate the IRR.

irr_value = np.irr(cash_flows)
print(f"Internal Rate of Return (IRR): {round(irr_value*100, 2)}%")

The round function is used to display the result as a percentage with two decimal places.

Step 5: Interpret the Result: Run your Python script or execute the cells in Jupyter Notebook. The output will display the calculated IRR as a percentage.

In our example, the IRR might be around 16.38%, indicating an expected annual return.

Tips for Using Python IRR Calculation:

  1. Library Installation: Ensure the numpy library is installed. You can install it using the command pip install numpy if you haven't already.

  2. Consistent Cash Flow Representation: Follow the convention of representing the initial investment as a negative value and subsequent returns as positive values.

  3. Adjust for Different Cash Flow Scenarios: Modify the cash_flows array if your project has different cash flow scenarios.

Conclusion:

Calculating IRR in Python offers a flexible and efficient way to analyze the potential return on investment. By following these steps, you can leverage Python's capabilities for financial analysis and decision-making.

This article takes inspiration from a lesson found in FIN 4243 at the University of Florida.