[Numpy-svn] r4961 - in trunk/numpy/lib: . tests

numpy-svn@scip... numpy-svn@scip...
Fri Apr 4 01:37:48 CDT 2008


Author: oliphant
Date: 2008-04-04 01:37:45 -0500 (Fri, 04 Apr 2008)
New Revision: 4961

Modified:
   trunk/numpy/lib/financial.py
   trunk/numpy/lib/tests/test_financial.py
Log:
Add modified internal rate of return calculation which is more conservative and takes into account re-investing profits and expense of financing losses.

Modified: trunk/numpy/lib/financial.py
===================================================================
--- trunk/numpy/lib/financial.py	2008-04-04 06:11:24 UTC (rev 4960)
+++ trunk/numpy/lib/financial.py	2008-04-04 06:37:45 UTC (rev 4961)
@@ -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 @@
     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

Modified: trunk/numpy/lib/tests/test_financial.py
===================================================================
--- trunk/numpy/lib/tests/test_financial.py	2008-04-04 06:11:24 UTC (rev 4960)
+++ trunk/numpy/lib/tests/test_financial.py	2008-04-04 06:37:45 UTC (rev 4961)
@@ -22,6 +22,11 @@
 >>> 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 *



More information about the Numpy-svn mailing list