1""" 2Tests for dle.py file 3""" 4 5import sys 6import unittest 7import numpy as np 8from numpy.testing import assert_allclose 9from quantecon.dle import DLE 10 11ATOL = 1e-10 12 13class TestDLE(unittest.TestCase): 14 15 def setUp(self): 16 """ 17 Given LQ control is tested we will test the transformation 18 to alter the problem into a form suitable to solve using LQ 19 """ 20 # Initial Values 21 gam = 0 22 gamma = np.array([[gam], [0]]) 23 phic = np.array([[1], [0]]) 24 phig = np.array([[0], [1]]) 25 phi1 = 1e-4 26 phii = np.array([[0], [-phi1]]) 27 deltak = np.array([[.95]]) 28 thetak = np.array([[1]]) 29 beta = np.array([[1 / 1.05]]) 30 ud = np.array([[5, 1, 0], [0, 0, 0]]) 31 a22 = np.array([[1, 0, 0], [0, 0.8, 0], [0, 0, 0.5]]) 32 c2 = np.array([[0, 1, 0], [0, 0, 1]]).T 33 llambda = np.array([[0]]) 34 pih = np.array([[1]]) 35 deltah = np.array([[.9]]) 36 thetah = np.array([[1]]) - deltah 37 ub = np.array([[30, 0, 0]]) 38 39 information = (a22, c2, ub, ud) 40 technology = (phic, phig, phii, gamma, deltak, thetak) 41 preferences = (beta, llambda, pih, deltah, thetah) 42 43 self.dle = DLE(information, technology, preferences) 44 45 def tearDown(self): 46 del self.dle 47 48 def test_transformation_Q(self): 49 Q_solution = np.array([[5.e-09]]) 50 assert_allclose(Q_solution, self.dle.Q) 51 52 def test_transformation_R(self): 53 R_solution = np.array([[0., 0., 0., 0., 0.], 54 [0., 0., 0., 0., 0.], 55 [0., 0., 312.5, -12.5, 0.], 56 [0., 0., -12.5, 0.5, 0.], 57 [0., 0., 0., 0., 0.]]) 58 assert_allclose(R_solution, self.dle.R) 59 60 def test_transformation_A(self): 61 A_solution = np.array([[0.9, 0., 0.5, 0.1, 0.], 62 [0., 0.95, 0., 0., 0.], 63 [0., 0., 1., 0., 0.], 64 [0., 0., 0., 0.8, 0.], 65 [0., 0., 0., 0., 0.5]]) 66 assert_allclose(A_solution, self.dle.A) 67 68 def test_transformation_B(self): 69 B_solution = np.array([[-0.], 70 [1.], 71 [0.], 72 [0.], 73 [0.]]) 74 assert_allclose(B_solution, self.dle.B) 75 76 def test_transformation_C(self): 77 C_solution = np.array([[0., 0.], 78 [0., 0.], 79 [0., 0.], 80 [1., 0.], 81 [0., 1.]]) 82 assert_allclose(C_solution, self.dle.C) 83 84 def test_transformation_W(self): 85 W_solution = np.array([[0., 0., 0., 0., 0.]]) 86 assert_allclose(W_solution, self.dle.W) 87 88 def test_compute_steadystate(self): 89 solutions = { 90 'css' : np.array([[5.]]), 91 'sss' : np.array([[5.]]), 92 'iss' : np.array([[0.]]), 93 'dss' : np.array([[5.], [0.]]), 94 'bss' : np.array([[30.]]), 95 'kss' : np.array([[0.]]), 96 'hss' : np.array([[5.]]), 97 } 98 self.dle.compute_steadystate() 99 for item in solutions.keys(): 100 assert_allclose(self.dle.__dict__[ 101 item], solutions[item], atol=ATOL) 102 103 def test_canonical(self): 104 solutions = { 105 'pihat': np.array([[1.]]), 106 'llambdahat': np.array([[-1.48690584e-19]]), 107 'ubhat': np.array([[30., -0., -0.]]) 108 } 109 self.dle.canonical() 110 for item in solutions.keys(): 111 assert_allclose(self.dle.__dict__[ 112 item], solutions[item], atol=ATOL) 113 114 115if __name__ == '__main__': 116 suite = unittest.TestLoader().loadTestsFromTestCase(TestDLE) 117 unittest.TextTestRunner(verbosity=2, stream=sys.stderr).run(suite) 118