update blog post

parent 1ee538de
Using JSON Output from the Dynare Preprocessor
##############################################
:date: 2020-01-14
:date: 2020-01-23
:tags: Dynare, Preprocessor, JSON, MATLAB
:category: Dynare
:slug: dynare-preprocessor-w-json
......@@ -49,7 +49,7 @@ 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 understand, in a general sense, what is done at each stage:
.. image:: {static}/images/preprocessor-4.6.png
.. image:: {static}/dynare-json/images/preprocessor-4.6.png
:width: 85%
:alt: Dynare Preprocessor Flow Chart
:align: center
......@@ -161,21 +161,24 @@ First Modification
+// --+ options: json=compute +--
The first line of the file tells the Dynare Preprocessor to produce JSON output
after the Computing Pass. This creates the files ``Smets_Wouters_2007.json``,
``Smets_Wouters_2007_original.json``, ``Smets_Wouters_2007_static.json``, and
``Smets_Wouters_2007_dynamic.json``.
The first file, ``Smets_Wouters_2007.json``, is the equivalent of the
standard ``.m`` file output by the Dynare Preprocessor only in JSON
format. It contains lists of model variables, the model block
(transformed into ``t-1``, ``t``, ``t+1`` format), a list of Dynare
statements, the list of equation cross references, and some general
information about the model.
The second file, ``Smets_Wouters_2007_original.json`` contains a slightly
modified version of the model as written in the model block. It contains no
auxiliary variables or auxiliary equations, but it does expand ``adl`` nodes,
if there are any. Here is what the Taylor rule looks like in JSON format:
after the Computing Pass. This creates the following files in the
``Smets_Wouters_2007/model/json`` directory:
- ``modfile.json``,
- ``modfile-original.json``,
- ``static.json``, and
- ``dynamic.json``.
The first file, ``modfile.json``, is the equivalent of the standard ``.m``
driver file output by the Dynare Preprocessor only in JSON format. It contains
lists of model variables, the model block (transformed into ``t-1``, ``t``,
``t+1`` format), a list of Dynare statements, the list of equation cross
references, and some general information about the model.
The second file, ``modfile-original.json`` contains a slightly modified
version of the model as written in the model block. It contains no auxiliary
variables or auxiliary equations, but it does expand ``adl`` nodes, if there
are any. Here is what the Taylor rule looks like in JSON format:
.. code-block:: json
......@@ -200,10 +203,9 @@ information contained in the model block. This file is written when either
``json=compute`` or ``json=transform`` is passed as an option to the ``dynare``
command.
The final two files, ``Smets_Wouters_2007_static.json`` and
``Smets_Wouters_2007_dynamic.json``, contain the derivatives of the dynamic and
static models. These files are a byproduct of using ``json=compute``. Our OLS
routine doesnt need them.
The final two files, ``static.json`` and ``dynamic.json``, contain the
derivatives of the dynamic and static models. These files are a byproduct of
using ``json=compute``. Our OLS routine doesnt need them.
Second Modification
^^^^^^^^^^^^^^^^^^^
......@@ -215,11 +217,11 @@ Second Modification
+path(['..' filesep 'ols'], path);
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,
this line.
The second change I made to the original ``.mod`` file was to add the relative
path to the `ols` folder containing the OLS routine and its helper
functions. Since this routine is not part of the official Dynare release,
Dynare will not actually take care of adding its folder to the MATLAB/Octave
path. Hence, this line.
Third Modification
^^^^^^^^^^^^^^^^^^
......@@ -294,11 +296,11 @@ makes more clear that ``ygap`` is calculated differently for the estimation run
and the OLS estimation.
I further create two new parameters, ``crpiMcrpiXcrr`` and ``cryMcryXcrr``
because the parsing algorithm that I implemented in the OLS routine only
accounts for the situation where the linear combination of a single parameter
multiplies one or more endogenous variables. This change implies the changes on
lines 76 and 77 defining their initial values, and on lines 222 and 223
defining them as parameters to be estimated.
because the parsing algorithm implemented in the OLS routine only accounts for
the situation where the linear combination of a single parameter multiplies one
or more endogenous variables. This change implies the changes on lines 76 and
77 defining their initial values, and on lines 222 and 223 defining them as
parameters to be estimated.
Finally, I redefine ``ms`` as an exogenous variable and remove the equation
that defined it as well as the definition of the variable ``em``.
......@@ -323,11 +325,11 @@ Last Modification
+
+estimation(optim=('MaxIter',200),datafile=usmodel_data,mode_compute=4,first_obs=1, presample=4,lik_init=2,prefilter=0,mh_replic=0,mh_nblocks=2,mh_jscale=0.20,mh_drop=0.2, nograph, nodiagnostic, tex);
I load the data into a ``dseries`` called ``ds`` on line 234. As mentioned
above, I need data for ``ygap`` for the OLS estimation. This is obtained by
detrending the output series, as shown on line 235. I then call the OLS routine
on line 236, telling it to run OLS using the dataset ``ds`` on the equation
specified by the equation tag ``taylor_rule``.
I first load the data into a ``dseries`` called ``ds`` on line 234. As
mentioned above, I need data for ``ygap`` for the OLS estimation. This is
obtained by detrending the output series. I then call the OLS routine, telling
it to run OLS using the dataset ``ds`` on the equation specified by the
equation tag ``taylor_rule``.
As the OLS routine sets the parameter values it estimates in ``M_.params``, I
reset their initial values after the call to the routine on lines 238-241, in
......@@ -337,8 +339,8 @@ preparation for the call to the estimation routine.
The OLS routine in MATLAB: ``dyn_ols.m``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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
The OLS routine outlined herein was written in MATLAB but could just as easily
have 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:
......@@ -351,12 +353,12 @@ 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
doesnt offer JSON support out of the box, theres a widely-used and
well-tested toolbox called JSONlab that provides JSON support and is available
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 as an AST in just two lines:
has only provided JSON support since R2016b (via the commands
`jsonencode`/`jsondecode`), theres a widely-used and well-tested toolbox
called JSONlab that allows one to use JSON with older versions of MATLAB and
with Octave. Downloading JSONlab `from their website
<https://github.com/fangq/jsonlab>`__ and adding it to our path allows us to
access the model block specified as an AST in just two lines:
.. code-block:: MATLAB
:linenos: inline
......@@ -366,7 +368,7 @@ block specified as an AST in just two lines:
ast = tmp.abstract_syntax_tree;
These lines reside in the function ``get_ast.m``, a utility we wrote that is
used by ``dyn_ols.m`` to do exactly what was described above.
used by ``dyn_ols.m``.
Line 45 reads in ``Smets_Wouters_2007/model/json/modfile-original.json``
(stored in the ``jsonfile`` variable) and loads it into a temporary MATLAB
......@@ -376,7 +378,7 @@ the only one we’re interested in and sets it to the output variable
equation. Entry 23 of this cell array corresponds to the monetary policy
equation and looks like:
.. code-block:: MATLAB
.. code-block:: text
>> ast{23}
......@@ -411,14 +413,13 @@ equation and looks like:
As you can see, the JSON output contains a lot of information. First, we know
the 0-indexed equation number (Matlab indexing starts at 1, which explains the
difference between the index in the structure and the index in the JSON), and
the line it begins on (line 141). Digging into the structure, we see that there
is one tag associated with the equation that has key ``name`` and value
``'taylor_rule'``. We also see the root node of the AST.
the line it was on in the ``.mod`` file (line 141). Digging into the structure,
we see that there is one tag associated with the equation that has key ``name``
and value ``'taylor_rule'``. We also see the root node of the AST.
As we only want to perform OLS on this equation, we don't need the AST
corresponding to the other equations in the ``.mod`` file. Hence, we overwrite
it with the aid of a helper function, selecting only the equation that we
need:
it with the aid of a helper function, selecting only the necessary equation:
.. code-block:: MATLAB
:linenos: inline
......@@ -455,12 +456,12 @@ that handles the parsing of one OLS-style equation at a time.
end
Together, these functions return matrices corresponding to the observed
variable, ``Y``, the regressors, ``X``, the constant term, ``lhssub``, and some
corollary information.
variable, ``Y``, the regressors, ``X``, the constant term, ``lhssub``, and the
first and last periods of the observed data (``fp`` and ``lp``).
Parsing proceeds as follows. After, the function arguments are verified in
lines 38-66, we know that the LHS contains either a ``VariableNode`` or a
``UnaryOpNode``.
Parsing proceeds as follows in ``parse_ols_style_equation.m``. After, the
function arguments are verified at the beginning of the function (lines 38-66),
we know that the LHS contains either a ``VariableNode`` or a ``UnaryOpNode``.
.. note:: Several different types of nodes that can appear in the AST,
corresponding to the types of operations that are available in a
......@@ -1007,17 +1008,17 @@ the filtered variable data and the original data used in the OLS above:
|fig1| |fig2|
|fig3|
.. |fig1| image:: {static}/images/ygap.png
.. |fig1| image:: {static}/dynare-json/images/ygap.png
:alt: Output Gap
:align: middle
:width: 49%
.. |fig2| image:: {static}/images/pinf.png
.. |fig2| image:: {static}/dynare-json/images/pinf.png
:alt: Inflation
:align: middle
:width: 49%
.. |fig3| image:: {static}/images/r.png
.. |fig3| image:: {static}/dynare-json/images/r.png
:alt: Interest Rate
:align: middle
:width: 49%
......
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