Ejemplo n.º 1
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))
Ejemplo n.º 2
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))
Ejemplo n.º 3
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)
Ejemplo n.º 4
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)
Ejemplo n.º 5
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))
Ejemplo n.º 6
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))
Ejemplo n.º 7
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'])
Ejemplo n.º 8
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'])
Ejemplo n.º 9
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)
Ejemplo n.º 10
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])
Ejemplo n.º 11
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])
Ejemplo n.º 12
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']))
Ejemplo n.º 13
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
Ejemplo n.º 14
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
Ejemplo n.º 15
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
Ejemplo n.º 16
0
# In[6]:

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)

# ###### Define the equations

# Adding an equation is just adding the textual form of the equation.  There are some restrictions. Linear systems only.

# In[7]:

model.add('Cs = Cd')
model.add('Gs = Gd')
model.add('Ts = Td')
model.add('Ns = Nd')

# These four equations imply that demand equals supply for this period, no supply constraints of any kind.

# In[8]:

model.add('YD = (W*Ns) - Ts')

# Disposable income (*YD*) is the wages earned by households minus taxes.

# In[9]:

model.add('Td = theta * W * Ns')
Ejemplo n.º 17
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']))
Ejemplo n.º 18
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']))
Ejemplo n.º 19
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
Ejemplo n.º 20
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
Ejemplo n.º 21
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