Skip to content

Commit 228ffaa

Browse files
committed
Updated
- Fixed error and changed IRR -> internal_rate_of_return - Included 2 more test cases
1 parent 2291b91 commit 228ffaa

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

numpy_financial/_financial.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -827,38 +827,38 @@ def irr(values, raise_exceptions=False):
827827
# as eirr = g - 1 (if we are close enough to a solution)
828828

829829
g = np.roots(values)
830-
IRR = np.real(g[np.isreal(g)]) - 1
830+
internal_rate_of_return = np.real(g[np.isreal(g)]) - 1
831831

832832
# realistic IRR
833-
IRR = IRR[IRR >= -1]
833+
internal_rate_of_return = internal_rate_of_return[internal_rate_of_return >= -1]
834834

835835
# if no real solution
836-
if len(IRR) == 0:
836+
if len(internal_rate_of_return) == 0:
837837
if raise_exceptions:
838838
raise NoRealSolutionError("No real solution is found for IRR.")
839839
return np.nan
840840

841841
# if only one real solution
842-
if len(IRR) == 1:
843-
return IRR[0]
842+
if len(internal_rate_of_return) == 1:
843+
return internal_rate_of_return[0]
844844

845845
# below is for the situation when there are more than 2 real solutions.
846846
# check sign of all IRR solutions
847-
same_sign = np.all(IRR > 0) if IRR[0] > 0 else np.all(IRR < 0)
847+
same_sign = np.all(internal_rate_of_return > 0) if internal_rate_of_return[0] > 0 else np.all(internal_rate_of_return < 0)
848848

849849
# if the signs of IRR solutions are not the same, first filter potential IRR
850850
# by comparing the total positive and negative cash flows.
851851
if not same_sign:
852852
pos = sum(values[values>0])
853853
neg = sum(values[values<0])
854-
if pos > neg:
855-
IRR = IRR[IRR > 0]
856-
else:
857-
IRR = IRR[IRR < 0]
854+
if pos >= neg:
855+
internal_rate_of_return = internal_rate_of_return[internal_rate_of_return >= 0]
856+
else:
857+
internal_rate_of_return = internal_rate_of_return[internal_rate_of_return < 0]
858858

859859
# pick the smallest one in magnitude and return
860-
abs_IRR = np.abs(IRR)
861-
return IRR[np.argmin(abs_IRR)]
860+
abs_internal_rate_of_return = np.abs(internal_rate_of_return)
861+
return internal_rate_of_return[np.argmin(abs_internal_rate_of_return)]
862862

863863

864864
@nb.njit(parallel=True)

tests/test_financial.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,8 @@ def test_npv_irr_congruence(self):
680680
([-100, 100, 0, -7], -0.0833),
681681
([-100, 100, 0, 7], 0.06206),
682682
([-5, 10.5, 1, -8, 1], 0.0886),
683+
([-10000, 4000, 200, 6800, -1000, 40000, -30000, -10000], 0.0000),
684+
([-10000, 4000, 2000, 1000, 3000], 0.0000),
683685
])
684686
def test_basic_values(self, v, desired):
685687
assert_almost_equal(npf.irr(v), desired, decimal=2)
@@ -764,4 +766,5 @@ def test_irr_no_real_solution_exception(self):
764766
cashflows = numpy.array([40000, 5000, 8000, 12000, 30000])
765767

766768
with pytest.raises(npf.NoRealSolutionError):
767-
npf.irr(cashflows, raise_exceptions=True)
769+
npf.irr(cashflows, raise_exceptions=True)
770+

0 commit comments

Comments
 (0)