From 0b92be65cc0063cabab6b9552df74788c4077e5b Mon Sep 17 00:00:00 2001 From: Philip Eliot Date: Sat, 1 Feb 2014 07:48:46 -0500 Subject: BUG: IRR was returning nan instead of valid negative answer. This change corrects the following two bugs in numpy.irr: * When the solution was negative, numpy.irr returned nan instead of the correct solution because of the mask applied to the roots. Corrected by removing the mask that 0 < res < 1. * When multiple roots were found, numpy.irr was returning an array of all roots rather than a single float. This bug was corrected by selecting the single root closest to zero (min(abs(root)). With these corrections, numpy.irr returns the same result as the corresponding spreadsheet function in LibreOffice Calc for all test cases (additional test cases were added to cover cases with multiple positive and negative roots) --- numpy/lib/financial.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) (limited to 'numpy/lib/financial.py') 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): -- cgit v1.2.1