@@ -164,7 +164,7 @@ def test_rate_maximum_iterations_exception_array(self):
164
164
class TestNpv :
165
165
def test_npv (self ):
166
166
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 ]),
168
168
122.89 , 2 )
169
169
170
170
def test_npv_decimal (self ):
@@ -174,17 +174,50 @@ def test_npv_decimal(self):
174
174
175
175
def test_npv_broadcast (self ):
176
176
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 ],
181
181
]
182
182
expected_npvs = [
183
- 122.8948549 , 122.8948549 , 122.8948549 , 122.8948549
183
+ [ 122.8948549 , 122.8948549 , 122.8948549 , 122.8948549 ]
184
184
]
185
185
actual_npvs = npf .npv (0.05 , cashflows )
186
186
assert_allclose (actual_npvs , expected_npvs )
187
187
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
+
188
221
189
222
class TestPmt :
190
223
def test_pmt_simple (self ):
0 commit comments