Skip to content

Commit a753576

Browse files
committed
BUG: Fixed error in logic for irr
Bug was found upon commit in IRR function. Logic was incorrect and returning an out of index error. Conditional statement was fixed to overcome this.
1 parent 370abb0 commit a753576

File tree

1 file changed

+20
-19
lines changed

1 file changed

+20
-19
lines changed

numpy_financial/_financial.py

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -835,25 +835,26 @@ def irr(values, *, raise_exceptions=False, selection_logic=_irr_default_selectio
835835
#
836836
# which we solve using Newton-Raphson and then reverse out the solution
837837
# as eirr = g - 1 (if we are close enough to a solution)
838-
g = np.roots(row)
839-
eirr = np.real(g[np.isreal(g)]) - 1
840-
841-
# Realistic IRR
842-
eirr = eirr[eirr >= -1]
843-
844-
# If no real solution
845-
if len(eirr) == 0:
846-
if raise_exceptions:
847-
raise NoRealSolutionError("No real solution is found for IRR.")
848-
irr_results.append(np.nan)
849-
# If only one real solution
850-
if len(eirr) == 1:
851-
irr_results.append(eirr[0])
852-
853-
eirr = selection_logic(eirr)
854-
irr_results.append(eirr)
855-
856-
return np.array(irr_results)
838+
else:
839+
g = np.roots(row)
840+
eirr = np.real(g[np.isreal(g)]) - 1
841+
842+
# Realistic IRR
843+
eirr = eirr[eirr >= -1]
844+
845+
# If no real solution
846+
if len(eirr) == 0:
847+
if raise_exceptions:
848+
raise NoRealSolutionError("No real solution is found for IRR.")
849+
irr_results.append(np.nan)
850+
# If only one real solution
851+
elif len(eirr) == 1:
852+
irr_results.append(eirr[0])
853+
else:
854+
eirr = selection_logic(eirr)
855+
irr_results.append(eirr)
856+
857+
return _ufunc_like(np.array(irr_results))
857858

858859

859860
def npv(rate, values):

0 commit comments

Comments
 (0)