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
- import pytest
13
13
14
14
15
15
class TestFinancial (object ):
@@ -23,6 +23,25 @@ def test_npv_irr_congruence(self):
23
23
def test_rate (self ):
24
24
assert_almost_equal (npf .rate (10 , 0 , - 3500 , 10000 ), 0.1107 , 4 )
25
25
26
+ @pytest .mark .parametrize ('number_type' , [Decimal , float ])
27
+ @pytest .mark .parametrize ('when' , [0 , 1 , 'end' , 'begin' ])
28
+ def test_rate_with_infeasible_solution (self , number_type , when ):
29
+ """
30
+ Test when no feasible rate can be found.
31
+
32
+ Rate will return NaN, if the Newton Raphson method cannot find a
33
+ feasible rate within the required tolerance or number of iterations.
34
+ This can occur if both `pmt` and `pv` have the same sign, as it is
35
+ impossible to repay a loan by making further withdrawls.
36
+ """
37
+ result = npf .rate (number_type (12.0 ),
38
+ number_type (400.0 ),
39
+ number_type (10000.0 ),
40
+ number_type (5000.0 ),
41
+ when = when )
42
+ is_nan = Decimal .is_nan if number_type == Decimal else numpy .isnan
43
+ assert is_nan (result )
44
+
26
45
def test_rate_decimal (self ):
27
46
rate = npf .rate (Decimal ('10' ), Decimal ('0' ), Decimal ('-3500' ),
28
47
Decimal ('10000' ))
@@ -411,30 +430,3 @@ def test_broadcast_decimal(self):
411
430
[Decimal ('-74.998201' ), Decimal ('-75.62318601' ),
412
431
Decimal ('-75.62318601' ), Decimal ('-76.88882405' ),
413
432
Decimal ('-76.88882405' )], 4 )
414
-
415
- @pytest .mark .parametrize ('number_type' , [Decimal , float ])
416
- def test_rate_nan (self , number_type ):
417
- """
418
- Test for checking inputs whose output is NaN
419
- Rate will return NaN, if newton raphson method's change or diff was not able to become
420
- less than default tolerance value i.e. 1e-6 in max iterations possible,
421
- Both payments and present value are positive, it is impossible to pay off the existing balance
422
- by making further withdrawals, regardless of the rate.
423
- """
424
- rate = npf .rate (number_type (12.0 ), number_type (400 ), number_type (10000.0 ), number_type (0 ))
425
- assert_equal (numpy .nan , float (rate ))
426
- rate = npf .rate (number_type (12.0 ), number_type (400 ), number_type (10000.0 ), number_type (5000 ))
427
- assert_equal (numpy .nan , float (rate ))
428
-
429
- # begin
430
- rate = npf .rate (number_type (12.0 ), number_type (400 ), number_type (10000.0 ), number_type (20000 ), 1 )
431
- assert_equal (numpy .nan , float (rate ))
432
- rate = npf .rate (number_type (12.0 ), number_type (400 ), number_type (10000.0 ), number_type (20000 ), 'begin' )
433
- assert_equal (numpy .nan , float (rate ))
434
-
435
- # end
436
- rate = npf .rate (number_type (12.0 ), number_type (400 ), number_type (10000.0 ), number_type (0 ))
437
- assert_equal (numpy .nan , float (rate ))
438
- rate = npf .rate (number_type (12.0 ), number_type (400 ), number_type (10000.0 ), number_type (0 ), 'end' )
439
- assert_equal (numpy .nan , float (rate ))
440
-
0 commit comments