Skip to content

Commit 3ea3a6e

Browse files
committed
TST: NPV: Add tests for NPV
1 parent a518eac commit 3ea3a6e

File tree

1 file changed

+39
-6
lines changed

1 file changed

+39
-6
lines changed

tests/test_financial.py

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ def test_rate_maximum_iterations_exception_array(self):
164164
class TestNpv:
165165
def test_npv(self):
166166
assert_almost_equal(
167-
npf.npv(0.05, [-15000, 1500, 2500, 3500, 4500, 6000]),
167+
npf.npv(0.05, [-15000.0, 1500.0, 2500.0, 3500.0, 4500.0, 6000.0]),
168168
122.89, 2)
169169

170170
def test_npv_decimal(self):
@@ -174,17 +174,50 @@ def test_npv_decimal(self):
174174

175175
def test_npv_broadcast(self):
176176
cashflows = [
177-
[-15000, 1500, 2500, 3500, 4500, 6000],
178-
[-15000, 1500, 2500, 3500, 4500, 6000],
179-
[-15000, 1500, 2500, 3500, 4500, 6000],
180-
[-15000, 1500, 2500, 3500, 4500, 6000],
177+
[-15000.0, 1500.0, 2500.0, 3500.0, 4500.0, 6000.0],
178+
[-15000.0, 1500.0, 2500.0, 3500.0, 4500.0, 6000.0],
179+
[-15000.0, 1500.0, 2500.0, 3500.0, 4500.0, 6000.0],
180+
[-15000.0, 1500.0, 2500.0, 3500.0, 4500.0, 6000.0],
181181
]
182182
expected_npvs = [
183-
122.8948549, 122.8948549, 122.8948549, 122.8948549
183+
[122.8948549, 122.8948549, 122.8948549, 122.8948549]
184184
]
185185
actual_npvs = npf.npv(0.05, cashflows)
186186
assert_allclose(actual_npvs, expected_npvs)
187187

188+
@pytest.mark.parametrize("dtype", [Decimal, float])
189+
def test_npv_broadcast_equals_for_loop(self, dtype):
190+
cashflows_str = [
191+
["-15000.0", "1500.0", "2500.0", "3500.0", "4500.0", "6000.0"],
192+
["-25000.0", "1500.0", "2500.0", "3500.0", "4500.0", "6000.0"],
193+
["-35000.0", "1500.0", "2500.0", "3500.0", "4500.0", "6000.0"],
194+
["-45000.0", "1500.0", "2500.0", "3500.0", "4500.0", "6000.0"],
195+
]
196+
rates_str = ["-0.05", "0.00", "0.05", "0.10", "0.15"]
197+
198+
cashflows = numpy.array([[dtype(x) for x in cf] for cf in cashflows_str])
199+
rates = numpy.array([dtype(x) for x in rates_str])
200+
201+
expected = numpy.empty((len(rates), len(cashflows)), dtype=dtype)
202+
for i, r in enumerate(rates):
203+
for j, cf in enumerate(cashflows):
204+
expected[i, j] = npf.npv(r, cf)
205+
206+
actual = npf.npv(rates, cashflows)
207+
assert_equal(actual, expected)
208+
209+
@pytest.mark.parametrize("rates", ([[1, 2, 3]], numpy.empty(shape=(1,1,1))))
210+
def test_invalid_rates_shape(self, rates):
211+
cashflows = [1, 2, 3]
212+
with pytest.raises(ValueError):
213+
npf.npv(rates, cashflows)
214+
215+
@pytest.mark.parametrize("cf", ([[[1, 2, 3]]], numpy.empty(shape=(1, 1, 1))))
216+
def test_invalid_cashflows_shape(self, cf):
217+
rates = [1, 2, 3]
218+
with pytest.raises(ValueError):
219+
npf.npv(rates, cf)
220+
188221

189222
class TestPmt:
190223
def test_pmt_simple(self):

0 commit comments

Comments
 (0)