summaryrefslogtreecommitdiff
path: root/numpy/lib
diff options
context:
space:
mode:
authorTravis Oliphant <oliphant@enthought.com>2008-04-04 06:37:45 +0000
committerTravis Oliphant <oliphant@enthought.com>2008-04-04 06:37:45 +0000
commit343841340122a5c5bd2071584e81ad8e027e6389 (patch)
treebf0eb275056b034b0ffebacbb27a9eaa2156a0e8 /numpy/lib
parenteffc09b20ea3146d3dcfc6884b4321ddf1638cb5 (diff)
downloadnumpy-343841340122a5c5bd2071584e81ad8e027e6389.tar.gz
Add modified internal rate of return calculation which is more conservative and takes into account re-investing profits and expense of financing losses.
Diffstat (limited to 'numpy/lib')
-rw-r--r--numpy/lib/financial.py27
-rw-r--r--numpy/lib/tests/test_financial.py5
2 files changed, 31 insertions, 1 deletions
diff --git a/numpy/lib/financial.py b/numpy/lib/financial.py
index 885fffeea..0ff46b280 100644
--- a/numpy/lib/financial.py
+++ b/numpy/lib/financial.py
@@ -1,8 +1,10 @@
# Some simple financial calculations
+# patterned after spreadsheet computations.
from numpy import log, where
import numpy as np
-__all__ = ['fv', 'pmt', 'nper', 'ipmt', 'ppmt', 'pv', 'rate', 'irr', 'npv']
+__all__ = ['fv', 'pmt', 'nper', 'ipmt', 'ppmt', 'pv', 'rate', 'irr', 'npv',
+ 'mirr']
_when_to_num = {'end':0, 'begin':1,
'e':0, 'b':1,
@@ -148,4 +150,27 @@ def npv(rate, values):
values = np.asarray(values)
return (values / (1+rate)**np.arange(1,len(values)+1)).sum(axis=0)
+def mirr(values, finance_rate, reinvest_rate):
+ """Modified internal rate of return
+
+ Parameters
+ ----------
+ values:
+ Cash flows (must contain at least one positive and one negative value)
+ or nan is returned.
+ finance_rate :
+ Interest rate paid on the cash flows
+ reinvest_rate :
+ Interest rate received on the cash flows upon reinvestment
+ """
+ values = np.asarray(values)
+ pos = values > 0
+ neg = values < 0
+ if not (pos.size > 0 and neg.size > 0):
+ return np.nan
+
+ n = pos.size + neg.size
+ numer = -npv(reinvest_rate, values[pos])*((1+reinvest_rate)**n)
+ denom = npv(finance_rate, values[neg])*(1+finance_rate)
+ return (numer / denom)**(1.0/(n-1)) - 1
diff --git a/numpy/lib/tests/test_financial.py b/numpy/lib/tests/test_financial.py
index bf0b4dfd4..4f7bb32e2 100644
--- a/numpy/lib/tests/test_financial.py
+++ b/numpy/lib/tests/test_financial.py
@@ -22,6 +22,11 @@ from numpy.lib.financial import *
>>> npv(0.05,[-15000,1500,2500,3500,4500,6000])
117.04271900089589
+>>> mirr([-4500,-800,800,800,600,600,800,800,700,3000],0.08,0.055)
+0.066471183500200537
+
+>>> mirr([-120000,39000,30000,21000,37000,46000],0.10,0.12)
+0.13439316981387006
"""
from numpy.testing import *