|
5 | 5 | # the versions in numpy instead of numpy_financial.
|
6 | 6 | import numpy
|
7 | 7 | from numpy.testing import (
|
8 |
| - assert_, assert_almost_equal, assert_allclose, assert_equal |
| 8 | + assert_, assert_almost_equal, assert_allclose, assert_equal, assert_raises |
9 | 9 | )
|
10 | 10 | import pytest
|
11 | 11 |
|
@@ -136,6 +136,26 @@ def test_gh48(self):
|
136 | 136 | actual = npf.rate(nper, pmt, pv, fv)
|
137 | 137 | assert_allclose(actual, des)
|
138 | 138 |
|
| 139 | + def test_rate_maximum_iterations_exception_scalar(self): |
| 140 | + # Test that if the maximum number of iterations is reached, |
| 141 | + # then npf.rate returns IterationsExceededException |
| 142 | + # when raise_exceptions is set to True. |
| 143 | + assert_raises(npf.IterationsExceededException, npf.rate, Decimal(12.0), |
| 144 | + Decimal(400.0), Decimal(10000.0), Decimal(5000.0), |
| 145 | + raise_exceptions=True) |
| 146 | + |
| 147 | + def test_rate_maximum_iterations_exception_array(self): |
| 148 | + # Test that if the maximum number of iterations is reached in at least |
| 149 | + # one rate, then npf.rate returns IterationsExceededException |
| 150 | + # when raise_exceptions is set to True. |
| 151 | + nper = 2 |
| 152 | + pmt = 0 |
| 153 | + pv = [-593.06, -4725.38, -662.05, -428.78, -13.65] |
| 154 | + fv = [214.07, 4509.97, 224.11, 686.29, -329.67] |
| 155 | + assert_raises(npf.IterationsExceededException, npf.rate, nper, |
| 156 | + pmt, pv, fv, |
| 157 | + raise_exceptions=True) |
| 158 | + |
139 | 159 |
|
140 | 160 | class TestNpv:
|
141 | 161 | def test_npv(self):
|
@@ -248,6 +268,13 @@ def test_mirr_decimal(self):
|
248 | 268 | Decimal('37000'), Decimal('46000')]
|
249 | 269 | assert_(numpy.isnan(npf.mirr(val, Decimal('0.10'), Decimal('0.12'))))
|
250 | 270 |
|
| 271 | + def test_mirr_no_real_solution_exception(self): |
| 272 | + # Test that if there is no solution because all the cashflows |
| 273 | + # have the same sign, then npf.mirr returns NoRealSolutionException |
| 274 | + # when raise_exceptions is set to True. |
| 275 | + val = [39000, 30000, 21000, 37000, 46000] |
| 276 | + assert_raises(npf.NoRealSolutionException, npf.mirr, val, 0.10, 0.12, raise_exceptions=True) |
| 277 | + |
251 | 278 |
|
252 | 279 | class TestNper:
|
253 | 280 | def test_basic_values(self):
|
@@ -575,6 +602,7 @@ def test_some_rates_zero(self):
|
575 | 602 |
|
576 | 603 |
|
577 | 604 | class TestIrr:
|
| 605 | + |
578 | 606 | def test_npv_irr_congruence(self):
|
579 | 607 | # IRR is defined as the rate required for the present value of
|
580 | 608 | # a series of cashflows to be zero, so we should have
|
@@ -671,3 +699,18 @@ def test_gh_44(self):
|
671 | 699 | # "true" value as calculated by Google sheets
|
672 | 700 | cf = [-1678.87, 771.96, 1814.05, 3520.30, 3552.95, 3584.99, 4789.91, -1]
|
673 | 701 | assert_almost_equal(npf.irr(cf), 1.00426, 4)
|
| 702 | + |
| 703 | + def test_irr_no_real_solution_exception(self): |
| 704 | + # Test that if there is no solution because all the cashflows |
| 705 | + # have the same sign, then npf.irr returns NoRealSolutionException |
| 706 | + # when raise_exceptions is set to True. |
| 707 | + cashflows = numpy.array([40000, 5000, 8000, 12000, 30000]) |
| 708 | + assert_raises(npf.NoRealSolutionException, npf.irr, cashflows, raise_exceptions=True) |
| 709 | + |
| 710 | + def test_irr_maximum_iterations_exception(self): |
| 711 | + # Test that if the maximum number of iterations is reached, |
| 712 | + # then npf.irr returns IterationsExceededException |
| 713 | + # when raise_exceptions is set to True. |
| 714 | + cashflows = numpy.array([-40000, 5000, 8000, 12000, 30000]) |
| 715 | + assert_raises(npf.IterationsExceededException, npf.irr, cashflows, |
| 716 | + maxiter=1, raise_exceptions=True) |
0 commit comments