capitalize MATLAB and update first two sections

parent 6455e7a0
Using the Dynare Preprocessor’s JSON Output
###########################################
Using JSON Output from the Dynare Preprocessor
##############################################
:date: 2018-03-19
:tags: Dynare, Preprocessor, JSON, Matlab
:tags: Dynare, Preprocessor, JSON, MATLAB
:category: Dynare
:slug: dynare-preprocessor-w-json
:authors: Houtan Bastani
......@@ -13,11 +13,11 @@ Using the Dynare Preprocessor’s JSON Output
We have recently added an option to produce JSON output from the Dynare
Preprocessor. If you're new to Dynare you should know that the Preprocessor is
the part of Dynare that transforms your ``.mod`` file into a file usable by
Matlab, Octave, C, or Julia. Providing JSON output allows us to communicate the
information contained in the ``.mod`` file, as well as the static and dynamic
derivatives of the model, in a way that is easily parsed by many programming
languages. This makes it possible to use the power of the Dynare Modeling
Language in any programming environment you may so desire.
MATLAB, Octave, C, or Julia. Providing JSON output allows the preprocessor to
communicate everything it knows about the model (e.g. the model equations,
variables, static and dynamic derivatives, etc.) in a way that is easily
parsed by many programming languages. This makes it possible to use the Dynare
Modeling Language in any programming environment that can parse JSON.
In this post, I'd like to walk you through an example_ of putting the JSON
output of the Dynare Preprocessor to use. We will write a routine that parses
......@@ -43,16 +43,15 @@ The Dynare Preprocessor
At the basic level, the `Dynare Preprocessor
<https://github.com/DynareTeam/dynare-preprocessor>`__ takes as input a Dynare
``.mod`` file and outputs the derivatives of the static and dynamic versions of
the model in addition to a “driver” file that guides the back end actions to be
taken. These outputs are provided for use with Matlab, Octave, C, and, as of
the current unstable version of Dynare, Julia.
the model in addition to a *driver* file that guides the backend actions to be
taken. These outputs are provided for use with MATLAB, Octave, C, and, as of
Dynare 4.6, Julia.
In addition to the aforementioned outputs, the unstable version of
Dynare provides output in JSON format that represents the ``.mod`` file
at every major preprocessing stage: Parsing, Check Pass, Transform Pass,
and Computing Pass. To better understand the type of JSON output that
can be obtained, it is helpful to see the Dynare Preprocessor Flow Chart
and know in a general sense what is done at each stage:
In addition to the aforementioned outputs, Dynare 4.6 provides JSON output that
represents the ``.mod`` file at every major preprocessing stage: Parsing, Check
Pass, Transform Pass, and Computing Pass. To better understand the type of JSON
output that can be obtained, it is helpful to see the Dynare Preprocessor Flow
Chart and know in a general sense what is done at each stage:
.. image:: {static}/images/preprocessor-4.6.png
:width: 85%
......@@ -66,11 +65,11 @@ stages:
perform textual manipulations of the ``.mod`` file. The output from
this stage is a ``.mod`` file that is ready to be parsed. You can
read more about the Dynare Macroprocessing language
`here <http://www.dynare.org/summerschool/2017/sebastien/macroprocessor.pdf>`__.
`here <https://www.dynare.org/assets/team-presentations/macroprocessor.pdf>`__.
2. **Parsing**: takes a potentially macro-expanded ``.mod`` file and parses it,
creating an internal representation of the ``.mod`` file. In doing so, among
other cursory checks, it verifies that the ``.mod`` file has valid Dynare
commands and options, and that all variables have been declared.
syntax, commands, and options.
3. **Check Pass**: verifies the coherence of the ``.mod`` file. For example,
this is where we ensure that the number of declared endogenous
variables equals the number of equations in the model block.
......@@ -79,7 +78,10 @@ stages:
the model into ``t-1``, ``t``, ``t+1`` form.
5. **Computing Pass**: calculates the derivatives of the transformed static
and dynamic models using the symbolic derivative engine.
6. **Write Output**: writes Matlab, Octave, C, or Julia files
6. **Write Output**: writes MATLAB, Octave, C, Julia, or JSON files
To read more about the Dynare Preprocessor, see the presentation `here
<https://www.dynare.org/assets/team-presentations/preprocessor.pdf>`__.
.. _JSON:
......@@ -144,7 +146,7 @@ The original ``.mod`` file, ``Smets_Wouters_2007.mod``, and data file,
repository <https://github.com/JohannesPfeifer/DSGE_mod>`__.
Below, I show the ``.mod`` and describe the modifications I made to it. After
that, I describe the construction of the Matlab routine that makes use of the
that, I describe the construction of the MATLAB routine that makes use of the
JSON representation of any ``.mod`` file to run OLS. I then run OLS on the
monetary policy rule, and compare the parameters estimated via OLS to those
estimated via MLE
......@@ -460,7 +462,7 @@ Second Modification
The second change I made to the original ``.mod`` file (on line 2) was to add
the relative path to the `ols` folder containing the OLS routine that we will
write. Since this routine is not part of the official Dynare release, Dynare
will not actually take care of adding its folder to the Matlab path. Hence,
will not actually take care of adding its folder to the MATLAB path. Hence,
this line.
Third Modification
......@@ -509,10 +511,10 @@ reset their initial values after the call to the routine on lines 237-240, in
preparation for the call to the estimation routine.
The OLS routine in Matlab: ``dyn_ols.m``
The OLS routine in MATLAB: ``dyn_ols.m``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The OLS routine outlined herein was written in Matlab but could have just as
The OLS routine outlined herein was written in MATLAB but could have just as
easily been written in Julia, Python, C, or the language of your choice. There
are three main steps involved in writing a routine that makes use of the Dynare
JSON output:
......@@ -525,10 +527,10 @@ Step 1: Parsing the JSON file
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
As JSON is widely supported, the first step is often straightforward,
regardless of your choice of programming language. In our case, though Matlab
regardless of your choice of programming language. In our case, though MATLAB
doesn’t offer JSON support out of the box, there’s a widely-used and
well-tested toolbox called JSONlab that provides JSON support and is available
on the `Matlab File Exchange
on the `MATLAB File Exchange
<https://fr.mathworks.com/matlabcentral/fileexchange/33381-jsonlab--a-toolbox-to-encode-decode-json-files>`__.
Downloading JSONlab and adding it to our path allows us to access the model
block specified in just two lines:
......@@ -541,7 +543,7 @@ block specified in just two lines:
jsonmodel = jsonmodel.model;
Line 52 reads in ``Smets_Wouters_2007_original.json`` (stored in the
``jsonfile`` variable) and loads it into a Matlab structure we call
``jsonfile`` variable) and loads it into a MATLAB structure we call
``jsonmodel``. Line 53 then selects the ``model`` field as that is the only one
we’re interested in and overwrites ``jsonmodel`` with it. When finished,
``jsonmodel`` is a cell array with 40 entries, one for each equation. Entry 23
......@@ -575,7 +577,7 @@ Below I describe the parsing algorithm that I implemented in a draft version of
be made to be more general (we impose, for example, that a parameter can only
appear once per equation; a more general parsing algorithm would allow a
parameter to appear multiple times and simplify the equation). Though parsing
is done in Matlab, one could imagine writing a full parser using Bison and Yacc
is done in MATLAB, one could imagine writing a full parser using Bison and Yacc
to deal with parsing the equations. You could also imagine using an
out-of-the-box equation parser, modifying it to work with leads and lags. In
short, the correct solution depends on the problem that you are trying to
......@@ -1022,7 +1024,7 @@ deeper into the problem, I rerun the OLS estimation using the filtered variable
data provided by the estimation routine. Though I could have included this code
in the original ``.mod`` file, as this is just a one-off, I create a ``.m``
file to run after the estimation routine, which uses the variables in the
Matlab workspace, namely ``oo_`` and ``ds``:
MATLAB workspace, namely ``oo_`` and ``ds``:
.. code-block:: matlab
:linenos: inline
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment