Go to file
Willi Mutschler 5a8c206760 Added parameter derivatives of perturbation solution up to 3 order
# Preliminary comments
I finished the identification toolbox at orders two and three using the pruned state space system, but before I merge request this, I decided to first merge the new functionality to compute parameter derivatives of perturbation solution matrices at higher orders. So after this is approved, I merge the identification toolbox.
I guess @rattoma, @sebastien, and @michel are best choices to review this.
I outline the main idea first and then provide some more detailed changes I made to the functions.

***

# Main idea
This merge request is concerned with the *analytical*computation of the parameter derivatives of first, second and third order perturbation solution matrices, i.e. using _closed-form_ expressions to efficiently compute the derivative of  $g_x$ , $g_u$, $g_{xx}$, $g_{xu}$, $g_{uu}$, $g_{\sigma\sigma}$, $g_{xxx}$, $g_{xxu}$, $g_{xuu}$, $g_{uuu}$, $g_{x\sigma\sigma}$, $g_{u\sigma\sigma}$ *with respect to model parameters*  $\theta$.  Note that $\theta$ contains model parameters, stderr and corr parameters of shocks. stderr and corr parameters of measurement errors are not yet supported, (they can easily be included as exogenous shocks). The availability of such derivatives is beneficial in terms of more reliable analysis of model sensitivity and parameter identifiability as well as more efficient estimation methods, in particular for models solved up to third order, as it is well-known that numerical derivatives are a tricky business, especially for large models.

References for my approach are:
* Iskrev (2008, 2010) and Schmitt-Grohé and Uribe (2012, Appendix)  who were the first to compute the parameter derivatives analytically at first order, however, using inefficient (sparse) Kronecker products.
* Mutschler (2015) who provides the expressions for a second-order, but again using inefficient (sparse) Kronecker products.
* Ratto and Iskrev (2012) who show how the first-order system can be solved accurately, fast and efficiently using existing numerical algorithms for generalized Sylvester equations by taking the parameter derivative with respect to each parameter separately.
* Julliard and Kamenik (2004) who provide the perturbation solution equation system in tensor notation at any order k.
* Levintal (2017) who introduces permutation matrices to express the perturbation solution equation system in matrix notation up to fifth order.
Note that @rattoma already implemented the parameter derivatives of $g_x$ and $g_u$ analytically (and numerically), and I rely heavily on his work in `get_first_order_solution_params_derivs.m` (previously `getH.m`). My additions are mainly to this function and thus it is renamed to `get_perturbation_params_derivs.m`.

The basic idea of this merge request is to take the second- and third-order perturbation solution systems in Julliard and Kamenik (2004), unfold these into an equivalent matrix representation using permutation matrices as in Levintal (2017). Then extending Ratto and Iskrev (2012) one takes the derivative with respect to each parameter separately and gets a computational problem that is linear, albeit large, as it involves either solving generalized Sylvester equations or taking inverses of highly sparse matrices. I will now briefly summarize the perturbation solution system at third order and the system that results when taking the derivative with respect to parameters.

## Perturbation Solution
The following systems arise at first, second, and third order:
$(ghx): f_{x} z_{x} = f_{y_{-}^*} + f_{y_0} g_{x} + f_{y_{+}^{**}} g^{**}_{x} g^{*}_{x}= A g_{x} + f_{y_{-}^*}=0$

$(ghu): f_{z} z_{u} = f_{y_0} g_{u} + f_{y_{+}^{**}} g^{**}_{x} g^{*}_{u} + f_{u}= A g_u + f_u = 0$

$(ghxx) : A g_{xx} + B g_{xx} \left(g^{*}_{x} \otimes g^{*}_{x}\right) + f_{zz} \left( z_{x} \otimes z_{x} \right) = 0$

$(ghxu) : A g_{xu} + B g_{xx} \left(g^{*}_{x} \otimes g^{*}_{u}\right) + f_{zz} \left( z_{x} \otimes z_{u} \right) = 0$

$(ghuu) : A g_{uu} + B g_{xx} \left(g^{*}_{u} \otimes g^{*}_{u}\right) + f_{zz} \left( z_{u} \otimes z_{u} \right) = 0$

$(ghs2) : (A+B) g_{\sigma\sigma} +  \left( f_{y^{**}_{+}y^{**}_{+}} \left(g^{**}_{u} \otimes g^{**}_{u}\right) + f_{y^{**}_{+}} g^{**}_{uu}\right)vec(\Sigma) = 0$

$(ghxxx) : A g_{xxx} + B g_{xxx} \left(g^{*}_{x} \otimes g^{*}_{x} \otimes g^{*}_{x}\right) + f_{y_{+}}g^{**}_{xx} \left(g^{*}_x \otimes g^{*}_{xx}\right)P_{x\_xx} + f_{zz} \left( z_{x} \otimes z_{xx} \right)P_{x\_xx} + f_{zzz} \left( z_{x} \otimes z_{x} \otimes z_{x} \right) = 0$

$(ghxxu) : A g_{xxu} + B g_{xxx} \left(g^{*}_{x} \otimes g^{*}_{x} \otimes g^{*}_{u}\right) + f_{zzz} \left( z_{x} \otimes z_{x} \otimes z_{u} \right) + f_{zz} \left( \left( z_{x} \otimes z_{xu} \right)P_{x\_xu} + \left(z_{xx} \otimes z_{u}\right) \right) + f_{y_{+}}g^{**}_{xx} \left( \left(g^{*}_{x} \otimes g^{*}_{xu}\right)P_{x\_xu} + \left(g^{*}_{xx} \otimes g^{*}_{u}\right) \right) = 0$

$(ghxuu) : A g_{xuu} + B g_{xxx} \left(g^{*}_{x} \otimes g^{*}_{u} \otimes g^{*}_{u}\right) + f_{zzz} \left( z_{x} \otimes z_{u} \otimes z_{u} \right)+ f_{zz} \left( \left( z_{xu} \otimes z_{u} \right)P_{xu\_u} + \left(z_{x} \otimes z_{uu}\right) \right) + f_{y_{+}}g^{**}_{xx} \left( \left(g^{*}_{xu} \otimes g^{*}_{u}\right)P_{xu\_u} + \left(g^{*}_{x} \otimes g^{*}_{uu}\right) \right) = 0$

$(ghuuu) : A g_{uuu} + B g_{xxx} \left(g^{*}_{u} \otimes g^{*}_{u} \otimes g^{*}_{u}\right) + f_{zzz} \left( z_{u} \otimes z_{u} \otimes z_{u} \right)+ f_{zz} \left( z_{u} \otimes z_{uu} \right)P_{u\_uu} + f_{y_{+}}g^{**}_{xx} \left(g^{*}_{u} \otimes g^{*}_{uu}\right)P_{u\_uu}  = 0$

$(ghx\sigma\sigma) : A g_{x\sigma\sigma} + B g_{x\sigma\sigma} g^{*}_x + f_{y_{+}} g^{**}_{xx}\left(g^{*}_{x} \otimes g^{*}_{\sigma\sigma}\right) + f_{zz} \left(z_{x} \otimes z_{\sigma\sigma}\right) + F_{xu_{+}u_{+}}\left(I_{n_x} \otimes vec(\Sigma)\right) = 0$
$F_{xu_{+}u_{+}} = f_{y_{+}^{\ast\ast}} g_{xuu}^{\ast\ast} (g_x^{\ast} \otimes I_{n_u^2}) + f_{zz} \left( \left( z_{xu_{+}} \otimes z_{u_{+}} \right)P_{xu\_u} + \left(z_{x} \otimes z_{u_{+}u_{+}}\right) \right) + f_{zzz}\left(z_{x} \otimes z_{u_{+}} \otimes z_{u_{+}}\right)$

$(ghu\sigma\sigma) : A g_{u\sigma\sigma} + B g_{x\sigma\sigma} g^{*}_{u} + f_{y_{+}} g^{**}_{xx}\left(g^{*}_{u} \otimes g^{*}_{\sigma\sigma}\right) + f_{zz} \left(z_{u} \otimes z_{\sigma\sigma}\right) + F_{uu_{+}u_{+}}\left(I_{n_u} \otimes vec(\Sigma_u)\right) = 0$
$F_{uu_{+}u_{+}} = f_{y_{+}^{\ast\ast}} g_{xuu}^{\ast\ast} (g_u^{\ast} \otimes I_{n_u^2})  + f_{zz} \left( \left( z_{uu_{+}} \otimes z_{u_{+}} \right)P_{uu\_u} + \left(z_{u} \otimes z_{u_{+}u_{+}}\right) \right) + f_{zzz}\left(z_{u} \otimes z_{u_{+}} \otimes z_{u_{+}}\right)$

A and B are the common perturbation matrices:

$A = f_{y_0} + \begin{pmatrix} \underbrace{0}_{n\times n_{static}} &\vdots& \underbrace{f_{y^{**}_{+}} \cdot g^{**}_{x}}_{n \times n_{spred}} &\vdots& \underbrace{0}_{n\times n_{frwd}}  \end{pmatrix}$and $B = \begin{pmatrix} \underbrace{0}_{n \times n_{static}}&\vdots & \underbrace{0}_{n \times n_{pred}} & \vdots & \underbrace{f_{y^{**}_{+}}}_{n \times n_{sfwrd}} \end{pmatrix}$

and $z=(y_{-}^{\ast}; y; y_{+}^{\ast\ast}; u)$ denotes the dynamic model variables as in `M_.lead_lag_incidence`, $y^\ast$ denote state variables, $y^{\ast\ast}$ denote forward looking variables, $y_+$ denote the variables with a lead, $y_{-}$ denote variables with a lag, $y_0$ denote variables at period t, $f$ the model equations, and $f_z$ the first-order dynamic model derivatives, $f_{zz}$ the second-order dynamic derivatives, and $f_{zzz}$ the third-order dynamic model derivatives. Then:
$z_{x} = \begin{pmatrix}I\\g_{x}\\g^{**}_{x} g^{*}_{x}\\0\end{pmatrix}$, $z_{u} =\begin{pmatrix}0\\g_{u}\\g^{**}_{x} \cdot g^{*}_{u}\\I\end{pmatrix}$, $z_{u_{+}} =\begin{pmatrix}0\\0\\g^{**}_{u}\\0\end{pmatrix}$
$z_{xx} = \begin{pmatrix} 0\\g_{xx}\\g^{**}_{x} \left( g^{*}_x \otimes g^{*}_{x} \right) + g^{**}_{x} g^{*}_{x}\\0\end{pmatrix}$, $z_{xu} =\begin{pmatrix}0\\g_{xu}\\g^{**}_{xx} \left( g^{*}_x \otimes g^{*}_{u} \right) + g^{**}_{x} g^{*}_{xu}\\0\end{pmatrix}$, $z_{uu} =\begin{pmatrix}0\\g_{uu}\\g^{**}_{xx} \left( g^{*}_u \otimes g^{*}_{u} \right) + g^{**}_{x} g^{*}_{uu}\\0\end{pmatrix}$,
$z_{xu_{+}} =\begin{pmatrix}0\\0\\g^{**}_{xu} \left( g^{*}_x \otimes I \right)\\0\end{pmatrix}$, $z_{uu_{+}} =\begin{pmatrix}0\\0\\g^{**}_{xu} \left( g^{*}_{u} \otimes I \right)\\0\end{pmatrix}$, $z_{u_{+}u_{+}} =\begin{pmatrix}0\\0\\g^{\ast\ast}_{uu}\\0\end{pmatrix}$, $z_{\sigma\sigma} = \begin{pmatrix}0\\ g_{\sigma\sigma}\\ g^{\ast\ast}_{x}g^{\ast}_{\sigma\sigma} + g^{\ast\ast}_{\sigma\sigma}\\0 \end{pmatrix}$

$P$ are permutation matrices that can be computed using Matlab's `ipermute` function.

## Parameter derivatives of perturbation solutions
First, we need the parameter derivatives of first, second, third, and fourth derivatives of the dynamic model (i.e. g1,g2,g3,g4 in dynamic files), I make use of the implicit function theorem: Let $f_{z^k}$ denote the kth derivative (wrt all dynamic variables) of the dynamic model, then let $df_{z^k}$ denote the first-derivative (wrt all model parameters) of $f_{z^k}$ evaluated at the steady state. Note that $f_{z^k}$  is a function of both the model parameters $\theta$  and of the steady state of all dynamic variables $\bar{z}$, which also depend on the parameters. Hence, implicitly $f_{z^k}=f_{z^k}(\theta,\bar{z}(\theta))$  and $df_{z^k}$ consists of two parts:
1. direct derivative wrt to all model parameters given by the preprocessor in the `_params_derivs.m` files
2. contribution of derivative of steady state of dynamic variables (wrt all model parameters): $f_{z^{k+1}} \cdot d\bar{z}$
Note that we already have functionality to compute $d\bar{z}$ analytically.

Having this, the above perturbation systems are basically equations of the following types
$AX +BXC = RHS$ or $AX = RHS$
Now when taking the derivative (wrt to one single parameter $\theta_j$), we get
$A\mathrm{d}\{X\} + B\mathrm{d}\{X\}C = \mathrm{d}\{RHS\} - \mathrm{d}\{A\}X -  \mathrm{d}\{B\}XC - BX\mathrm{d}\{C\}$
or
$A\mathrm{d}\{X\}  = \mathrm{d}\{RHS\} - \mathrm{d}\{A\}X$
The first one is a Sylvester type equation, the second one can be solved by taking the inverse of $A$. The only diffculty and tedious work arrises in computing (the highly sparse) derivatives of $RHS$.

***

# New functions: `
## get_perturbation_params_derivs.m`and `get_perturbation_params_derivs_numerical_objective.m`
* The parameter derivatives up to third order are computed in the new function`get_perturbation_params_derivs.m` both analytically and numerically. For numerical derivatives `get_perturbation_params_derivs_numerical_objective.m` is the objective for `fjaco.m` or `hessian_sparse.m` or `hessian.m`.
* `get_perturbation_params_derivs.m` is basically an extended version of the previous `get_first_order_solution_params_derivs.m` function.
* * `get_perturbation_params_derivs_numerical_objective.m`builds upon `identification_numerical_objective.m`. It is used for numerical derivatives, whenever `analytic_derivation_mode=-1|-2`. It takes from `identification_numerical_objective.m` the parts that compute numerical parameter Jacobians of steady state, dynamic model equations, and perturbation solution matrices. Hence, these parts are removed in `identification_numerical_objective.m` and it only computes numerical parameter Jacobian of moments and spectrum which are needed for identification analysis in `get_identification_jacobians.m`, when `analytic_derivation_mode=-1` only.
* Detailed changes:
      * Most important: notation of this function is now in accordance to the k_order_solver, i.e. we do not compute derivatives of Kalman transition matrices A and B, but rather the solution matrices ghx,ghu,ghxx,ghxu,ghuu,ghs2,ghxxx,ghxxu,ghxuu,ghuuu,ghxss,ghuss in accordance with notation used in `oo_.dr`. As a byproduct at first-order, focusing on ghx and ghu instead of Kalman transition matrices A and B makes the computations slightly faster for large models (e.g. for Quest the computations were faster by a couple of seconds, not much, but okay).
      * Removed use of `kstate`, see also Dynare/dynare#1653 and Dynare/dynare!1656
      * Output arguments are stored in a structure `DERIVS`, there is also a flag `d2flag` that computes parameter hessians needed only in `dsge_likelihood.m`.
      * Removed `kronflag` as input. `options_.analytic_derivation_mode` is now used instead of `kronflag`.
      * Removed `indvar`, an index that was used to selected specific variables in the derivatives. This is not needed, as we always compute the parameter derivatives for all variables first and then select a subset of variables. The selection now takes place in other functions, like `dsge_likelihood.m`.
      * Introduced some checks: (i) deterministic exogenous variables are not supported, (ii) Kronecker method only compatible with first-order approximation so reset to sylvester method, (iii) for purely backward or forward models we need to be careful with the rows in `M_.lead_la	g_incidence`, (iv) if `_params_derivs.m` files are missing an error is thrown.
      * For numerical derivatives, if mod file does not contain an `estimated_params_block`, a temporary one with the most important parameter information is created.
## `unfold_g4.m`
* When evaluating g3 and g4 one needs to take into account that these do not contain symmetric elements, so one needs to use `unfold_g3.m` and the new function `unfold_g4.m`. This returns an unfolded version of the same matrix (i.e. with symmetric elements).

***

# New test models
`.gitignore` and `Makefile.am` are changed accordingly. Also now it is possible to run test suite on analytic_derivatives, i.e. run `make check m/analytic_derivatives`

## `analytic_derivatives/BrockMirman_PertParamsDerivs.mod`
* This is the Brock Mirman model, where we know the exact policy function $g$ for capital and consumption. As this does not imply a nonzero $g_{\sigma\sigma}$, $g_{x\sigma\sigma}$, $g_{u\sigma\sigma}$ I added some artificial equations to get nonzero solution matrices with respect to $\sigma$. The true perturbation solution matrices  $g_x$ , $g_u$, $g_{xx}$, $g_{xu}$, $g_{uu}$, $g_{\sigma\sigma}$, $g_{xxx}$, $g_{xxu}$, $g_{xuu}$, $g_{uuu}$, $g_{x\sigma\sigma}$, $g_{u\sigma\sigma}$ are then computed analytically with Matlab's symbolic toolbox and saved in `nBrockMirmanSYM.mat`. There is a preprocessor flag that recreates these analytical computations if changes are needed (and to check whether I made some errors here ;-) )
* Then solution matrices up to third order and their parameter Jacobians are then compared to the ones computed by Dynare's `k_order_solver` and by `get_perturbation_params_derivs` for all `analytic_derivation_mode`'s. There will be an error if the maximum absolute deviation is too large, i.e. for numerical derivatives (`analytic_derivation_mode=-1|-2`) the tolerance is choosen lower (around 1e-5); for analytical methods we are stricter: around 1e-13 for first-order,  1e-12 for second order, and 1e-11 for third-order.
* As a side note, this mod file also checks Dynare's `k_order_solver` algorithm and throws an error if something is wrong.
* This test model shows that the new functionality works well. And analytical derivatives perform way better and accurate than numerical ones, even for this small model.
## `analytic_derivatives/burnside_3_order_PertParamsDerivs.mod`
* This builds upon `tests/k_order_perturbation/burnside_k_order.mod` and computes the true parameter derivatives analytically by hand.
      * This test model also shows that the new functionality works well.

## `analytic_derivatives/LindeTrabandt2019.mod`
* Shows that the new functionality also works for medium-sized models, i.e. a SW type model solved at third order with 35 variables (11 states). 2 shocks and 20 parameters.
* This mod file can be used to tweak the speed of the computations in the future.
* Compares numerical versus analytical parameter derivatives (for first, second and third order). Note that this model clearly shows that numerical ones are quite different than analytical ones even at first order!
## `identification/LindeTrabandt2019_xfail.mod`
* This model is a check for issue Dynare/dynare#1595, see fjaco.m below, and will fail.
* Removed `analytic_derivatives/ls2003.mod` as this mod file is neither in the testsuite nor does it work.

***

# Detailed changes in other functions
## `get_first_order_solution_params_derivs.m`
* Deleted, or actually, renamed to `get_perturbation_params_derivs.m`, as this function now is able to compute the derivatives up to third order

## `identification_numerical_objective.m`
* `get_perturbation_params_derivs_numerical_objective.m`builds upon `identification_numerical_objective.m`. It takes from `identification_numerical_objective.m` the parts that compute numerical parameter Jacobians of steady state, dynamic model equations, and perturbation solution matrices. Hence, these parts are removed in `identification_numerical_objective.m` and it only computes numerical parameter Jacobian of moments and spectrum which are needed for identification analysis in `get_identification_jacobians.m`, when `analytic_derivation_mode=-1` only.

## `dsge_likelihood.m`
* As `get_first_order_solution_params_derivs.m`is renamed to `get_perturbation_params_derivs.m`, the call is adapted. That is,`get_perturbation_params_derivs` does not compute the derivatives of the Kalman transition `T`matrix anymore, but instead of the dynare solution matrix `ghx`. So we recreate `T` here as this amounts to adding some zeros and focusing on selected variables only.
* Added some checks to make sure the first-order approximation is selected.
* Removed `kron_flag` as input, as `get_perturbation_params_derivs` looks into `options_.analytic_derivation_mode` for `kron_flag`.

## `dynare_identification.m`
* make sure that setting `analytic_derivation_mode` is set both in `options_ident` and `options_`. Note that at the end of the function we restore the `options_` structure, so all changes are local. In a next merge request, I will remove the global variables to make all variables local.

## `get_identification_jacobians.m`
* As `get_first_order_solution_params_derivs.m`is renamed to `get_perturbation_params_derivs.m`, the call is adapted. That is,`get_perturbation_params_derivs` does not compute the derivatives of the Kalman transition `A` and `B` matrix anymore, but instead of the dynare solution matrix `ghx` and `ghu`. So we recreate these matrices here instead of in `get_perturbation_params_derivs.m`.
* Added `str2func` for better function handles in `fjaco.m`.

## `fjaco.m`
* make `tol`an option, which can be adjusted by changing `options_.dynatol.x`for identification and parameter derivatives purposes.
* include a check and an informative error message, if numerical derivatives (two-sided finite difference method) yield errors in `resol.m` for identification and parameter derivatives purposes. This closes issue  Dynare/dynare#1595.
* Changed year of copyright to 2010-2017,2019

***

# Further suggestions and questions
* Ones this is merged, I will merge request an improvement of the identification toolbox, which will work up to third order using the pruned state space. This will also remove some issues and bugs, and also I will remove global variables in this request.
* The third-order derivatives can be further improved by taking sparsity into account and use mex versions for kronecker products etc. I leave this for further testing (and if anybody actually uses this ;-) )
2019-12-17 18:17:09 +00:00
contrib Drop support for MatIO < 1.5 2019-02-12 15:58:29 +01:00
doc update doc to reflect changes in macOS installer 2019-12-10 16:53:12 +01:00
dynare++ Build system: by default, error out if some dependency is missing 2019-11-26 13:53:42 +01:00
examples Remove global variables from user-defined steady state files 2019-11-26 17:05:14 +01:00
julia More Unicode copyright symbol 2019-04-16 14:34:05 +02:00
m4 Use nodisplay option when performing tests (linux only). 2019-11-18 21:49:20 +01:00
macOS macOS packaging: compile against the version of Octave distributed via https://octave-app.org (currently 4.4.1) 2019-12-10 15:14:16 +01:00
matlab Added parameter derivatives of perturbation solution up to 3 order 2019-12-17 18:17:09 +00:00
mex macOS packaging: compile against the version of Octave distributed via https://octave-app.org (currently 4.4.1) 2019-12-10 15:14:16 +01:00
preprocessor@031c0cff5f Preprocessor update 2019-12-16 19:44:52 +01:00
scripts Emacs mode: font locking for now “evaluate_planner_objective” 2019-12-13 18:22:12 +01:00
tests Added parameter derivatives of perturbation solution up to 3 order 2019-12-17 18:17:09 +00:00
windows Windows package: ensure that Kalman steady state MEX is compiled under Octave 2019-11-21 16:32:01 +01:00
.dir-locals.el Fix indentation in Makefiles with Emacs 26 2019-04-05 15:10:21 +02:00
.gitignore build slicot, x13as when building macOS package and make some simplifications 2019-10-02 12:03:09 +02:00
.gitlab-ci.yml CI: fix pkg_source job following change in previous commit 2019-11-26 15:13:54 +01:00
.gitmodules Use relative paths in .gitmodules for projects hosted on git.dynare.org 2019-09-23 16:55:28 +02:00
CONTRIBUTING.md Update CONTRIBUTING.md for move to GitLab (#1114) 2019-11-26 16:55:27 +01:00
COPYING Add the GPL text 2014-02-21 14:16:18 +01:00
Makefile.am Build system: by default, error out if some dependency is missing 2019-11-26 13:53:42 +01:00
NEWS capitalize Cepremap and Matlab, uncapitalize DYNARE 2019-10-02 14:09:21 +02:00
README.md Under Debian, latexmk is needed to compile the manual 2019-10-01 13:40:50 +02:00
VERSION.in Added VERSION file. 2017-05-29 14:30:33 +02:00
configure.ac Build system: really error out if some dependency is missing 2019-11-28 14:43:44 +01:00
license.txt Add a list of former Dynare Team members 2019-11-29 17:53:06 +01:00

README.md

Dynare

Described on the homepage: https://www.dynare.org/

Most users should use the precompiled package available for their OS, also available via the Dynare homepage: https://www.dynare.org/download/.

Contributions

To contribute to Dynare and participate in the Dynare community, please see: CONTRIBUTING.md

License

Most of the source files are covered by the GNU General Public Licence version 3 or later (there are some exceptions to this, see license.txt in Dynare distribution for specifics).

Building Dynare From Source

Here, we explain how to build from source:

  • Dynare, including preprocessor and MEX files for MATLAB and Octave
  • Dynare++
  • all the associated documentation (PDF and HTML)

This source can be retrieved in three forms:

Note that if you obtain the source code via git, you will need to install more tools (see below).

The first section of this page gives general instructions, which apply to all platforms. Then some specific platforms are discussed.

Note: Here, when we refer to 32-bit or 64-bit, we refer to the type of MATLAB or Octave installation, not the type of operating system installation. For example, it is perfectly possible to run a 32-bit MATLAB on a 64-bit Windows: in that case, instructions for Windows 32-bit should be followed. To determine the type of your MATLAB/Octave installation, type:

>> computer

at the MATLAB/Octave prompt. Under MATLAB, if it returns PCWIN64, GLNX64 or MACI64, then it is a 64-bit MATLAB; if it returns PCWIN, MACI or GLNX, then it is a 32-bit MATLAB. Under Octave, if it returns a string that begins with x86_64, it is a 64-bit Octave; if the strings begins with i686, it is a 32-bit Octave.

Contents

  1. General Instructions
  2. Debian or Ubuntu
  3. Windows
  4. macOS

General Instructions

Prerequisites

A number of tools and libraries are needed in order to recompile everything. You don't necessarily need to install everything, depending on what you want to compile.

  • A POSIX compliant shell and an implementation of Make (mandatory)
  • The GNU Compiler Collection, version 8 or later, with gcc, g++ and gfortran (mandatory)
  • MATLAB (if you want to compile the MEX for MATLAB)
  • GNU Octave, with the development headers (if you want to compile the MEX for Octave)
  • Boost libraries, version 1.36 or later
  • Bison, version 3.2 or later (only if you get the source through Git)
  • Flex, version 2.5.4 or later (only if you get the source through Git)
  • Autoconf, version 2.62 or later (only if you get the source through Git)
  • Automake, version 1.11.2 or later (only if you get the source through Git)
  • An implementation of BLAS and LAPACK: either ATLAS, OpenBLAS, Netlib (BLAS, LAPACK) or MKL (only if you want to build Dynare++)
  • MAT File I/O library, version 1.5 or later (if you want to compile Markov-Switching code, the estimation DLL, k-order DLL and Dynare++)
  • SLICOT (if you want to compile the Kalman steady state DLL)
  • GSL library (if you want to compile Markov-Switching code)
  • A decent LaTeX distribution (if you want to compile PDF documentation), ideally with Beamer
  • For building the reference manual:
  • Doxygen (if you want to build Dynare preprocessor source documentation)
  • For Octave, the development libraries corresponding to the UMFPACK packaged with Octave

Preparing the sources

If you have downloaded the sources from an official source archive or the source snapshot, just unpack it.

If you want to use Git, do the following from a terminal:

git clone --recurse-submodules https://git.dynare.org/Dynare/dynare.git
cd dynare
autoreconf -si

The last line runs Autoconf and Automake in order to prepare the build environment (this is not necessary if you got the sources from an official source archive or the source snapshot).

Configuring the build tree

Simply launch the configure script from a terminal:

./configure

If you have MATLAB, you need to indicate both the MATLAB location and version. For example, on GNU/Linux:

./configure --with-matlab=/usr/local/MATLAB/R2019a MATLAB_VERSION=9.6

Note that the MATLAB version can also be specified via the MATLAB family product release (R2019a, R2018b, …).

Alternatively, you can disable the compilation of MEX files for MATLAB with the --disable-matlab flag, and MEX files for Octave with --disable-octave.

You may need to specify additional options to the configure script, see the output of the --help option, and also the platform specific instructions below.

Note that if you don't want to compile the C/C++ programs with debugging information, you can specify the CFLAGS and CXXFLAGS variables to the configure script, such as:

./configure CFLAGS="-O3" CXXFLAGS="-O3"

To remove debugging information for MATLAB MEX functions, the analogous call would be:

./configure MATLAB_MEX_CFLAGS="-O3" MATLAB_MEX_CXXFLAGS="-O3"

If the configuration goes well, the script will tell you which components are correctly configured and will be built.

Note that it is possible that some MEX files cannot be compiled, due to missing build dependencies. If you find no way of installing the missing dependencies, a workaround can be to give up on compiling these MEX files and rather use slower implementations (in the MATLAB/Octave language) that are available under the matlab/missing/mex/ subdirectories. For example, if you fail to compile the gensylv MEX, you can type the following at the MATLAB/Octave prompt before running Dynare:

addpath <DYNARE_ROOT>/matlab/missing/mex/gensylv

(where you need to replace <DYNARE_ROOT> with the full path to your Dynare copy).

Building

Binaries and Info documentation are built with:

make

PDF and HTML documentation are respectively built with:

make pdf
make html

The testsuites can be run with:

make check

Note that running the testsuite with Octave requires the additional packages pstoedit, epstool, xfig, and gnuplot.

Check

The Git source comes with unit tests (in the MATLAB functions) and integration tests (under the tests subfolder). All the tests can be run with:

make check

In the tests subfolder. If Dynare has been compiled against MATLAB and Octave, the tests will be run with MATLAB and Octave. Depending on the performance of your machine, this can take several hours. It is possible to run the tests only with MATLAB:

make check-matlab

or only with Octave:

make check-octave

A summary of the results is available in tests/run_test_matlab_output.txt or tests/run_test_octave_output.txt. Often, it does not make sense to run the complete testsuite. For instance, if you modify codes only related to the perfect foresight model solver, you can decide to run only a subset of the integration tests, with:

make deterministic_simulations

This will run all the integration tests in tests/deterministic_simulations with MATLAB and Octave. Again, it is possible to do this only with MATLAB:

make m/deterministic_simulations

or with Octave:

make o/deterministic_simulations

Finally if you want to run a single integration test, e.g. deterministic_simulations/lbj/rbc.mod with MATLAB:

make deterministic_simulations/lbj/rbc.m.trs

or with Octave:

make deterministic_simulations/lbj/rbc.o.trs

The result of the test (PASSED or FAILED) will be printed in the terminal, the produced log can be displayed with:

make deterministic_simulations/lbj/rbc.m.drs

or

make deterministic_simulations/lbj/rbc.o.drs

Note that only tests will be executed where the m.trs/o.trs does not yet exist. You can run

make clean

in the tests folder to delete files that were created by the run of the testsuite. You can also manually delete the desired m.trs/o.trs file(s).

Debian or Ubuntu

All the prerequisites are packaged:

  • build-essential (for gcc, g++ and make)
  • gfortran
  • liboctave-dev
  • libboost-graph-dev
  • libgsl-dev
  • libmatio-dev
  • libslicot-dev and libslicot-pic
  • libsuitesparse-dev
  • flex
  • bison
  • autoconf
  • automake
  • texlive
  • texlive-publishers (for Econometrica bibliographic style)
  • texlive-latex-extra (for fullpage.sty)
  • texlive-fonts-extra (for ccicons)
  • texlive-latex-recommended
  • texlive-science (for amstex)
  • texlive-generic-extra (for Sphinx)
  • lmodern (for macroprocessor PDF)
  • python3-sphinx
  • latexmk
  • libjs-mathjax
  • doxygen

You can install them all at once with:

apt install build-essential gfortran liboctave-dev libboost-graph-dev libgsl-dev libmatio-dev libslicot-dev libslicot-pic libsuitesparse-dev flex bison autoconf automake texlive texlive-publishers texlive-latex-extra texlive-fonts-extra texlive-latex-recommended texlive-science texlive-generic-extra lmodern python3-sphinx latexmk libjs-mathjax doxygen

Windows

  • Install MSYS2 (pick the 64-bit version, unless you have a 32-bit Windows, in which case see below)
  • Run a MSYS MinGW 64-bit shell
  • Update the system:
pacman -Syu

You may be asked to close the window at the end of the first upgrade batch, in which case you should rerun the upgrade in a new window to complete the upgrade.

  • Install all needed dependencies:
pacman -S git autoconf automake-wrapper bison flex make tar texinfo mingw-w64-x86_64-gcc mingw-w64-x86_64-gcc-fortran mingw-w64-x86_64-boost mingw-w64-x86_64-gsl mingw-w64-x86_64-matio mingw-w64-x86_64-openblas
  • (Optional) compile and install SLICOT, needed for the kalman_steady_state MEX file
wget https://deb.debian.org/debian/pool/main/s/slicot/slicot_5.0+20101122.orig.tar.gz
tar xf slicot_5.0+20101122.orig.tar.gz
cd slicot-5.0+20101122
make FORTRAN=gfortran OPTS="-O2 -fno-underscoring -fdefault-integer-8" LOADER=gfortran slicot.a
mkdir -p /usr/local/lib
cp slicot.a /usr/local/lib/libslicot64_pic.a
cd ..
  • Clone and prepare the Dynare sources:
git clone --recurse-submodules https://git.dynare.org/Dynare/dynare.git
cd dynare
autoreconf -si
  • Configure Dynare:
./configure --with-slicot=/usr/local --with-matlab=<…> MATLAB_VERSION=<…> --disable-octave

where the path and version of MATLAB are specified. Note that you should use the MSYS2 notation and not put spaces in the MATLAB path, so you probably want to use something like /c/Progra~1/MATLAB/….

  • Compile:
make
  • Run the testsuite:
make -C tests check-matlab

Note: The above assumes that you have a 64-bit version of MATLAB. It can be adapted to a 32-bit MATLAB with the following modifications:

  • run the MSYS MinGW 32-bit shell
  • replace x86_64 by i686 in packages names on the pacman command-line
  • for SLICOT, remove the -fdefault-integer-8 option, and instead copy the library into /usr/local/lib/libslicot_pic.a

Note: Compiling the MEX files for Octave and the documentation under MSYS2 is currently not supported.

macOS

To simply use a snapshot of Dynare, you have two choices. On MATLAB, you can use the snapshot build provided by Dynare. On Octave, you can simply install Homebrew and run brew install dynare --HEAD (See the Install Dynare (unstable) section of this webpage for more details).

If you do not wish to use the snapshots provided by Dynare or Homebrew, follow the directions below to build Dynare on your local machine.

Preparatory work:

  • Install the Xcode Command Line Tools:
    • Open Terminal.app and type xcode-select --install
  • Install Homebrew by following the instructions on their website

The following commands will install the programs that Dynare needs to compile. They should be entered at the command prompt in Terminal.app.

  • brew install automake bison flex boost fftw gcc gsl hdf5 libmatio metis veclibfort
  • (Optional) To compile Dynare mex files for use on Octave:
    • brew install octave
  • (Optional) To compile Dynare documentation
    • Install the latest version of MacTeX, deselecting the option to install Ghostscript
    • brew install doxygen latex2html

The following commands will download the Dynare source code and compile it. They should be entered at the command prompt in Terminal.app from the folder where you want Dynare installed.

  • git clone --recurse-submodules https://git.dynare.org/Dynare/dynare.git
  • cd dynare
  • PATH="/usr/local/opt/bison/bin:/usr/local/opt/flex/bin:$PATH"
  • autoreconf -si
  • CC=gcc-9 CXX=g++-9 ./configure --disable-octave --with-matlab=/Applications/MATLAB_R2019a.app MATLAB_VERSION=R2019a --with-matio=/usr/local --with-gsl=/usr/local --with-slicot=/usr/local, adjusting the MATLAB path and version to accord with your local installation. If you don't have MATLAB, simply remove --with-matlab=/Applications/MATLAB_R2019a.app MATLAB_VERSION=R2019a from the above command
  • make -j
  • (Optional) To then build mex files for Octave, run
    • cd mex/build/octave
    • CC=gcc-9 CXX=g++-9 ./configure --with-matio=/usr/local --with-gsl=/usr/local --with-slicot=/usr/local LDFLAGS=-L/usr/local/lib
    • make -j