diff options
Diffstat (limited to 'numpy')
-rw-r--r-- | numpy/lib/financial.py | 20 | ||||
-rw-r--r-- | numpy/lib/tests/test_financial.py | 15 |
2 files changed, 29 insertions, 6 deletions
diff --git a/numpy/lib/financial.py b/numpy/lib/financial.py index ec642afd3..c085a5d53 100644 --- a/numpy/lib/financial.py +++ b/numpy/lib/financial.py @@ -628,21 +628,29 @@ def irr(values): Examples -------- - >>> print round(np.irr([-100, 39, 59, 55, 20]), 5) + >>> round(irr([-100, 39, 59, 55, 20]), 5) 0.28095 + >>> round(irr([-100, 0, 0, 74]), 5) + -0.0955 + >>> round(irr([-100, 100, 0, -7]), 5) + -0.0833 + >>> round(irr([-100, 100, 0, 7]), 5) + 0.06206 + >>> round(irr([-5, 10.5, 1, -8, 1]), 5) + 0.0886 (Compare with the Example given for numpy.lib.financial.npv) """ res = np.roots(values[::-1]) - # Find the root(s) between 0 and 1 - mask = (res.imag == 0) & (res.real > 0) & (res.real <= 1) - res = res[mask].real + mask = (res.imag == 0) & (res.real > 0) if res.size == 0: return np.nan + res = res[mask].real + # NPV(rate) = 0 can have more than one solution so we return + # only the solution closest to zero. rate = 1.0/res - 1 - if rate.size == 1: - rate = rate.item() + rate = rate.item(np.argmin(np.abs(rate))) return rate def npv(rate, values): diff --git a/numpy/lib/tests/test_financial.py b/numpy/lib/tests/test_financial.py index 6b7c6ef53..41a060a3f 100644 --- a/numpy/lib/tests/test_financial.py +++ b/numpy/lib/tests/test_financial.py @@ -13,6 +13,21 @@ class TestFinancial(TestCase): v = [-150000, 15000, 25000, 35000, 45000, 60000] assert_almost_equal(np.irr(v), 0.0524, 2) + v = [-100, 0, 0, 74] + assert_almost_equal(np.irr(v), + -0.0955, 2) + v = [-100, 39, 59, 55, 20] + assert_almost_equal(np.irr(v), + 0.28095, 2) + v = [-100, 100, 0, -7] + assert_almost_equal(np.irr(v), + -0.0833, 2) + v = [-100, 100, 0, 7] + assert_almost_equal(np.irr(v), + 0.06206, 2) + v = [-5, 10.5, 1, -8, 1] + assert_almost_equal(np.irr(v), + 0.0886, 2) def test_pv(self): assert_almost_equal(np.pv(0.07, 20, 12000, 0), |