diff options
author | bebert218 <guillaume.viry@gmail.com> | 2013-05-22 13:58:38 +0900 |
---|---|---|
committer | Charles Harris <charlesr.harris@gmail.com> | 2013-05-29 08:17:14 -0600 |
commit | e50475a64050bdea104d39ad2c487e847fe4b031 (patch) | |
tree | a9faba486bfecddceef1b6ede79345eefcb21bbd /numpy/lib | |
parent | 1f11f22ad71da9cb95b541be01fbfb4eed6d3d5b (diff) | |
download | numpy-e50475a64050bdea104d39ad2c487e847fe4b031.tar.gz |
BUG: The npv function in financial.py was incorrectly implemented.
Correct the implementation of the npv function, its documentation, and
the mirr function that depends on it. The test_financial.py is also
corrected to take into account those modifications
The npv function behavior was contrary to what the documentation stated
as it summed indexes 1 to M instead of 0 to M-1. The mirr function used
a corrective factor to get the correct result in spite of that error so
that factor is removed.
Closes #649
Diffstat (limited to 'numpy/lib')
-rw-r--r-- | numpy/lib/financial.py | 10 | ||||
-rw-r--r-- | numpy/lib/tests/test_financial.py | 2 |
2 files changed, 6 insertions, 6 deletions
diff --git a/numpy/lib/financial.py b/numpy/lib/financial.py index cd21db0f2..0be12f2c7 100644 --- a/numpy/lib/financial.py +++ b/numpy/lib/financial.py @@ -675,7 +675,7 @@ def npv(rate, values): ----- Returns the result of: [G]_ - .. math :: \\sum_{t=0}^M{\\frac{values_t}{(1+rate)^{t}}} + .. math :: \\sum_{t=0}^{M-1}{\\frac{values_t}{(1+rate)^{t}}} References ---------- @@ -685,13 +685,13 @@ def npv(rate, values): Examples -------- >>> np.npv(0.281,[-100, 39, 59, 55, 20]) - -0.0066187288356340801 + -0.0084785916384548798 (Compare with the Example given for numpy.lib.financial.irr) """ values = np.asarray(values) - return (values / (1+rate)**np.arange(1,len(values)+1)).sum(axis=0) + return (values / (1+rate)**np.arange(0,len(values))).sum(axis=0) def mirr(values, finance_rate, reinvest_rate): """ @@ -720,8 +720,8 @@ def mirr(values, finance_rate, reinvest_rate): neg = values < 0 if not (pos.any() and neg.any()): return np.nan - numer = np.abs(npv(reinvest_rate, values*pos))*(1 + reinvest_rate) - denom = np.abs(npv(finance_rate, values*neg))*(1 + finance_rate) + numer = np.abs(npv(reinvest_rate, values*pos)) + denom = np.abs(npv(finance_rate, values*neg)) return (numer/denom)**(1.0/(n - 1))*(1 + reinvest_rate) - 1 if __name__ == '__main__': diff --git a/numpy/lib/tests/test_financial.py b/numpy/lib/tests/test_financial.py index d76948582..1a276a429 100644 --- a/numpy/lib/tests/test_financial.py +++ b/numpy/lib/tests/test_financial.py @@ -41,7 +41,7 @@ class TestFinancial(TestCase): def test_npv(self): assert_almost_equal(np.npv(0.05,[-15000,1500,2500,3500,4500,6000]), - 117.04, 2) + 122.89, 2) def test_mirr(self): val = [-4500,-800,800,800,600,600,800,800,700,3000] |