plot contributions

time-shift
Houtan Bastani 2017-06-26 22:27:55 +02:00
parent c2d4d47f6d
commit d840695ae0
2 changed files with 111 additions and 0 deletions

View File

@ -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 <http://www.gnu.org/licenses/>.
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

27
tests/ECB/SUR/var.mod Normal file
View File

@ -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);