<jupyter_start><jupyter_text>Monetary Econonmics: Chapter 12  Preliminaries<jupyter_code># This line configures matplotlib to show figures embedded in the notebook, 
# instead of opening a new window for each figure. More about that later. 
# If you are using an old version of IPython, try using '%pylab inline' instead.
%matplotlib inline

from pysolve3.model import Model
from pysolve3.utils import is_close,round_solution

import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>Model OPENFIXG<jupyter_code>def create_openfixg_model():
    model = Model()
        
    model.set_var_default(0)
    model.set_param_default(0)
    model.var('BdUKUK', desc='Bills issued by the UK acquired by the UK: demand')
    model.var('BsUKUK', desc='Bills issued by the UK acquired by the UK: supply')
    model.var('BcbdUKUS', desc='Bills issued by the US, demanded by the UK central bank')
    model.var('BcbdUKUK', desc='Bills issued by the UK, demanded by the UK central bank')
    model.var('BcbsUKUS', desc='Bills issued by the US, supplied to the UK central bank')
    model.var('BsUK', desc='Bills issued by the UK - total supply')
    model.var('BdUKUS', desc='Bills issued by the US acquired by the UK: demand')
    model.var('BsUKUS', desc='Bills issued by the US acquired by the UK: supply')
    model.var('BdUSUK', desc='Bills issued by the UK acquired by the US: demand')
    model.var('BsUSUK', desc='Bills issued by the UK acquired by the US: supply')
    model.var('BsUS', desc='Bills issued by the US - total supply')
    model.var('BdUSUS', desc='Bills issued by the US acquired by the US: demand')
    model.var('BsUSUS', desc='Bills issued by the US acquired by the US: supply')
    model.var('BcbdUSUS', desc='Bills issued by the US, demanded by the US central bank')
    model.var('BcbsUSUS', desc='Bills issued by the US, supplied to the US central bank')
    model.var('CkUK', desc='Real consumption in the UK')
    model.var('CkUS', desc='Real consumption in the US')
    model.var('CABUK', desc='Current account balance in the UK')
    model.var('CABUS', desc='Current account balance in the US')
    model.var('CONSUK', desc='Consumption in the UK')
    model.var('CONSUS', desc='Consumption in the US')
    model.var('DSUK', desc='Domestic sales in the UK')
    model.var('DSUS', desc='Domestic sales in the US')
    model.var('DSkUK', desc='Real domestic sales in the UK')
    model.var('DSkUS', desc='Real domestic sales in the US')
    model.var('FcbUK', desc='Profits of the central bank in the UK')
    model.var('FcbUS', desc='Profits of the central bank in the US')
    model.var('GkUK', desc='Real government expenditure in the UK')
    model.var('GUK', desc='Government expenditure in the UK')
    model.var('GUS', desc='Government expenditure in the US')
    model.var('HdUK', desc='Demand for cash of the UK')
    model.var('HsUK', desc='Supply of cash for the UK')
    model.var('HdUS', desc='Demand for cash of the US')
    model.var('HsUS', desc='Supply for cash for the US')
    model.var('IMUK', desc='Imports of the UK from the US')
    model.var('IMUS', desc='Imports of the US from the UK')
    model.var('IMkUK', desc='Real imports of the UK from the US')
    model.var('IMkUS', desc='Real imports of the US from the UK')
    model.var('KABUK', desc='Current account balance in the UK')
    model.var('KABUS', desc='Current account balance in the US')
    model.var('KABPUK', desc='Capital account balance in the UK, excluding official transactions')
    model.var('KABPUS', desc='Capital account balance in the US, excluding official transactions')
    model.var('NAFAUK', desc='Net accumulation of financial assets in the UK')
    model.var('NAFAUS', desc='Net accumulation of financial assets in the US')
    model.var('NUK', desc='Employment in the UK')
    model.var('NUS', desc='Employment in the US')
    model.var('PDSUK', desc='Price of domestic sales in the UK')
    model.var('PDSUS', desc='Price of domestic sales in the US')
    model.var('PGUK', desc='Price of gold in the UK')
    model.var('PMUK', desc='Price of imports in the UK')
    model.var('PMUS', desc='Price of imports in the US')
    model.var('PSUK', desc='Price of sales in the UK')
    model.var('PSUS', desc='Price of sales in the US')
    model.var('PSBRUK', desc='Government deficit in the UK')
    model.var('PSBRUS', desc='Government deficit in the US')
    model.var('PYUK', desc='Price of output in the UK')
    model.var('PYUS', desc='Price of output in the US')
    model.var('PXUK', desc='Price of exports in the UK')
    model.var('PXUS', desc='Price of exports in the US')
    model.var('SUK', desc='Real sales in the UK')
    model.var('SUS', desc='Real sales in the US')
    model.var('SkUK', desc='Real sales in the UK')
    model.var('SkUS', desc='Real sales in the US')
    model.var('TUK', desc='Tax revenue in the UK')
    model.var('TUS', desc='Tax revenue in the US')
    model.var('VUK', desc='Net financial assets of the UK')
    model.var('VUS', desc='Net financial assets of the US')
    model.var('VkUK', desc='Real net financial assets of the UK')
    model.var('VkUS', desc='Real net financial assets of the US')
    model.var('XUK', desc='Exports from the UK to the US')
    model.var('XUS', desc='Exports from the US to the UK')
    model.var('XkUK', desc='Real exports from the U to the UK')
    model.var('XkUS', desc='Real exports from the U to the US')
    model.var('XRUS', desc='Exchange rate: units of US currency against 1 unit of UK currency')
    model.var('YDrUK', desc='Disposable income in the UK')
    model.var('YDrUS', desc='Disposable income in the US')
    model.var('YDhsUK', desc='Haig-Simons disposable income in the UK')
    model.var('YDhsUS', desc='Haig-Simons disposable income in the US')
    model.var('YDhskUK', desc='Real Haig-Simons disposable income in the UK')
    model.var('YDhskUS', desc='Real Haig-Simons disposable income in the US')
    model.var('YDhsekUK', desc='Expected real Haig-Simons disposable income in the UK')
    model.var('YDhsekUS', desc='Expected real Haig-Simons disposable income in the US')
    model.var('YUK', desc='Income in the UK')
    model.var('YUS', desc='Income in the US')
    model.var('YkUK', desc='Real income in the UK')
    model.var('YkUS', desc='Real income in the US')

    model.param('BcbsUKUK', desc='Bills issued by the UK, supplied to the UK central bank')
    model.param('DXREUK', desc='Expected change in the exchange rate of the UK (measured as units of the UK currency against 1 unit of the US currency)')
    model.param('DXREUS', desc='Expected change in the exchange rate of the US (measured as units of the US currency against 1 unit of the UK currency)')
    model.param('GkUS', desc='Real government expenditure in the US')
    model.param('ORUK', desc='Gold reserves in the UK')
    model.param('ORUS', desc='Gold reserves in the US')
    model.param('PGUS', desc='Price of gold in the US')
    model.param('PRUK', desc='Productivity in the UK')
    model.param('PRUS', desc='Productivity in the US')
    model.param('RUK', desc='Interest rate on the UK bills')
    model.param('RUS', desc='Interest rate on the US bills')
    model.param('WUK', desc='Nominal wage rate in the UK')
    model.param('WUS', desc='Nominal wage rate in the US')
    model.param('XREUK', desc='Expected exchange rate: units of UK currency against 1 unit of US currency')
    model.param('XREUS', desc='Expected exchange rate: units of US currency against 1 unit of UK currency')
    model.param('XRUK', desc='Exchange rate: units of UK currency against 1 unit of US currency')


    model.param('alpha1UK', desc='Propensity to consume out of income in the UK')
    model.param('alpha2UK', desc='Propensity to consume out of wealth in the UK')
    model.param('alpha1US', desc='Propensity to consume out of income in the US')
    model.param('alpha2US', desc='Propensity to consume out of wealth in the US')
    model.param('eps0', desc='Parameter determining real exports in the UK')
    model.param('eps1', desc='Parameter determining real exports in the UK')
    model.param('eps2', desc='Parameter determining real exports in the UK')
    model.param('lambda10', desc='Parameter in asset demand function')
    model.param('lambda11', desc='Parameter in asset demand function')
    model.param('lambda12', desc='Parameter in asset demand function')
    model.param('lambda20', desc='Parameter in asset demand function')
    model.param('lambda21', desc='Parameter in asset demand function')
    model.param('lambda22', desc='Parameter in asset demand function')
    model.param('lambda30', desc='Parameter in asset demand function')
    model.param('lambda31', desc='Parameter in asset demand function')
    model.param('lambda32', desc='Parameter in asset demand function')
    model.param('lambda40', desc='Parameter in asset demand function')
    model.param('lambda41', desc='Parameter in asset demand function')
    model.param('lambda42', desc='Parameter in asset demand function')
    model.param('lambda50', desc='Parameter in asset demand function')
    model.param('lambda51', desc='Parameter in asset demand function')
    model.param('lambda52', desc='Parameter in asset demand function')
    model.param('mu0', desc='Parameter determining real imports in the UK')
    model.param('mu1', desc='Parameter determining real imports in the UK')
    model.param('mu2', desc='Parameter determining real imports in the UK')
    model.param('nu0m', desc='Parameter determining import prices in the UK')
    model.param('nu1m', desc='Parameter determining import prices in the UK')
    model.param('nu0x', desc='Parameter determining import prices in the UK')
    model.param('nu1x', desc='Parameter determining import prices in the UK')
    model.param('thetaUK', desc='Tax rate in the UK')
    model.param('thetaUS', desc='Tax rate in the US')
    model.param('phiUK', desc='mark-up in the UK')
    model.param('phiUS', desc='mark-up in the US')


    # Accounting Identities
    # ---------------------
    # 12.1 : Disposable income in the UK
    model.add('YDrUK = (YUK + RUK(-1)*BdUKUK(-1) + XRUS*RUS(-1)*BsUKUS(-1))*(1 - thetaUK) + d(XRUS)*BsUKUS(-1)')
    model.add('YDhsUK = YDrUK + d(XRUS)*BsUKUS(-1)') # 12.2 : Haig-Simons disposable income in the UK
    model.add('VUK - VUK(-1) = YDrUK - CONSUK')      # 12.3 : Wealth accumulation in the UK
    # 12.4 : Disposable income in the US
    model.add('YDrUS = (YUS + RUS(-1)*BdUSUS(-1) + XRUK*RUK(-1)*BsUSUK(-1))*(1 - thetaUS) + d(XRUK)*BsUSUK(-1)')
    model.add('YDhsUS = YDrUS + d(XRUK)*BsUSUK(-1)') # 12.5 : Haig-Simons disposable income in the US
    model.add('VUS - VUS(-1) = YDrUS - CONSUS')      # 12.6 : Wealth accumulation in the US
    model.add('TUK = thetaUK*(YUK + RUK(-1)*BdUKUK(-1) + XRUS*RUS(-1)*BsUKUS(-1))')  # 12.7 : Taxes in the UK
    model.add('TUS = thetaUS*(YUS + RUS(-1)*BdUSUS(-1) + XRUK*RUK(-1)*BsUSUK(-1))')  # 12.8 : Taxes in the US

    # Equations 12.9 and 12.10 dropped in favor on 12.53 and 12.54

    model.add('FcbUK = RUK(-1)*BcbdUKUK(-1) + RUS(-1)*BcbsUKUS(-1)*XRUS')  # 12.11 : UK central bank profits
    model.add('FcbUS = RUS(-1)*BcbdUSUS(-1)')        # 12.12 : US central bank profits
    # 12.13G : Government budget constraint, now solved for GUK
    model.add('GUK = BsUK - BsUK(-1) - (RUK(-1)*BsUK(-1) - TUK - FcbUK)')
    # model.add('BsUK = BsUK(-1) + GUK + RUK(-1)*BsUK(-1) - TUK - FcbUK')  # 12.13 : UK Govt budget constraint
    model.add('BsUS = BsUS(-1) + GUS + RUS(-1)*BsUS(-1) - TUS - FcbUS')  # 12.14 : US Govt budget constraint
    # 12.15 : UK Current account balance
    model.add('CABUK = XUK - IMUK + XRUS*RUS(-1)*BsUKUS(-1) - RUK(-1)*BsUSUK(-1) + RUS(-1)*BcbsUKUS(-1)*XRUS')
    # 12.16 : UK Capital account balance
    model.add('KABUK = KABPUK - (XRUS*d(BcbsUKUS) + PGUK*d(ORUK))')
    # 12.17 : US Current acount balance
    model.add('CABUS = XUS - IMUS + XRUK*RUK(-1)*BsUSUK(-1) - RUS(-1)*BsUKUS(-1) - RUS(-1)*BcbsUKUS(-1)')
    # 12.18 : US Capital account balance
    model.add('KABUS = KABPUS + d(BcbsUKUS) - PGUS*d(ORUS)')
    model.add('KABPUK = -d(BsUKUS)*XRUS + d(BsUSUK)')   # 12.19 : UK capital account balance, net of official transactions
    model.add('KABPUS = -d(BsUSUK)*XRUK + d(BsUKUS)')   # 12.20 : US capital account balance, net of official transactions

    # Trade
    # -----
    # 12.21 : Import prices in UK
    model.add('PMUK = exp(nu0m + nu1m*log(PYUS) + (1 - nu1m)*log(PYUK) - nu1m*log(XRUK))')
    # 12.22 : Export prices in UK
    model.add('PXUK = exp(nu0x + nu1x*log(PYUS) + (1 - nu1x)*log(PYUK) - nu1x*log(XRUK))')
    model.add('PXUS = PMUK*XRUK')               # 12.23 : Export prices in US
    model.add('PMUS = PXUK*XRUK')               # 12.24 : Import prices in US
    # 12.25 : Real exports from UK, depends on current relative price    
    model.add('XkUK = exp(eps0 - eps1*log(PMUS/PYUS) + eps2*log(YkUS))')
    # 12.26 : Real imports of UK
    model.add('IMkUK = exp(mu0 - mu1*log(PMUK(-1)/PYUK(-1)) + mu2*log(YkUK))')
    model.add('XkUS = IMkUK')                   # 12.27 : Real exports from US
    model.add('IMkUS = XkUK')                   # 12.28 : Real imports of US
    model.add('XUK = XkUK*PXUK')                # 12.29 : Exports of UK
    model.add('XUS = XkUS*PXUS')                # 12.30 : Exports of US
    model.add('IMUK = IMkUK*PMUK')              # 12.31 : Imports of UK
    model.add('IMUS = IMkUS*PMUS')              # 12.32 : Imports of US

    # Income and expenditure
    # ----------------------
    model.add('VkUK = VUK/PDSUK')               # 12.33 : Real wealth in UK
    model.add('VkUS = VUS/PDSUS')               # 12.34 : Real wealth in US
    # 12.35 : Real Haig-Simons disposable income in UK
    model.add('YDhskUK = YDrUK/PDSUK - VkUK(-1)*d(PDSUK)/PDSUK')
    # 12.36 : Real Haig-Simons disposable income in US
    model.add('YDhskUS = YDrUS/PDSUS - VkUS(-1)*d(PDSUS)/PDSUS') 
    # 12.37 : Real consumption in UK
    model.add('CkUK = alpha1UK*YDhsekUK + alpha2UK*VkUK(-1)')
    # 12.38 : Real consumption in US
    model.add('CkUS = alpha1US*YDhsekUS + alpha2US*VkUS(-1)')
    # 12.39  Expected real Haig-Simons disposable income in UK
    model.add('YDhsekUK = (YDhskUK + YDhskUK(-1))/2')
    # 12.40  Expected real Haig-Simons disposable income in US
    model.add('YDhsekUS = (YDhskUS + YDhskUS(-1))/2')
    model.add('SkUK = CkUK + GkUK + XkUK')      # 12.41 : Real sales in UK
    model.add('SkUS = CkUS + GkUS + XkUS')      # 12.42 : Real sales in US
    model.add('SUK = SkUK*PSUK')                # 12.43 : Value of sales in UK
    model.add('SUS = SkUS*PSUS')                # 12.44 : Value of sales in US
    model.add('PSUK = (1 + phiUK)*(WUK*NUK + IMUK)/SkUK') # 12.45 : Price of sales in UK
    model.add('PSUS = (1 + phiUS)*(WUS*NUS + IMUS)/SkUS') # 12.46 : Price of sales in US
    model.add('PDSUK = (SUK - XUK)/(SkUK - XkUK)') # 12.47 : Price of domestic sales in UK
    model.add('PDSUS = (SUS - XUS)/(SkUS - XkUS)') # 12.48 : Price of domestic sales in US
    model.add('DSUK = SUK - XUK')               # 12.49 : Domestic sales in UK
    model.add('DSUS = SUS - XUS')               # 12.50 : Domestic sales in US
    model.add('DSkUK = CkUK + GkUK')            # 12.51 : Real domestic sales in UK
    model.add('DSkUS = CkUS + GkUS')            # 12.52 : Real domestic sales in US
    model.add('YUK = SUK - IMUK')               # 12.53 : Value of output in UK
    model.add('YUS = SUS - IMUS')               # 12.54 : Value of output in US
    model.add('YkUK = SkUK - IMkUK')            # 12.55 : Value of real output in UK
    model.add('YkUS = SkUS - IMkUS')            # 12.56 : Value of real output in US
    model.add('PYUK = YUK/YkUK')                # 12.57 : Price of output in UK
    model.add('PYUS = YUS/YkUS')                # 12.58 : Price of output in US
    model.add('CONSUK = CkUK*PDSUK')            # 12.59 : Consumption in UK
    model.add('CONSUS = CkUS*PDSUS')            # 12.60 : Consumption in US
    # model.add('GUK = GkUK*PDSUK')               # 12.61 : Govt expenditure in UK
    # 12.61G : Govt expenditure in UK, now solved for GkUK
    model.add('GkUK = GUK/PDSUK')
    model.add('GUS = GkUS*PDSUS')               # 12.62 : Govt expenditure in US

    # Note : tax definitions in the book as eqns 12.63 and 12.64 are
    # already defined here as eqns 12.7 and 12.8

    model.add('NUK = YkUK/PRUK')                # 12.65 : Employment in UK
    model.add('NUS = YkUS/PRUS')                # 12.66 : Employment in US

    # Asset Demands
    # -------------
    # 12.67 : Demand for UK bills in UK
    model.add('BdUKUK = VUK*(lambda10 + lambda11*RUK - lambda12*(RUS + DXREUS))')
    # 12.68 : Demand for US bills in UK
    model.add('BdUKUS = VUK*(lambda20 - lambda21*RUK + lambda22*(RUS + DXREUS))')
    model.add('HdUK = VUK - BdUKUK - BdUKUS')   # 12.69 : Demand for money in UK
    # 12.70 : Demand for US bills in US
    model.add('BdUSUS = VUS*(lambda40 + lambda41*RUS - lambda42*(RUK + DXREUK))')
    # 12.71 : Demand for UK bills in US
    model.add('BdUSUK = VUS*(lambda50 - lambda51*RUS + lambda52*(RUK + DXREUK))')
    model.add('HdUS = VUS - BdUSUS - BdUSUK')   # 12.72 : Demand for money in US

    # Asset Supplies
    # --------------
    model.add('HsUS = HdUS')                    # 12.77 : Supply of cash in US
    model.add('BsUSUS = BdUSUS')                # 12.78 : Supply of US bills to US
    model.add('BcbsUSUS = BcbdUSUS')            # 12.79 : Supply of US bills to US central bank
    model.add('HsUK = HdUK')                    # 12.80 : Supply of cash in UK
    model.add('BsUKUK = BdUKUK')                # 12.81 : Bills issued by UK acquired by UK
    # model.add('BcbsUKUK = BcbdUKUK')            # 12.82 : Supply of UK bills to UK central bank
    # 12.82G : Supply of UK bills to UK central bank
    model.add('BsUK = BsUKUK + BsUSUK + BcbsUKUK')
    # 12.83 : Balance sheet of US central bank
    model.add('BcbdUSUS = BcbdUSUS(-1) + d(HsUS) - d(ORUS)*PGUS ')
    # 12.84 : Balance sheet of UK central bank
    model.add('BcbdUKUK = BcbdUKUK(-1) + d(HsUK) - d(BcbsUKUS)*XRUS - d(ORUK)*PGUK')
    model.add('PGUK = PGUS/XRUK')               # 12.85 : Price of gold is equal in US and UK
    model.add('XRUS = 1/XRUK')                  # 12.86 : US exchange rate
    model.add('BsUSUK = BdUSUK*XRUS')           # 12.87 : Equilibrium condition for bills issued by UK acquired by US
    model.add('BcbdUKUS = BcbsUKUS*XRUS')       # 12.88 : Equilibrium conditioin for bills issued by US acquired by UK central bank
    # XRUK is exogenous
    # model.add('XRUK = BsUKUS/BdUKUS')           # 12.89FL : 
    # 12.89G : Bills supply from UK to US
    model.add('BsUKUS = XRUK*BdUKUS')
    # 12.90GA : Supply of UK bills to US, now solved for BcbsUKUS
    model.add('BcbsUKUS = BsUS - BsUSUS - BcbdUSUS - BsUKUS')
    # Government deficits in the UK
    model.add('PSBRUK = GUK + RUK(-1)*BsUK(-1) - TUK - FcbUK')
    # Government deficits in the US
    model.add('PSBRUS = GUS + RUS(-1)*BsUS(-1) - TUS - FcbUS')
    model.add('NAFAUK = PSBRUK + CABUK')        # Net accumulation of financial assets in the UK
    model.add('NAFAUS = PSBRUS + CABUS')        # Net accumulation of financial assets in the US

    return model

openfixg_parameters = {'alpha1UK': 0.75,
                      'alpha1US': 0.75,
                      'alpha2UK': 0.13333,
                      'alpha2US': 0.13333,
                      'eps0': -2.1,
                      'eps1': 0.7,
                      'eps2': 1,
                      'lambda10': 0.7,
                      'lambda11': 5,
                      'lambda12': 5,
                      'lambda20': 0.25,
                      'lambda21': 5,
                      'lambda22': 5,
                      'lambda40': 0.7,
                      'lambda41': 5,
                      'lambda42': 5,
                      'lambda50': 0.25,
                      'lambda51': 5,
                      'lambda52': 5,
                      'mu0': -2.1,
                      'mu1': 0.7,
                      'mu2': 1,
                      'nu0m': -0.00001,
                      'nu0x': -0.00001,
                      'nu1m': 0.7,
                      'nu1x': 0.5,
                      'phiUK': 0.2381,
                      'phiUS': 0.2381,
                      'thetaUK': 0.2,
                      'thetaUS': 0.2,
                     }

openfixg_exogenous = {'BcbsUKUS': 0.02031,
                     'DXREUS': 0,
                     'GkUK': 16,
                     'GkUS': 16,
                     'ORUK': 7,
                     'PGUS': 1,
                     'PRUK': 1.3333,
                     'PRUS': 1.3333,
                     'RUK': 0.03,
                     'RUS': 0.03,
                     'WUK': 1,
                     'WUS': 1,
                     'BcbdUKUK': 0.27984,
                     'BcbsUKUK': 0.27984,
                     'BcbdUKUS': 0.0203,
                     'BcbdUSUS': 0.29843,
                     'BcbsUSUS': 0.29843,
                     'BsUK': 138.94,
                     'BdUKUK': 102.18,
                     'BsUKUK': 102.18,
                     'BdUKUS': 36.493,
                     'BsUKUS': 36.504,
                     'BsUS': 139.02,
                     'BdUSUK': 36.497,
                     'BsUSUK': 36.487,
                     'BdUSUS': 102.19,
                     'BsUSUS': 102.19,
                     'HdUK': 7.2987,
                     'HsUK': 7.2987,
                     'HdUS': 7.2995,
                     'HsUS': 7.2995,
                     'ORUS': 7,
                     'VkUK': 152.62,
                     'VkUS': 152.63,
                     'VUK': 145.97,
                     'VUS': 145.99001,
                     'CkUK': 81.393,
                     'CkUS': 81.401,
                     'CABUK': 0,
                     'CABUS': 0,
                     'CONSUK': 77.851,
                     'CONSUS': 77.86,
                     'DSkUK': 97.393,
                     'DSkUS': 97.401,
                     'DSUK': 93.154,
                     'DSUS': 93.164,
                     'DXREUK': 0,
                     'FcbUK': 0.00869,
                     'FcbUS': 0.00895,
                     'GUK': 15.304,
                     'GUS': 15.304,
                     'IMkUK': 11.928,
                     'IMkUS': 11.926,
                     'IMUK': 11.407,
                     'IMUS': 11.409,
                     'KABPUK': 0.00002,
                     'KABPUS': -0.00002,
                     'NUK': 73.046,
                     'NUS': 73.054,
                     'PDSUK': 0.95648,
                     'PDSUS': 0.95649,
                     'PGUK': 0.99971,
                     'PMUK': 0.95628,
                     'PMUS': 0.95661,
                     'PSUK': 0.95646,
                     'PSUS': .9565,
                     'PXUK': 0.95634,
                     'PXUS': 0.95656,
                     'PYUK': 0.95648,
                     'PYUS': 0.95649,
                     'SkUK': 109.32,
                     'SkUS': 109.33,
                     'SUK': 104.56,
                     'SUS': 104.57,
                     'TUK': 19.463,
                     'TUS': 19.465,
                     'XkUK': 11.926,
                     'XkUS': 11.928,
                     'XUK': 11.406,
                     'XUS': 11.41,
                     'XRUK': 1.0003,
                     'XRUS': 0.99971,
                     'XREUK': 1.0003,
                     'XREUS': 0.99971,
                     'YkUK': 97.392,
                     'YkUS': 97.403,
                     'YUK': 93.154,
                     'YUS': 93.164,
                     'YDrUK': 77.851,
                     'YDrUS': 77.86,
                     'YDhskUK': 81.394,
                     'YDhskUS': 81.402,
                     'YDhsekUK': 81.394,
                     'YDhsekUS': 81.402,
                    }<jupyter_output><empty_output><jupyter_text>Scenario: Model OPENFIXG, baseline<jupyter_code>baseline = create_openfixg_model()
baseline.set_values(openfixg_parameters)
baseline.set_values(openfixg_exogenous)

# To get the model to converge, I use a different method for solving the set of equations.
for i in range(100):
    baseline.solve(iterations=200, threshold=1e-4, method='broyden')<jupyter_output><empty_output><jupyter_text>Scenario: Model OPENFIXG, increase the UK propensity to import<jupyter_code>mu0 = create_openfixg_model()
mu0.set_values(openfixg_parameters)
mu0.set_values(openfixg_exogenous)

for _ in range(10):
    mu0.solve(iterations=200, threshold=1e-4, method='broyden')

mu0.set_values({'mu0': -2.0})

for _ in range(90):
    mu0.solve(iterations=200, threshold=1e-4, method='broyden')<jupyter_output><empty_output><jupyter_text>Figure 12.3A<jupyter_code>caption = '''
    Figure 12.3A  Effect of an increase in the UK propensity to import within a fixed
    exchange rate regime with endogenous UK government expenditures, on the US and
    UK real GDP.'''
usdata = [s['YkUS'] for s in mu0.solutions[5:50]]
ukdata = [s['YkUK'] for s in mu0.solutions[5:50]]

fig = plt.figure()
axes = fig.add_axes([0.1, 0.1, 1.1, 1.1])
axes.tick_params(top=False, right=False)
axes.spines['top'].set_visible(False)
axes.spines['right'].set_visible(False)

axes.plot(usdata, linestyle='-', color='b')
axes.plot(ukdata, linestyle='--', linewidth=2, color='g')

# add labels
plt.text(15, 97, 'US real GDP')
plt.text(15, 90, 'UK real GDP')
fig.text(0.1, -.1, caption);<jupyter_output><empty_output><jupyter_text>Figure 12.3B<jupyter_code>caption = '''
    Figure 12.3B  Effect of an increase in the UK propensity to import within a fixed
    exchange rate regime with endogenous UK interest rates, on various UK variables:
    the capital account balance, the current account balance and the trade balance.'''
cabdata = [s['CABUK'] for s in mu0.solutions[5:50]]
xidata = [s['XUK'] - s['IMUK'] for s in mu0.solutions[5:50]]
kabpdata = [s['KABPUK'] for s in mu0.solutions[5:50]]


fig = plt.figure()
axes = fig.add_axes([0.1, 0.1, 1.1, 1.1])
axes.tick_params(top=False, right=False)
axes.spines['top'].set_visible(False)
axes.spines['right'].set_visible(False)

axes.plot(cabdata, linestyle='-', color='b')
axes.plot(xidata, linestyle='--', linewidth=2, color='g')
axes.plot(kabpdata, linestyle=':', linewidth=2, color='r')

# add labels
plt.text(25, -0.15, 'UK current account balance')
plt.text(30, 0.15, 'UK trade balance')
plt.text(9, 0.3, 'UK capital account balance')
fig.text(0.1, -.1, caption);<jupyter_output><empty_output><jupyter_text>Figure 12.3C<jupyter_code>caption = '''
    Figure 12.3C  Effect of an increase in the UK propensity to import within a fixed
    exchange rate regime with endogenous UK interest rates, on various UK variables:
    the capital account balance, the government budget balance, and the net accumulation
    of financial assets.'''
nafadata = [s['NAFAUK'] for s in mu0.solutions[5:50]]
cabdata = [s['CABUK'] for s in mu0.solutions[5:50]]
psbrdata = [-s['PSBRUK'] for s in mu0.solutions[5:50]]


fig = plt.figure()
axes = fig.add_axes([0.1, 0.1, 1.1, 1.1])
axes.tick_params(top=False, right=False)
axes.spines['top'].set_visible(False)
axes.spines['right'].set_visible(False)

axes.plot(nafadata, linestyle='-', color='b')
axes.plot(cabdata, linestyle='--', linewidth=2, color='g')
axes.plot(psbrdata, linestyle=':', linewidth=2, color='r')

# add labels
plt.text(10, -1, 'UK net accumulation')
plt.text(10, -1.15, 'of financial assets')
plt.text(7, -0.05, 'UK current account balance')
plt.text(10, 0.65, 'UK government budget balance')
fig.text(0.1, -.15, caption);<jupyter_output><empty_output><jupyter_text>Figure 12.3D<jupyter_code>caption = '''
    Figure 12.3D  Effect of an increase in the UK propensity to import within a fixed
    exchange rate regime with endogenous UK interest rates, on the UK and US
    debt to GDP ratios.'''
usdata = [s['BsUS']/s['YUS'] for s in mu0.solutions[5:50]]
ukdata = [s['BsUK']/s['YUK'] for s in mu0.solutions[5:50]]

fig = plt.figure()
axes = fig.add_axes([0.1, 0.1, 1.1, 1.1])
axes.tick_params(top=False, right=False)
axes.spines['top'].set_visible(False)
axes.spines['right'].set_visible(False)

axes.plot(usdata, linestyle='-', color='b')
axes.plot(ukdata, linestyle='--', linewidth=2, color='g')

# add labels
plt.text(20, 1.46, 'US debt to GDP ratio')
plt.text(20, 1.54, 'UK debt to GDP ratio')
fig.text(0.1, -.15, caption);<jupyter_output><empty_output><jupyter_text>Scenario: Model OPENFIXG, devaluation of pound sterling and increase in UK propensity to import<jupyter_code>xruk0 = create_openfixg_model()
xruk0.set_values(openfixg_parameters)
xruk0.set_values(openfixg_exogenous)

for _ in range(10):
    xruk0.solve(iterations=200, threshold=1e-4, method='newton-raphson')

xruk0.set_values({'mu0': -2.0})

for _ in range(5):
    xruk0.solve(iterations=200, threshold=1e-4, method='newton-raphson')

xruk0.set_values({'XRUK': 0.8375})

for _ in range(35):
    xruk0.solve(iterations=200, threshold=1e-4, method='newton-raphson')<jupyter_output><empty_output><jupyter_text>Figure 12.4A<jupyter_code>caption = '''
    Figure 12.4A  Effect on the UK current account balance and trade balance
    of a successful one-step devaluation of the pound sterling, following an
    increase in the UK propensity to import, within a fixed exchange rate
    regime with endogenous foreign reserves.'''
cabdata = [s['CABUK'] for s in xruk0.solutions[5:50]]
xidata = [s['XUK'] - s['IMUK'] for s in xruk0.solutions[5:50]]

fig = plt.figure()
axes = fig.add_axes([0.1, 0.1, 1.1, 1.1])
axes.tick_params(top=False, right=False)
axes.spines['top'].set_visible(False)
axes.spines['right'].set_visible(False)

axes.plot(cabdata, linestyle='-', color='b')
axes.plot(xidata, linestyle='--', linewidth=2, color='g')

# add labels
plt.text(20, -0.6, 'UK current account balance')
plt.text(20, 0.4, 'UK trade balance')
fig.text(0.1, -.15, caption);<jupyter_output><empty_output><jupyter_text>Figure 12.4B<jupyter_code>caption = '''
    Figure 12.4B  Effect of a successful one-step devaluation of the pound
    sterling on UK real GDP, following an increase in the UK propensity to
    import, within a fixed exchange rate regime with endogenous foreign reserves.'''
ydata = [s['YkUK'] for s in xruk0.solutions[5:50]]
xrdata = [s['XRUK'] for s in xruk0.solutions[5:50]]

fig = plt.figure()
axes = fig.add_axes([0.1, 0.1, 1.1, 1.1])
axes.tick_params(top=False, right=False)
axes.spines['top'].set_visible(False)
axes.spines['right'].set_visible(False)
axes.set_ylim(85, 140)
axes.plot(ydata, linestyle='-', color='b')

plt.text(15, 104, 'UK real rate')

axes2 = axes.twinx()
axes2.tick_params(top=False)
axes2.spines['top'].set_visible(False)
axes2.set_ylim(0.83, 1.02)
axes2.plot(xrdata, linestyle='--', linewidth=2, color='g')

plt.text(20, 0.85, 'Value of UK currency in dollars')

# add labels
fig.text(0, 1.25, 'Real output')
fig.text(1.1, 1.25, 'Exchange rate')
fig.text(0.1, -.15, caption);<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1445833601856029, 'fraction_numerical': 0.04578849004317845, 'mean_word_length': 2.5263038291103284, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '167005', 'license': 'MIT', 'n_tokens_mistral': 11801, 'n_tokens_neox': 10535, 'n_words': 2938, 'path': 'godley_&_lavoie/Python 3 - Chapter 12 Model OPENFIXG.ipynb', 'repo_name': 'gpetrini/pysolve3', 'size': '31034'}