示例#1
0
def test_mkdynare_other():

    '''
    print "Now testing dynare translation of Monika Merz's model"
    # Test mk_dynare method on model
    rbc = pm.newMOD(models.stable.merz,mesg=True)
    rbc.mk_dynare()
    '''
    '''
    print "Now testing dynare translation of RBC2 model"
    # Test mk_dynare method on model
    rbc = pm.newMOD(models.stable.rbc2,mesg=True)
    rbc.mk_dynare()
    '''
    
    print "Now testing dynare translation of Grohe and Uribe's RBC model"
    # Test mk_dynare method on model
    rbc = pm.newMOD(models.stable.grohurib03,mesg=True)
    rbc.modsolvers.dynarepp.solve()
    
    print "Now testing dynare translation of Chris Sim's RBC model"
    # Test mk_dynare method on model
    rbc = pm.newMOD(models.stable.sims,mesg=True)
    rbc.modsolvers.dynarepp.solve()
    



     
示例#2
0
def test_cee():

    # Instantiate model in the normal way
    cee = pm.newMOD(models.testing.cee,mesg=True)
    params = copy.deepcopy(cee.template_paramdic)

    # Now use the template to do it again
    modstr = cee.translators.pml_to_pml(template_paramdic=params)
    cee2 = pm.newMOD(modstr,mesg=True)

    # Check if the two template_paramdics are identical
    for keyo in cee.template_paramdic.keys():
        print "Now testing key: ",keyo
        print cee.template_paramdic[keyo]
        print '--------------------------'
        print cee2.template_paramdic[keyo]
        if keyo == 'sigma':
            assert cee.template_paramdic[keyo].all() == cee2.template_paramdic[keyo].all()
        elif keyo == 'paramdic':
            for keyo2 in cee.template_paramdic[keyo].keys():
                assert round(cee.template_paramdic[keyo][keyo2],6) == round(cee2.template_paramdic[keyo][keyo2],6)
        elif keyo == 'ssili':
            for i1,elem in enumerate([x[0] for x in cee.template_paramdic[keyo]]):
                assert round(float(cee.template_paramdic[keyo][i1][1]),6) == round(float(cee2.template_paramdic[keyo][i1][1]),6)
        elif keyo == 'ssidic':
            for keyo2 in cee.template_paramdic[keyo].keys():
                assert round(cee.template_paramdic[keyo][keyo2],6) == round(cee2.template_paramdic[keyo][keyo2],6)
        else:
            assert cee.template_paramdic[keyo] == cee2.template_paramdic[keyo]
示例#3
0
def test3():
    print "Now Loading and differentiating model rbc1..."
    rbc1 = pm.newMOD(models.stable.rbc1_res,mesg=True,ncpus='auto')
    print
    print "Now loading and differentiating model rbc2..."
    rbc2 = pm.newMOD(models.stable.rbc2,mesg=True,ncpus='auto')
    print
    print "Now loading and differentiating model merz..."
    merz = pm.newMOD(models.stable.merz,mesg=True,ncpus='auto')
    print
    print "Now loading and differentiating model cee..."
    cee  = pm.newMOD(models.testing.cee,mesg=True,ncpus='auto')
示例#4
0
def test3():
    print "Now Loading and differentiating model rbc1..."
    rbc1 = pm.newMOD(models.stable.rbc1_res, mesg=True, ncpus='auto')
    print
    print "Now loading and differentiating model rbc2..."
    rbc2 = pm.newMOD(models.stable.rbc2, mesg=True, ncpus='auto')
    print
    print "Now loading and differentiating model merz..."
    merz = pm.newMOD(models.stable.merz, mesg=True, ncpus='auto')
    print
    print "Now loading and differentiating model cee..."
    cee = pm.newMOD(models.testing.cee, mesg=True, ncpus='auto')
示例#5
0
def test8():

    # Test the steady state method in which USE_FOCS was declared inside the model
    rbc_focs = pm.newMOD(models.stable.rbc1_focs, mesg=True)
    # Did it work?
    assert 'sstate' in dir(rbc_focs)

    # Now copy the sstate dictionary and test the steady state method in which it is passed from outside the model
    statedic = deepcopy(rbc_focs.sstate)
    rbc_ext = pm.newMOD(models.stable.rbc1_extss, mesg=True, sstate=statedic)
    # Did it work?
    assert 'sstate' in dir(rbc_ext)
示例#6
0
def test8():

    # Test the steady state method in which USE_FOCS was declared inside the model
    rbc_focs = pm.newMOD(models.stable.rbc1_focs,mesg=True)
    # Did it work?
    assert 'sstate' in dir(rbc_focs)
    
    # Now copy the sstate dictionary and test the steady state method in which it is passed from outside the model
    statedic = deepcopy(rbc_focs.sstate)
    rbc_ext = pm.newMOD(models.stable.rbc1_extss,mesg=True,sstate=statedic)
    # Did it work?
    assert 'sstate' in dir(rbc_ext)
示例#7
0
def test_filters():
    rbc = pm.newMOD(models.stable.rbc1_num,mesg=True)
    rbc.modsolvers.forkleind.solve()
    # Also test here the one-element shock array at work
    rbc.modsolvers.forkleind.sim(250,('productivity'))
    
    # Use consumption and test the filtering options
    
    # First turn this into a ordinary Python list and test it
    consmat = rbc.modsolvers.forkleind.insim[1][0]
    consmat2 = consmat.reshape(consmat.shape[1],consmat.shape[0])
    consarr = consmat.__array__()
    consarr2 = consarr.flatten()
    consli = [x for x in rbc.modsolvers.forkleind.insim[1][0].__array__()[0]]
    
    datli = []
    datli.append(consmat)
    datli.append(consmat2)
    datli.append(consarr)
    datli.append(consarr2)
    datli.append(consli)

    for datar in datli:
        cons_cycle = filters.hpfilter(data=datar)[0]
        cons_cycle2 = filters.bkfilter(data=datar)[0]
        cons_cycle3 = filters.cffilter(data=datar)[0]
示例#8
0
def test7():
    # Define the ssidic of initial guesses or starting values
    alpha      = 0.36
    chi        = 0.819
    xi         = 1.0/4.3
    g          = 0.005
    tau        = 5.0
    betta      = 1.014
    bettaa     = betta*(1+g)**(-tau)
    delta      = 0.025
    rho        = 0.95
    a1         = (g+delta)**(1.0/xi)
    a2         = (g+delta)/(1.0-xi)
    z_bar      = 1.0
    sigma_eps  = 0.01
    
    ssidic = {}
    ssidic['k_bar']     = 36.0                                                  #1
    ssidic['inv_bar']   = 1.0                                                   #2
    ssidic['c_bar']     = 2.5                                                   #3
    ssidic['mu_bar']    = 8.0                                                   #4

    # Pick first FOC equations to leave out exogenous law of motion
    focs = range(4)

    # Instantiate a new DSGE model instance like so
    asset = pm.newMOD(models.stable.jermann98_ext,mesg=True,use_focs=focs,ssidic=ssidic)
    
    return asset
示例#9
0
def test_filters():
    rbc = pm.newMOD(models.stable.rbc1_num, mesg=True)
    rbc.modsolvers.forkleind.solve()
    # Also test here the one-element shock array at work
    rbc.modsolvers.forkleind.sim(250, ('productivity'))

    # Use consumption and test the filtering options

    # First turn this into a ordinary Python list and test it
    consmat = rbc.modsolvers.forkleind.insim[1][0]
    consmat2 = consmat.reshape(consmat.shape[1], consmat.shape[0])
    consarr = consmat.__array__()
    consarr2 = consarr.flatten()
    consli = [x for x in rbc.modsolvers.forkleind.insim[1][0].__array__()[0]]

    datli = []
    datli.append(consmat)
    datli.append(consmat2)
    datli.append(consarr)
    datli.append(consarr2)
    datli.append(consli)

    for datar in datli:
        cons_cycle = filters.hpfilter(data=datar)[0]
        cons_cycle2 = filters.bkfilter(data=datar)[0]
        cons_cycle3 = filters.cffilter(data=datar)[0]
示例#10
0
def test5():
    # Instantiate a new DSGE model instance like so
    rbc1 = pm.newMOD(models.stable.rbc1_res)

    # Now solve and simulate the model
    rbc1.modsolvers.forkleind.solve()
    rbc1.modsolvers.forkleind.irf(100, ('productivity', ))

    # Plot the simulation and show it on screen
    rbc1.modsolvers.forkleind.show_irf(('output', 'consumption'))
示例#11
0
def test4():
    # Instantiate a new DSGE model instance like so
    rbc1 = pm.newMOD(models.stable.rbc1_res)

    # Now simulate the model
    rbc1.modsolvers.forkleind.solve()
    rbc1.modsolvers.forkleind.sim(200)

    # Plot the simulation and show it on screen
    rbc1.modsolvers.forkleind.show_sim(('output','consumption'))
示例#12
0
def test5():
    # Instantiate a new DSGE model instance like so
    rbc1 = pm.newMOD(models.stable.rbc1_res)

    # Now solve and simulate the model
    rbc1.modsolvers.forkleind.solve()
    rbc1.modsolvers.forkleind.irf(100,('productivity',))

    # Plot the simulation and show it on screen
    rbc1.modsolvers.forkleind.show_irf(('output','consumption'))
示例#13
0
def test_cee():

    # Instantiate model in the normal way
    cee = pm.newMOD(models.testing.cee,mesg=True)
    params = copy.deepcopy(cee.template_paramdic)
    
    # Now use the template to do it again
    modstr = template.render(params)
    cee2 = pm.newMOD(modstr,mesg=True)
    
    # Check if the two template_paramdics are identical
    for keyo in cee.template_paramdic.keys():
        print "Now testing key: ",keyo
        if keyo == 'sigma':
            assert cee.template_paramdic[keyo].all() == cee2.template_paramdic[keyo].all()
        elif keyo == 'paramdic':
            for keyo2 in cee.template_paramdic[keyo].keys():
                assert round(cee.template_paramdic[keyo][keyo2],6) == round(cee2.template_paramdic[keyo][keyo2],6)
        else:
            assert cee.template_paramdic[keyo] == cee2.template_paramdic[keyo]
示例#14
0
def test_mkdynare_basic():

    # Test mk_dynare method on model
    rbc = pm.newMOD(models.stable.rbc1_focs,mesg=True)
    rbc.modsolvers.dynarepp.solve()
    
    # Test mk_dynare method on model
    rbc = pm.newMOD(models.stable.rbc1_cf,mesg=True)
    rbc.modsolvers.dynarepp.solve()
    
    # Test mk_dynare method on model
    rbc = pm.newMOD(models.stable.rbc1_num,mesg=True)
    rbc.modsolvers.dynarepp.solve()
    
    # Test mk_dynare method on model
    rbc = pm.newMOD(models.stable.rbc1_res,mesg=True)
    rbc.modsolvers.dynarepp.solve()
    
    # Test mk_dynare method on model
    rbc = pm.newMOD(models.stable.rbc1_sug,mesg=True)
    rbc.modsolvers.dynarepp.solve()
示例#15
0
def test_cee():

    # Instantiate model in the normal way
    cee = pm.newMOD(models.testing.cee, mesg=True)
    params = copy.deepcopy(cee.template_paramdic)

    # Now use the template to do it again
    modstr = cee.translators.pml_to_pml(template_paramdic=params)
    cee2 = pm.newMOD(modstr, mesg=True)

    # Check if the two template_paramdics are identical
    for keyo in cee.template_paramdic.keys():
        print "Now testing key: ", keyo
        print cee.template_paramdic[keyo]
        print '--------------------------'
        print cee2.template_paramdic[keyo]
        if keyo == 'sigma':
            assert cee.template_paramdic[keyo].all(
            ) == cee2.template_paramdic[keyo].all()
        elif keyo == 'paramdic':
            for keyo2 in cee.template_paramdic[keyo].keys():
                assert round(cee.template_paramdic[keyo][keyo2],
                             6) == round(cee2.template_paramdic[keyo][keyo2],
                                         6)
        elif keyo == 'ssili':
            for i1, elem in enumerate(
                [x[0] for x in cee.template_paramdic[keyo]]):
                assert round(float(cee.template_paramdic[keyo][i1][1]),
                             6) == round(
                                 float(cee2.template_paramdic[keyo][i1][1]), 6)
        elif keyo == 'ssidic':
            for keyo2 in cee.template_paramdic[keyo].keys():
                assert round(cee.template_paramdic[keyo][keyo2],
                             6) == round(cee2.template_paramdic[keyo][keyo2],
                                         6)
        else:
            assert cee.template_paramdic[keyo] == cee2.template_paramdic[keyo]
示例#16
0
def test_mkdynare_other():

    '''
    print "Now testing dynare translation of Monika Merz's model"
    # Test mk_dynare method on model
    rbc = pm.newMOD(models.stable.merz,mesg=True)
    rbc.mk_dynare()
    '''
    '''
    print "Now testing dynare translation of RBC2 model"
    # Test mk_dynare method on model
    rbc = pm.newMOD(models.stable.rbc2,mesg=True)
    rbc.mk_dynare()
    '''
    
    print "Now testing dynare translation of Grohe and Uribe's RBC model"
    # Test mk_dynare method on model
    rbc = pm.newMOD(models.stable.grohurib03,mesg=True)
    rbc.modsolvers.dynarepp.solve()
    
    print "Now testing dynare translation of Chris Sim's RBC model"
    # Test mk_dynare method on model
    rbc = pm.newMOD(models.stable.sims,mesg=True)
    rbc.modsolvers.dynarepp.solve()
示例#17
0
def test6():
    # Instantiate a new DSGE model instance like so
    rbc1 = pm.newMOD(models.stable.rbc1_res,mk_hessian=False)

    # Now solve and simulate the model
    rbc1.modsolvers.forkleind.solve()
    rbc1.modsolvers.forkleind.sim(200)

    # Plot the simulation and show it on screen
    rbc1.modsolvers.forkleind.show_sim(('output','consumption'))
    #plt.show()

    # Now save the shocks, by saving a clone or copy, instead of a reference
    shockv = deepcopy(rbc1.modsolvers.forkleind.shockvec)

    # Change the filtering assumption of output and consumption using the queued updater branch
    rbc1.updaters_queued.vardic['con']['mod'][0][1] = 'hp'
    rbc1.updaters_queued.vardic['con']['mod'][1][1] = 'hp'
    rbc1.updaters_queued.process_queue()

    # Now we could run the simulation again, this time passing the randomly drawn shocks
    rbc1.modsolvers.forkleind.solve()
    rbc1.modsolvers.forkleind.sim(200,shockvec=shockv)

    # Plot the simulation and show it on screen
    rbc1.modsolvers.forkleind.show_sim(('output','consumption'))
    #plt.show()

    # Change the filterin assumption of output and consumption using the queued updater branch
    rbc1.updaters_queued.vardic['con']['mod'][0][1] = 'cf'
    rbc1.updaters_queued.vardic['con']['mod'][1][1] = 'cf'
    rbc1.updaters_queued.process_queue()

    # Now we could run the simulation again, this time passing the randomly drawn shocks
    rbc1.modsolvers.forkleind.solve()
    rbc1.modsolvers.forkleind.sim(200,shockvec=shockv)

    # Plot the simulation and show it on screen
    rbc1.modsolvers.forkleind.show_sim(('output','consumption'))
示例#18
0
def test2():
    # Instantiate a new DSGE model instance like so
    rbc1 = pm.newMOD(models.stable.rbc1_res)

    # Create an array representing a finely-spaced range of possible impatience values
    # Then convert to corresponding steady state gross real interest rate value
    betarr = np.arange(0.8, 0.99, 0.001)
    betarr = 1.0 / betarr

    ss_capital = []
    for betar in betarr:
        rbc1.updaters.paramdic['R_bar'] = betar
        rbc1.sssolvers.fsolve.solve()
        ss_capital.append(rbc1.sssolvers.fsolve.fsout['k_bar'])

    # Create a nice figure
    fig1 = plt.figure()
    plt.grid()
    plt.title('Plot of steady state physical capital against R\_bar')
    plt.xlabel(r'Steady state gross real interest rate')
    plt.ylabel(r'Steady State of physical capital')
    plt.plot(betarr, ss_capital, 'k-')
示例#19
0
文件: test6.py 项目: yoki/pymaclab
def test6():
    # Instantiate a new DSGE model instance like so
    rbc1 = pm.newMOD(models.stable.rbc1_res, mk_hessian=False)

    # Now solve and simulate the model
    rbc1.modsolvers.forkleind.solve()
    rbc1.modsolvers.forkleind.sim(200)

    # Plot the simulation and show it on screen
    rbc1.modsolvers.forkleind.show_sim(("output", "consumption"))
    # plt.show()

    # Now save the shocks, by saving a clone or copy, instead of a reference
    shockv = deepcopy(rbc1.modsolvers.forkleind.shockvec)

    # Change the filtering assumption of output and consumption using the queued updater branch
    rbc1.updaters_queued.vardic["con"]["mod"][0][1] = "hp"
    rbc1.updaters_queued.vardic["con"]["mod"][1][1] = "hp"
    rbc1.updaters_queued.process_queue()

    # Now we could run the simulation again, this time passing the randomly drawn shocks
    rbc1.modsolvers.forkleind.solve()
    rbc1.modsolvers.forkleind.sim(200, shockvec=shockv)

    # Plot the simulation and show it on screen
    rbc1.modsolvers.forkleind.show_sim(("output", "consumption"))
    # plt.show()

    # Change the filterin assumption of output and consumption using the queued updater branch
    rbc1.updaters_queued.vardic["con"]["mod"][0][1] = "cf"
    rbc1.updaters_queued.vardic["con"]["mod"][1][1] = "cf"
    rbc1.updaters_queued.process_queue()

    # Now we could run the simulation again, this time passing the randomly drawn shocks
    rbc1.modsolvers.forkleind.solve()
    rbc1.modsolvers.forkleind.sim(200, shockvec=shockv)

    # Plot the simulation and show it on screen
    rbc1.modsolvers.forkleind.show_sim(("output", "consumption"))
示例#20
0
def test2():
    # Instantiate a new DSGE model instance like so
    rbc1 = pm.newMOD(models.stable.rbc1_res)

    # Create an array representing a finely-spaced range of possible impatience values
    # Then convert to corresponding steady state gross real interest rate value
    betarr = np.arange(0.8,0.99,0.001)
    betarr = 1.0/betarr


    ss_capital = []
    for betar in betarr:
	rbc1.updaters.paramdic['R_bar'] = betar
	rbc1.sssolvers.fsolve.solve()
	ss_capital.append(rbc1.sssolvers.fsolve.fsout['k_bar'])

    # Create a nice figure
    fig1 = plt.figure()
    plt.grid()
    plt.title('Plot of steady state physical capital against R\_bar')
    plt.xlabel(r'Steady state gross real interest rate')
    plt.ylabel(r'Steady State of physical capital')
    plt.plot(betarr,ss_capital,'k-')
示例#21
0
# Import the pymaclab module into its namespace, also import os module
import pymaclab as pm
from pymaclab.modfiles import models

# Define the ssidic of initial guesses or starting values
alpha      = 0.36
chi        = 0.819
xi         = 1.0/4.3
g          = 0.005
tau        = 5.0
betta      = 1.014
bettaa     = betta*(1+g)**(-tau)
delta      = 0.025
rho        = 0.95
a1         = (g+delta)**(1.0/xi)
a2         = (g+delta)/(1.0-xi)
z_bar      = 1.0
sigma_eps  = 0.01

ssidic = {}
ssidic['k_bar']     = 36.0                                                  #1
ssidic['inv_bar']   = 1.0                                                   #2
ssidic['c_bar']     = 2.5                                                   #3
ssidic['mu_bar']    = 8.0                                                   #4

# Pick first FOC equations to leave out exogenous law of motion
focs = range(4)

# Instantiate a new DSGE model instance like so
asset = pm.newMOD(models.stable.jermann98,mesg=True,use_focs=focs,ssidic=ssidic)
示例#22
0
# Import the pymaclab module into its namespace, also import os module
import pymaclab as pm
from pymaclab.modfiles import models
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import rc
rc('text', usetex=True)

# Instantiate a new DSGE model instance like so
rbc1 = pm.newMOD(models.stable.rbc1_res)

# Create an array representing a finely-spaced range of possible impatience values
# Then convert to corresponding steady state gross real interest rate value
betarr = np.arange(0.8,0.99,0.001)
betarr = 1.0/betarr


ss_capital = []
for betar in betarr:
    rbc1.updaters.paramdic['R_bar'] = betar
    rbc1.sssolvers.fsolve.solve()
    ss_capital.append(rbc1.sssolvers.fsolve.fsout['k_bar'])

# Create a nice figure
fig1 = plt.figure()
plt.grid()
plt.title('Plot of steady state physical capital against R\_bar')
plt.xlabel(r'Steady state gross real interest rate')
plt.ylabel(r'Steady State of physical capital')
plt.plot(betarr,ss_capital,'k-')
plt.show()
示例#23
0
def test9():
    
    #####################################
    ## STANDARD COOLEY HANSE CIA MODEL ##
    #####################################
    # Load and solve the manually linearized model
    rbc1 = pm.newMOD(models.abcs_rbcs.cooley_hansen_cia_linear,mesg=True)
    rbc1.modsolvers.pyuhlig.solve()
    rbc1.modsolvers.forklein.solve()
    
    # Load and solve the automatically linearized model
    rbc2 = pm.newMOD(models.abcs_rbcs.cooley_hansen_cia_cf,mesg=True)
    rbc2.modsolvers.forkleind.solve()
    
    # Check equivalence of steady states
    for keyo in rbc1.sstate.keys():
        if keyo in rbc2.sstate.keys():
            assert round(rbc1.sstate[keyo],5) == round(rbc2.sstate[keyo],5)
    
    # Check equivalence of results
    nexo = len(rbc2.vardic['exo']['var'])
    nendo = len(rbc2.vardic['endo']['var'])
    
    modlin1 = MAT.hstack((rbc1.modsolvers.pyuhlig.Q,rbc1.modsolvers.pyuhlig.P))
    modlin1 = [round(modlin1[0,i1],5) for i1 in range(modlin1.shape[1])]
    modnlin1 = rbc2.modsolvers.forkleind.P[-nendo:,:]
    modnlin1 = [round(modnlin1[0,i1],5) for i1 in range(modnlin1.shape[1])]
    print 'Comparison: Standard CIA model'
    print "Linear is: ",modlin1
    print '----------------------'
    print "Nonlinear is: ",modnlin1
    assert modlin1 == modnlin1
    
    modlin2 = MAT.hstack((rbc1.modsolvers.pyuhlig.S,rbc1.modsolvers.pyuhlig.R))
    modlin2 = [[round(modlin2[i2,i1],5) for i1 in range(modlin2.shape[1])] for i2 in range(modlin2.shape[0])]
    modnlin2 = rbc2.modsolvers.forkleind.F
    modnlin2 = [[round(modnlin2[i2,i1],5) for i1 in range(modnlin2.shape[1])] for i2 in range(modnlin2.shape[0])]
    print modlin2
    print '----------------------'
    print modnlin2    
    assert modlin2 == modnlin2
    

    #####################################################
    ## STANDARD COOLEY HANSE CIA MODEL WITH SEIGNORAGE ##
    #####################################################
    # Load and solve the manually linearized model
    rbc1 = pm.newMOD(models.abcs_rbcs.cooley_hansen_cia_seignorage_linear,mesg=True)
    rbc1.modsolvers.pyuhlig.solve()
    rbc1.modsolvers.forklein.solve()
    
    # Load and solve the automatically linearized model
    rbc2 = pm.newMOD(models.abcs_rbcs.cooley_hansen_cia_seignorage_cf,mesg=True)
    rbc2.modsolvers.forkleind.solve()
    
    # Check equivalence of steady states
    for keyo in rbc1.sstate.keys():
        if keyo in rbc2.sstate.keys():
            assert round(rbc1.sstate[keyo],5) == round(rbc2.sstate[keyo],5)
    
    # Check equivalence of results
    nexo = len(rbc2.vardic['exo']['var'])
    nendo = len(rbc2.vardic['endo']['var'])
    
    modlin1 = MAT.hstack((rbc1.modsolvers.pyuhlig.Q,rbc1.modsolvers.pyuhlig.P))
    modlin1 = [round(modlin1[0,i1],5) for i1 in range(modlin1.shape[1])]
    modnlin1 = rbc2.modsolvers.forkleind.P[-nendo:,:]
    modnlin1 = [round(modnlin1[0,i1],5) for i1 in range(modnlin1.shape[1])]
    print 'Comparison: Standard CIA model with seignorage'
    print "Linear is: ",modlin1
    print '----------------------'
    print "Nonlinear is: ",modnlin1
    assert modlin1 == modnlin1
    
    modlin2 = MAT.hstack((rbc1.modsolvers.pyuhlig.S,rbc1.modsolvers.pyuhlig.R))
    modlin2 = [[round(modlin2[i2,i1],5) for i1 in range(modlin2.shape[1])] for i2 in range(modlin2.shape[0])]
    modnlin2 = rbc2.modsolvers.forkleind.F
    modnlin2 = [[round(modnlin2[i2,i1],5) for i1 in range(modnlin2.shape[1])] for i2 in range(modnlin2.shape[0])]
    print modlin2
    print '----------------------'
    print modnlin2    
    assert modlin2 == modnlin2


    #####################################################################
    ## STANDARD COOLEY HANSE CIA MODEL WITH SEIGNORAGE AND CES UTILITY ##
    #####################################################################
    # Be careful, here in the log-linearized version we have two endogenous states, k and mg
    # But in the automatically linearized version using the Jacobian we only have one, k
    
    # Load and solve the manually linearized model
    rbc1 = pm.newMOD(models.abcs_rbcs.cooley_hansen_cia_seignorage_ces_linear,mesg=True)
    rbc1.modsolvers.pyuhlig.solve()
    rbc1.modsolvers.forklein.solve()
    
    # Load and solve the automatically linearized model
    rbc2 = pm.newMOD(models.abcs_rbcs.cooley_hansen_cia_seignorage_ces_cf,mesg=True)
    rbc2.modsolvers.forkleind.solve()
    
    # Check equivalence of steady states
    for keyo in rbc1.sstate.keys():
        if keyo in rbc2.sstate.keys():
            assert round(rbc1.sstate[keyo],5) == round(rbc2.sstate[keyo],5)
    
    # Check equivalence of results
    nexo = len(rbc2.vardic['exo']['var'])
    nendo = len(rbc2.vardic['endo']['var'])
    
    modlin1 = MAT.hstack((rbc1.modsolvers.pyuhlig.Q[:nendo,:],rbc1.modsolvers.pyuhlig.P[:nendo,:nendo]))
    modlin1 = [[round(modlin1[i2,i1],5) for i1 in range(modlin1.shape[1])] for i2 in range(modlin1.shape[0])]
    modnlin1 = rbc2.modsolvers.forkleind.P[-nendo:,:]
    modnlin1 = [[round(modnlin1[i2,i1],5) for i1 in range(modnlin1.shape[1])] for i2 in range(modnlin1.shape[0])]
    print 'Comparison: Standard CIA model with seignorage and CES utility'
    print "Linear is: ",modlin1
    print '----------------------'
    print "Nonlinear is: ",modnlin1
    assert modlin1 == modnlin1
    
    modlin2 = MAT.hstack((rbc1.modsolvers.pyuhlig.S[:,:],rbc1.modsolvers.pyuhlig.R[:,:nendo]))
    modlin2 = [[round(modlin2[i2,i1],5) for i1 in range(modlin2.shape[1])] for i2 in range(modlin2.shape[0])]
    modnlin2 = rbc2.modsolvers.forkleind.F[:-1,:]
    modnlin2 = [[round(modnlin2[i2,i1],5) for i1 in range(modnlin2.shape[1])] for i2 in range(modnlin2.shape[0])]
    print modlin2
    print '----------------------'
    print modnlin2    
    assert modlin2 == modnlin2
示例#24
0
# Import the pymaclab module into its namespace, also import os module
import pymaclab as pm
from pymaclab.modfiles import models

# Also import matplotlib.pyplot for showing the graph
from matplotlib import pyplot as plt
from copy import deepcopy

# Instantiate a new DSGE model instance like so
rbc1 = pm.newMOD(models.stable.rbc1_res,mk_hessian=False)

# Now solve and simulate the model
rbc1.modsolvers.forkleind.solve()
rbc1.modsolvers.forkleind.sim(200)

# Plot the simulation and show it on screen
rbc1.modsolvers.forkleind.show_sim(('output','consumption'))
plt.show()

# Now save the shocks, by saving a clone or copy, instead of a reference
shockv = deepcopy(rbc1.modsolvers.forkleind.shockvec)

# Change the filtering assumption of output and consumption using the queued updater branch
rbc1.updaters_queued.vardic['con']['mod'][0][1] = 'hp'
rbc1.updaters_queued.vardic['con']['mod'][1][1] = 'hp'
rbc1.updaters_queued.process_queue()

# Now we could run the simulation again, this time passing the randomly drawn shocks
rbc1.modsolvers.forkleind.solve()
rbc1.modsolvers.forkleind.sim(200,shockvec=shockv)
示例#25
0
import pymaclab as pm
import pymaclab.modfiles.models as models

rbc = pm.newMOD(models.stable.rbc1_num, mesg=False, ncpus='auto')


def test_all():
    # Do for paramdic
    eta_key = 'eta'
    eta_old = 2.0
    eta_new = 5.0
    rho_key = 'rho'
    rho_old = 0.36
    rho_new = 0.35
    rbc.updaters_queued.paramdic[eta_key] = eta_new
    rbc.updaters_queued.paramdic[rho_key] = rho_new

    # Do for nlsubsdic
    U_key = '@U(t)'
    U_old = 'c(t)**(1-eta)/(1-eta)'
    U_new = 'c(t)**(1-eta*1.01)/(1-eta*1.01)'
    F_key = '@F(t)'
    F_old = 'z(t)*k(t-1)**rho'
    F_new = 'z(t)*k(t-1)**rho*1.01'
    rbc.updaters_queued.nlsubsdic[U_key] = U_new
    rbc.updaters_queued.nlsubsdic[F_key] = F_new

    # Do for vardic
    var_key = ['c(t)', 'consumption']
    indexo = rbc.vardic['con']['var'].index(var_key)
    var_old = 'bk'
示例#26
0
import pymaclab as pm
import pymaclab.modfiles.models as models

rbc = pm.newMOD(models.stable.rbc1_num,mesg=False,ncpus='auto')

# Try to update all of the wrapped objects and test if this has worked

# Do for paramdic, set_item
def test_paramdic_item():
    eta_key = 'eta'
    eta_old = 2.0
    eta_new = 5.0
    rbc.updaters.paramdic[eta_key] = eta_new
    
    # Did it work?
    assert rbc.updaters.paramdic.wrapobj[eta_key] == eta_new
    assert rbc.updaters.paramdic[eta_key] == eta_new
    assert rbc.paramdic[eta_key] == eta_new
    

# Do for paramdic, update
def test_paramdic_update():
    eta_key = 'eta'
    eta_old = 2.0
    eta_new = 5.0
    rho_key = 'rho'
    rho_old = 0.36
    rho_new = 0.35
    tmp_dic = {}
    tmp_dic[eta_key] = eta_new
    tmp_dic[rho_key] = rho_new
示例#27
0
# Define the ssidic of initial guesses or starting values
alpha = 0.3
theta = 0.85
gamma = 0.25
delta_k = 0.025
betta = 0.99
B     = 0.65
inf_bar = 1.012
ssidic = {}
ssidic['k_bar']    = 30.0
ssidic['n_bar']    = 0.33
ssidic['inf_bar']  = inf_bar
ssidic['r_bar']    = 1/betta
ssidic['y_bar']    = ssidic['k_bar']**alpha*ssidic['n_bar']**(1-alpha)
ssidic['c_bar']    = ssidic['y_bar'] - delta_k*ssidic['k_bar']
ssidic['m_bar']    = 5.0
# Define marginal cost
rk = alpha*(ssidic['n_bar']/ssidic['k_bar'])**(1-alpha)
rn = (1-alpha)*(ssidic['k_bar']/ssidic['n_bar'])**alpha
mc = (1/(1-alpha))**(1-alpha)*(1/alpha)**alpha*(rk)**alpha*(rn)
ssidic['aa_bar']   = inf_bar*mc*ssidic['y_bar']/(1-(1-gamma)*betta*ssidic['inf_bar']**((2-theta)/(1-theta)))
ssidic['bb_bar']   = ssidic['y_bar']/(1-(1-gamma)*betta*ssidic['inf_bar']**(1/(1-theta)))
ssidic['infr_bar'] = (1/theta)*(ssidic['aa_bar']/ssidic['bb_bar'])
ssidic['i_bar']    = ssidic['r_bar']*inf_bar
ssidic['lam_bar']  = 1.0/ssidic['c_bar']
ssidic['w_bar'] = (1/theta)*rn

# Instantiate a new DSGE model instance like so
foc_li = [0,1,2,3,4,5,6,7,8,9,10,11,12]
nkm1 = pm.newMOD(models.nkm,use_focs=foc_li,ssidic=ssidic,initlev=0) 
示例#28
0
# Import the pymaclab module into its namespace, also import os module
import pymaclab as pm
from pymaclab.modfiles import models
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import rc

rc('text', usetex=True)

# Instantiate a new DSGE model instance like so
rbc1 = pm.newMOD(models.stable.rbc1_res)

# Create an array representing a finely-spaced range of possible impatience values
# Then convert to corresponding steady state gross real interest rate value
betarr = np.arange(0.8, 0.99, 0.001)
betarr = 1.0 / betarr

ss_capital = []
for betar in betarr:
    rbc1.updaters.paramdic['R_bar'] = betar
    rbc1.sssolvers.fsolve.solve()
    ss_capital.append(rbc1.sssolvers.fsolve.fsout['k_bar'])

# Create a nice figure
fig1 = plt.figure()
plt.grid()
plt.title('Plot of steady state physical capital against R\_bar')
plt.xlabel(r'Steady state gross real interest rate')
plt.ylabel(r'Steady State of physical capital')
plt.plot(betarr, ss_capital, 'k-')
plt.show()
示例#29
0
gamma = 0.25
delta_k = 0.025
betta = 0.99
B = 0.65
inf_bar = 1.012
ssidic = {}
ssidic['k_bar'] = 30.0
ssidic['n_bar'] = 0.33
ssidic['inf_bar'] = inf_bar
ssidic['r_bar'] = 1 / betta
ssidic['y_bar'] = ssidic['k_bar']**alpha * ssidic['n_bar']**(1 - alpha)
ssidic['c_bar'] = ssidic['y_bar'] - delta_k * ssidic['k_bar']
ssidic['m_bar'] = 5.0
# Define marginal cost
rk = alpha * (ssidic['n_bar'] / ssidic['k_bar'])**(1 - alpha)
rn = (1 - alpha) * (ssidic['k_bar'] / ssidic['n_bar'])**alpha
mc = (1 / (1 - alpha))**(1 - alpha) * (1 / alpha)**alpha * (rk)**alpha * (rn)
ssidic['aa_bar'] = inf_bar * mc * ssidic['y_bar'] / (
    1 - (1 - gamma) * betta * ssidic['inf_bar']**((2 - theta) / (1 - theta)))
ssidic['bb_bar'] = ssidic['y_bar'] / (1 -
                                      (1 - gamma) * betta * ssidic['inf_bar']**
                                      (1 / (1 - theta)))
ssidic['infr_bar'] = (1 / theta) * (ssidic['aa_bar'] / ssidic['bb_bar'])
ssidic['i_bar'] = ssidic['r_bar'] * inf_bar
ssidic['lam_bar'] = 1.0 / ssidic['c_bar']
ssidic['w_bar'] = (1 / theta) * rn

# Instantiate a new DSGE model instance like so
foc_li = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
nkm1 = pm.newMOD(models.nkm, use_focs=foc_li, ssidic=ssidic, initlev=0)
示例#30
0
def test9():

    #####################################
    ## STANDARD COOLEY HANSE CIA MODEL ##
    #####################################
    # Load and solve the manually linearized model
    rbc1 = pm.newMOD(models.abcs_rbcs.cooley_hansen_cia_linear, mesg=True)
    rbc1.modsolvers.pyuhlig.solve()
    rbc1.modsolvers.forklein.solve()

    # Load and solve the automatically linearized model
    rbc2 = pm.newMOD(models.abcs_rbcs.cooley_hansen_cia_cf, mesg=True)
    rbc2.modsolvers.forkleind.solve()

    # Check equivalence of steady states
    for keyo in rbc1.sstate.keys():
        if keyo in rbc2.sstate.keys():
            assert round(rbc1.sstate[keyo], 5) == round(rbc2.sstate[keyo], 5)

    # Check equivalence of results
    nexo = len(rbc2.vardic['exo']['var'])
    nendo = len(rbc2.vardic['endo']['var'])

    modlin1 = MAT.hstack(
        (rbc1.modsolvers.pyuhlig.Q, rbc1.modsolvers.pyuhlig.P))
    modlin1 = [round(modlin1[0, i1], 5) for i1 in range(modlin1.shape[1])]
    modnlin1 = rbc2.modsolvers.forkleind.P[-nendo:, :]
    modnlin1 = [round(modnlin1[0, i1], 5) for i1 in range(modnlin1.shape[1])]
    print 'Comparison: Standard CIA model'
    print "Linear is: ", modlin1
    print '----------------------'
    print "Nonlinear is: ", modnlin1
    assert modlin1 == modnlin1

    modlin2 = MAT.hstack(
        (rbc1.modsolvers.pyuhlig.S, rbc1.modsolvers.pyuhlig.R))
    modlin2 = [[round(modlin2[i2, i1], 5) for i1 in range(modlin2.shape[1])]
               for i2 in range(modlin2.shape[0])]
    modnlin2 = rbc2.modsolvers.forkleind.F
    modnlin2 = [[
        round(modnlin2[i2, i1], 5) for i1 in range(modnlin2.shape[1])
    ] for i2 in range(modnlin2.shape[0])]
    print modlin2
    print '----------------------'
    print modnlin2
    assert modlin2 == modnlin2

    #####################################################
    ## STANDARD COOLEY HANSE CIA MODEL WITH SEIGNORAGE ##
    #####################################################
    # Load and solve the manually linearized model
    rbc1 = pm.newMOD(models.abcs_rbcs.cooley_hansen_cia_seignorage_linear,
                     mesg=True)
    rbc1.modsolvers.pyuhlig.solve()
    rbc1.modsolvers.forklein.solve()

    # Load and solve the automatically linearized model
    rbc2 = pm.newMOD(models.abcs_rbcs.cooley_hansen_cia_seignorage_cf,
                     mesg=True)
    rbc2.modsolvers.forkleind.solve()

    # Check equivalence of steady states
    for keyo in rbc1.sstate.keys():
        if keyo in rbc2.sstate.keys():
            assert round(rbc1.sstate[keyo], 5) == round(rbc2.sstate[keyo], 5)

    # Check equivalence of results
    nexo = len(rbc2.vardic['exo']['var'])
    nendo = len(rbc2.vardic['endo']['var'])

    modlin1 = MAT.hstack(
        (rbc1.modsolvers.pyuhlig.Q, rbc1.modsolvers.pyuhlig.P))
    modlin1 = [round(modlin1[0, i1], 5) for i1 in range(modlin1.shape[1])]
    modnlin1 = rbc2.modsolvers.forkleind.P[-nendo:, :]
    modnlin1 = [round(modnlin1[0, i1], 5) for i1 in range(modnlin1.shape[1])]
    print 'Comparison: Standard CIA model with seignorage'
    print "Linear is: ", modlin1
    print '----------------------'
    print "Nonlinear is: ", modnlin1
    assert modlin1 == modnlin1

    modlin2 = MAT.hstack(
        (rbc1.modsolvers.pyuhlig.S, rbc1.modsolvers.pyuhlig.R))
    modlin2 = [[round(modlin2[i2, i1], 5) for i1 in range(modlin2.shape[1])]
               for i2 in range(modlin2.shape[0])]
    modnlin2 = rbc2.modsolvers.forkleind.F
    modnlin2 = [[
        round(modnlin2[i2, i1], 5) for i1 in range(modnlin2.shape[1])
    ] for i2 in range(modnlin2.shape[0])]
    print modlin2
    print '----------------------'
    print modnlin2
    assert modlin2 == modnlin2

    #####################################################################
    ## STANDARD COOLEY HANSE CIA MODEL WITH SEIGNORAGE AND CES UTILITY ##
    #####################################################################
    # Be careful, here in the log-linearized version we have two endogenous states, k and mg
    # But in the automatically linearized version using the Jacobian we only have one, k

    # Load and solve the manually linearized model
    rbc1 = pm.newMOD(models.abcs_rbcs.cooley_hansen_cia_seignorage_ces_linear,
                     mesg=True)
    rbc1.modsolvers.pyuhlig.solve()
    rbc1.modsolvers.forklein.solve()

    # Load and solve the automatically linearized model
    rbc2 = pm.newMOD(models.abcs_rbcs.cooley_hansen_cia_seignorage_ces_cf,
                     mesg=True)
    rbc2.modsolvers.forkleind.solve()

    # Check equivalence of steady states
    for keyo in rbc1.sstate.keys():
        if keyo in rbc2.sstate.keys():
            assert round(rbc1.sstate[keyo], 5) == round(rbc2.sstate[keyo], 5)

    # Check equivalence of results
    nexo = len(rbc2.vardic['exo']['var'])
    nendo = len(rbc2.vardic['endo']['var'])

    modlin1 = MAT.hstack((rbc1.modsolvers.pyuhlig.Q[:nendo, :],
                          rbc1.modsolvers.pyuhlig.P[:nendo, :nendo]))
    modlin1 = [[round(modlin1[i2, i1], 5) for i1 in range(modlin1.shape[1])]
               for i2 in range(modlin1.shape[0])]
    modnlin1 = rbc2.modsolvers.forkleind.P[-nendo:, :]
    modnlin1 = [[
        round(modnlin1[i2, i1], 5) for i1 in range(modnlin1.shape[1])
    ] for i2 in range(modnlin1.shape[0])]
    print 'Comparison: Standard CIA model with seignorage and CES utility'
    print "Linear is: ", modlin1
    print '----------------------'
    print "Nonlinear is: ", modnlin1
    assert modlin1 == modnlin1

    modlin2 = MAT.hstack((rbc1.modsolvers.pyuhlig.S[:, :],
                          rbc1.modsolvers.pyuhlig.R[:, :nendo]))
    modlin2 = [[round(modlin2[i2, i1], 5) for i1 in range(modlin2.shape[1])]
               for i2 in range(modlin2.shape[0])]
    modnlin2 = rbc2.modsolvers.forkleind.F[:-1, :]
    modnlin2 = [[
        round(modnlin2[i2, i1], 5) for i1 in range(modnlin2.shape[1])
    ] for i2 in range(modnlin2.shape[0])]
    print modlin2
    print '----------------------'
    print modnlin2
    assert modlin2 == modnlin2
示例#31
0
# Define the ssidic of initial guesses or starting values
alpha = 0.36
chi = 0.819
xi = 1.0 / 4.3
g = 0.005
tau = 5.0
betta = 1.014
bettaa = betta * (1 + g)**(-tau)
delta = 0.025
rho = 0.95
a1 = (g + delta)**(1.0 / xi)
a2 = (g + delta) / (1.0 - xi)
z_bar = 1.0
sigma_eps = 0.01

ssidic = {}
ssidic['k_bar'] = 36.0  #1
ssidic['inv_bar'] = 1.0  #2
ssidic['c_bar'] = 2.5  #3
ssidic['mu_bar'] = 8.0  #4

# Pick first FOC equations to leave out exogenous law of motion
focs = range(4)

# Instantiate a new DSGE model instance like so
asset = pm.newMOD(models.stable.jermann98,
                  mesg=True,
                  use_focs=focs,
                  ssidic=ssidic)
示例#32
0
import pymaclab as pm
from pymaclab.modfiles import models

print "Now Loading and differentiating model rbc1..."
rbc1 = pm.newMOD(models.stable.rbc1_res,mesg=True,ncpus='auto')
print
print "Now loading and differentiating model rbc2..."
rbc2 = pm.newMOD(models.stable.rbc2,mesg=True,ncpus='auto')
print
print "Now loading and differentiating model merz..."
merz = pm.newMOD(models.stable.merz,mesg=True,ncpus='auto')
print
print "Now loading and differentiating model cee..."
cee  = pm.newMOD(models.testing.cee,mesg=True,ncpus='auto')