Skip to content

Commit dffb38c

Browse files
authored
Merge pull request #43 from Kai-Striega/negative_irr
2 parents 518a142 + 53cef84 commit dffb38c

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

numpy_financial/_financial.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,12 @@ def irr(values):
781781
# NPV(rate) = 0 can have more than one solution so we return
782782
# only the solution closest to zero.
783783
rate = 1/res - 1
784+
785+
# If there are any positive solutions prefer those over negative
786+
# rates.
787+
if (rate > 0).any():
788+
rate = np.where(rate > 0, rate, np.inf)
789+
784790
rate = rate.item(np.argmin(np.abs(rate)))
785791
return rate
786792

numpy_financial/tests/test_financial.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,3 +624,16 @@ def test_gh_15(self):
624624
# Very rough approximation taken from the issue.
625625
desired = -0.9999999990596069
626626
assert_allclose(result, desired, rtol=1e-9)
627+
628+
def test_gh_39(self):
629+
cashflows = numpy.array([
630+
-217500.0, -217500.0, 108466.80462450592, 101129.96439328062,
631+
93793.12416205535, 86456.28393083003, 79119.44369960476,
632+
71782.60346837944, 64445.76323715414, 57108.92300592884,
633+
49772.08277470355, 42435.24254347826, 35098.40231225296,
634+
27761.56208102766, 20424.721849802358, 13087.88161857707,
635+
5751.041387351768, -1585.7988438735192, -8922.639075098821,
636+
-16259.479306324123, -23596.31953754941, -30933.159768774713,
637+
-38270.0, -45606.8402312253, -52943.680462450604,
638+
-60280.520693675906, -67617.36092490121])
639+
assert_almost_equal(npf.irr(cashflows), 0.12)

0 commit comments

Comments
 (0)