99TODO: add multivariate case
1010
1111"""
12- import unittest
1312import numpy as np
14- from nose . tools import ok_ , raises
13+ from numpy . testing import assert_ , assert_raises
1514from quantecon import compute_fixed_point
1615
1716
18- class TestFPLogisticEquation (unittest . TestCase ):
17+ class TestFPLogisticEquation ():
1918
2019 @classmethod
21- def setUpClass (cls ):
20+ def setup (cls ):
2221 cls .mu_1 = 0.2 # 0 is unique fixed point forall x_0 \in [0, 1]
2322
2423 # (4mu - 1)/(4mu) is a fixed point forall x_0 \in [0, 1]
@@ -38,34 +37,31 @@ def test_contraction_1(self):
3837 f = lambda x : self .T (x , self .mu_1 )
3938 for i in self .unit_inverval :
4039 # should have fixed point of 0.0
41- self .assertTrue (abs (compute_fixed_point (f , i , ** self .kwargs ))
42- < 1e-4 )
40+ assert_ (abs (compute_fixed_point (f , i , ** self .kwargs )) < 1e-4 )
4341
4442 def test_not_contraction_2 (self ):
4543 "compute_fp: no convergence outside interval of convergence"
4644 f = lambda x : self .T (x , self .mu_2 )
4745 for i in self .unit_inverval :
4846 # This shouldn't converge to 0.0
49- self .assertFalse (abs (compute_fixed_point (f , i , ** self .kwargs ))
50- < 1e-4 )
47+ assert_ (not (abs (compute_fixed_point (f , i , ** self .kwargs )) < 1e-4 ))
5148
5249 def test_contraction_2 (self ):
5350 "compute_fp: convergence inside interval of convergence"
5451 f = lambda x : self .T (x , self .mu_2 )
5552 fp = (4 * self .mu_2 - 1 ) / (4 * self .mu_2 )
5653 for i in self .unit_inverval :
5754 # This should converge to fp
58- self .assertTrue (abs (compute_fixed_point (f , i , ** self .kwargs )- fp )
59- < 1e-4 )
55+ assert_ (abs (compute_fixed_point (f , i , ** self .kwargs )- fp ) < 1e-4 )
6056
6157 def test_not_contraction_1 (self ):
6258 "compute_fp: no convergence outside interval of convergence"
6359 f = lambda x : self .T (x , self .mu_1 )
6460 fp = (4 * self .mu_1 - 1 ) / (4 * self .mu_1 )
6561 for i in self .unit_inverval :
6662 # This should not converge (b/c unique fp is 0.0)
67- self . assertFalse (abs (compute_fixed_point (f , i , ** self .kwargs )- fp )
68- < 1e-4 )
63+ assert_ ( not (abs (compute_fixed_point (f , i , ** self .kwargs )- fp )
64+ < 1e-4 ) )
6965
7066 def test_imitation_game_method (self ):
7167 "compute_fp: Test imitation game method"
@@ -76,22 +72,22 @@ def test_imitation_game_method(self):
7672 for i in self .unit_inverval :
7773 fp_computed = compute_fixed_point (self .T , i , method = method ,
7874 mu = mu , ** self .kwargs )
79- self . assertTrue (
75+ assert_ (
8076 abs (self .T (fp_computed , mu = mu ) - fp_computed ) <= error_tol
8177 )
8278
8379 # numpy array input
8480 i = np .asarray (self .unit_inverval )
8581 fp_computed = compute_fixed_point (self .T , i , method = method , mu = mu ,
8682 ** self .kwargs )
87- self . assertTrue (
83+ assert_ (
8884 abs (self .T (fp_computed , mu = mu ) - fp_computed ).max () <=
8985 error_tol
9086 )
9187
9288
9389class TestComputeFPContraction ():
94- def setUp (self ):
90+ def setup (self ):
9591 self .coeff = 0.5
9692 self .methods = ['iteration' , 'imitation_game' ]
9793
@@ -106,7 +102,7 @@ def test_num_iter_one(self):
106102 fp_computed = compute_fixed_point (self .f , init ,
107103 error_tol = error_tol ,
108104 method = method )
109- ok_ (fp_computed <= error_tol * 2 )
105+ assert_ (fp_computed <= error_tol * 2 )
110106
111107 def test_num_iter_large (self ):
112108 init = 1.
@@ -119,7 +115,7 @@ def test_num_iter_large(self):
119115 error_tol = error_tol ,
120116 max_iter = max_iter , method = method ,
121117 print_skip = max_iter )
122- ok_ (fp_computed <= error_tol * 2 )
118+ assert_ (fp_computed <= error_tol * 2 )
123119
124120 def test_2d_input (self ):
125121 error_tol = self .coeff ** 4
@@ -129,12 +125,11 @@ def test_2d_input(self):
129125 fp_computed = compute_fixed_point (self .f , init ,
130126 error_tol = error_tol ,
131127 method = method )
132- ok_ ((fp_computed <= error_tol * 2 ).all ())
128+ assert_ ((fp_computed <= error_tol * 2 ).all ())
133129
134130
135- @raises (ValueError )
136131def test_raises_value_error_nonpositive_max_iter ():
137132 f = lambda x : 0.5 * x
138133 init = 1.
139134 max_iter = 0
140- compute_fixed_point ( f , init , max_iter = max_iter )
135+ assert_raises ( ValueError , compute_fixed_point , f , init , max_iter = max_iter )
0 commit comments