def test_equation_validate(self): """ Test the error checking within the solve() function """ model = Model() model.var('x') with self.assertRaises(EquationError) as context: model.solve() self.assertEquals('under-specified', context.exception.errorid)
def test_model_with_function(self): """ Test model with builtin function call test """ model = Model() model.var('x', default=0) model.var('y', default=0) model.add('2*x = 12 - y') model.add('y = if_true(x > 10) + 5') model.solve(iterations=10, threshold=1e-4) self.assertEquals(2, len(model.solutions)) self.assertEquals(0, model.solutions[0]['x']) self.assertEquals(0, model.solutions[0]['y']) self.assertEquals(3.5, model.solutions[1]['x']) self.assertEquals(5, model.solutions[1]['y']) model = Model() model.var('x', default=0) model.var('y', default=0) model.add('2*x = 12 + y') model.add('y = if_true(x > 5)') model.solve(iterations=10, threshold=1e-4) self.assertEquals(2, len(model.solutions)) self.assertEquals(0, model.solutions[0]['x']) self.assertEquals(0, model.solutions[0]['y']) self.assertEquals(6.5, model.solutions[1]['x']) self.assertEquals(1, model.solutions[1]['y'])
def test_model_with_function(self): """ Test model with builtin function call test """ model = Model() model.var('x', default=0) model.var('y', default=0) model.add('2*x = 12 - y') model.add('y = if_true(x > 10) + 5') model.solve(iterations=10, threshold=1e-4) self.assertEquals(2, len(model.solutions)) self.assertEquals(0, model.solutions[0]['x']) self.assertEquals(0, model.solutions[0]['y']) self.assertEquals(3.5, model.solutions[1]['x']) self.assertEquals(5, model.solutions[1]['y']) model = Model() model.var('x', default=0) model.var('y', default=0) model.add('2*x = 12 + y') model.add('y = if_true(x > 5)') model.solve(iterations=10, threshold=1e-4) self.assertEquals(2, len(model.solutions)) self.assertEquals(0, model.solutions[0]['x']) self.assertEquals(0, model.solutions[0]['y']) self.assertEquals(6.5, model.solutions[1]['x']) self.assertEquals(1, model.solutions[1]['y'])
def test_equation_validate(self): """ Test the error checking within the solve() function """ model = Model() model.var('x') with self.assertRaises(EquationError) as context: model.solve() self.assertEquals('under-specified', context.exception.errorid)
def test_model_failure(self): """ Test for divergence """ model = Model() model.var('x', default=1.1) model.var('y', default=2.3) model.add('2*x = 11 - 3*y') model.add('7*y = 13 - 5*x') with self.assertRaises(SolutionNotFoundError): model.solve(iterations=100, threshold=1e-4)
def test_model_failure(self): """ Test for divergence """ model = Model() model.var('x', default=1.1) model.var('y', default=2.3) model.add('2*x = 11 - 3*y') model.add('7*y = 13 - 5*x') with self.assertRaises(SolutionNotFoundError): model.solve(iterations=100, threshold=1e-4)
def test_calculation_error(self): """ Test an error while calculating """ model = Model() model.var('y', default=0) model.var('x', default=0) model.add('y = 2/x') model.add('x = 12') with self.assertRaises(CalculationError) as context: model.solve(iterations=10, threshold=1e-4) self.assertTrue(isinstance(context.exception.inner, ZeroDivisionError))
def test_calculation_error(self): """ Test an error while calculating """ model = Model() model.var('y', default=0) model.var('x', default=0) model.add('y = 2/x') model.add('x = 12') with self.assertRaises(CalculationError) as context: model.solve(iterations=10, threshold=1e-4) self.assertTrue(isinstance(context.exception.inner, ZeroDivisionError))
def test_broyden(self): """ Test solving with Broyden's method, instead of the default Gauss-Seidel """ # pylint: disable=too-many-statements model = Model() model.set_var_default(0) model.vars('Y', 'YD', 'Ts', 'Td', 'Hs', 'Hh', 'Gs', 'Cs', 'Cd', 'Ns', 'Nd') model.set_param_default(0) Gd = model.param('Gd') W = model.param('W') alpha1 = model.param('alpha1') alpha2 = model.param('alpha2') theta = model.param('theta') model.add('Cs = Cd') model.add('Gs = Gd') model.add('Ts = Td') model.add('Ns = Nd') model.add('YD = (W*Ns) - Ts') model.add('Td = theta * W * Ns') model.add('Cd = alpha1*YD + alpha2*Hh(-1)') model.add('Hs - Hs(-1) = Gd - Td') model.add('Hh - Hh(-1) = YD - Cd') model.add('Y = Cs + Gs') model.add('Nd = Y/W') # setup default parameter values Gd.value = 20. W.value = 1.0 alpha1.value = 0.6 alpha2.value = 0.4 theta.value = 0.2 debuglist = [] model.solve(iterations=100, threshold=1e-4, debuglist=debuglist, method='broyden') soln = round_solution(model.solutions[-1], decimals=1) print(soln) self.assertTrue(numpy.isclose(38.5, soln['Y'])) self.assertTrue(numpy.isclose(7.7, soln['Ts'])) self.assertTrue(numpy.isclose(30.8, soln['YD'])) self.assertTrue(numpy.isclose(18.5, soln['Cs'])) self.assertTrue(numpy.isclose(12.3, soln['Hs'])) self.assertTrue(numpy.isclose(12.3, soln['Hh'])) self.assertTrue(numpy.isclose(0, soln['_Hs__1'])) self.assertTrue(numpy.isclose(0, soln['_Hh__1']))
# In[14]: model.add('Nd = Y/W') # The determination of employment. # We now have 11 equations and 11 unknowns. **Each of the eleven unknowns has been set on the left-hand side of an equation** (This implies that we can use the Gauss-Seidel algorithm to iterate to a solution, convergence is not guaranteed but we can try.) # ###### Solve # We have set the default for all of the variables to 0, and that will be used as an initial solution. # In[15]: model.solve(iterations=100, threshold=1e-4) # In[16]: prev = round_solution(model.solutions[-2], decimals=1) solution = round_solution(model.solutions[-1], decimals=1) print("Y : " + str(solution['Y'])) print("T : " + str(solution['Ts'])) print("YD : " + str(solution['YD'])) print("C : " + str(solution['Cs'])) print("Hs-Hs(-1) : " + str(solution['Hs'] - prev['Hs'])) print("Hh-Hh(-1) : " + str(solution['Hh'] - prev['Hh'])) print("H : " + str(solution['Hh'])) # ### The code for the full model
def test_full_model(self): """ Test by implementing a model This model is taken from the book Monetary Economics 2ed, Godley and Lavoie, 2012 Chapter 3, The Simplest Model wtih Government Money Model SIM """ # pylint: disable=too-many-statements model = Model() model.set_var_default(0) model.vars('Y', 'YD', 'Ts', 'Td', 'Hs', 'Hh', 'Gs', 'Cs', 'Cd', 'Ns', 'Nd') model.set_param_default(0) Gd = model.param('Gd') W = model.param('W') alpha1 = model.param('alpha1') alpha2 = model.param('alpha2') theta = model.param('theta') model.add('Cs = Cd') model.add('Gs = Gd') model.add('Ts = Td') model.add('Ns = Nd') model.add('YD = (W*Ns) - Ts') model.add('Td = theta * W * Ns') model.add('Cd = alpha1*YD + alpha2*Hh(-1)') model.add('Hs - Hs(-1) = Gd - Td') model.add('Hh - Hh(-1) = YD - Cd') model.add('Y = Cs + Gs') model.add('Nd = Y/W') # setup default parameter values Gd.value = 20. W.value = 1.0 alpha1.value = 0.6 alpha2.value = 0.4 theta.value = 0.2 model.solve(iterations=200, threshold=1e-3) soln = round_solution(model.solutions[-1], decimals=1) self.assertTrue(numpy.isclose(38.5, soln['Y'])) self.assertTrue(numpy.isclose(7.7, soln['Ts'])) self.assertTrue(numpy.isclose(30.8, soln['YD'])) self.assertTrue(numpy.isclose(18.5, soln['Cs'])) self.assertTrue(numpy.isclose(12.3, soln['Hs'])) self.assertTrue(numpy.isclose(12.3, soln['Hh'])) self.assertTrue(numpy.isclose(0, soln['_Hs__1'])) self.assertTrue(numpy.isclose(0, soln['_Hh__1'])) model.solve(iterations=200, threshold=1e-3) soln = round_solution(model.solutions[-1], decimals=1) self.assertTrue(numpy.isclose(47.9, soln['Y'])) self.assertTrue(numpy.isclose(9.6, soln['Ts'])) self.assertTrue(numpy.isclose(38.3, soln['YD'])) self.assertTrue(numpy.isclose(27.9, soln['Cs'])) self.assertTrue(numpy.isclose(22.7, soln['Hs'])) self.assertTrue(numpy.isclose(22.7, soln['Hh'])) self.assertTrue(numpy.isclose(12.3, soln['_Hs__1'])) self.assertTrue(numpy.isclose(12.3, soln['_Hh__1'])) # Now run until the solutions themselves converge prev_soln = model.solutions[-1] converges = False for _ in range(100): model.solve(iterations=100, threshold=1e-3) # run until we converge soln = model.solutions[-1] if is_close(prev_soln, soln, atol=1e-3): converges = True break prev_soln = soln self.assertTrue(converges) prev = round_solution(model.solutions[-2], decimals=1) soln = round_solution(model.solutions[-1], decimals=1) self.assertTrue(numpy.isclose(100, soln['Y'])) self.assertTrue(numpy.isclose(20, soln['Ts'])) self.assertTrue(numpy.isclose(80, soln['YD'])) self.assertTrue(numpy.isclose(80, soln['Cs'])) self.assertTrue(numpy.isclose(0, soln['Hs'] - prev['Hs'])) self.assertTrue(numpy.isclose(0, soln['Hh'] - prev['Hh']))
def test_full_model(self): """ Test by implementing a model This model is taken from the book Monetary Economics 2ed, Godley and Lavoie, 2012 Chapter 3, The Simplest Model wtih Government Money Model SIM """ # pylint: disable=too-many-statements model = Model() model.set_var_default(0) model.vars('Y', 'YD', 'Ts', 'Td', 'Hs', 'Hh', 'Gs', 'Cs', 'Cd', 'Ns', 'Nd') model.set_param_default(0) Gd = model.param('Gd') W = model.param('W') alpha1 = model.param('alpha1') alpha2 = model.param('alpha2') theta = model.param('theta') model.add('Cs = Cd') model.add('Gs = Gd') model.add('Ts = Td') model.add('Ns = Nd') model.add('YD = (W*Ns) - Ts') model.add('Td = theta * W * Ns') model.add('Cd = alpha1*YD + alpha2*Hh(-1)') model.add('Hs - Hs(-1) = Gd - Td') model.add('Hh - Hh(-1) = YD - Cd') model.add('Y = Cs + Gs') model.add('Nd = Y/W') # setup default parameter values Gd.value = 20. W.value = 1.0 alpha1.value = 0.6 alpha2.value = 0.4 theta.value = 0.2 model.solve(iterations=200, threshold=1e-3) soln = round_solution(model.solutions[-1], decimals=1) self.assertTrue(numpy.isclose(38.5, soln['Y'])) self.assertTrue(numpy.isclose(7.7, soln['Ts'])) self.assertTrue(numpy.isclose(30.8, soln['YD'])) self.assertTrue(numpy.isclose(18.5, soln['Cs'])) self.assertTrue(numpy.isclose(12.3, soln['Hs'])) self.assertTrue(numpy.isclose(12.3, soln['Hh'])) model.solve(iterations=200, threshold=1e-3) soln = round_solution(model.solutions[-1], decimals=1) self.assertTrue(numpy.isclose(47.9, soln['Y'])) self.assertTrue(numpy.isclose(9.6, soln['Ts'])) self.assertTrue(numpy.isclose(38.3, soln['YD'])) self.assertTrue(numpy.isclose(27.9, soln['Cs'])) self.assertTrue(numpy.isclose(22.7, soln['Hs'])) self.assertTrue(numpy.isclose(22.7, soln['Hh'])) # Now run until the solutions themselves converge prev_soln = model.solutions[-1] converges = False for _ in xrange(100): model.solve(iterations=100, threshold=1e-3) # run until we converge soln = model.solutions[-1] if is_close(prev_soln, soln, atol=1e-3): converges = True break prev_soln = soln self.assertTrue(converges) prev = round_solution(model.solutions[-2], decimals=1) soln = round_solution(model.solutions[-1], decimals=1) self.assertTrue(numpy.isclose(100, soln['Y'])) self.assertTrue(numpy.isclose(20, soln['Ts'])) self.assertTrue(numpy.isclose(80, soln['YD'])) self.assertTrue(numpy.isclose(80, soln['Cs'])) self.assertTrue(numpy.isclose(0, soln['Hs'] - prev['Hs'])) self.assertTrue(numpy.isclose(0, soln['Hh'] - prev['Hh']))