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_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_Market_handling(self): mod = Model() ext = ExternalSector(mod) 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.) mod.AddExogenous('EXT_XR', 'CAD', '[2.0,]*3') mod.EquationSolver.MaxTime = 1 mod.main() mod.TimeSeriesSupressTimeZero = True # The business sector nets USD$20 self.assertEqual([20.], mod.GetTimeSeries('US_BUS__F')) # The USD market is unbalanced; shortage of 20 USD self.assertEqual([-20.], mod.GetTimeSeries('EXT_FX__NET_USD')) # The CAD market is unbalanced; excess of 10 CAD self.assertEqual([10.], mod.GetTimeSeries('EXT_FX__NET_CAD')) # The supply in the US sector is USD $20 self.assertEqual([20.], mod.GetTimeSeries('US_BUS__SUP_CA_GOOD')) # THe supply on the Canadian side is CAD $10 self.assertEqual([10.], mod.GetTimeSeries('CA_GOOD__SUP_US_BUS'))
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_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_IsSharedCurrencyZone(self): mod = Model() ca = Country(mod, 'CA', 'Name', currency='CAD') ca_h = Sector(ca, 'HH', 'Sec') us = Country(mod, 'US', 'Name', currency='RMB') us_h = Sector(us, 'HH', 'name') china = Country(mod, 'China', 'Name', currency='RMB') china_h = Sector(china, 'HH', 'name') self.assertFalse(ca_h.IsSharedCurrencyZone(us_h)) self.assertFalse(us_h.IsSharedCurrencyZone(ca_h)) self.assertTrue(us_h.IsSharedCurrencyZone(china_h))
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_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_GetVariableName_2(self): mod = Model() us = Country(mod, 'US', 'USA') household = Household(us, 'HH', 'Household', .9) ID = household.ID target = '_{0}__{1}'.format(ID, 'AlphaFin') self.assertEqual(target, household.GetVariableName('AlphaFin'))
def test_GenerateEquations_alpha(self): mod = Model() can = Country(mod, 'Eh', 'Canada') hh = Household(can, 'HH', 'Household', alpha_fin=0.2, alpha_income=0.9) hh._GenerateEquations() self.assertEqual(hh.EquationBlock['AlphaFin'].RHS(), '0.2000') self.assertEqual(hh.EquationBlock['AlphaIncome'].RHS(), '0.9000')
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_AddCashFlow_3(self): mod = Model() us = Country(mod, 'US', 'USA') s = Sector(us, 'HH', 'Household') s.AddVariable('X', 'desc', '') s.AddCashFlow('X', 'equation', 'Desc A') self.assertEqual('equation', s.EquationBlock['X'].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_AddTerm(self): mod = Model() us = Country(mod, 'US', 'USA! USA!') s = Sector(us, 'SEC', 'Desc') s.AddVariable('SUP_GOOD', 'Supply of goods', '') s.AddTermToEquation('SUP_GOOD', 'Kaboom') self.assertEqual('Kaboom', s.EquationBlock['SUP_GOOD'].RHS())
def test_GenerateAssetWeightingAbsolute(self): mod = Model() us = Country(mod, 'US', 'USA') s = Sector(us, 'HH', 'Household') with self.assertRaises(NotImplementedError): s.GenerateAssetWeighting([('BOND', '0.5'), ], 'MON', is_absolute_weighting=True)
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_GenerateAssetWeightings_1(self): mod = Model() us = Country(mod, 'US', 'USA') s = Sector(us, 'HH', 'Household') s.GenerateAssetWeighting((), 'MON') self.assertEqual('1.0', s.EquationBlock['WGT_MON'].RHS()) self.assertEqual('F*WGT_MON', kill_spaces(s.EquationBlock['DEM_MON'].RHS()))
def test_GenerateIncomeEquations_2(self): mod = Model() us = Country(mod, 'US', 'USA') s = Sector(us, 'HH', 'Household') s.AddCashFlow('X', 'eq') self.assertEqual('LAG_F+X', s.EquationBlock['F'].RHS()) self.assertEqual('F(k-1)', s.EquationBlock['LAG_F'].RHS())
def test_AddCashFlow_bad(self): mod = Model() us = Country(mod, 'US', 'USA') s = Sector(us, 'HH', 'Household') s.AddVariable('X', 'desc', '') with self.assertRaises(NotImplementedError): # Must be a simple variable as the cash flow s.AddCashFlow('f(X)', 'equation', 'Desc A')
def test_AddInitialConditions(self): mod = Model() us = Country(mod, 'US', 'desc') s = Sector(us, 'HH', 'desc', has_F=False) s.AddVariable('x', 'desc','1.0') s.AddInitialCondition('x', '10.0') targ = [(s.ID, 'x', '10.0'), ] self.assertEqual(targ, mod.InitialConditions)
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_AddCashFlow(self): mod = Model() us = Country(mod, 'US', 'USA') s = Sector(us, 'HH', 'Household') s.AddCashFlow('A', 'H_A', 'Desc A') s.AddCashFlow('- B', 'H_B', 'Desc B') s.AddCashFlow(' - C', 'H_C', 'Desc C') self.assertEqual('LAG_F+A-B-C', s.EquationBlock['F'].RHS())
def test_GetVariables(self): mod = Model() can = Country(mod, 'Eh', 'Canada') # Need to block the automatic creation of F, INC can_hh = Sector(can, 'HH', 'Household', has_F=False) can_hh.AddVariable('y', 'Vertical axis', '2.0') can_hh.AddVariable('x', 'Horizontal axis', 'y - t') self.assertEqual(can_hh.GetVariables(), ['x', 'y'])
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_GoldSectors(self): # Relatively big test, but it takes a lot of work to get a model # where we can create a GoldStandardCentralBank, and to set up the # equations so that they need to intervene. # Consider this an end-to-end test. mod = Model() ext = ExternalSector(mod) ca = Country(mod, 'CA', 'Canada', currency='CAD') us = Country(mod, 'US', 'United States', currency='USD') gov_us = GoldStandardGovernment(us, 'GOV') # gov_ca = GoldStandardGovernment(ca, 'CA Gov', 'GOV', 200.) tre_ca = Treasury(ca, 'TRE', 'Ministry of Finance') cb_ca = GoldStandardCentralBank(ca, 'CB', treasury=tre_ca) mon = MoneyMarket(ca, issuer_short_code='CB') dep = DepositMarket(ca, issuer_short_code='TRE') gov_us.AddVariable('T', 'Government Taxes', '0.') tre_ca.AddVariable('T', 'Government Taxes', '0.') tre_ca.SetEquationRightHandSide('DEM_GOOD', '20.') market = Market(ca, 'GOOD', 'Market') supplier_ca = Sector(ca, 'BUS', 'Canada supplier') supplier_us = Sector(us, 'BUS', 'US Supplier') market.AddSupplier(supplier_ca) market.AddSupplier(supplier_us, '10.') mod.EquationSolver.MaxTime = 1 mod.EquationSolver.MaxIterations = 90 mod.EquationSolver.ParameterErrorTolerance = 1e-1 mod.main() mod.TimeSeriesSupressTimeZero = True # markets should be balanced self.assertAlmostEqual(0., mod.GetTimeSeries('EXT_FX__NET_CAD')[0], places=2) self.assertAlmostEqual(0., mod.GetTimeSeries('EXT_FX__NET_USD')[0], places=2) # U.S. buys 10 units of GOLD self.assertAlmostEqual(10., mod.GetTimeSeries('US_GOV__GOLDPURCHASES')[0], places=2) # Canada sells 10 units self.assertAlmostEqual(-10., mod.GetTimeSeries('CA_CB__GOLDPURCHASES')[0], places=2)
def test_CashFlow(self): mod = Model() ext = ExternalSector(mod) ca = Country(mod, 'CA', 'Canada', currency='CAD') us = Country(mod, 'US', 'United States', currency='USD') hh_ca = Sector(ca, 'HH', 'Canada HH') hh_us = Sector(us, 'HH', 'US HH') hh_ca.AddVariable('GIFT', 'Gifts!', '5.') xr = ext['XR'] xr.SetExogenous('CAD', '[1.5,]*100') mod.EquationSolver.MaxTime = 3 mod.RegisterCashFlow(hh_ca, hh_us, 'GIFT', False, True) mod.main() testfn = mod.GetTimeSeries self.assertEqual([0., -5., -10., -15.], testfn('CA_HH__F')) self.assertEqual([5., 5., 5., 5.], testfn('EXT_FX__NET_CAD')) self.assertEqual([-7.5, -7.5, -7.5, -7.5], testfn('EXT_FX__NET_USD')) self.assertEqual([0., 0., 0., 0.], testfn('EXT_FX__NET_NUMERAIRE'))
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_generate_eqn(self): mod = Model() ca = Country(mod, 'CA') cap = Capitalists(ca, 'CAP') cap.AlphaIncome = 0.99 cap.AlphaFin = 0.11 cap._GenerateEquations() self.assertEqual('0.9900', cap.EquationBlock['AlphaIncome'].RHS()) self.assertEqual('0.1100', cap.EquationBlock['AlphaFin'].RHS())
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()