diff options
author | Travis Oliphant <oliphant@enthought.com> | 2008-04-04 06:37:45 +0000 |
---|---|---|
committer | Travis Oliphant <oliphant@enthought.com> | 2008-04-04 06:37:45 +0000 |
commit | 343841340122a5c5bd2071584e81ad8e027e6389 (patch) | |
tree | bf0eb275056b034b0ffebacbb27a9eaa2156a0e8 /numpy/lib/financial.py | |
parent | effc09b20ea3146d3dcfc6884b4321ddf1638cb5 (diff) | |
download | numpy-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/financial.py')
-rw-r--r-- | numpy/lib/financial.py | 27 |
1 files changed, 26 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 |