A small code library for easily working with economic models in Python with three objectives:
- Provide standard functionality for copying, saving and loading.
- Provide an easy interface to call numba JIT compilled functions.
- Provide an easy interface to call C++ functions.
Examples are shown in EconModelNotebooks.
The package can be installed with
pip install EconModel
Basic usage starts with:
from EcnoModel import EconModelClass
class MyModelClass(EconModelClass):
def settings(self):
pass
def setup(self):
pass
def allocate(self):
pass
mymodel = MyModelClass(name='mymodel')
The model is required to have the following three methods:
.settings()
: Choose fundamental settings..setup()
: Set free parameters..allocate()
: Set compound parameters and allocate arrays.
When the model is initialized .settings
, .setup
and .allocate
are all called (in that order). Afterwards all namespace elements should not change type, and arrays should not change number of dimensions, though they can change shape.
In .settings()
the following internal attributes can be specified:
self.savefolder = str
: Filepath to save in and load from (default: saved).self.namespaces = [str]
: List of namespaces available in numba and C++ functions.self.other_attrs = [str]
: List of additional attributes to be copied and saved.self.cpp_filename = str
: Filepath of C++ file to link to.
The namespaces .par
, .sim
, and .sol
are always available.
The following standard functionality is provided:
.copy()
: Copy model..save()
: Saves model insavefolder/name
..as_dict()
: Returns model packaged in a dictionary..link_to_cpp()
: Compile and link to C++ file.
A saved model can be loaded as:
mymodel = MyModelClass(name='mymodel',load=True,skipattrs=None)
Where skipattrs [str]
is a list of attributes to not load.
A model can be created from a dictionary as:
mymodeldict = mymodel.as_dict()
mymodel_new = MyMOdelClass(name='mymodel',from_dict=mymodeldict)
A numba function is called as e.g.:
from EcnoModel import jit
with jit(mymodel) as mymodel_jit:
numba_function(mymodel_jit.par)
C++ functions are called as e.g.:
mymodel.cpp.cpp_funct(mymodel.par)
The libarary also contains interfaces to C++ packages such as:
To develop the package follow these steps:
- Clone this repository
- Locate the cloned repostiory in a terminal
- Run
pip install -e .
Changes you make to the package is now immediately effective on your own computer.
- ConSav for consumption-saving models.
- GEModelTools for general equilibrium models.
- BabyMAKRO for policy-like model for teaching.