This page describes how to use Python libraries for macroeconomic modelling, allowing to extend Dynare functionalities.

Python ressources

* Formal calculus

* Numerical computing

* Other

Writing/extending modfiles in Python

Here is the code of ramsey.py, which translates ramst.mod into python code. It is run by the standard Python interpreter.

   1 ###
   2 Model Definition
   3 ###
   4 
   5 from daredare import *
   6 
   7 set_variables("k c")
   8 set_exovariables("z")
   9 set_shocks("x")
  10 set_parameters("alpha beta gamma delta aa")
  11 
  12 equations = [
  13     Equation(c + k , (1 - delta)*k(-1) + k(-1)**alpha*(1 + z)*aa),
  14     Equation(0 , -1/(1 + beta)*c(1)**(-gamma)*(1 - delta + k**(-1 + alpha)*(1 + z(1))*aa*alpha) + c**(-gamma)),
  15     Equation(z(1),x)
  16 ]
  17 
  18 parameters_values = {
  19     alpha : 0.5,
  20     gamma : 1*alpha,
  21     delta : 0.02,
  22     beta : 0.05,
  23     aa : 0.5
  24 }
  25 
  26 covariances = matrix([[0.01]])
  27 
  28 init_values = {
  29     k:(1/aa/alpha*(beta + delta))**(1/(-1 + alpha)),
  30     c:k**alpha*aa - delta*k
  31 }
  32 
  33 ###
  34 Model processing
  35 ###

Language extension (for model definition)

On the very first line of this code from daredare import * imports the library. The second block

   1 set_variables("k c")
   2 set_exovariables("z")
   3 set_shocks("x")
   4 set_parameters("alpha beta gamma delta aa")

creates formal objects "k,c,z" which all derive of the same class Variable. This class is a subclass of the standard Sympy class Symbol, which only adds the possibility to take lags or leads with a function call : for instance k(1) returns another Variable whose formal name is k_{+1}. k.lag returns the lag so that k(-k.lag))}} or equivalently {{{k.p() will always return a variable with lag 0. Parameters are of the class Parameter which doesn't have the same facility. It should be noted that, a priori, the formal object, and the Python reference name which is used by the compiler are to different things. For instance,

   1 somestrangename = Variable('x')
   2 anotherstrangename = somestrangename
   3 y = Variable('y')
   4 eq = somestrangename * anotherstrangename / y
   5 print(eq)

will print : x**2/y. In this respect the functions like set_variables are notable because instead of returning the created objects, they also add references to the interpreter context, with the same names as the variables. They also add the python names variables,shocks,exovariables,parameters. The next block

   1 equations = [
   2     Equation(c + k , (1 - delta)*k(-1) + k(-1)**alpha*(1 + z)*aa),
   3     Equation(0 , -1/(1 + beta)*c(1)**(-gamma)*(1 - delta + k**(-1 + alpha)*(1 + z(1))*aa*alpha) + c**(-gamma)),
   4     Equation(z(1),x)
   5 ]

defines equations. The object Equation is a child of Sympy's Equality object. It contains the expression as well as some attributes as (optionally) equation name, expectational nature, and other information that could be computed later. Three remarks :

(for model definition)

Import and export