Skip to content

Commit

Permalink
BUG: Fixed error in logic for irr
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Eugenia-Mazur committed Apr 24, 2024
1 parent 370abb0 commit a753576
Showing 1 changed file with 20 additions and 19 deletions.
39 changes: 20 additions & 19 deletions numpy_financial/_financial.py
Original file line number Diff line number Diff line change
Expand Up @@ -835,25 +835,26 @@ def irr(values, *, raise_exceptions=False, selection_logic=_irr_default_selectio
#
# which we solve using Newton-Raphson and then reverse out the solution
# as eirr = g - 1 (if we are close enough to a solution)
g = np.roots(row)
eirr = np.real(g[np.isreal(g)]) - 1

# Realistic IRR
eirr = eirr[eirr >= -1]

# If no real solution
if len(eirr) == 0:
if raise_exceptions:
raise NoRealSolutionError("No real solution is found for IRR.")
irr_results.append(np.nan)
# If only one real solution
if len(eirr) == 1:
irr_results.append(eirr[0])

eirr = selection_logic(eirr)
irr_results.append(eirr)

return np.array(irr_results)
else:
g = np.roots(row)
eirr = np.real(g[np.isreal(g)]) - 1

# Realistic IRR
eirr = eirr[eirr >= -1]

# If no real solution
if len(eirr) == 0:
if raise_exceptions:
raise NoRealSolutionError("No real solution is found for IRR.")
irr_results.append(np.nan)
# If only one real solution
elif len(eirr) == 1:
irr_results.append(eirr[0])
else:
eirr = selection_logic(eirr)
irr_results.append(eirr)

return _ufunc_like(np.array(irr_results))


def npv(rate, values):
Expand Down

0 comments on commit a753576

Please sign in to comment.