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_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_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 build_model(): mod = Model() ext = ExternalSector(mod) ca = Country(mod, 'Canada', 'CA', currency='CAD') us = Country(mod, 'United States', 'US', currency='USD') xrate = ext.GetCrossRate('CAD', 'USD') xrate = ext.GetCrossRate('USD', 'CAD') hh_ca = Sector(ca, 'Canada HH', 'HH') hh_us = Sector(us, 'US HH', 'HH') hh_ca.AddVariable('GIFT', 'Gifts!', '5.') fx = ext['FX'] xr = ext['XR'] # hh_ca.AddVariable('GOLDPURCHASES', 'Yeah', '-GIFT') # ext['GOLD'].SetGoldPurchases(hh_ca, 'GOLDPURCHASES', 100.) tre = Treasury(ca, 'Ministry O Finance', 'TRE') cb_ca = GoldStandardCentralBank(ca, 'Bank O Canada', 'CB', tre, 1000) mm = MoneyMarket(ca, issuer_short_code='CB') dep = DepositMarket(ca, issuer_short_code='TRE') tax = TaxFlow(ca, 'TaxFlow', 'TF', .2, taxes_paid_to='TRE') xr.SetExogenous('CAD', '[1.5,]*100') mod.EquationSolver.MaxTime = 10 mod.RegisterCashFlow(hh_ca, hh_us, 'GIFT', False, True) mod.EquationSolver.TraceStep = 4 # fx._SendMoney(hh_ca, 'GIFT') # fx._ReceiveMoney(hh_us, hh_ca, 'GIFT') return mod
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_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_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 __init__(self, country, code, long_name='', treasury=None): if long_name == '': long_name = 'Central Bank {0} in Country {1}'.format( code, country.Code) Sector.__init__(self, country, code, long_name, has_F=True) self.Treasury = treasury # Demand for deposits = F + Supply of money (Central bank net worth plus money supply) self.AddVariable('DEM_DEP', 'Demand for deposits', 'F + SUP_MON')
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_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_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_ProcessExogenous(self): mod = Model() us = Country(mod, 'US', 'USA') household = Sector(us, 'HH', 'Household') household.AddVariable('foo', 'desc', 'x') mod._GenerateFullSectorCodes() mod.Exogenous = [('HH', 'foo', 'TEST')] mod._ProcessExogenous() self.assertEqual('EXOGENOUSTEST', household.EquationBlock['foo'].RHS())
def test_GenerateFullCodes_2(self): mod = Model() us = Country(mod, 'US', 'USA') can = Country(mod, 'Eh?', 'Canada') household = Sector(us, 'HH', 'Household') can_hh = Sector(can, 'HH', 'Household') mod._GenerateFullSectorCodes() self.assertEqual(household.FullCode, 'US_HH') self.assertEqual(can_hh.FullCode, 'Eh?_HH')
def test_GenerateInitialCond(self): mod = Model() us = Country(mod, 'US', 'USA') household = Sector(us, 'HH', 'Household') household.AddVariable('foo', 'desc', 'x') mod._GenerateFullSectorCodes() mod.InitialConditions = [('HH', 'foo', '0.1')] out = mod._GenerateInitialConditions() self.assertEqual([('HH__foo(0)', '0.1', 'Initial Condition')], out)
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_dumpequations(self): # Since dump format will change, keep this as a very loose test. We can tighten the testing # on Sector.Dump() later mod = Model() country = Country(mod, 'US', 'USA! USA!') household = Sector(country, 'HH', 'Household') hh_dump = household.Dump() mod_dump = mod.DumpEquations() self.assertEqual(hh_dump, mod_dump)
def __init__(self, external_sector, code='FX', long_name='Foreign Exchange Transactions'): Sector.__init__(self, country=external_sector, code=code, long_name=long_name, has_F=False)
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 __init__(self, country, code, long_name=''): if long_name == '': long_name = 'Government {0} in Country {1}'.format( code, country.Code) Sector.__init__(self, country, code, long_name) self.AddVariable('DEM_GOOD', 'Government Consumption of Goods', '0.0') self.AddVariable('PRIM_BAL', 'Government Primary Fiscal Balance', 'T - DEM_GOOD') self.AddVariable('FISC_BAL', 'Government Fiscal Balance', 'INC') self.AddVariable('T', 'Government Taxes', '0.')
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_setRHS(self): mod = Model() us = Country(mod, 'US', 'USA') s = Sector(us, 'HH', 'Household') s.AddVariable('foo', 'variable foo', 'x') self.assertEqual('x', s.EquationBlock['foo'].RHS()) s.SetEquationRightHandSide('foo', 'y') self.assertEqual('y', s.EquationBlock['foo'].RHS()) with self.assertRaises(KeyError): s.SetEquationRightHandSide('LittleBunnyFooFoo', 'z')
def test_CashFlowCrossCurrencyFail(self): mod = Model() ca = Country(mod, 'CA', 'Canada') us = Country(mod, 'US', 'U.S.') sec_ca = Sector(ca, 'HH', 'household') sec_ca.AddVariable('GIFT', 'Gifts', '5.') sec_us = Sector(us, 'HH', 'hh') # We can register this; we might add an ExternalSector later. mod.RegisterCashFlow(sec_ca, sec_us, 'GIFT') with self.assertRaises(LogicError): mod._GenerateRegisteredCashFlows()
def test_LookupSector(self): mod = Model() us = Country(mod, 'US', 'USA') can = Country(mod, 'Eh?', 'Canada') household = Sector(us, 'HH', 'Household') can_hh = Sector(can, 'HH', 'Household') mod._GenerateFullSectorCodes() self.assertEqual(household, mod.LookupSector('US_HH')) self.assertEqual(can_hh, mod.LookupSector('Eh?_HH')) with self.assertRaises(KeyError): mod.LookupSector('HH')
def __init__(self, country, code, long_name=''): if long_name == '': long_name = 'Treasury {0} in Country {1}'.format( code, country.Code) Sector.__init__(self, country, code, long_name) self.AddVariable('DEM_GOOD', 'Government Consumption of Goods', '0.0') self.AddVariable('PRIM_BAL', 'Government Primary Fiscal Balance', 'T - DEM_GOOD') # Treasury has no money holdings self.AddVariable('DEM_MON', 'Demand for Money', '0.0') self.AddVariable('T', 'Taxes received', '0.0')
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 __init__(self, country, code, long_name='', taxrate=0.0, taxes_paid_to='GOV'): if long_name == '': long_name = 'TaxFlow Object {0} in Country {1}'.format( code, country.Code) Sector.__init__(self, country, code, long_name, has_F=False) self.AddVariable('TaxRate', 'Tax rate', '%0.4f' % (taxrate, )) self.AddVariable('T', 'Taxes Paid', '') self.TaxingSector = taxes_paid_to self.TaxRate = taxrate
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)