44"""
55import numpy as np
66from numpy .testing import assert_almost_equal
7+ from nose .tools import raises
78from numba import njit
89
910from quantecon .optimize import brent_max
1011
12+
1113@njit
1214def f (x ):
1315 """
1416 A function for testing on.
1517 """
1618 return - (x + 2.0 )** 2 + 1.0
1719
20+
1821def test_brent_max ():
1922 """
20- Uses the function f defined above to test the scalar maximization
23+ Uses the function f defined above to test the scalar maximization
2124 routine.
2225 """
2326 true_fval = 1.0
2427 true_xf = - 2.0
2528 xf , fval , info = brent_max (f , - 2 , 2 )
2629 assert_almost_equal (true_fval , fval , decimal = 4 )
2730 assert_almost_equal (true_xf , xf , decimal = 4 )
28-
31+
32+
2933@njit
3034def g (x , y ):
3135 """
3236 A multivariate function for testing on.
3337 """
3438 return - x ** 2 + y
35-
39+
40+
3641def test_brent_max ():
3742 """
38- Uses the function f defined above to test the scalar maximization
43+ Uses the function f defined above to test the scalar maximization
3944 routine.
4045 """
4146 y = 5
@@ -46,6 +51,21 @@ def test_brent_max():
4651 assert_almost_equal (true_xf , xf , decimal = 4 )
4752
4853
54+ @raises (ValueError )
55+ def test_invalid_a_brent_max ():
56+ brent_max (f , - np .inf , 2 )
57+
58+
59+ @raises (ValueError )
60+ def test_invalid_b_brent_max ():
61+ brent_max (f , - 2 , np .inf )
62+
63+
64+ @raises (ValueError )
65+ def test_invalid_a_b_brent_max ():
66+ brent_max (f , 1 , 0 )
67+
68+
4969if __name__ == '__main__' :
5070 import sys
5171 import nose
@@ -54,5 +74,3 @@ def test_brent_max():
5474 argv .append ('--verbose' )
5575 argv .append ('--nocapture' )
5676 nose .main (argv = argv , defaultTest = __file__ )
57-
58-
0 commit comments