Exemplo n.º 1
0
 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)
Exemplo n.º 2
0
 def test_set_var_default(self):
     """ Test the set_var_default """
     model = Model()
     var = model.var('test')
     self.assertEquals(None, var.default)
     model.set_var_default(12)
     var = model.var('test2')
     self.assertEquals(12, var.default)
Exemplo n.º 3
0
    def test_series_get_at_errors(self):
        """ Test bad parameters to get_at """
        model = Model()
        varx = model.var('x')
        vary = model.var('y')

        with self.assertRaises(EquationError):
            model.get_at(varx, vary)
Exemplo n.º 4
0
    def test_series_get_at_errors(self):
        """ Test bad parameters to get_at """
        model = Model()
        varx = model.var('x')
        vary = model.var('y')

        with self.assertRaises(EquationError):
            model.get_at(varx, vary)
Exemplo n.º 5
0
 def test_param_default(self):
     """ Test the set_param_default """
     model = Model()
     param = model.param('test')
     self.assertEquals(None, param.default)
     model.set_param_default(122)
     param = model.param('test2')
     self.assertEquals(122, param.default)
Exemplo n.º 6
0
    def test_rule_with_coefficients(self):
        """ Test creating rules with simple coefficents """
        model = Model()
        model.var('x')
        model.var('y')
        model.add('2*x = 3*y')

        self.assertEquals(2, len(model.variables))
        self.assertEquals(0, len(model.parameters))
Exemplo n.º 7
0
 def test_series_derivative(self):
     model = Model()
     varx = model.var('x')
     vary = model.var('y')
     equation = model.add('x = y + x(-1)')
     df = equation.expr.diff(varx)
     self.assertEquals(0, df)
     df = equation.expr.diff(vary)
     self.assertEquals(1, df)
Exemplo n.º 8
0
    def test_evaluate_series_access(self):
        """ Test evaluation with series accessor """
        model = Model()
        model.var('x', default=1)
        model.var('y', default=10)
        model.param('a', default=.5)
        model.solutions = [{'x': 2, 'y': 11, 'a': 60}]

        self.assertEquals(11, model.evaluate('y(-1)'))
        self.assertEquals(73, model.evaluate('x(-1) + y(-1) + a(-1)'))
Exemplo n.º 9
0
 def test_calling_sympy_functions(self):
     """ Test the calling of sympy functions """
     model = Model()
     model.var('x')
     model.param('a')
     model.set_values({'x': 12, 'a': 5})
     self.assertEquals(1.1, model.evaluate('Max(1, 1.1)'))
Exemplo n.º 10
0
    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))
Exemplo n.º 11
0
    def test_evaluate(self):
        """ Test arbitrary function evaluation """
        model = Model()
        model.var('x', default=1)
        model.var('y', default=10)
        model.param('a', default=.5)

        self.assertEquals(11, model.evaluate('x+y'))
Exemplo n.º 12
0
    def test_rule(self):
        """ Test creating rules """
        model = Model()
        model.var('x')
        model.var('y')
        eqn = model.add('x = y')

        self.assertEquals(2, len(model.variables))
        self.assertEquals(0, len(model.parameters))
        self.assertIsNotNone(model.variables['x'].equation)
        self.assertIsNone(model.variables['y'].equation)

        self.assertEquals(1, len(model.equations))
        self.assertEquals(eqn, model.equations[0])
Exemplo n.º 13
0
    def test_rule(self):
        """ Test creating rules """
        model = Model()
        model.var('x')
        model.var('y')
        eqn = model.add('x = y')

        self.assertEquals(2, len(model.variables))
        self.assertEquals(0, len(model.parameters))
        self.assertIsNotNone(model.variables['x'].equation)
        self.assertIsNone(model.variables['y'].equation)

        self.assertEquals(1, len(model.equations))
        self.assertEquals(eqn, model.equations[0])
Exemplo n.º 14
0
    def test_delta_error(self):
        model = Model()
        model.var('x', default=-1)
        model.var('y', default=10)
        model.param('a', default=.5)
        model.solutions = [{'x': 2, 'y': 11, 'a': 60}]
        model.variables['x'].value = 5

        with self.assertRaises(EquationError) as context:
            model.evaluate('d(-1)')
        self.assertEquals('d-arg-not-a-variable', context.exception.errorid)
Exemplo n.º 15
0
    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))
Exemplo n.º 16
0
    def test_set_parameters_equation(self):
        """ Test set_values of parameters using equations """
        model = Model()
        model.param('a', default=-1)
        model.var('x', default=0)

        model.set_values({'a': 'x+12'})
        self.assertEquals(12, model.parameters['a'].value)
Exemplo n.º 17
0
    def test_set_variables_equation(self):
        """ Test set_values of variables using equations """
        model = Model()
        model.param('a', default=-1)
        model.var('x', default=0)

        model.set_values({'x': 'a+12'})
        self.assertEquals(11, model.variables['x'].value)
Exemplo n.º 18
0
    def test_evaluate(self):
        """ Test arbitrary function evaluation """
        model = Model()
        model.var('x', default=1)
        model.var('y', default=10)
        model.param('a', default=.5)

        self.assertEquals(11, model.evaluate('x+y'))
Exemplo n.º 19
0
    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)
Exemplo n.º 20
0
    def test_delta(self):
        """ test the d() function """
        model = Model()
        model.var('x', default=-1)
        model.var('y', default=10)
        model.param('a', default=.5)
        model.solutions = [{'x': 2, 'y': 11, 'a': 60}]

        model.variables['x'].value = 5
        self.assertEquals(3, model.evaluate('d(x)'))
Exemplo n.º 21
0
    def test_series_get_at(self):
        """ Test the series accessor creation, get_at() """
        model = Model()
        varx = model.var('x')
        param = model.get_at(varx, 0)
        self.assertIsNotNone(param)
        self.assertEquals('_x_0', param.name)
        self.assertEquals(0, param.iteration)
        self.assertEquals(varx, param.variable)
        self.assertTrue(param.name in model._private_parameters)

        param = model.get_at(varx, 1)
        self.assertIsNotNone(param)
        self.assertEquals('_x_1', param.name)
        self.assertEquals(1, param.iteration)
        self.assertEquals(varx, param.variable)
        self.assertTrue(param.name in model._private_parameters)

        param = model.get_at(varx, -1)
        self.assertIsNotNone(param)
        self.assertEquals('_x__1', param.name)
        self.assertEquals(-1, param.iteration)
        self.assertEquals(varx, param.variable)
        self.assertTrue(param.name in model._private_parameters)

        param = model.get_at(varx, 10000)
        self.assertIsNotNone(param)
        self.assertEquals('_x_10000', param.name)
        self.assertEquals(10000, param.iteration)
        self.assertEquals(varx, param.variable)
        self.assertTrue(param.name in model._private_parameters)
Exemplo n.º 22
0
 def test_if_true(self):
     """ Test the if_true builtin function """
     model = Model()
     model.var('x', default=12)
     model.var('y', default=3131)
     self.assertEquals(0, model.evaluate('if_true(x > 1000)'))
     self.assertEquals(1, model.evaluate('if_true(y > 1000)'))
Exemplo n.º 23
0
    def test_setting_multiple_variables(self):
        """ Test set_values() with multiple variables """
        model = Model()
        model.param('a', default=3)
        model.param('b', default=4)
        model.var('x', default=-1)
        model.var('y', default=-2)

        model.set_values({'a': 'a+11', 'b': '4*b'})
        self.assertEquals(14, model.parameters['a'].value)
        self.assertEquals(16, model.parameters['b'].value)
Exemplo n.º 24
0
    def test_set_variables_equation(self):
        """ Test set_values of variables using equations """
        model = Model()
        model.param('a', default=-1)
        model.var('x', default=0)

        model.set_values({'x': 'a+12'})
        self.assertEquals(11, model.variables['x'].value)
Exemplo n.º 25
0
    def test_set_parameters_equation(self):
        """ Test set_values of parameters using equations """
        model = Model()
        model.param('a', default=-1)
        model.var('x', default=0)

        model.set_values({'a': 'x+12'})
        self.assertEquals(12, model.parameters['a'].value)
Exemplo n.º 26
0
    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)
Exemplo n.º 27
0
    def test_set_variables(self):
        """ Test set_values of variables """
        model = Model()
        var_x = model.var('x', default=-1)
        self.assertEquals(-1, var_x.value)
        model.set_values({'x': 22})
        self.assertEquals(22, var_x.value)

        with self.assertRaises(ValueError):
            model.set_values({'zz': -1})
Exemplo n.º 28
0
    def test_set_parameters(self):
        """ Test set_values of parameters """
        model = Model()
        param_a = model.param('a', default=-1)
        self.assertEquals(-1, param_a.value)
        model.set_values({'a': 22})
        self.assertEquals(22, param_a.value)

        with self.assertRaises(ValueError):
            model.set_values({'zz': -1})
Exemplo n.º 29
0
 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)
Exemplo n.º 30
0
 def test_if_true(self):
     """ Test the if_true builtin function """
     model = Model()
     model.var('x', default=12)
     model.var('y', default=3131)
     self.assertEquals(0, model.evaluate('if_true(x > 1000)'))
     self.assertEquals(1, model.evaluate('if_true(y > 1000)'))
Exemplo n.º 31
0
    def test_series_get_at(self):
        """ Test the series accessor creation, get_at() """
        model = Model()
        varx = model.var('x')
        param = model.get_at(varx, 0)
        self.assertIsNotNone(param)
        self.assertEquals('_x_0', param.name)
        self.assertEquals(0, param.iteration)
        self.assertEquals(varx, param.variable)
        self.assertTrue(param.name in model._private_parameters)

        param = model.get_at(varx, 1)
        self.assertIsNotNone(param)
        self.assertEquals('_x_1', param.name)
        self.assertEquals(1, param.iteration)
        self.assertEquals(varx, param.variable)
        self.assertTrue(param.name in model._private_parameters)

        param = model.get_at(varx, -1)
        self.assertIsNotNone(param)
        self.assertEquals('_x__1', param.name)
        self.assertEquals(-1, param.iteration)
        self.assertEquals(varx, param.variable)
        self.assertTrue(param.name in model._private_parameters)

        param = model.get_at(varx, 10000)
        self.assertIsNotNone(param)
        self.assertEquals('_x_10000', param.name)
        self.assertEquals(10000, param.iteration)
        self.assertEquals(varx, param.variable)
        self.assertTrue(param.name in model._private_parameters)
Exemplo n.º 32
0
 def test_param_default(self):
     """ Test the set_param_default """
     model = Model()
     param = model.param('test')
     self.assertEquals(None, param.default)
     model.set_param_default(122)
     param = model.param('test2')
     self.assertEquals(122, param.default)
Exemplo n.º 33
0
 def test_set_var_default(self):
     """ Test the set_var_default """
     model = Model()
     var = model.var('test')
     self.assertEquals(None, var.default)
     model.set_var_default(12)
     var = model.var('test2')
     self.assertEquals(12, var.default)
Exemplo n.º 34
0
    def test_rule_with_coefficients(self):
        """ Test creating rules with simple coefficents """
        model = Model()
        model.var('x')
        model.var('y')
        model.add('2*x = 3*y')

        self.assertEquals(2, len(model.variables))
        self.assertEquals(0, len(model.parameters))
Exemplo n.º 35
0
    def test_param(self):
        """ Test parameter creation """
        model = Model()
        param_x = model.param('x')
        self.assertIsNotNone(param_x)
        with self.assertRaises(DuplicateNameError):
            model.var('x')

        with self.assertRaises(DuplicateNameError):
            model.param('x')

        self.assertTrue('x' not in model.variables)
        self.assertTrue('x' in model.parameters)
Exemplo n.º 36
0
    def test_set_variables(self):
        """ Test set_values of variables """
        model = Model()
        var_x = model.var('x', default=-1)
        self.assertEquals(-1, var_x.value)
        model.set_values({'x': 22})
        self.assertEquals(22, var_x.value)

        with self.assertRaises(ValueError):
            model.set_values({'zz': -1})
Exemplo n.º 37
0
    def test_set_parameters(self):
        """ Test set_values of parameters """
        model = Model()
        param_a = model.param('a', default=-1)
        self.assertEquals(-1, param_a.value)
        model.set_values({'a': 22})
        self.assertEquals(22, param_a.value)

        with self.assertRaises(ValueError):
            model.set_values({'zz': -1})
Exemplo n.º 38
0
    def test_param(self):
        """ Test parameter creation """
        model = Model()
        param_x = model.param('x')
        self.assertIsNotNone(param_x)
        with self.assertRaises(DuplicateNameError):
            model.var('x')

        with self.assertRaises(DuplicateNameError):
            model.param('x')

        self.assertTrue('x' not in model.variables)
        self.assertTrue('x' in model.parameters)
Exemplo n.º 39
0
    def test_update_solutions(self):
        """ Test _update_solutions function """
        model = Model()
        varx = model.var('x')
        vary = model.var('y')

        model._update_solutions({'x': 1.1, 'y': 2.2})
        self.assertEquals(1, len(model.solutions))
        self.assertEquals(1.1, varx.value)
        self.assertTrue('x' in model.solutions[0])
        self.assertEquals(1.1, model.solutions[0]['x'])
        self.assertEquals(2.2, vary.value)
        self.assertTrue('y' in model.solutions[0])
        self.assertEquals(2.2, model.solutions[0]['y'])

        model._update_solutions({'x': 3.3, 'y': 4.4})
        self.assertEquals(2, len(model.solutions))
        self.assertEquals(3.3, varx.value)
        self.assertTrue('x' in model.solutions[1])
        self.assertEquals(3.3, model.solutions[1]['x'])
        self.assertEquals(4.4, vary.value)
        self.assertTrue('y' in model.solutions[1])
        self.assertEquals(4.4, model.solutions[1]['y'])
Exemplo n.º 40
0
    def test_update_solutions(self):
        """ Test _update_solutions function """
        model = Model()
        varx = model.var('x')
        vary = model.var('y')

        model._update_solutions({'x': 1.1, 'y': 2.2})
        self.assertEquals(1, len(model.solutions))
        self.assertEquals(1.1, varx.value)
        self.assertTrue('x' in model.solutions[0])
        self.assertEquals(1.1, model.solutions[0]['x'])
        self.assertEquals(2.2, vary.value)
        self.assertTrue('y' in model.solutions[0])
        self.assertEquals(2.2, model.solutions[0]['y'])

        model._update_solutions({'x': 3.3, 'y': 4.4})
        self.assertEquals(2, len(model.solutions))
        self.assertEquals(3.3, varx.value)
        self.assertTrue('x' in model.solutions[1])
        self.assertEquals(3.3, model.solutions[1]['x'])
        self.assertEquals(4.4, vary.value)
        self.assertTrue('y' in model.solutions[1])
        self.assertEquals(4.4, model.solutions[1]['y'])
Exemplo n.º 41
0
# ###### Preliminaries

# In order to build the model, we must first start off by importing several modules that will be used to build the model. *pysolve* is a Python module that I have developed to make it easier to specify and solve linear models.
#
# The first line will import the main Model class.  The second line imports several utility functions that will prove useful.

# In[2]:

# ###### Create the model

# The first step when developing a pysolve model is to create the model.  This is just an empty model for now, but we will be adding the rest of the information to this.

# In[3]:

model = Model()

# ###### Define the variables

# The second step is to define the (endogenous) variables.  These are the variables that we are allowed to manipulate within the model.  This is pretty straigtforward.
#
# As a useful step, I define the default value for all variables.  This can be changed on an individual basis.  This is the value that the variable will start off with if nothing is changed.

# In[4]:

model.set_var_default(0)

# Next, we create the variables used by the sim.  Most of these have been explained above.

# In[5]:
Exemplo n.º 42
0
def create_simex_model():
    """ Create the SIMEX model """
    model = Model()

    model.set_var_default(0)
    model.var('Cd', desc='Consumption goods demand by households')
    model.var('Cs', desc='Consumption goods supply')
    model.var('Gs', desc='Government goods, supply')
    model.var('Hd', desc='Cash money demanded by households')
    model.var('Hh', desc='Cash money held by households')
    model.var('Hs', desc='Cash money supplied by the government')
    model.var('Nd', desc='Demand for labor')
    model.var('Ns', desc='Supply of labor')
    model.var('Td', desc='Taxes, demand')
    model.var('Ts', desc='Taxes, supply')
    model.var('Y', desc='Income = GDP')
    model.var('YD', desc='Disposable income of households')
    model.var('YDe', desc='Expected disposable income')

    model.set_param_default(0)
    model.param('Gd', desc='Government goods, demand')
    model.param('W', desc='Wage rate')
    model.param('alpha1', desc='Propensity to consume out of income')
    model.param('alpha2', desc='Propensity to consume o of wealth')
    model.param('theta', desc='Tax rate')

    model.add('Cs = Cd')  # 3.1
    model.add('Gs = Gd')  # 3.2
    model.add('Ts = Td')  # 3.3
    model.add('Ns = Nd')  # 3.4
    model.add('YD = (W*Ns) - Ts')  # 3.5
    model.add('Td = theta * W * Ns')  # 3.6, theta < 1.0
    model.add('Cd = alpha1*YDe + alpha2*Hh(-1)')  # 3.7E
    model.add('Hs - Hs(-1) =  Gd - Td')  # 3.8
    model.add('Hh - Hh(-1) = YD - Cd')  # 3.9
    model.add('Hd - Hs(-1) = YDe - Cd')  # 3.18
    model.add('Y = Cs + Gs')  # 3.10
    model.add('Nd = Y/W')  # 3.11
    model.add('YDe = YD(-1)')   # 3.20

    return model
Exemplo n.º 43
0
    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']))
Exemplo n.º 44
0
    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']))
Exemplo n.º 45
0
    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'])
Exemplo n.º 46
0
    def test_get_value(self):
        """ Test the get_value function """
        model = Model()
        varx = model.var('x', default=-1)
        vary = model.var('y')

        model._update_solutions({'x': 1.1, 'y': 2.2})
        model._update_solutions({'x': 3.3, 'y': 4.4})

        self.assertEquals(1.1, model.get_value(varx, 0))
        self.assertEquals(1.1, model.get_value(varx, -2))
        self.assertEquals(4.4, model.get_value(vary, 1))
        self.assertEquals(4.4, model.get_value(vary, -1))

        with self.assertRaises(IndexError):
            self.assertEquals(-1, model.get_value(varx, -1000))

        with self.assertRaises(IndexError):
            self.assertEquals(-1, model.get_value(varx, 1000))
Exemplo n.º 47
0
    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'])
Exemplo n.º 48
0
 def test_model(self):
     """ Create an empty model class """
     model = Model()
     self.assertIsNotNone(model)
Exemplo n.º 49
0
    def test_evaluate_series_access(self):
        """ Test evaluation with series accessor """
        model = Model()
        model.var('x', default=1)
        model.var('y', default=10)
        model.param('a', default=.5)
        model.solutions = [{'x': 2, 'y': 11, 'a': 60}]

        self.assertEquals(11, model.evaluate('y(-1)'))
        self.assertEquals(73, model.evaluate('x(-1) + y(-1) + a(-1)'))
Exemplo n.º 50
0
def create_lp_model():
    """ Creates model LP """
    # pylint: disable=too-many-statements
    model = Model()

    model.set_var_default(0)
    model.var('Bcb', desc='Government bills held by the Central Bank')
    model.var('Bd', desc='Demand for government bills')
    model.var('Bh', desc='Government bills held by households')
    model.var('Bs', desc='Government bills supplied by government')
    model.var('BLd', desc='Demand for government bonds')
    model.var('BLh', desc='Government bonds held by households')
    model.var('BLs', desc='Supply of government bonds')
    model.var('CG', desc='Capital gains on bonds')
    model.var('CGe', desc='Expected capital gains on bonds')
    model.var('C', desc='Consumption')
    model.var('ERrbl', desc='Expected rate of return on bonds')
    model.var('Hd', desc='Demand for cash')
    model.var('Hh', desc='Cash held by households')
    model.var('Hs', desc='Cash supplied by the central bank')
    model.var('Pbl', desc='Price of bonds')
    model.var('Pble', desc='Expected price of bonds')
    model.var('Rb', desc='Interest rate on government bills')
    model.var('Rbl', desc='Interest rate on government bonds')
    model.var('T', desc='Taxes')
    model.var('V', desc='Household wealth')
    model.var('Ve', desc='Expected household wealth')
    model.var('Y', desc='Income = GDP')
    model.var('YDr', desc='Regular disposable income of households')
    model.var('YDre', desc='Expected regular disposable income of households')

    model.set_param_default(0)
    model.param('alpha1', desc='Propensity to consume out of income')
    model.param('alpha2', desc='Propensit to consume out of wealth')
    model.param('chi', desc='Weight of conviction in expected bond price')
    model.param('lambda10', desc='Parameter in asset demand function')
    model.param('lambda12', desc='Parameter in asset demand function')
    model.param('lambda13', desc='Parameter in asset demand function')
    model.param('lambda14', desc='Parameter in asset demand function')
    model.param('lambda20', desc='Parameter in asset demand function')
    model.param('lambda22', desc='Parameter in asset demand function')
    model.param('lambda23', desc='Parameter in asset demand function')
    model.param('lambda24', desc='Parameter in asset demand function')
    model.param('lambda30', desc='Parameter in asset demand function')
    model.param('lambda32', desc='Parameter in asset demand function')
    model.param('lambda33', desc='Parameter in asset demand function')
    model.param('lambda34', desc='Parameter in asset demand function')
    model.param('theta', desc='Tax rate')

    model.param('G', desc='Government goods')
    model.param('Rbar', desc='Exogenously set interest rate on govt bills')
    model.param('Pblbar', desc='Exogenously set price of bonds')

    model.add('Y = C + G')                                  # 5.1
    model.add('YDr = Y - T + Rb(-1)*Bh(-1) + BLh(-1)')      # 5.2
    model.add('T = theta *(Y + Rb(-1)*Bh(-1) + BLh(-1))')    # 5.3
    model.add('V - V(-1) = (YDr - C) + CG')                 # 5.4
    model.add('CG = (Pbl - Pbl(-1))*BLh(-1)')
    model.add('C = alpha1*YDre + alpha2*V(-1)')
    model.add('Ve = V(-1) + (YDre - C) + CG')
    model.add('Hh = V - Bh - Pbl*BLh')
    model.add('Hd = Ve - Bd - Pbl*BLd')
    model.add('Bd = Ve*lambda20 + Ve*lambda22*Rb - ' +
              'Ve*lambda23*ERrbl - lambda24*YDre')
    model.add('BLd = (Ve*lambda30 - Ve*lambda32*Rb ' +
              ' + Ve*lambda33*ERrbl - lambda34*YDre)/Pbl')
    model.add('Bh = Bd')
    model.add('BLh = BLd')
    model.add('Bs - Bs(-1) = (G + Rb(-1)*Bs(-1) + ' +
              'BLs(-1)) - (T + Rb(-1)*Bcb(-1)) - (BLs - BLs(-1))*Pbl')
    model.add('Hs - Hs(-1) = Bcb - Bcb(-1)')
    model.add('Bcb = Bs - Bh')
    model.add('BLs = BLh')
    model.add('ERrbl = Rbl + chi * (Pble - Pbl) / Pbl')
    model.add('Rbl = 1./Pbl')
    model.add('Pble = Pbl')
    model.add('CGe = chi * (Pble - Pbl)*BLh')
    model.add('YDre = YDr(-1)')
    model.add('Rb = Rbar')
    model.add('Pbl = Pblbar')

    # if_true(x) returns 1 if x is true, else 0 is returned
    model.add('z1 = if_true(tp > top)')
    model.add('z2 = if_true(tp < bot)')
    return model
Exemplo n.º 51
0
    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']))
Exemplo n.º 52
0
    def test_get_value(self):
        """ Test the get_value function """
        model = Model()
        varx = model.var('x', default=-1)
        vary = model.var('y')

        model._update_solutions({'x': 1.1, 'y': 2.2})
        model._update_solutions({'x': 3.3, 'y': 4.4})

        self.assertEquals(1.1, model.get_value(varx, 0))
        self.assertEquals(1.1, model.get_value(varx, -2))
        self.assertEquals(4.4, model.get_value(vary, 1))
        self.assertEquals(4.4, model.get_value(vary, -1))

        with self.assertRaises(IndexError):
            self.assertEquals(-1, model.get_value(varx, -1000))

        with self.assertRaises(IndexError):
            self.assertEquals(-1, model.get_value(varx, 1000))
Exemplo n.º 53
0
def create_sim_model():
    model = Model()

    model.set_var_default(0)
    model.var('Cd', desc='Consumption goods demand by households')
    model.var('Cs', desc='Consumption goods supply')
    model.var('Gs', desc='Government goods, supply')
    model.var('Hh', desc='Cash money held by households')
    model.var('Hs', desc='Cash money supplied by the government')
    model.var('Nd', desc='Demand for labor')
    model.var('Ns', desc='Supply of labor')
    model.var('Td', desc='Taxes, demand')
    model.var('Ts', desc='Taxes, supply')
    model.var('Y', desc='Income = GDP')
    model.var('YD', desc='Disposable income of households')

    model.param('Gd', desc='Government goods, demand')
    model.param('W', desc='Wage rate')
    model.param('alpha1', desc='Propensity to consume out of income')
    model.param('alpha2', desc='Propensity to consume out of wealth')
    model.param('theta', desc='Tax rate')

    model.add('Cs = Cd')  # 3.1
    model.add('Gs = Gd')  # 3.2
    model.add('Ts = Td')  # 3.3
    model.add('Ns = Nd')  # 3.4
    model.add('YD = (W*Ns) - Ts')  # 3.5
    model.add('Td = theta * W * Ns')  # 3.6, theta < 1.0
    model.add('Cd = alpha1*YD + alpha2*Hh(-1)')  # 3.7, 0 < alpha2 < alpha1 < 1
    model.add('Hs - Hs(-1) =  Gd - Td')  # 3.8
    model.add('Hh - Hh(-1) = YD - Cd')  # 3.9
    model.add('Y = Cs + Gs')  # 3.10
    model.add('Nd = Y/W')  # 3.11

    return model
Exemplo n.º 54
0
def create_model():
    """ Creates model SIM """
    model = Model()

    model.set_var_default(0)
    model.var('Cd', desc='Consumption goods demand by households')
    model.var('Cs', desc='Consumption goods supply')
    model.var('Gs', desc='Government goods, supply')
    model.var('Hh', desc='Cash money held by households')
    model.var('Hs', desc='Cash money supplied by the government')
    model.var('Nd', desc='Demand for labor')
    model.var('Ns', desc='Supply of labor')
    model.var('Td', desc='Taxes, demand')
    model.var('Ts', desc='Taxes, supply')
    model.var('Y', desc='Income = GDP')
    model.var('YD', desc='Disposable income of households')

    # This is a shorter way to declare multiple variables
    # model.vars('Y', 'YD', 'Ts', 'Td', 'Hs', 'Hh', 'Gs', 'Cs',
    #            'Cd', 'Ns', 'Nd')
    model.param('Gd', desc='Government goods, demand', default=20)
    model.param('W', desc='Wage rate', default=1)
    model.param('alpha1',
                desc='Propensity to consume out of income',
                default=0.6)
    model.param('alpha2',
                desc='Propensity to consume o of wealth',
                default=0.4)
    model.param('theta', desc='Tax rate', default=0.2)

    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')

    return model