/EconModel

Library for easily working with economic models in Python

Primary LanguagePythonMIT LicenseMIT

EconModel

A small code library for easily working with economic models in Python with three objectives:

  1. Provide standard functionality for copying, saving and loading.
  2. Provide an easy interface to call numba JIT compilled functions.
  3. Provide an easy interface to call C++ functions.

Examples are shown in EconModelNotebooks.

Installation

The package can be installed with

pip install EconModel

Usage

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:

  1. .settings(): Choose fundamental settings.
  2. .setup(): Set free parameters.
  3. .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:

  1. self.savefolder = str: Filepath to save in and load from (default: saved).
  2. self.namespaces = [str]: List of namespaces available in numba and C++ functions.
  3. self.other_attrs = [str]: List of additional attributes to be copied and saved.
  4. 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:

  1. .copy(): Copy model.
  2. .save(): Saves model in savefolder/name.
  3. .as_dict(): Returns model packaged in a dictionary.
  4. .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:

  1. NLopt 2.4.2
  2. TASMANIAN 7.0
  3. ALGLIB 3.17
  4. Eigen
  5. autodiff

Development

To develop the package follow these steps:

  1. Clone this repository
  2. Locate the cloned repostiory in a terminal
  3. Run pip install -e .

Changes you make to the package is now immediately effective on your own computer.

Used by

  1. ConSav for consumption-saving models.
  2. GEModelTools for general equilibrium models.
  3. BabyMAKRO for policy-like model for teaching.