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_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 main(): # The next line of code sets the name of the output files based on the code file's name. # This means that if you paste this code into a new file, get a new log name. sfc_models.register_standard_logs('output', __file__) # Create model, which holds all entities mod = Model() # Create first country - Canada. (This model only has one country.) can = Country(mod, 'CA', 'Canada') # Create sectors gov = ConsolidatedGovernment(can, 'GOV', 'Government') hh = Household(can, 'HH', 'Household') # A literally non-profit business sector bus = FixedMarginBusiness(can, 'BUS', 'Business Sector') # Create the linkages between sectors - tax flow, markets - labour ('LAB'), goods ('GOOD') tax = TaxFlow(can, 'TF', 'TaxFlow', .2) labour = Market(can, 'LAB', 'Labour market') goods = Market(can, 'GOOD', 'Goods market') # Add the financial markets # GOV -> issuing sector mm = MoneyMarket(can, issuer_short_code='GOV') dep = DepositMarket(can, issuer_short_code='GOV') # -------------------------------------------- # Financial asset demand equations # Need the full variable name for 'F' in household hh_F = hh.GetVariableName('F') hh.AddVariable('DEM_MON', 'Demand for Money', '0.5 * ' + hh_F) hh.AddVariable('DEM_DEP', 'Demand for deposits', '0.5 * ' + hh_F) # ----------------------------------------------------------------- # Need to set the exogenous variables # Government demand for Goods ("G" in economist symbology) mod.AddExogenous('GOV', 'DEM_GOOD', '[20.,] * 105') mod.AddExogenous('DEP', 'r', '[0.0,] * 5 + [0.04]*100') mod.AddInitialCondition('HH', 'F', 80.) mod.main() mod.TimeSeriesSupressTimeZero = True mod.TimeSeriesCutoff = 20 Quick2DPlot(mod.GetTimeSeries('t'), mod.GetTimeSeries('GOOD__SUP_GOOD'), 'Goods supplied (national production Y)') Quick2DPlot(mod.GetTimeSeries('t'), mod.GetTimeSeries('HH__F'), 'Household Financial Assets (F)')
def main(): # The next line of code sets the name of the output files based on the code file's name. # This means that if you paste this code into a new file, get a new log name. sfc_models.register_standard_logs('output', __file__) # Create model, which holds all entities mod = Model() # Create first country - Canada. can = Country(mod, 'CA', 'Canada') # Create sectors gov = ConsolidatedGovernment(can, 'GOV', 'Government') hh = Household(can, 'HH', 'Household') # A literally non-profit business sector bus = FixedMarginBusiness(can, 'BUS', 'Business Sector') # Create the linkages between sectors - tax flow, markets - labour ('LAB'), goods ('GOOD') tax = TaxFlow(can, 'TF', 'TaxFlow', .2) labour = Market(can, 'LAB', 'Labour market') goods = Market(can, 'GOOD', 'Goods market') # Create a second country, with non-zero profits # This is a very error-prone way of building the model; if we repeat code blocks, they should be in # a function. # Create United States - Almost identical to Canada. us = Country(mod, 'US', 'United States') # Create sectors gov2 = ConsolidatedGovernment(us, 'GOV', 'Government') hh2 = Household(us, 'HH', 'Household') # ********** Profit margin of 10% ***************** bus2 = FixedMarginBusiness(us, 'BUS', 'Business Sector', profit_margin=.1) # Create the linkages between sectors - tax flow, markets - labour ('LAB'), goods ('GOOD') tax2 = TaxFlow(us, 'TF', 'TaxFlow', .2) labor2 = Market(us, 'LAB', 'Labor market') goods2 = Market(us, 'GOOD', 'Goods market') # ***************************************************************** # Need to set the exogenous variable - Government demand for Goods ("G" in economist symbology) # Since we have a two country model, we need to specify the full sector code, which includes the country code. mod.AddExogenous('CA_GOV', 'DEM_GOOD', '[20.,] * 105') mod.AddExogenous('US_GOV', 'DEM_GOOD', '[20.,] * 105') # Do the main work of building and solving the model mod.main() CUT = 25 t = mod.GetTimeSeries('t', cutoff=CUT) Y_CA = mod.GetTimeSeries('CA_GOOD__SUP_GOOD', cutoff=CUT) Y_US = mod.GetTimeSeries('US_GOOD__SUP_GOOD', cutoff=CUT) p = Quick2DPlot([t, t], [Y_CA, Y_US], 'Output - Y', run_now=False) p.Legend = ['Canada (0% profit)', 'U.S. (10% Profit)'] p.DoPlot() F_CA = mod.GetTimeSeries('CA_BUS__F', cutoff=CUT) F_US = mod.GetTimeSeries('US_BUS__F', cutoff=CUT) p = Quick2DPlot([t, t], [F_CA, F_US], 'Business Sector Financial Assets (F)', run_now=False) p.Legend = ['Canada (0% profit)', 'U.S. (10% Profit)'] p.DoPlot() BAL_CA = mod.GetTimeSeries('CA_GOV__FISC_BAL', cutoff=CUT) BAL_US = mod.GetTimeSeries('US_GOV__FISC_BAL', cutoff=CUT) p = Quick2DPlot([t, t], [BAL_CA, BAL_US], 'Government Financial Balance', run_now=False) p.Legend = ['Canada (0% profit)', 'U.S. (10% Profit)'] p.DoPlot()