In this tutorial, I’ll show you how to calculate the Internal Rate of Return (IRR) using Numpy. I’ll also share with you the code to create a tool to calculate IRR.
In general, here is a template that you can use in order to calculate the IRR using numpy:
import numpy as np irr = np.irr([initial investment in negative terms, cash flows each year separated by commas]) print (irr)
In the next section, I’ll show you the steps to calculate the IRR using a simple example.
And by the end of this tutorial, you’ll be able to launch the following tool in Python:
Steps to calculate the IRR using Numpy
Step 1: Install the numpy package
If you haven’t already done so, you’ll need to install the numpy package using:
pip install numpy
Step 2: Calculate the IRR using numpy
To calculate the IRR using numpy, you’ll need to use this template:
import numpy as np irr = np.irr([initial investment in negative terms, cash flows each year separated by commas]) print (irr)
Let’s now review an example to see how to use this template in practice.
Imagine that you have the following information about a project:
- The initial investment is $1700
- The project is expected to generate:
- $500 in year-1
- $750 in year-2
- $800 in year-3
What is the IRR?
To calculate the IRR using numpy, you’ll need to apply this code in Python:
import numpy as np irr = np.irr([-1700,500,750,800]) print(irr)
And if you run this code you’ll get the IRR of 0.09236 (or 9.236%).
Tool to calculate the IRR
Now, I’ll share with you the code to launch a tool that will allow you to calculate the IRR using a graphical interface:
import tkinter as tk import numpy as np root= tk.Tk() canvas1 = tk.Canvas(root, width = 600, height = 300) canvas1.pack() label0 = tk.Label(root, text='Calculate IRR') label0.config(font=('helvetica', 14)) canvas1.create_window(300, 40, window=label0) entry1 = tk.Entry (root, width = 40) canvas1.create_window(450, 100, window=entry1) entry2 = tk.Entry (root, width = 40) canvas1.create_window(450, 140, window=entry2) entry3 = tk.Entry (root, width = 30) canvas1.create_window(300, 250, window=entry3) label1 = tk.Label(root, text=' Initial Investment (Absolute Value):') label1.config(font=('helvetica', 10)) canvas1.create_window(130, 100, window=label1) label2 = tk.Label(root, text='Cash Flow Each Year (Separated by Commas):') label2.config(font=('helvetica', 10)) canvas1.create_window(168, 140, window=label2) def calcIRR (): cf0 = entry1.get() cf0 = -1*float(cf0) cashflows = entry2.get().split(',') #Enter values Separated by Commas cf0_and_cashflows = [cf0] + cashflows irr = np.irr(cf0_and_cashflows) label3 = tk.Label(root, text= irr*100,font=('helvetica', 10, 'bold'),bg='white') canvas1.create_window(300, 250, window=label3) button1 = tk.Button(text='Calculate IRR', command=calcIRR, bg='green', fg='white', font=('helvetica', 9, 'bold'), width = 25) canvas1.create_window(300, 200, window=button1) root.mainloop()
Run the code in Python and you’ll see the following display:
You can now test the tool by typing the values that you saw in the example earlier:
- For the initial investment, type the amount in absolute terms (1700)
- The cash flow for each year should be separated by commas (500,750,800)
Once you’re done inputting the values, click on the green button to Calculate the IRR:
You’ll get the same IRR of 9.236%.