Exemple #1
0
 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())
Exemple #2
0
 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'])
Exemple #3
0
 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_Fixaliases(self):
     mod = Model()
     c = Country(mod, 'co', 'co')
     sec1 = Sector(c, 'sec1', 'sec1')
     sec1.AddVariable('x', 'eqn x', '')
     varname = sec1.GetVariableName('x')
     ID = "{0}".format(sec1.ID)
     self.assertIn(ID, varname)
     sec2 = Sector(c, 'sec2', 'sec2')
     sec2.AddVariable('two_x', 'Test variable', '2 * {0}'.format(varname))
     self.assertEqual('2*' + varname,
                      kill_spaces(sec2.EquationBlock['two_x'].RHS()))
     mod._GenerateFullSectorCodes()
     mod._FixAliases()
     self.assertEqual('2*sec1__x',
                      kill_spaces(sec2.EquationBlock['two_x'].RHS()))
Exemple #5
0
 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())
Exemple #6
0
 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'))
Exemple #7
0
 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())
Exemple #8
0
 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())
Exemple #9
0
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
Exemple #10
0
 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)
Exemple #11
0
 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')
Exemple #12
0
 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_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)
Exemple #14
0
 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_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())
Exemple #16
0
 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())
Exemple #17
0
 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')
Exemple #18
0
 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())
Exemple #19
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_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()
Exemple #21
0
 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())
Exemple #22
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())
Exemple #23
0
 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_fix_aliases_2(self):
     mod = Model()
     c = Country(mod, 'co', 'co')
     sec1 = Sector(c, 'sec1', 'sec1')
     sec1.AddVariable('x', 'eqn x', '')
     varname = sec1.GetVariableName('x')
     # The ID is the key part of the alias
     self.assertIn('{0}'.format(sec1.ID), varname)
     sec2 = Sector(c, 'sec2', 'sec2')
     mod.RegisterCashFlow(sec1, sec2, 'x')
     mod._GenerateRegisteredCashFlows()
     self.assertIn('-' + varname, sec1.EquationBlock['F'].RHS())
     self.assertIn(varname, sec2.EquationBlock['F'].RHS())
     mod._GenerateFullSectorCodes()
     mod._FixAliases()
     self.assertIn('-sec1__x', sec1.EquationBlock['F'].RHS())
     self.assertEqual('LAG_F+sec1__x',
                      kill_spaces(sec2.EquationBlock['F'].RHS()))
Exemple #25
0
 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'))
limitations under the License.
"""

# Imports
# This next line looks bizarre, but is needed for backwards compatibility with Python 2.7.
from __future__ import print_function

import sfc_models
from sfc_models.models import Model, Country
from sfc_models.sector import Sector

sfc_models.register_standard_logs(output_dir='output', base_file_name=__file__)

mod = Model()
can = Country(mod, 'CA', 'Canada')
# has_F=False: turns off creation of financial asset variables.
sector_yy = Sector(can, 'YY', has_F=False)
sector_yy.AddVariable('W', 'Variable W <constant>', '4.0')
sector_yy.AddVariable('Y', 'Variable Y - depends on local variable', '2*W')

sector_xx = Sector(can, 'XX', has_F=False)
variable_name = sector_yy.GetVariableName('Y')
# format: inserts variable_name where {0} is
eqn = '{0} + 2.0'.format(variable_name)
sector_xx.AddVariable('X', 'Variable x; depends on other sector', eqn)
# Bind the model; solve
eqns = mod.main()
print(eqns)

mod.main()
from __future__ import print_function
from sfc_models.objects import *
from sfc_models.sector import Sector
from sfc_models.examples.Quick2DPlot import Quick2DPlot

register_standard_logs('output', __file__)
mod = Model()
ExternalSector(mod)
ca = Country(mod, 'CA', currency='CAD')
us = Country(mod, 'US', currency='USD')

hh_ca = Sector(ca, 'HH', has_F=True)
hh_ca.AddVariable('GIFT', 'Sending money..', '5.')
hh_us = Sector(us, 'HH', has_F=True)
mod.RegisterCashFlow(hh_ca, hh_us, 'GIFT')
mod.main()
mod.TimeSeriesCutoff = 1
series_list = ('CA_HH__F', 'US_HH__F', 'EXT_FX__NET_CAD', 'EXT_FX__NET_USD')
for s in series_list:
    print(s, mod.GetTimeSeries(s)[1])

print('Try #2 - Add GoldStandardGovernment objects')
mod = Model()
ExternalSector(mod)
ca = Country(mod, 'CA', currency='CAD')
us = Country(mod, 'US', currency='USD')
gov_ca = GoldStandardGovernment(ca, 'GOV')
gov_us = GoldStandardGovernment(us, 'GOV')
# The need for the next step may be fixed...
gov_ca.AddVariable('T', 'Taxes', '0.')
gov_us.AddVariable('T', 'Taxes', '0.')