EconForge / dolo.py

Economic modelling in python
BSD 2-Clause "Simplified" License
98 stars 72 forks source link

How to get into the code : write developer's guide. #38

Open albop opened 11 years ago

albop commented 11 years ago

Roughly speaking the dolo library can be separated in two parts,

Since the second part is much less intricated than the former one (it actually consists in a lot of independent modules), it is suggested to start with it. Then a natural learning goal would be to implement a simple solution algorithm using a compiled model. (for instance a discrete choice value function optimization)

albop commented 11 years ago

The most important object is the GModel class in dolo.compiler.compiler_python. To get a sense of how it work type model = yaml_import('rbc.yaml'). You will get a compiled model with a couple with a couple of simple attributes. The two parts I mentioned are "how to construct that object" and "how to use it".

Warning, the GModel obect is here to stay, as I'm quite happy with the API. However, some solution routines, were developed for an older class, the CModel object. The global_solve function mentioned below uses the old API. A good step to understand how things work, would be to solve a GModel object without using global_solve.

Here are the functions implementing the time iteration algorithm:

The first one currently resides in numeric/global_solve.py and is merely a user-oriented wrapper. The two others are in numeric/global_solution.py.

step_residuals is very simple. Just ignore the computation of derivatives. It uses the convention that variables at date t are lower case, variables at date t+1 are upper case, and variables depending on a random shock are repeated. For instance, the state tomorrow, for all realizations of the shocks is written SS. Note that for each array in this function, the last dimension represents the index of the points. For instance x is a n_x \times N array, where each column, is a value for the controls on the grid (n_x is the number of controls). In other words, the function vectorized w.r.t. last dimension. Understanding the dimensions of the variables in this function is 60% of the job already done.

A lot of functions in dolo are affected by this convention. For instance, serial_multiplication in the numeric/serial_inversion.py file takes a p \times p \times N array representing N p \times pmatrices. The same is true for the newton_solver.py functions which are extremely simple: they take a function f that represented several functions stacked and solves all of them at once.

A big part of the code is also the interpolation routines in dolo.numeric.interpolation . They all have the same signature and are documented online.

albop commented 11 years ago

We need to explain how to explain how to define models with recipes in order to import a custom compiled model. It would actually be good to have a small tutorial for that.