Skip to content

Commit 4b977f1

Browse files
committed
ENH: Allow npv calculation to be broadcastable
1 parent 2e6ab00 commit 4b977f1

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

numpy_financial/_financial.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -863,8 +863,15 @@ def npv(rate, values):
863863
3065.22267
864864
865865
"""
866-
values = np.asarray(values)
867-
return (values / (1+rate)**np.arange(0, len(values))).sum(axis=0)
866+
values = np.atleast_2d(values)
867+
timestep_array = np.arange(0, values.shape[1])
868+
npv = (values / (1 + rate) ** timestep_array).sum(axis=1)
869+
try:
870+
# If size of array is one, return scalar
871+
return npv.item()
872+
except ValueError:
873+
# Otherwise, return entire array
874+
return npv
868875

869876

870877
def mirr(values, finance_rate, reinvest_rate):

numpy_financial/tests/test_financial.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,19 @@ def test_npv_decimal(self):
148148
npf.npv(Decimal('0.05'), [-15000, 1500, 2500, 3500, 4500, 6000]),
149149
Decimal('122.894854950942692161628715'))
150150

151+
def test_npv_broadcast(self):
152+
cashflows = [
153+
[-15000, 1500, 2500, 3500, 4500, 6000],
154+
[-15000, 1500, 2500, 3500, 4500, 6000],
155+
[-15000, 1500, 2500, 3500, 4500, 6000],
156+
[-15000, 1500, 2500, 3500, 4500, 6000],
157+
]
158+
expected_npvs = [
159+
122.8948549, 122.8948549, 122.8948549, 122.8948549
160+
]
161+
actual_npvs = npf.npv(0.05, cashflows)
162+
assert_allclose(actual_npvs, expected_npvs)
163+
151164

152165
class TestPmt:
153166
def test_pmt_simple(self):

0 commit comments

Comments
 (0)