remove comparison to results from Dynare `estimation` command

other small fixes
parent 775c90df
Using JSON Output from the Dynare Preprocessor Using JSON Output from the Dynare Preprocessor
############################################## ##############################################
:date: 2020-01-24 :date: 2020-01-28
:tags: Dynare, Preprocessor, JSON, MATLAB :tags: Dynare, Preprocessor, JSON, MATLAB
:category: Dynare :category: Dynare
:slug: dynare-preprocessor-w-json :slug: dynare-preprocessor-w-json
...@@ -13,20 +13,19 @@ Using JSON Output from the Dynare Preprocessor ...@@ -13,20 +13,19 @@ Using JSON Output from the Dynare Preprocessor
We have recently added an option to produce JSON output from the Dynare 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 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 the part of Dynare that transforms your ``.mod`` file into a file usable by
MATLAB, Octave, C, or Julia. Providing JSON output allows the preprocessor to MATLAB, Octave, Julia, or the C compiler. Providing JSON output allows the
communicate everything it knows about the model (e.g. the model equations, preprocessor to communicate everything it knows about the model (e.g. the model
variables, static and dynamic derivatives, etc.) in a way that is easily parsed equations, variables, static and dynamic derivatives, etc.) in a way that is
by many programming languages. This makes it possible to use the Dynare easily parsed by many programming languages. This makes it possible to use the
Modeling Language in any programming environment that can parse JSON. Dynare Modeling Language in any programming environment that can parse JSON.
In this post, I'd like to walk you through an example_ of using the JSON output In this post, I'd like to walk you through an example_ of using the JSON output
of the Dynare Preprocessor. We will write a routine that parses the JSON output of the Dynare Preprocessor. We will write a routine that parses the JSON output
and estimates the parameters, equation by equation, via Ordinary Least and estimates the parameters, equation by equation, via Ordinary Least
Squares. We will then use this routine to estimate the Taylor rule parameters Squares. We will then use this routine to estimate the Taylor rule parameters
from Smets and Wouters (2007). These OLS-estimated parameters can in turn be from Smets and Wouters (2007). However, before getting to the example, I'd like
compared against the parameters estimated via a MLE estimation of the model as to briefly give you some background on `the Dynare Preprocessor`_ and the JSON_
a whole. However, before getting to the example, I'd like to briefly give you output it produces.
some background on `the Dynare Preprocessor`_ and the JSON_ output it produces.
On a final, practical, note, you should know that the OLS routine and the On a final, practical, note, you should know that the OLS routine and the
modified ``.mod`` file described herein work with `Dynare 4.6 and later modified ``.mod`` file described herein work with `Dynare 4.6 and later
...@@ -64,8 +63,8 @@ stages: ...@@ -64,8 +63,8 @@ stages:
`here <https://www.dynare.org/assets/team-presentations/macroprocessor.pdf>`__. `here <https://www.dynare.org/assets/team-presentations/macroprocessor.pdf>`__.
2. **Parsing**: takes a potentially macro-expanded ``.mod`` file and parses it 2. **Parsing**: takes a potentially macro-expanded ``.mod`` file and parses it
into an `Abstract Syntax Tree into an `Abstract Syntax Tree
<https://en.wikipedia.org/wiki/Abstract_syntax_tree>`__, comprising the <https://en.wikipedia.org/wiki/Abstract_syntax_tree>`__ (AST), comprising
internal representation of the ``.mod`` file. In doing so, among other the internal representation of the ``.mod`` file. In doing so, among other
cursory checks, it verifies that the ``.mod`` file has valid Dynare syntax, cursory checks, it verifies that the ``.mod`` file has valid Dynare syntax,
commands, and options. commands, and options.
3. **Check Pass**: verifies the coherence of the ``.mod`` file. For example, 3. **Check Pass**: verifies the coherence of the ``.mod`` file. For example,
...@@ -142,15 +141,15 @@ repository <https://github.com/JohannesPfeifer/DSGE_mod>`__. ...@@ -142,15 +141,15 @@ repository <https://github.com/JohannesPfeifer/DSGE_mod>`__.
Below, I show the ``.mod`` file and describe the necessary modifications to run Below, I show the ``.mod`` file and describe the necessary modifications to run
OLS. After that, I describe the construction of the MATLAB routine that uses OLS. After that, I describe the construction of the MATLAB routine that uses
the Dynare Preprocessor JSON output to run OLS (this routine is general and the Dynare Preprocessor JSON output to run OLS (this routine is general and
would work with the JSON output provided for any ``.mod`` file). I then run OLS would work with the JSON output provided for any ``.mod`` file). Finally, I run
on the monetary policy rule and compare the parameters estimated via OLS to OLS on the monetary policy rule.
those estimated via MLE.
The .mod file The .mod file
~~~~~~~~~~~~~ ~~~~~~~~~~~~~
The following are the parts of ``Smets_Wouters_2007.mod`` that I modified for The following are the parts of ``Smets_Wouters_2007.mod`` that I modified for
this post. The entire file used can be found `here <https://git.dynare.org/houtanb/obsmacro-dynare-json/blob/master/sw2007/Smets_Wouters_2007.mod>`__. this post. The entire file used can be found `here
<https://git.dynare.org/houtanb/obsmacro-dynare-json/blob/master/sw2007/Smets_Wouters_2007.mod>`__.
First Modification First Modification
^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^
...@@ -292,8 +291,8 @@ on line 143, ``ygap = y - yf``, replacing ``y-yf`` in the Taylor rule with ...@@ -292,8 +291,8 @@ on line 143, ``ygap = y - yf``, replacing ``y-yf`` in the Taylor rule with
``ygap``. This allows us to estimate the model as before while providing ``ygap``. This allows us to estimate the model as before while providing
observed data on ``ygap`` for OLS estimation (more on that later). NB: I could observed data on ``ygap`` for OLS estimation (more on that later). NB: I could
have left ``y-yf`` in the equation and provided data for ``yf`` but this change have left ``y-yf`` in the equation and provided data for ``yf`` but this change
makes more clear that ``ygap`` is calculated differently for the estimation run makes it more clear that ``ygap`` is calculated differently for the call to the
and the OLS estimation. Dynare ``estimation`` command than for the call to ``dyn_ols``.
I further create two new parameters, ``crpiMcrpiXcrr`` and ``cryMcryXcrr`` I further create two new parameters, ``crpiMcrpiXcrr`` and ``cryMcryXcrr``
because the parsing algorithm implemented in the OLS routine only accounts for because the parsing algorithm implemented in the OLS routine only accounts for
...@@ -333,7 +332,7 @@ equation tag ``taylor_rule``. ...@@ -333,7 +332,7 @@ equation tag ``taylor_rule``.
As the OLS routine sets the parameter values it estimates in ``M_.params``, I 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 reset their initial values after the call to the routine on lines 238-241, in
preparation for the call to the estimation routine. preparation for the call to the Dynare ``estimation`` routine.
The OLS routine in MATLAB: ``dyn_ols.m`` The OLS routine in MATLAB: ``dyn_ols.m``
...@@ -348,6 +347,9 @@ JSON output: ...@@ -348,6 +347,9 @@ JSON output:
#. Parse this structure for your purposes #. Parse this structure for your purposes
#. Run your computational task, in our case estimation via OLS #. Run your computational task, in our case estimation via OLS
The files described in this section can be found `here
<https://git.dynare.org/houtanb/obsmacro-dynare-json/tree/master/ols>`__.
Step 1: Parsing the JSON file Step 1: Parsing the JSON file
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...@@ -355,10 +357,10 @@ As JSON is widely supported, the first step is often straightforward, ...@@ -355,10 +357,10 @@ 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
has only provided JSON support since R2016b (via the commands has only provided JSON support since R2016b (via the commands
``jsonencode``/``jsondecode``), theres a widely-used and well-tested toolbox ``jsonencode``/``jsondecode``), theres a widely-used and well-tested toolbox
called JSONlab that allows one to use JSON with older versions of MATLAB and called `JSONLab <https://github.com/fangq/jsonlab>`__ that allows one to use
with Octave. Downloading JSONlab `from their website JSON with older versions of MATLAB and with Octave. Using the JSONLab
<https://github.com/fangq/jsonlab>`__ and adding it to our path allows us to distributed with Dynare allows us to access the model block specified as an AST
access the model block specified as an AST in just two lines: in just two lines:
.. code-block:: MATLAB .. code-block:: MATLAB
:linenos: inline :linenos: inline
...@@ -411,7 +413,7 @@ equation and looks like: ...@@ -411,7 +413,7 @@ equation and looks like:
arg2: [1x1 struct] arg2: [1x1 struct]
As you can see, the JSON output contains a lot of information. First, we know 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 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 difference between the index in the structure and the index in the JSON), and
the line it was on in the ``.mod`` file (line 141). Digging into the structure, 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`` we see that there is one tag associated with the equation that has key ``name``
...@@ -1012,119 +1014,6 @@ displaying the estimated parameters in a table: ...@@ -1012,119 +1014,6 @@ displaying the estimated parameters in a table:
Durbin-Watson: 1.703493 Durbin-Watson: 1.703493
_____________________________________________________________________________ _____________________________________________________________________________
We can now compare the parameters estimated via MLE to the parameters estimated
via OLS. The relevant lines from the estimation routine run by Dynare
(``mode_compute=4``) is:
.. code:: matlab
prior mean mode s.d. prior pstdev
...
crpiMcrpiXcrr 1.500 0.2311 0.0246 norm 0.2500
cryMcryXcrr 0.125 0.0199 0.0035 norm 0.0500
crdy 0.125 0.1455 0.0190 norm 0.0500
crr 0.750 0.9240 0.0199 beta 0.1000
We see that the ``crr`` estimates are quite close. However, we see that there
are noticeable differences among the other estimated parameters. To dig a bit
deeper into the problem, I rerun the OLS estimation using the filtered variable
data provided by the estimation routine, modifying ``Smets_Wouters_2007.mod``
as follows
.. code-block:: diff
@@ -245,35 +244,13 @@ shock_decomposition y;
+ds1 = dseries();
+ds1.r = dseries(oo_.FilteredVariables.r);
+ds1.pinf = dseries(oo_.FilteredVariables.pinf);
+ds1.ygap = dseries(oo_.FilteredVariables.ygap);
+dyn_ols(ds1, {}, {'taylor_rule'}, {'second'});
+figure
+plot(ds1.ygap.data)
+hold on
+plot(ds.ygap(2).data)
+title('ygap')
+legend('Filtered', 'Detrended')
+hold off
+saveas(gcf, 'ygap.png')
+figure
+plot(ds1.r.data)
+hold on
+plot(ds.r(2).data)
+title('r')
+legend('Filtered', 'Observed')
+hold off
+saveas(gcf, 'r.png')
+figure
+plot(ds1.pinf.data)
+hold on
+plot(ds.pinf(2).data)
+title('pinf')
+legend('Filtered', 'Observed')
+hold off
+saveas(gcf, 'pinf.png')
Before looking at the OLS output, it's nice to look at a few graphs plotting
the filtered variable data and the original data used in the OLS above:
|fig1| |fig2|
|fig3|
.. |fig1| image:: {static}/dynare-json/images/ygap.png
:alt: Output Gap
:align: middle
:width: 49%
.. |fig2| image:: {static}/dynare-json/images/pinf.png
:alt: Inflation
:align: middle
:width: 49%
.. |fig3| image:: {static}/dynare-json/images/r.png
:alt: Interest Rate
:align: middle
:width: 49%
Here we see that the Interest Rate and Inflation data track each other pretty
well (as they are observed variables) while the Output Gap is quite different,
as expected.
Rerunning the OLS Estimation, we have:
.. code::
OLS Estimation of equation 'second'
Dependent Variable: r
No. Independent Variables: 4
Observations: 229 from 2Y to 230Y
Estimates t-statistic Std. Error
________________ ________________ ________________
crpiMcrpiXcrr 0.082584 2.9779 0.027733
cryMcryXcrr 0.0072622 1.8895 0.0038435
crdy 0.055383 3.1112 0.017801
crr 0.96704 42.391 0.022813
R^2: 0.937412
R^2 Adjusted: 0.936578
s^2: 0.039161
Durbin-Watson: 1.494715
_____________________________________________________________________________
Here we see that the OLS-estimated parameters do not change much when using the
filtered variable data. Hence we conclude that the differences in the estimated
parameters are due to structural differences in the models.
Though in this case the comparison between the OLS-estimated parameters and
those estimated via the Dynare ``estimation`` routine is not very useful, we
can imagine cases where this sort of comparison could be informative.
Conclusion Conclusion
----------------------- -----------------------
......
...@@ -243,37 +243,3 @@ cryMcryXcrr = 0.0073; ...@@ -243,37 +243,3 @@ cryMcryXcrr = 0.0073;
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, filtered_vars); 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, filtered_vars);
shock_decomposition y; shock_decomposition y;
ds1 = dseries();
ds1.r = dseries(oo_.FilteredVariables.r);
ds1.pinf = dseries(oo_.FilteredVariables.pinf);
ds1.ygap = dseries(oo_.FilteredVariables.ygap);
dyn_ols(ds1, {}, {'taylor_rule'}, {'second'});
figure
plot(ds1.ygap.data)
hold on
plot(ds.ygap(2).data)
title('ygap')
legend('Filtered', 'Detrended')
hold off
saveas(gcf, 'ygap.png')
figure
plot(ds1.r.data)
hold on
plot(ds.r(2).data)
title('r')
legend('Filtered', 'Observed')
hold off
saveas(gcf, 'r.png')
figure
plot(ds1.pinf.data)
hold on
plot(ds.pinf(2).data)
title('pinf')
legend('Filtered', 'Observed')
hold off
saveas(gcf, 'pinf.png')
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