summaryrefslogtreecommitdiff
path: root/numpy/lib/financial.py
diff options
context:
space:
mode:
authorTim Cera <tim@cerazone.net>2012-04-29 02:38:50 -0400
committerCharles Harris <charlesr.harris@gmail.com>2012-05-20 17:46:11 -0600
commitebffab2feca24dbd7b503d9b2519f60d6810091b (patch)
treecaadeca96da8d1aee781b5c372017da35093ab7b /numpy/lib/financial.py
parentb081857c29ce946c503e07c7d53597f443d1a646 (diff)
downloadnumpy-ebffab2feca24dbd7b503d9b2519f60d6810091b.tar.gz
BUG: Changed ipmt to accept array_like arguments.
The ipmt function was also fixed to handle broadcasting. The tests were improved and extended to cover the broadcasting capability.
Diffstat (limited to 'numpy/lib/financial.py')
-rw-r--r--numpy/lib/financial.py28
1 files changed, 17 insertions, 11 deletions
diff --git a/numpy/lib/financial.py b/numpy/lib/financial.py
index d686aeca2..599a36198 100644
--- a/numpy/lib/financial.py
+++ b/numpy/lib/financial.py
@@ -18,9 +18,13 @@ _when_to_num = {'end':0, 'begin':1,
'finish':0}
def _convert_when(when):
+ #Test to see if when has already been converted to ndarray
+ #This will happen if one function calls another, for example ppmt
+ if isinstance(when, np.ndarray):
+ return when
try:
return _when_to_num[when]
- except KeyError:
+ except (KeyError, TypeError):
return [_when_to_num[x] for x in when]
@@ -236,8 +240,8 @@ def nper(rate, pmt, pv, fv=0, when='end'):
If you only had $150/month to pay towards the loan, how long would it take
to pay-off a loan of $8,000 at 7% annual interest?
- >>> np.nper(0.07/12, -150, 8000)
- 64.073348770661852
+ >>> print round(np.nper(0.07/12, -150, 8000), 5)
+ 64.07335
So, over 64 months would be required to pay off the loan.
@@ -354,17 +358,19 @@ def ipmt(rate, per, nper, pv, fv=0.0, when='end'):
12 -216.26 -1.49 -0.00
>>> interestpd = np.sum(ipmt)
- >>> interestpd
- -112.98308424136215
+ >>> np.round(interestpd, 2)
+ -112.98
"""
when = _convert_when(when)
- if when == 1 and per == 1:
- return 0.0
+ rate, per, nper, pv, fv, when = np.broadcast_arrays(rate, per, nper, pv, fv, when)
total_pmt = pmt(rate, nper, pv, fv, when)
ipmt = _rbl(rate, per, total_pmt, pv, when)*rate
- if when == 1:
- return ipmt/(1 + rate)
+ try:
+ ipmt = np.where(when == 1, ipmt/(1 + rate), ipmt)
+ ipmt = np.where(np.logical_and(when == 1, per == 1), 0.0, ipmt)
+ except IndexError:
+ pass
return ipmt
def _rbl(rate, per, pmt, pv, when):
@@ -620,8 +626,8 @@ def irr(values):
Examples
--------
- >>> np.irr([-100, 39, 59, 55, 20])
- 0.2809484211599611
+ >>> print round(np.irr([-100, 39, 59, 55, 20]), 5)
+ 0.28095
(Compare with the Example given for numpy.lib.financial.npv)