From d840695ae039a2d392ce60b7b01bfacb51d59e3f Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Mon, 26 Jun 2017 22:27:55 +0200 Subject: [PATCH] plot contributions --- matlab/plot_contributions.m | 84 +++++++++++++++++++++++++++++++++++++ tests/ECB/SUR/var.mod | 27 ++++++++++++ 2 files changed, 111 insertions(+) create mode 100644 matlab/plot_contributions.m create mode 100644 tests/ECB/SUR/var.mod diff --git a/matlab/plot_contributions.m b/matlab/plot_contributions.m new file mode 100644 index 000000000..f03761834 --- /dev/null +++ b/matlab/plot_contributions.m @@ -0,0 +1,84 @@ +function plot_contributions(tagn, tagv, dseriesdata, params) +% function plot_contributions(tagn, tagv, dseriesdata, params) +% Return the lhs, rhs of an equation and the line it was defined +% on given its tag +% +% INPUTS +% tagn string tag name +% tagv string tag value +% dseriesdata matrix nobs x n endogenous in equation +% params struct params.param_name = param_value +% +% OUTPUTS +% None +% +% SPECIAL REQUIREMENTS +% none + +% Copyright (C) 2017 Dynare Team +% +% This file is part of Dynare. +% +% Dynare is free software: you can redistribute it and/or modify +% it under the terms of the GNU General Public License as published by +% the Free Software Foundation, either version 3 of the License, or +% (at your option) any later version. +% +% Dynare is distributed in the hope that it will be useful, +% but WITHOUT ANY WARRANTY; without even the implied warranty of +% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +% GNU General Public License for more details. +% +% You should have received a copy of the GNU General Public License +% along with Dynare. If not, see . + +global M_; + +jsonfile = [M_.fname '_original.json']; +if exist(jsonfile, 'file') ~= 2 + error(['Could not find ' jsonfile]); +end + +% Get equation +jsonmodel = loadjson(jsonfile); +jsonmodel = jsonmodel.model; +[lhs, rhs, ~] = getEquationByTag(jsonmodel, tagn, tagv); + +w% replace variables with dseriesdata.variablename +for i = 1:length(dseriesdata.name) + rhs = strrep(rhs, [dseriesdata{i}.name{:} '('], ['dseriesdata.' dseriesdata{i}.name{:} '(']); +end + +% create inline function for rhs +rhsfunc = inline(rhs); + +% construct call +rhsfuncargs = argnames(rhsfunc); +nargs = length(rhsfuncargs); +parameters = cell(nargs-1, 1); +for i = 2:nargs + parameters{i-1} = params.(rhsfuncargs{i}); +end + +contribution = zeros(dseriesdata.nobs, nargs+1); +funceval = rhsfunc(dseriesdata, parameters{:}); +contribution(:, 1) = funceval.data; + +nvars = length(dseriesdata.name); +dseriesdatabak = dseriesdata; +dseriesdatazero = dseries(zeros(dseriesdata.nobs, nvars), ... + dseriesdata.firstdate, ... + dseriesdata.name); +for i = 1:nvars + dseriesdata = dseriesdatazero; + dseriesdata{dseriesdatabak.name{i}} = dseriesdatabak{i}; + funceval = rhsfunc(dseriesdata, parameters{:}); + contribution(:, i+1) = funceval.data; +end + +figure('Name', lhs); +plot(1:dseriesdata.nobs, contribution) +seriesnames = dseriesdata.name; +legend('All Endogenous',seriesnames{:}) + +end \ No newline at end of file diff --git a/tests/ECB/SUR/var.mod b/tests/ECB/SUR/var.mod new file mode 100644 index 000000000..8351bb95e --- /dev/null +++ b/tests/ECB/SUR/var.mod @@ -0,0 +1,27 @@ +% +% Run using command: dynare var json=compute +% +var ffr, unrate, cpi; +varexo e_ffr, e_unrate, e_cpi; + +model; + +[eqnum='ffr'] + ffr = adl(ffr, 'p_ffr_ffr', 2) + adl(unrate, 'p_ffr_unrate', 1) + adl(cpi, 'p_ffr_cpi', 1); + +[eqnum='unrate'] + unrate = adl(unrate, 'p_ffr_unrate', 6) + adl(cpi, 'p_unrate_cpi', 6); + +[eqnum='cpi'] + cpi = adl(ffr, 'p_cpi_ffr', 6) + adl(cpi, 'p_cpi_cpi', 6); + +end; + +mydata = dseries(rand(30, 3), 1, {'ffr', 'unrate', 'cpi'}); + +params.p_ffr_ffr_lag_1 = 1; +params.p_ffr_ffr_lag_2 = 2; +params.p_ffr_unrate_lag_1 = 3; +params.p_ffr_cpi_lag_1 = 4; + +plot_contributions('eqnum', 'ffr', mydata, params); \ No newline at end of file