def test_ctor_default(self): mod = Model() can = Country(mod, 'CA', 'Canada', currency='LOC') marca = Market(can, 'GOOD', 'market') us = Country(mod, 'US', 'US', currency='LOC') marus = Market(us, 'GOOD', 'market') bus = FixedMarginBusinessMultiOutput(can, 'BUS', 'Business', market_list=[marca, marus]) bus2 = FixedMarginBusinessMultiOutput(us, 'BUS', 'Business', market_list=[marca, marus], profit_margin=.1) self.assertIn('SUP_GOOD', bus.EquationBlock.Equations) self.assertIn('SUP_US_GOOD', bus.EquationBlock.Equations) marus.AddSupplier(bus2, 'allocation_equation') marus.AddSupplier(bus) #marus.SupplyAllocation = [[[bus2, 'allocation_equation'],], bus] mod._GenerateFullSectorCodes() marus._GenerateEquations() self.assertFalse(marus.ShareParent(bus)) self.assertEqual('US_GOOD__SUP_CA_BUS', bus.EquationBlock['SUP_US_GOOD'].RHS()) self.assertEqual('US_GOOD__SUP_US_BUS', bus2.EquationBlock['SUP_GOOD'].RHS()) bus2._GenerateEquations() self.assertEqual('0.900*SUP', bus2.EquationBlock['DEM_LAB'].RHS()) self.assertEqual('SUP_CA_GOOD+SUP_GOOD', bus2.EquationBlock['SUP'].RHS())
def test_GetVariableName_3(self): mod = Model() us = Country(mod, 'US', 'USA') hh = Sector(us, 'HH', 'Household') mod._GenerateFullSectorCodes() with self.assertRaises(KeyError): hh.GetVariableName('Kaboom')
def test_all(self): mod = Model() can = Country(mod, 'Eh', 'Canada') gov = ConsolidatedGovernment(can, 'GOV', 'Government') hou = Household(can, 'HH', 'Household', .5) dummy = Sector(can, 'DUM', 'Dummy') mm = MoneyMarket(can) dep = DepositMarket(can) # Need to add demand functions in household sector mod._GenerateFullSectorCodes() hou.AddVariable('DEM_MON', 'Demand for Money', '0.5 * ' + hou.GetVariableName('F')) hou.AddVariable('DEM_DEP', 'Demand for Deposits', '0.5 * ' + hou.GetVariableName('F')) mod._GenerateEquations() # Supply = Demand self.assertEqual('GOV__SUP_DEP', dep.EquationBlock['SUP_DEP'].RHS()) # Demand = Demand of two sectors self.assertEqual('HH__DEM_DEP', dep.EquationBlock['DEM_DEP'].RHS().replace(' ', '')) # At the sector level, demand = F self.assertEqual('0.5*HH__F', kill_spaces(hou.EquationBlock['DEM_MON'].RHS())) self.assertEqual('0.5*HH__F', kill_spaces(hou.EquationBlock['DEM_DEP'].RHS())) # Make sure the dummy does not have cash flows self.assertEqual('LAG_F', dummy.EquationBlock['F'].RHS()) # Household has a deposit interest cash flow self.assertIn('INTDEP', hou.EquationBlock['F'].RHS())
def test_IncomeEquation_2(self): mod = Model() us = Country(mod, 'US', 'US') hh = Sector(us, 'HH', 'HH', has_F=True) hh.AddCashFlow('x', eqn='2.0', is_income=False) mod._GenerateFullSectorCodes() self.assertEqual('0.0', hh.EquationBlock['INC'].RHS())
def test_GetVariableName_1(self): mod = Model() us = Country(mod, 'US', 'USA') household = Household(us, 'HH', 'Household', .9) mod._GenerateFullSectorCodes() household._GenerateEquations() self.assertEqual(household.GetVariableName('AlphaFin'), 'HH__AlphaFin')
def test_GenerateEquations_2_supply_multicountry_3(self): mod = Model() # Need to stay in the same currency zone can = Country(mod, 'CA', 'Canada, Eh?', currency='LOC') US = Country(mod, 'US', 'USA! USA!', currency='LOC') mar = Market(can, 'LAB', 'Market') bus = Sector(can, 'BUS', 'Business') hh = Sector(can, 'HH', 'Household') # Somehow, Americans are supplying labour in Canada... hh2 = Sector(US, 'HH2', 'Household') hh3 = Sector(US, 'HH3', 'Household#3') bus.AddVariable('DEM_LAB', 'desc', 'x') hh.AddVariable('SUP_LAB', 'desc 2', '') hh2.AddVariable('SUP_CA_LAB', 'desc 2', '') hh3.AddVariable('SUP_CA_LAB', 'desc 2', '') mod._GenerateFullSectorCodes() #mar.SupplyAllocation = [[(hh, 'SUP_LAB/2'), (hh3, '0.')], hh2] mar.AddSupplier(hh2) mar.AddSupplier(hh, 'SUP_LAB/2') mar.AddSupplier(hh3, '0.') mar._GenerateEquations() self.assertEqual('SUP_LAB/2', mar.EquationBlock['SUP_CA_HH'].RHS()) self.assertEqual('SUP_LAB-SUP_CA_HH-SUP_US_HH3', kill_spaces(mar.EquationBlock['SUP_US_HH2'].RHS())) self.assertEqual('CA_LAB__SUP_CA_HH', hh.EquationBlock['SUP_LAB'].RHS()) self.assertIn('SUP_LAB', hh.EquationBlock['F'].RHS()) self.assertEqual('CA_LAB__SUP_US_HH2', hh2.EquationBlock['SUP_CA_LAB'].RHS()) self.assertIn('SUP_CA_LAB', hh2.EquationBlock['F'].RHS()) self.assertIn('SUP_CA_LAB', hh3.EquationBlock['F'].RHS())
def test_GenerateEquations(self): mod = Model() can = Country(mod, 'Eh', 'Canada') tre = Treasury(can, 'TRE', 'Treasury') cb = CentralBank(can, 'CB', 'Central Bank', tre) mod._GenerateFullSectorCodes() cb._GenerateEquations() self.assertEqual(cb.Treasury, tre)
def test_GenerateEquations(self): mod = Model() can = Country(mod, 'Eh', 'Canada') bus = FixedMarginBusiness(can, 'BUS', 'Business', profit_margin=0.1) mar = Market(can, 'GOOD', 'market') mod._GenerateFullSectorCodes() bus._GenerateEquations() self.assertEqual('0.900*GOOD__SUP_GOOD', bus.EquationBlock['DEM_LAB'].RHS().replace(' ', ''))
def test_GetCrossRate_CurrencyZone(self): mod = Model() ext = ExternalSector(mod) c1 = Country(mod, 'C1', 'C1') c2 = Country(mod, 'C2', 'C2') mod._GenerateFullSectorCodes() xr = ext['XR'] xrate = ext.GetCrossRate(c1.CurrencyZone, c2.CurrencyZone) self.assertEqual('EXT_XR__C1_C2', xrate)
def test_GenerateEquations_no_supply(self): mod = Model() can = Country(mod, 'Eh', 'Canada') mar = Market(can, 'LAB', 'Market') bus = Sector(can, 'BUS', 'Business') bus.AddVariable('DEM_LAB', 'desc', '') mod._GenerateFullSectorCodes() with self.assertRaises(ValueError): mar._GenerateEquations()
def test_dividend(self): mod = Model() can = Country(mod, 'CA', 'Canada') us = Country(mod, 'US', 'US of A') bus = FixedMarginBusiness(can, 'BUS', 'Business') mar = Market(can, 'GOOD', 'market') cap = Capitalists(can, 'CAP', 'Capitalists', .4) mod._GenerateFullSectorCodes() bus._GenerateEquations() self.assertEqual('CA_BUS__PROF', cap.EquationBlock['DIV'].RHS())
def test_GenerateTermsLowLevel(self): mod = Model() can = Country(mod, 'Eh', 'Canada') mar = Market(can, 'LAB', 'Market') bus = Sector(can, 'BUS', 'Business') bus.AddVariable('DEM_LAB', 'desc', '') mod._GenerateFullSectorCodes() mar._GenerateTermsLowLevel('DEM', 'Demand') self.assertIn('-DEM_LAB', bus.EquationBlock['F'].RHS()) self.assertEqual('0.0', bus.EquationBlock['DEM_LAB'].RHS())
def test_SendMoney(self): mod = Model() ext = ExternalSector(mod) ca = Country(mod, 'CA', 'Canada', currency='CAD') hh_ca = Sector(ca, 'HH', 'Canada HH') hh_ca.AddVariable('GIFT', 'Gifts!', '5.') fx = ext['FX'] mod._GenerateFullSectorCodes() fx._SendMoney(hh_ca, 'GIFT') self.assertEqual('CA_HH__GIFT', fx.EquationBlock['NET_CAD'].RHS())
def test_GenerateEquations_2_supply_fail(self): mod = Model() can = Country(mod, 'Eh', 'Canada') mar = Market(can, 'LAB', 'Market') bus = Sector(can, 'BUS', 'Business') hh = Sector(can, 'HH', 'Household') hh2 = Sector(can, 'HH2', 'Household') bus.AddVariable('DEM_LAB', 'desc', 'x') hh.AddVariable('SUP_LAB', 'desc 2', '') hh2.AddVariable('SUP_LAB', 'desc 2', '') mod._GenerateFullSectorCodes() with self.assertRaises(LogicError): mar._GenerateEquations()
def test_ReceiveMoney(self): mod = Model() ext = ExternalSector(mod) ca = Country(mod, 'CA', 'Canada', currency='CAD') us = Country(mod, 'US', 'U.S.', currency='USD') hh_ca = Sector(ca, 'HH', 'Canada HH') hh_ca.AddVariable('GIFT', 'Gifts!', '5.') hh_us = Sector(us, 'HH', 'Household') fx = ext['FX'] mod._GenerateFullSectorCodes() fx._ReceiveMoney(hh_us, hh_ca, 'GIFT') self.assertEqual('-CA_HH__GIFT*EXT_XR__CAD_USD', fx.EquationBlock['NET_USD'].RHS())
def test_GetCrossRate(self): mod = Model() ext = ExternalSector(mod) c1 = Country(mod, 'C1', 'C1') c2 = Country(mod, 'C2', 'C2') mod._GenerateFullSectorCodes() xr = ext['XR'] self.assertIn('C1', xr.EquationBlock) self.assertIn('C2', xr.EquationBlock) self.assertNotIn('C1_C2', xr.EquationBlock) xrate = ext.GetCrossRate('C1', 'C2') self.assertEqual('EXT_XR__C1_C2', xrate) self.assertIn('C1_C2', xr.EquationBlock) self.assertEqual('C1/C2', xr.EquationBlock['C1_C2'].RHS())
def test_GenerateFinalEquations(self): mod = Model() us = Country(mod, 'US', 'USA') s = Sector(us, 'HH', 'Household', has_F=False) mod._GenerateFullSectorCodes() # Can no longer directly inject data into the Sector object s.AddVariableFromEquation('x=a+1 # foo') s.AddVariableFromEquation('a=cat') out = s._CreateFinalEquations() # Since F has an empty equation, does not appear. targ = [('HH__a', 'cat', '[a] '), ('HH__x', 'HH__a+1', '[x] foo')] # Kill spacing in equations out = [(x[0], x[1].replace(' ', ''), x[2]) for x in out] self.assertEqual(targ, out)
def test_GenerateEquations(self): mod = Model() can = Country(mod, 'Eh', 'Canada') mar = Market(can, 'LAB', 'Market') bus = Sector(can, 'BUS', 'Business') hh = Sector(can, 'HH', 'Household') bus.AddVariable('DEM_LAB', 'desc', 'x') hh.AddVariable('SUP_LAB', 'desc 2', '') mod._GenerateFullSectorCodes() mar._GenerateEquations() self.assertIn('-DEM_LAB', bus.EquationBlock['F'].RHS()) self.assertEqual('x', bus.EquationBlock['DEM_LAB'].RHS()) self.assertEqual('LAG_F+SUP_LAB', hh.EquationBlock['F'].RHS()) # self.assertEqual('BUS_DEM_LAB', hh.Equations['SUP_LAB'].strip()) self.assertEqual('SUP_LAB', mar.EquationBlock['SUP_HH'].RHS()) self.assertEqual('LAB__SUP_HH', hh.EquationBlock['SUP_LAB'].RHS().strip())
def test_Market_fail_no_external(self): mod = Model() ca = Country(mod, 'CA', 'Canada', currency='CAD') us = Country(mod, 'US', 'United States', currency='USD') # gov_us.AddVariable('T', 'Government Taxes', '0.') gov_ca = Sector(ca, 'GOV', 'Gummint') gov_ca.AddVariable('DEM_GOOD', 'desc', '20.') market = Market(ca, 'GOOD', 'Market') supplier_ca = Sector(ca, 'BUS', 'Canada supplier') supplier_us = Sector(us, 'BUS', 'US Supplier') # Set supply so that CAD$10 is paid to each supplier. market.AddSupplier(supplier_ca) market.AddSupplier(supplier_us, '10.') # Set CAD = 2, so 2 USD = 1 CAD (USD is weaker.) with self.assertRaises(LogicError): mod._GenerateFullSectorCodes() market._GenerateEquations()
def test_all(self): mod = Model() can = Country(mod, 'Eh', 'Canada') gov = ConsolidatedGovernment(can, 'GOV', 'Government') hou = Household(can, 'HH', 'Household', .5) hou2 = Household(can, 'HH2', 'Household2', .5) mm = MoneyMarket(can) mod._GenerateFullSectorCodes() mod._GenerateEquations() # Supply = Demand self.assertEqual('GOV__SUP_MON', mm.EquationBlock['SUP_MON'].RHS()) # Demand = Demand of two sectors self.assertEqual('HH__DEM_MON+HH2__DEM_MON', mm.EquationBlock['DEM_MON'].RHS().replace(' ', '')) # At the sector level, demand = F self.assertEqual('HH__F', hou.EquationBlock['DEM_MON'].RHS()) self.assertEqual('HH2__F', hou2.EquationBlock['DEM_MON'].RHS())
def test_BadUnderBars(self): mod = Model() country = Country(mod, 'US', 'US') sec = Sector(country, 'B__A__D', 'Bad') sec.AddVariable('foo', 'desc', 'x') sec2 = Sector(country, 'OK', 'OK sector') mod._GenerateFullSectorCodes() with self.assertRaises(ValueError): sec.GetVariableName('foo') with self.assertRaises(ValueError): sec2.AddVariable('b__ad', 'desc', 'x') # Force it in.. # Do not go through the constructor, in case the EquationBlock # enforces the naming convention sec2.EquationBlock.Equations['b__ad'] = 'Dummy' with self.assertRaises(ValueError): sec2.GetVariableName('b__ad')
def test_GenerateEquations(self): mod = Model() can = Country(mod, 'Eh', 'Canada') tf = TaxFlow(can, 'Tax', taxrate=.1, taxes_paid_to='GOV') # Supply side for the win! self.assertTrue('T' in tf.EquationBlock.Equations) self.assertTrue('TaxRate' in tf.EquationBlock.Equations) hh = Household(can, 'HH', 'Household') gov = ConsolidatedGovernment(can, 'GOV', 'Gummint') mod._GenerateFullSectorCodes() tf._GenerateEquations() self.assertEqual('0.1000', tf.EquationBlock['TaxRate'].RHS()) self.assertEqual('Tax__TaxRate*HH__INC', tf.EquationBlock['T'].RHS().replace(' ', '')) self.assertIn('-T', hh.EquationBlock['F'].RHS()) self.assertEqual('Tax__TaxRate*HH__INC', hh.EquationBlock['T'].RHS()) self.assertEqual('LAG_F+T', gov.EquationBlock['F'].RHS()) self.assertEqual('Tax__T', gov.EquationBlock['T'].RHS())
def test_GenerateEquationsSpecialised(self): mod = Model() can = Country(mod, 'Eh', 'Canada') tf = TaxFlow(can, 'Tax', 'Taxation Flows', .1) self.assertTrue('T' in tf.EquationBlock.Equations) self.assertTrue('TaxRate' in tf.EquationBlock.Equations) hh = Household(can, 'HH', 'Household') hh.AddVariable('TaxRate', 'Sector level tax rate', '0,2') gov = ConsolidatedGovernment(can, 'GOV', 'Gummint') mod._GenerateFullSectorCodes() tf._GenerateEquations() self.assertEqual('0.1000', tf.EquationBlock['TaxRate'].RHS()) self.assertEqual('HH__TaxRate*HH__INC', tf.EquationBlock['T'].RHS().replace(' ', '')) self.assertIn('-T', hh.EquationBlock['F'].RHS()) self.assertEqual('HH__TaxRate*HH__INC', hh.EquationBlock['T'].RHS()) # self.assertEqual(['+T', ], gov.CashFlows) self.assertEqual('Tax__T', gov.EquationBlock['T'].RHS())
def test_GenerateEquations_insert_supply(self): mod = Model() can = Country(mod, 'Eh', 'Canada') mar = Market(can, 'LAB', 'Market') bus = Sector(can, 'BUS', 'Business') hh = Sector(can, 'HH', 'Household') hh2 = Sector(can, 'HH2', 'Household') bus.AddVariable('DEM_LAB', 'desc', 'x') hh.AddVariable('SUP_LAB_WRONG_CODE', 'desc 2', '') hh2.AddVariable('SUP_LAB_WRONG_CODE', 'desc 2', '') mod._GenerateFullSectorCodes() mar.AddSupplier(hh2) mar.AddSupplier(hh, 'SUP_LAB/2') # mar.SupplyAllocation = [[(hh, 'SUP_LAB/2')], hh2] mar._GenerateEquations() self.assertEqual('SUP_LAB/2', mar.EquationBlock['SUP_HH'].RHS()) self.assertEqual('SUP_LAB-SUP_HH', kill_spaces(mar.EquationBlock['SUP_HH2'].RHS())) self.assertEqual('LAB__SUP_HH', hh.EquationBlock['SUP_LAB'].RHS()) self.assertEqual('LAB__SUP_HH2', hh2.EquationBlock['SUP_LAB'].RHS())
def test_GenerateEquations_2_supply_multicountry_2(self): mod = Model() can = Country(mod, 'CA', 'Canada, Eh?') US = Country(mod, 'US', 'USA! USA!') mar = Market(can, 'LAB', 'Market') bus = Sector(can, 'BUS', 'Business') hh = Sector(can, 'HH', 'Household') hh2 = Sector(can, 'HH2', 'Household') bus.AddVariable('DEM_LAB', 'desc', 'x') hh.AddVariable('SUP_LAB', 'desc 2', '') hh2.AddVariable('SUP_LAB', 'desc 2', '') mod._GenerateFullSectorCodes() mar.AddSupplier(hh2) mar.AddSupplier(hh, 'SUP_LAB/2') #nmar.SupplyAllocation = [[(hh, 'SUP_LAB/2')], hh2] mar._GenerateEquations() self.assertEqual('SUP_LAB/2', mar.EquationBlock['SUP_CA_HH'].RHS()) self.assertEqual('SUP_LAB-SUP_CA_HH', mar.EquationBlock['SUP_CA_HH2'].RHS()) self.assertEqual('CA_LAB__SUP_CA_HH', hh.EquationBlock['SUP_LAB'].RHS()) self.assertIn('SUP_LAB', hh.EquationBlock['F'].RHS()) self.assertEqual('CA_LAB__SUP_CA_HH2', hh2.EquationBlock['SUP_LAB'].RHS()) self.assertIn('SUP_LAB', hh2.EquationBlock['F'].RHS())