From 85e946b0a8459edf08cfb89f45ddeb180a015082 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Adjemian=20=28Scylla=29?= Date: Fri, 28 Jul 2017 12:39:14 +0200 Subject: [PATCH] Rewrote examples for single equation contribution plots. The mod files are self documented, and serve as a user guide for the `plot_contributions` command. --- tests/ecb/contribution-plots/clean | 9 +++ tests/ecb/contribution-plots/contrib1.mod | 61 ++++++++++++++ tests/ecb/contribution-plots/contrib2.mod | 90 +++++++++++++++++++++ tests/ecb/contribution-plots/varexample.mod | 37 --------- 4 files changed, 160 insertions(+), 37 deletions(-) create mode 100755 tests/ecb/contribution-plots/clean create mode 100644 tests/ecb/contribution-plots/contrib1.mod create mode 100644 tests/ecb/contribution-plots/contrib2.mod delete mode 100644 tests/ecb/contribution-plots/varexample.mod diff --git a/tests/ecb/contribution-plots/clean b/tests/ecb/contribution-plots/clean new file mode 100755 index 000000000..5546dd8b9 --- /dev/null +++ b/tests/ecb/contribution-plots/clean @@ -0,0 +1,9 @@ +#!/bin/sh + +rm -rf contrib1 +rm -f contrib1.m contrib1_dynamic.m contrib1_set_auxiliary_variables.m contrib1.json contrib1_original.json +rm -f contrib1_static.json contrib1.log contrib1_dynamic.json contrib1_results.mat contrib1_static.m + +rm -rf contrib2 +rm -f contrib2.m contrib2_dynamic.m contrib2_set_auxiliary_variables.m contrib2.json contrib2_original.json +rm -f contrib2_static.json contrib2.log contrib2_dynamic.json contrib2_results.mat contrib2_static.m diff --git a/tests/ecb/contribution-plots/contrib1.mod b/tests/ecb/contribution-plots/contrib1.mod new file mode 100644 index 000000000..5b7a9e678 --- /dev/null +++ b/tests/ecb/contribution-plots/contrib1.mod @@ -0,0 +1,61 @@ +// --+ options: json=compute +-- + +/* REMARK +** ------ +** +** You need to have the first line on top of the mod file. The options defined on this line are passed +** to the dynare command (you can add other options, separated by spaces or commas). The option defined +** here is mandatory for the decomposition. It forces Dynare to output another representation of the +** model in a json file (additionaly to the matlab files) which is used here to manipulate the equation +** of interest. +*/ + +// Declaration of the endogenous variables +var ffr, unrate, cpi; + +// Declaration of the exogenous variables +varexo e_ffr, e_unrate, e_cpi; + + +// Declaration of the model. Note that you must associate a name to the equations. This is mandatory to +// select the equation for which you need to perform the decomposition. +model; + +[name='ffr'] + ffr = adl(ffr, 'p_ffr_ffr', [1:3]) + adl(unrate, 'p_ffr_unrate', 1) + adl(cpi, 'p_ffr_cpi', [4]); + +[name='unrate'] + unrate = adl(unrate, 'p_ffr_unrate', [4 2 5]) + adl(cpi, 'p_unrate_cpi', 6); + +[name='cpi'] + cpi = adl(ffr, 'p_cpi_ffr', 2) + adl(cpi, 'p_cpi_cpi', [2]); + +end; + +// Implicit parameters associated to the adl command must be calibrated after the model block. +p_ffr_ffr_lag_1 = 1; +p_ffr_ffr_lag_2 = p_ffr_ffr_lag_1*.5; +p_ffr_ffr_lag_3 = p_ffr_ffr_lag_2*.5; +p_ffr_unrate_lag_1 = 2; +p_ffr_cpi_lag_4 = 3; + +// Actual paths for the variables. You migth instead do a stochastic simulation of the model +// to define these paths. +ds1 = dseries(randn(30, 3), 1990Q1, {'ffr', 'unrate', 'cpi'}); + +// Baseline paths for the variables. +ds0 = dseries(zeros(30, 3), 1990Q1, {'ffr', 'unrate', 'cpi'}); + +/* Trigger the decomposition in levels of an equation +** +** - First argument (ffr) is the name of the equation to be decomposed. +** - Second argument (ds1) is a dseries object containing the actual paths of the endogenous and exogenous variables. +** - Third argument (ds0) is a dseries object containing the baseline paths of the endogenous and exogenous variables. +** +** If there is no error (missing variables, undefined equations, ...) a figure will pop up with displaying the +** contributions of the variables appearing on the right hand side of equation `ffr`. Note that the overall contribution +** of each variable (at all lags) is reported. If you want to decompose these aggregates, you need to rewrite the +** model as shown in the other mod file in the same folder (contrib2.mod). +*/ + +plot_contributions ffr ds1 ds0 ; diff --git a/tests/ecb/contribution-plots/contrib2.mod b/tests/ecb/contribution-plots/contrib2.mod new file mode 100644 index 000000000..c3030c2ba --- /dev/null +++ b/tests/ecb/contribution-plots/contrib2.mod @@ -0,0 +1,90 @@ +// --+ options: json=compute +-- + +/* REMARKS +** ------- +** +** Please, firt read the content of contrib1.mod. +** +** I consider the same model as in contrib1.mod, but here we want to obtain the contributions of +** the variables to `ffr` at all lags (individually). To do this we need to add variables for all +** the lags, add corresponding equations (defining the new variables) and add the new variables +** in the dseries objects. +*/ + +// Declaration of the endogenous variables +var ffr, unrate, cpi; + +// Define additional variables +var ffr_lag_1, ffr_lag_2, ffr_lag_3, unrate_lag_1, cpi_lag_4; + +// Declaration of the exogenous variables +varexo e_ffr, e_unrate, e_cpi; + + +// Declare the parameters appearing in the equation to be decomposed. +parameters p_ffr_ffr_lag_1 p_ffr_ffr_lag_2 p_ffr_ffr_lag_3 p_ffr_unrate_lag_1 p_ffr_cpi_lag_4; + +// Declaration of the model. Note that you must associate a name to the equations. This is mandatory to +// select the equation for which you need to perform the decomposition. +model; + +[name='ffr'] + //ffr = adl(ffr, 'p_ffr_ffr', [1:3]) + adl(unrate, 'p_ffr_unrate', 1) + adl(cpi, 'p_ffr_cpi', [4]); + ffr = p_ffr_ffr_lag_1*ffr_lag_1 + p_ffr_ffr_lag_2*ffr_lag_2 + p_ffr_ffr_lag_3*ffr_lag_3 + + p_ffr_unrate_lag_1*unrate_lag_1 + + p_ffr_cpi_lag_4*cpi_lag_4; + +[name='unrate'] + unrate = adl(unrate, 'p_ffr_unrate', [4 2 5]) + adl(cpi, 'p_unrate_cpi', 6); + +[name='cpi'] + cpi = adl(ffr, 'p_cpi_ffr', 2) + adl(cpi, 'p_cpi_cpi', [2]); + +// Definitions of the auxiliary variables (we don't need names for these equations). +ffr_lag_1 = ffr(-1); +ffr_lag_2 = ffr(-2); +ffr_lag_3 = ffr(-3); +unrate_lag_1 = unrate(-1); +cpi_lag_4 = cpi(-4); + +end; + +// Implicit parameters associated to the adl command must be calibrated after the model block. +p_ffr_ffr_lag_1 = 1; +p_ffr_ffr_lag_2 = p_ffr_ffr_lag_1*.5; +p_ffr_ffr_lag_3 = p_ffr_ffr_lag_2*.5; +p_ffr_unrate_lag_1 = 2; +p_ffr_cpi_lag_4 = 3; + +// Actual paths for the variables. You migth instead do a stochastic simulation of the model +// to define these paths. +ds1 = dseries(randn(30, 3), 1990Q1, {'ffr', 'unrate', 'cpi'}); + +// Create auxiliary variables as dseries objects +verbatim; + + ffr_lag_1 = dseries(ds1.ffr(-1).data, ds1.firstdate, 'ffr_lag_1'); + ffr_lag_2 = dseries(ds1.ffr(-2).data, ds1.firstdate, 'ffr_lag_2'); + ffr_lag_3 = dseries(ds1.ffr(-3).data, ds1.firstdate, 'ffr_lag_3'); + unrate_lag_1 = dseries(ds1.unrate(-1).data, ds1.firstdate, 'unrate_lag_1'); + cpi_lag_4 = dseries(ds1.cpi(-4).data, ds1.firstdate, 'cpi_lag_4'); + +end; + +// Put them in ds1 +ds1 = [ds1, ffr_lag_1, ffr_lag_2, ffr_lag_3, unrate_lag_1, cpi_lag_4]; + +// Baseline paths for the variables. +ds0 = dseries(zeros(30, 8), 1990Q1, {'ffr', 'unrate', 'cpi', 'ffr_lag_1', 'ffr_lag_2', 'ffr_lag_3', 'unrate_lag_1', 'cpi_lag_4'}); + +/* Trigger the decomposition in levels of an equation +** +** - First argument (ffr) is the name of the equation to be decomposed. +** - Second argument (ds1) is a dseries object containing the actual paths of the endogenous and exogenous variables. +** - Third argument (ds0) is a dseries object containing the baseline paths of the endogenous and exogenous variables. +** +** If there is no error (missing variables, undefined equations, ...) a figure will pop up with displaying the +** contributions of the variables appearing on the right hand side of equation `ffr`. +*/ + +plot_contributions ffr ds1 ds0 ; diff --git a/tests/ecb/contribution-plots/varexample.mod b/tests/ecb/contribution-plots/varexample.mod deleted file mode 100644 index cbe8626cb..000000000 --- a/tests/ecb/contribution-plots/varexample.mod +++ /dev/null @@ -1,37 +0,0 @@ -// --+ options: json=compute +-- -var ffr, unrate, cpi; -varexo e_ffr, e_unrate, e_cpi; - -model; - -[eqnum='ffr'] - ffr = adl(ffr, 'p_ffr_ffr', [1:3]) + adl(unrate, 'p_ffr_unrate', 1) + adl(cpi, 'p_ffr_cpi', [4]); - -[eqnum='unrate'] - unrate = adl(unrate, 'p_ffr_unrate', [4 2 5]) + adl(cpi, 'p_unrate_cpi', 6); - -[eqnum='cpi'] - cpi = adl(ffr, 'p_cpi_ffr', 2) + adl(cpi, 'p_cpi_cpi', [2]); - -end; - -// Must be calibrated after the model block -p_ffr_ffr_lag_1 = 1; -p_ffr_ffr_lag_2 = p_ffr_ffr_lag_1*.5; -p_ffr_ffr_lag_3 = p_ffr_ffr_lag_2*.5; -p_ffr_unrate_lag_1 = 2; -p_ffr_cpi_lag_4 = 3; - -// Actual paths for the variables. -ds1 = dseries(randn(30, 3), 1, {'ffr', 'unrate', 'cpi'}); - -// Baseline paths for the variables. -ds0 = dseries(zeros(30, 3), 1, {'ffr', 'unrate', 'cpi'}); - -olseqs(ds1, 'eqnum', {'ffr', 'cpi'}); - -sur(ds1); - -surgibbs(ds1, randn(17,17), 1000); - -plot_contributions('eqnum', 'ffr', ds1, ds0); \ No newline at end of file