dynare/matlab/plot_contributions.m

78 lines
2.3 KiB
Matlab
Raw Normal View History

2017-06-28 18:34:48 +02:00
function plot_contributions(tagn, tagv, ds, params)
% Plots the contribution to the lhs variable of the rhs variables in an equation.
2017-06-26 22:27:55 +02:00
%
% INPUTS
2017-06-28 18:34:48 +02:00
% - tagn [string] Equation tag name
% - tagv [string] Equation tag value
% - ds [dseries] Object containing all the variables (exogenous and endogenous) appearing in the equation.
% - params [struct] parameter values
2017-06-26 22:27:55 +02:00
%
% 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/>.
2017-06-27 10:54:16 +02:00
2017-06-28 18:34:48 +02:00
global M_
2017-06-26 22:27:55 +02:00
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);
2017-06-28 18:34:48 +02:00
% replace variables with ds.variablename
for i = 1:length(ds.name)
rhs = regexprep(rhs, ['([\s\+\-\*\/\^]{1}|^)(' ds{i}.name{:} ')([\s\(\+\-\*\/\^|$]{1})'], '$1ds.$2$3');
2017-06-26 22:27:55 +02:00
end
2017-06-27 10:53:29 +02:00
fields = fieldnames(params);
2017-06-26 22:27:55 +02:00
2017-06-27 10:53:29 +02:00
for i = 1:length(fields)
2017-06-27 11:24:35 +02:00
rhs = regexprep(rhs, ['([\s\+\-\*\/\^]{1}|^)(' fields{i} ')([\s\+\-\*\/\^]{1}|$)'], '$1params.$2$3');
2017-06-26 22:27:55 +02:00
end
2017-06-27 10:53:29 +02:00
% call function with all variable values
2017-06-28 18:34:48 +02:00
contribution = zeros(ds.nobs, ds.vobs + 1);
2017-06-27 10:53:29 +02:00
rhseval = eval(rhs);
contribution(:, 1) = rhseval.data;
2017-06-28 18:34:48 +02:00
dsbak = ds;
dszero = dseries(zeros(ds.nobs, ds.vobs), ...
ds.firstdate, ...
ds.name);
for i = 1:ds.vobs
ds = dszero;
ds{dsbak.name{i}} = dsbak{dsbak.name{i}};
2017-06-27 10:53:29 +02:00
rhseval = eval(rhs);
contribution(:, i+1) = rhseval.data;
2017-06-26 22:27:55 +02:00
end
figure('Name', lhs);
2017-06-28 18:34:48 +02:00
plot(1:ds.nobs, contribution);
seriesnames = ds.name;
2017-06-27 11:19:05 +02:00
legend('All Endogenous', seriesnames{:});
2017-06-27 10:54:16 +02:00
2017-06-26 22:27:55 +02:00
end