PyMacLab - API Documentation

Modules and Classes for DSGE modelling

The pymaclab module

pymaclab.db_graph(dbase, tseries)
pymaclab.explain(model)
pymaclab.ldbs()
pymaclab.lmods()
pymaclab.lvars()
pymaclab.modedit(model)
pymaclab.modinfo(model)
pymaclab.modsolve(model, stype)
pymaclab.newDB()
pymaclab.newFAVAR(dates=None, rdates=None, data=None, freq='M', vnames=None, pnames=None, svnames=None, irfs=True, rescale=False, boot=True, plot=True, sfacs='auto', init=None, conf=None, mesg=False)
pymaclab.newMOD(txtfile=None, dbase=None, initlev=3, mesg=False, ncpus=4, mk_hessian=True, use_focs=False, ssidic=None, sstate=None, vtiming={'exo': [-1, 0], 'endo': [-1, 0], 'con': [0, 1]})

Model’s second intialisation method called by newMOD() function call. The model’s __init__() method only creates the instance and adds information, but does no parsing and computations whatsoever. init2() parses and ready’s the model for calculating the steady state and the dynamic solution to the model.

init_lev = 0: only parsing, no calculations, but preparing for manual SS solution init_lev = 1: parsing and steady state calculations init_lev = 2: parsing, steady state calculations and dynamic solution computation

pymaclab.newVAR(data=None, vnames=None, pnames=None, svnames=None, irfs=True, boot=True, plot=True, conf=None, mesg=False)
pymaclab.pyed(pyfile=None)
pymaclab.test()
pymaclab.texedit(model)

The pymaclab.dsge module

The pymaclab.dsge.macrolab module

class pymaclab.dsge.macrolab.DSGEmodel(ffile=None, dbase=None, initlev=2, mesg=False, ncpus='auto', mk_hessian=True, use_focs=False, ssidic=None, sstate=None, vtiming={'exo': [-1, 0], 'endo': [-1, 0], 'con': [0, 1]})

This is the macrolab DSGEmodel class. It is the main class of the packages and instantiates DSGE model instances which possess many features such as model file parsers, solvers, etc. The __init__ function first called mostly attaches the passed arguments to the DSGE model instance in form of private _X data fields.

Note

Notice that the various init method, i.e. init1, init1a, etc. are not called here, but they are called externally in the pymaclab package when instantiating a new DSGE model using pymaclab.newMOD().

Parameters:
  • ffile (str) – The absolute path to the PyMacLab DSGE model file to be parsed on instantiation
  • dbase – A database with time series data for estimation purposes. Not implemented at the moment
  • initlev (int) – Takes values 0,1,2. Determines how deep the instantiation cascades through all methods. If 0 then the file is parsed and the instance is only prepared for steady state solving. If 1 then the file is parsed, the SS is automatically computed and instance is prepared for dynamic solving. If 2 then the instance is solved all the way.
  • mesg (bool) – If True then lots of diagnostics are printed to the screen during instantiation.
  • ncpus (int|str) – The number of CPU core to be employed, defaults to 1. But ‘auto’ can also be used for detection
  • mk_hessian (bool) – Whether the Hessian should be computed as this is expensive.
  • use_focs (tuple) – Should the FOCs be used directly to look for the steady state? Must use list|tuple to pick equations.
  • ssidic (dic) – A Python ssidic with the initial starting values for solving for the SS numerically
  • sstate (dic) – A Python ssidic with the externally computed steady state values of the model
Return self:

(dsge_inst) - A populated DSGE model instance with fields and methods

ccv(instring=None)

This is just a convenience method which allows the current preferred dynamic solution method to be stored (attached) here and made accessible without having to traverse too deeply into the model instance tree structure.

def_differ_periods()

A convenience method which uses the vtiming attribute in order to create a number of lists of variables with their time definitions (subscripts) for which derivates have to be taken using the non-linear system of FOCs.

Parameters:self (dsge_inst) – object instance
Return self.jAA:
 (arr2d) - attaches numerical AA matrix used in Forkleind solution method
Return self.jBB:
 (arr2d) - attaches numerical BB matrix used in Forkleind solution method
deltex()

A simple method which allows you to delete the current tex file associated with this model. Will ask for confirmation though, so cannot be used in a fire-and-forget batch file.

find_rss(mesg=False, rootm='hybr', scale=0.0)

The is a method which can be called to find the risky steady state

Parameters:self (dsge_inst) – The DSGE model instance itself.
info()

A convience method for collecting and displaying the model’s properties in a browser using html language.

mk_dynare(order=1, centralize=False, fpath=None, focli=None)
mkjahe()

An unparallelized method using native Python and Sympycore in oder to calculate the numerical and analytical Jacobian and Hessian of the model.

Parameters:self (dsge_inst) – object instance
Return self.numj:
 (arr2d) - attaches numerical model Jacobian to instance
Return self.jdic:
 (dic) - attaches the analytical model Jacobian to instance
Return self.numh:
 (arr2d) - attaches numerical model Hessian to instance
Return self.hdic:
 (dic) - attaches the analytical 3D Hessian to instance
Return self.jAA:
 (arr2d) - attaches numerical AA matrix used in Forkleind solution method
Return self.jBB:
 (arr2d) - attaches numerical BB matrix used in Forkleind solution method
mkjahen()

An unparallelized method using native Python and Sympycore in oder to calculate the numerical and analytical Jacobian and Hessian of the model. This is the corresponding method to “self.mkjahe()” but is only called when some parameters were changed and the model needs dynamic updating. In particular in this case the expensive computation of the analytical expressions can be avoided, as only parameters affecting the steady state may have been altered. Uses existing self.jdic for Jacobian and self.hdic for Hessian of model.

Parameters
self: object instance
Returns
self.numj: attaches numerical model Jacobian to instance self.numh: attaches numerical model Hessian to instance self.jAA: attaches numerical AA matrix used in Forkleind solution method self.jBB: attaches numerical BB matrix used in Forkleind solution method
mkjahenmat(msess=<PyCObject object at 0xa655b00>)

A method using mlabwrap to call an external Matlab code in oder to calculate the numerical Jacobian and Hessian of the model.

Parameters
self: object instance msess: active mlabwrap session to be used
Returns
self.numj: attaches numerical model Jacobian to instance self.numh: attaches numerical model Hessian to instance self.jAA: attaches numerical AA matrix used in Forkleind solution method self.jBB: attaches numerical BB matrix used in Forkleind solution method
mkjahepp()

A parallelized method using native Python and Sympy in oder to calculate the numerical and analytical Jacobian and Hessian of the model. This is the parallelized version of method self.mkjahe using the Python pp library.

Parameters
self: object instance
Returns
self.numj: attaches numerical model Jacobian to instance self.jdic: attaches the analytical model Jacobian to instance self.numh: attaches numerical model Hessian to instance self.hdic: attaches the analytical 3D Hessian to instance self.jAA: attaches numerical AA matrix used in Forkleind solution method self.jBB: attaches numerical BB matrix used in Forkleind solution method
mkjaheppn()

A parallelized method using native Python and Sympycore in oder to calculate the numerical and analytical Jacobian and Hessian of the model. This is the corresponding method to “self.mkjahe()” but is only called when some parameters were changed and the model needs dynamic updating. In particular in this case the expensive computation of the analytical expressions can be avoided, as only parameters affecting the steady state may have been altered. Uses existing self.jdic for Jacobian and self.hdic for Hessian of model.

Parameters
self: object instance
Returns
self.numj: attaches numerical model Jacobian to instance self.numh: attaches numerical model Hessian to instance self.jAA: attaches numerical AA matrix used in Forkleind solution method self.jBB: attaches numerical BB matrix used in Forkleind solution method
pdf()

This will pdflatex the model’s tex file and then view it in the system’s pdf viewer.

setauthor(author=None)

Convience method for setting the model’s author’s name.

texed()

A convenience method which allows users to launch the model’s tex file in an editor specified in the configuration settings of pymaclab.

txted()

A convience method for launching an editor editing the model’s associated model txt file. If the file gets saved (even if unaltered) the model is re-initialized.

updf()

Method useful for completely re-initializing model based on a different or else altered external modfile. It is also perceivable to load the existing modfile at runtime, change it in a program, save it as a temporary file and then re-load it using this method.

vreg(paratuple=(None, 'all', '0'), cinstring='', iter=False, info='min')

The regex function for variable detection out of strings

class pymaclab.dsge.macrolab.Derivatives
class pymaclab.dsge.macrolab.Inits(other=None)

This is the factored-out macrolab DSGEmodel Initialisation class. This used to be part of the DSGEmodel class itself in the form of methods, but has been factored out into a separate class which takes a newly instantiated DSGEmodel instance as and argument. It then possess several init methods which “work on the instance” and produce desired attributes

Note

Notice that the various init method, i.e. init1, init1a, etc. are not called here, but they are called externally in the pymaclab package when instantiating a new DSGE model using pymaclab.newMOD().

Parameters:other (DSGEmodel) – The DSGEmodel instance to be passed to the Inits instance
init1(no_wrap=False)

The init1 method. Model population proceeds from the __init__function here. In particular the data gets read in (not implemented at the moment) and the model parsing begins.

Note

The other.vardic gets created and the manual as well as the numerical steady state sections gets parsed and attached to the DSGE model instance. So the most import fields created here using function populate_model_stage_one() are:

  • other.vardic - variable names dictionary with transform and filtering info
  • other.mod_name - short name of the DSGE model from mod file
  • other.mod_desc - longer model description from mod file
  • other.paramdic - dic of defined parameters with their numerical values
  • other.manss_sys - list of equations from the closed form steady state section
  • other.ssys_list - list of equations from the numerical steady state section

Also the updaters and updaters_queued branches are opened here and the other.vardic gets wrapped for dynamic updating behaviour.

Parameters:other (dsge_inst) – The DSGE model instance itother.
init1a()

The init1a method. Model population proceeds from the init1 method here. The only field which get created here is the raw (i.e. unsubstituted) substitution dictionary.

Note

Field which are created here using the function populate_model_stage_one_a() which in turn calls mk_subs_dic():

  • other.nlsubs_raw1 - a list of the @items and their replacements
  • other.nlsubsdic - the above just expressed as a keyed list
Parameters:other (dsge_inst) – The DSGE model instance itother.
init1b(no_wrap=False)

The init1b method. Model population proceeds from the init1a method here.

Note

The only thing which gets done here purposefully after calling init1a() is to wrap these fields:

  • other.nlsubsdic - the above just expressed as a keyed list
  • other.paramdic - dic of defined parameters with their numerical values

in order to give them dynamic updating behaviour. No more is done in this init method call.

Parameters:other (dsge_inst) – The DSGE model instance itother.
init1c(no_wrap=False)

The init1c method. Model population proceeds from the init1b method here. We call populate_model_stage_one_bb() which does quite a bit of substitution/replacement of the @-prefixed variables. It does this in the numerical and closed form steady state calculation sections, as well as in the actual FOCs themselves.

Note

After that we can wrap various fields for updating which are:

  • other.foceqs - list of firs-order conditions with @ replacements done
  • other.manss_sys - the list of equations from closed form SS section
  • other.syss_list - the list of equations from numerical SS section

Notice also that here we replace or generate fields in case the FOCs are supposed to be used directly in SS calculation because the “use_focs” parameter was not passed empty.

Parameters:other (dsge_inst) – The DSGE model instance itother.
init2()

The init2 method. Model population proceeds from the init1c method here. In this initialisation method the only thing which is being done is to open up the sssolvers branch and pass down required objects to the manuals closed from solver and the numerical root-finding solver depending on whether information for this has been included in the DSGE model file. No attempt is made at solving for the steady state, the respective solvers are only being prepared.

Parameters:other (dsge_inst) – The DSGE model instance itother.
init3()

Initialisation method init3 is quite complex and goes through a number of logical tests in order to determine how to solve for the steady state of the model.

Note

There are 7 different ways a DSGE model can obtain its steady state solution depending on what information has been provided:

  1. The steady state values dictionary has been passed as argument, then init3() will NEVER be called
  2. Information has been provided using the “use_focs” parameter to use FOCs directly, externally passed using use_focs
  3. Information has been provided using the “use_focs” parameter to use FOCs directly, but inside model file
  4. Information has only been provided in the numerical SS section
  5. Information has only been provided in the closed form SS section
  6. Both CF-SS and NUM-SS info are present and NUM-SS is subset if CF-SS
  7. Both CF-SS and NUM-SS info are present and CF is residual

These options are better explained in the documentation to PyMacLab in the steady state solver section.

Parameters:other (dsge_inst) – The DSGE model instance itother.
init4(no_wrap=False)

This model instance sub-initializor only calls the section which use the computed steady state in order to compute derivatives and open dynamic solver branches on the instance. But Jacobian and Hessian are not computed here, this is postponed to the next init level.

Note

The following last field is wrapped for dynamic execution:

  • other.sigma - the variance-covariance matrix of the iid shocks

Notice that after wrapping this last field the process_queue class is instantiated at last, because it needs to have access to all of the wrapped fields. Also in this method, the function populate_model_stage_two() is called which prepares the nonlinear FOCs for derivative-taking.

Parameters:other (dsge_inst) – The DSGE model instance itother.
init5(update=False)

This model instance initialisation step is the last substantial one in which the dynamic solution of the DSGE model instance is finally computed using a choice of methods which can be called at runtime.

Parameters:other (dsge_inst) – The DSGE model instance itother.
init_out()

The final intializor section does some extra stuff after all has been done.

Parameters:self (dsge_inst) – The DSGE model instance itself.

The pymaclab.dsge.updaters.one_off module

class pymaclab.dsge.updaters.one_off.Updaters
class pymaclab.dsge.updaters.one_off.dicwrap(other, wrapobj_str, initlev)
update(dico)
class pymaclab.dsge.updaters.one_off.dicwrap_deep(other, wrapobj_str, initlev)
class pymaclab.dsge.updaters.one_off.dicwrapk(other, wrapobj, initlev)
update(dico)
class pymaclab.dsge.updaters.one_off.listwrap(other, wrapobj_str, initlev)
class pymaclab.dsge.updaters.one_off.listwrapk(other, wrapobj, wrapobj_str, wrapdic, initlev)
reinit(other)
class pymaclab.dsge.updaters.one_off.matwrap(other, wrapobj_str, initlev)

The pymaclab.dsge.updaters.queued module

class pymaclab.dsge.updaters.queued.Process_Queue(other=None)
reinit()
class pymaclab.dsge.updaters.queued.Updaters_Queued
class pymaclab.dsge.updaters.queued.dicwrap_deep_queued(other, wrapobj_str, initlev)
all_update()
class pymaclab.dsge.updaters.queued.dicwrap_queued(other, wrapobj_str, initlev)
update(dico)
class pymaclab.dsge.updaters.queued.dicwrapk(other, wrapobj, initlev)
update(dico)
class pymaclab.dsge.updaters.queued.listwrap_queued(other, wrapobj_str, initlev)
class pymaclab.dsge.updaters.queued.listwrapk(other, wrapobj, initlev)
class pymaclab.dsge.updaters.queued.matwrap_queued(other, wrapobj_str, initlev)

The pymaclab.dsge.helpers module

pymaclab.dsge.helpers.ctr(A)

returns conjugate transpose

pymaclab.dsge.helpers.eig(A, B)

To ensure matlab compatibility, we need to swap matrices A and B around !!

pymaclab.dsge.helpers.eig2(A, B, lapackname='', lapackpath='')

Calculates generalized eigenvalues of pair (A,B).

This should correspond to Matlab’s lambda = eig(A,B), and also to some (the same?) scipy function.

Eigenvalues will be of complex type, are unsorted, and are returned as 1d.

pymaclab.dsge.helpers.now_is()
pymaclab.dsge.helpers.null(A, eps=1e-15)

Function to get the nullspace of a matrix.

Usage: B = nullspace(A,eps)

where A is some matrix, B is the returned corresponding nullspace and eps is the optional machine precision (smallest number).

pymaclab.dsge.helpers.sortrows(A, integer)

Sort the rows of a matrix using the column given by the index number

The pymaclab.dsge.parsers module

The pymaclab.dsge.solvers module

The pymaclab.filters module

Modules and Classes for Macroeconometrics

The pymaclab.stats.var module

class pymaclab.stats.var.VAR(data=None, vnames=None, pnames=None, svnames=None, irfs=True, boot=True, plot=True, conf=None, mesg=False)
compbettax(matd=None, nmatd=None, smbetta=True, const='standard', func=False)
genFEVD(y, coefs, steps, func=False)
mkCompMat(betta=None, betta_one=None, func=False)
mkboot(bdraw=None, yfmat=None, resiarray=None)
mkboot_pp(bdraw=None, yfmat=None, resiarray=None)
mkfitres(matd=None, nmatd=None, betta=None, nlags=None, func=False)
mkphis(bettasm=None, betta=None, compmatr=None, maxphi=None, cholmat=None, cholimp=None, func=False)
mksigma(resmat=None, func=False)
plot_irfs()
plot_one_irf(sname=None, rname=None, save_hd=True)
plot_vals()
refac_killian_pp(intup)
refac_pp(intup)

The pymaclab.stats.favar module

class pymaclab.stats.favar.FAVAR(dates=None, rdates=None, data=None, freq='M', vnames=None, pnames=None, svnames=None, irfs=True, rescale=False, boot=True, plot=True, sfacs='auto', init=None, conf=None, mesg=False)
clean_dirs(modpath=None)
compBNcrit(resmat=None, nobs=None, tnobs=None, nfacs=None, func=False)
compFacErrors(resmat=None, func=False)
do_irfs(irfs=None)
estFBetta(vnames=None, data=None, func=False)
estFonly(data=None, vnames=None, sfacs=None, func=False)
estimate_bbe_fac_var()
estimate_fac_var(fdata=None, func=False)
findDynFacs(sfacs=None, crit=None, func=None)
findDynFacs_pp(sfacs=None, crit=None, func=None)
findStatFacs(crit=None, statlimit=None, data=None, vnames=None, func=False)
firstpass_estimation(stepo=None, sfacs=None)
genBBEH()
genBBEH2()
genCholH(facresmat=None, func=False)
genFEVD(y, coefs, steps, func=False)
genFiltData(data=None, bettax=None, nlags=None, func=False)
genLFF(fdata=None, flags=None, func=False)
genLFS(data=None, wcurr=True, larr=None, func=False)
genLXF(vnames=None, xdic=None, fdata=None, func=False)
identification()
mkCompAll(ff_betta_comp=None, deltaphi=None, dlmat=None, func=False)
mkCompAuto(bettaxm=None, func=False)
mkCompFF(bettafm=None, func=False)
mkCompFactor(deltam=None, func=False)
mkboot(bdraw=None, yfmat=None, resiarray=None, rescale=False)
mkboot_pp(bdraw=None, yfmat=None, resiarray=None, rescale=False)
mkcompmatrices()
mkfitres_auto(ymat=None, xdic=None, bettadic=None, mlags=None, func=False)
mkfitres_ffvar(ymat=None, xmat=None, betta=None, flags=None, func=False)
mkphis(betta=None, maxphi=None, cholmat=None, cholimp=None, compmats=None, rescale=False, func=False)
outmatrices()
plotFactorResids(fresids=None)
plotFactors(fdata=None)
plot_all(plot=None)
plot_facs_irfs()
plot_factors_all(plot=None)
plot_irfs()
plot_scree()
plot_vals()
plot_vals2()
plot_xdata_irfs()
populate(data=None, freq=None, vnames=None, pnames=None, svnames=None, conf=None)
populate_diagnostics()
prepSSF(tdata=None, func=False)
redefine_post_bbe()
refac_killian_pp(intup)
refac_pp(intup)
refac_pp2(stato, dyno, data, nlags, pcmat, statfacs)
scaleHmat(func=False)
transdata()
writeArray(inarray=None, fname=None)
writeFData()