4
4
"""
5
5
import numpy as np
6
6
from numpy .testing import assert_almost_equal
7
+ from nose .tools import raises
7
8
from numba import njit
8
9
9
10
from quantecon .optimize import brent_max
10
11
12
+
11
13
@njit
12
14
def f (x ):
13
15
"""
14
16
A function for testing on.
15
17
"""
16
18
return - (x + 2.0 )** 2 + 1.0
17
19
20
+
18
21
def test_brent_max ():
19
22
"""
20
- Uses the function f defined above to test the scalar maximization
23
+ Uses the function f defined above to test the scalar maximization
21
24
routine.
22
25
"""
23
26
true_fval = 1.0
24
27
true_xf = - 2.0
25
28
xf , fval , info = brent_max (f , - 2 , 2 )
26
29
assert_almost_equal (true_fval , fval , decimal = 4 )
27
30
assert_almost_equal (true_xf , xf , decimal = 4 )
28
-
31
+
32
+
29
33
@njit
30
34
def g (x , y ):
31
35
"""
32
36
A multivariate function for testing on.
33
37
"""
34
38
return - x ** 2 + y
35
-
39
+
40
+
36
41
def test_brent_max ():
37
42
"""
38
- Uses the function f defined above to test the scalar maximization
43
+ Uses the function f defined above to test the scalar maximization
39
44
routine.
40
45
"""
41
46
y = 5
@@ -46,6 +51,21 @@ def test_brent_max():
46
51
assert_almost_equal (true_xf , xf , decimal = 4 )
47
52
48
53
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
+
49
69
if __name__ == '__main__' :
50
70
import sys
51
71
import nose
@@ -54,5 +74,3 @@ def test_brent_max():
54
74
argv .append ('--verbose' )
55
75
argv .append ('--nocapture' )
56
76
nose .main (argv = argv , defaultTest = __file__ )
57
-
58
-
0 commit comments