Calculate the percentage internal rate of return

2k Views Asked by At

Following is the code I am trying to use to get my result, and I am getting an error:

import numpy

array = [-1000,0,0,1094.60,0,0,1094.60]

for b in array:
    a = round(numpy.irr(array), b-1) * 100 
print (round(a,2))

Error:

TypeError: 'float' object cannot be interpreted as an integer

But, just replacing that "b-1" makes my code work, but I can't use that because array could be as large or as small as possible. I am not allowed to manually enter numbers replacing that "b-1". Following is the same working code.

import numpy

array = [-1000,0,0,1094.60,0,0,1094.60]

for b in array:
    a = round(numpy.irr(array), 6) * 100 
print (round(a,2))

I need a way to do it automatically for any size of array.

1

There are 1 best solutions below

3
On

You got the error because you tried to to round the rate to 1093.60 decimal places (that's b-1 for the 4th iteration).

numpy.arr works on any size of array. You do not have to give it a size (and you didn't). I gather that you're trying to print the interest rate as a percentage, rounded to 2 decimal places. Use this instead:

rate = numpy.irr(array) * 100
print "rate =", round(rate, 2)

Using this at the end of the code I gave you earlier gives this output for comparison:

Present Value (P) at rate 0.0125 = 0.217803365612
Present Value (P) at rate 0.01875 = -0.143198101517
Present Value (P) at rate 0.015625 = 0.0349275148787
Present Value (P) at rate 0.0171875 = -0.0547196000329
Present Value (P) at rate 0.01640625 = -0.0100432897584
Present Value (P) at rate 0.016015625 = 0.0124051532756
Present Value (P) at rate 0.0162109375 = 0.00117171042648
rate = 1.62