7
7
from numpy .testing import (
8
8
assert_ , assert_almost_equal , assert_allclose , assert_equal , assert_raises
9
9
)
10
+ import pytest
10
11
11
12
import numpy_financial as npf
12
13
@@ -133,14 +134,6 @@ def raise_error_because_not_equal():
133
134
Decimal ('10000000000' )),
134
135
Decimal ('-90238044.2322778884413969909' ))
135
136
136
- def test_ipmt (self ):
137
- assert_almost_equal (numpy .round (npf .ipmt (0.1 / 12 , 1 , 24 , 2000 ), 2 ),
138
- - 16.67 )
139
-
140
- def test_ipmt_decimal (self ):
141
- result = npf .ipmt (Decimal ('0.1' ) / Decimal ('12' ), 1 , 24 , 2000 )
142
- assert_equal (result .flat [0 ], Decimal ('-16.66666666666666666666666667' ))
143
-
144
137
def test_npv (self ):
145
138
assert_almost_equal (
146
139
npf .npv (0.05 , [- 15000 , 1500 , 2500 , 3500 , 4500 , 6000 ]),
@@ -231,15 +224,6 @@ def test_when(self):
231
224
assert_equal (npf .ppmt (0.1 / 12 , 1 , 60 , 55000 , 0 , 0 ),
232
225
npf .ppmt (0.1 / 12 , 1 , 60 , 55000 , 0 , 'end' ))
233
226
234
- # begin
235
- assert_equal (npf .ipmt (0.1 / 12 , 1 , 24 , 2000 , 0 , 1 ),
236
- npf .ipmt (0.1 / 12 , 1 , 24 , 2000 , 0 , 'begin' ))
237
- # end
238
- assert_equal (npf .ipmt (0.1 / 12 , 1 , 24 , 2000 , 0 ),
239
- npf .ipmt (0.1 / 12 , 1 , 24 , 2000 , 0 , 'end' ))
240
- assert_equal (npf .ipmt (0.1 / 12 , 1 , 24 , 2000 , 0 , 0 ),
241
- npf .ipmt (0.1 / 12 , 1 , 24 , 2000 , 0 , 'end' ))
242
-
243
227
# begin
244
228
assert_equal (npf .nper (0.075 , - 2000 , 0 , 100000. , 1 ),
245
229
npf .nper (0.075 , - 2000 , 0 , 100000. , 'begin' ))
@@ -364,44 +348,40 @@ def test_broadcast(self):
364
348
assert_almost_equal (npf .nper (0.075 , - 2000 , 0 , 100000. , [0 , 1 ]),
365
349
[21.5449442 , 20.76156441 ], 4 )
366
350
367
- assert_almost_equal (npf .ipmt (0.1 / 12 , list (range (5 )), 24 , 2000 ),
368
- [- 17.29165168 , - 16.66666667 , - 16.03647345 ,
369
- - 15.40102862 , - 14.76028842 ], 4 )
370
-
371
351
assert_almost_equal (npf .ppmt (0.1 / 12 , list (range (5 )), 24 , 2000 ),
372
- [- 74.998201 , - 75.62318601 , - 76.25337923 ,
352
+ [numpy . nan , - 75.62318601 , - 76.25337923 ,
373
353
- 76.88882405 , - 77.52956425 ], 4 )
374
354
375
355
assert_almost_equal (npf .ppmt (0.1 / 12 , list (range (5 )), 24 , 2000 , 0 ,
376
356
[0 , 0 , 1 , 'end' , 'begin' ]),
377
- [- 74.998201 , - 75.62318601 , - 75.62318601 ,
357
+ [numpy . nan , - 75.62318601 , - 75.62318601 ,
378
358
- 76.88882405 , - 76.88882405 ], 4 )
379
359
380
360
def test_broadcast_decimal (self ):
381
361
# Use almost equal because precision is tested in the explicit tests,
382
362
# this test is to ensure broadcast with Decimal is not broken.
383
- assert_almost_equal (npf .ipmt (Decimal ('0.1' ) / Decimal ('12' ),
384
- list (range (5 )), Decimal ('24' ),
385
- Decimal ('2000' )),
386
- [Decimal ('-17.29165168' ), Decimal ('-16.66666667' ),
387
- Decimal ('-16.03647345' ), Decimal ('-15.40102862' ),
388
- Decimal ('-14.76028842' )], 4 )
389
-
390
363
assert_almost_equal (npf .ppmt (Decimal ('0.1' ) / Decimal ('12' ),
391
- list (range (5 )), Decimal ('24' ),
364
+ list (range (1 , 5 )), Decimal ('24' ),
392
365
Decimal ('2000' )),
393
- [Decimal ('-74.998201' ), Decimal ( '- 75.62318601' ),
366
+ [Decimal ('-75.62318601' ),
394
367
Decimal ('-76.25337923' ), Decimal ('-76.88882405' ),
395
368
Decimal ('-77.52956425' )], 4 )
396
369
397
- assert_almost_equal (npf .ppmt (Decimal ('0.1' ) / Decimal ('12' ),
398
- list (range (5 )), Decimal ('24' ),
399
- Decimal ('2000' ), Decimal ('0' ),
400
- [Decimal ('0' ), Decimal ('0' ), Decimal ('1' ),
401
- 'end' , 'begin' ]),
402
- [Decimal ('-74.998201' ), Decimal ('-75.62318601' ),
403
- Decimal ('-75.62318601' ), Decimal ('-76.88882405' ),
404
- Decimal ('-76.88882405' )], 4 )
370
+ result = npf .ppmt (
371
+ Decimal ('0.1' ) / Decimal ('12' ),
372
+ list (range (1 , 5 )),
373
+ Decimal ('24' ),
374
+ Decimal ('2000' ),
375
+ Decimal ('0' ),
376
+ [Decimal ('0' ), Decimal ('1' ), 'end' , 'begin' ]
377
+ )
378
+ desired = [
379
+ Decimal ('-75.62318601' ),
380
+ Decimal ('-75.62318601' ),
381
+ Decimal ('-76.88882405' ),
382
+ Decimal ('-76.88882405' )
383
+ ]
384
+ assert_almost_equal (result , desired , decimal = 4 )
405
385
406
386
407
387
class TestNper :
@@ -426,3 +406,72 @@ def test_infinite_payments(self):
426
406
427
407
def test_no_interest (self ):
428
408
assert_ (npf .nper (0 , - 100 , 1000 ) == 10 )
409
+
410
+
411
+ class TestIpmt :
412
+ def test_float (self ):
413
+ assert_allclose (
414
+ npf .ipmt (0.1 / 12 , 1 , 24 , 2000 ),
415
+ - 16.666667 , # Computed using Google Sheet's IPMT
416
+ rtol = 1e-6 ,
417
+ )
418
+
419
+ def test_decimal (self ):
420
+ result = npf .ipmt (Decimal ('0.1' ) / Decimal ('12' ), 1 , 24 , 2000 )
421
+ assert result == Decimal ('-16.66666666666666666666666667' )
422
+
423
+ @pytest .mark .parametrize ('when' , [1 , 'begin' ])
424
+ def test_when_is_begin (self , when ):
425
+ assert npf .ipmt (0.1 / 12 , 1 , 24 , 2000 , 0 , when ) == 0
426
+
427
+ @pytest .mark .parametrize ('when' , [None , 0 , 'end' ])
428
+ def test_when_is_end (self , when ):
429
+ if when is None :
430
+ result = npf .ipmt (0.1 / 12 , 1 , 24 , 2000 )
431
+ else :
432
+ result = npf .ipmt (0.1 / 12 , 1 , 24 , 2000 , 0 , when )
433
+ assert_allclose (result , - 16.666667 , rtol = 1e-6 )
434
+
435
+ @pytest .mark .parametrize ('per, desired' , [
436
+ (0 , numpy .nan ),
437
+ (1 , 0 ),
438
+ (2 , - 594.107158 ),
439
+ (3 , - 592.971592 ),
440
+ ])
441
+ def test_gh_17 (self , per , desired ):
442
+ # All desired results computed using Google Sheet's IPMT
443
+ rate = 0.001988079518355057
444
+ result = npf .ipmt (rate , per , 360 , 300000 , when = "begin" )
445
+ if numpy .isnan (desired ):
446
+ assert numpy .isnan (result )
447
+ else :
448
+ assert_allclose (result , desired , rtol = 1e-6 )
449
+
450
+ def test_broadcasting (self ):
451
+ desired = [
452
+ numpy .nan ,
453
+ - 16.66666667 ,
454
+ - 16.03647345 ,
455
+ - 15.40102862 ,
456
+ - 14.76028842
457
+ ]
458
+ assert_allclose (
459
+ npf .ipmt (0.1 / 12 , numpy .arange (5 ), 24 , 2000 ),
460
+ desired ,
461
+ rtol = 1e-6 ,
462
+ )
463
+
464
+ def test_decimal_broadcasting (self ):
465
+ desired = [
466
+ Decimal ('-16.66666667' ),
467
+ Decimal ('-16.03647345' ),
468
+ Decimal ('-15.40102862' ),
469
+ Decimal ('-14.76028842' )
470
+ ]
471
+ result = npf .ipmt (
472
+ Decimal ('0.1' ) / Decimal ('12' ),
473
+ list (range (1 , 5 )),
474
+ Decimal ('24' ),
475
+ Decimal ('2000' )
476
+ )
477
+ assert_almost_equal (result , desired , decimal = 4 )
0 commit comments