2017-07-28 12:43:16 +02:00
|
|
|
function plot_contributions(equationname, ds1, ds0)
|
2018-01-25 15:53:31 +01:00
|
|
|
%function plot_contributions(equationname, ds1, ds0)
|
2017-06-28 18:34:48 +02:00
|
|
|
% Plots the contribution to the lhs variable of the rhs variables in an equation.
|
2017-06-26 22:27:55 +02:00
|
|
|
%
|
|
|
|
% INPUTS
|
2017-07-28 12:43:16 +02:00
|
|
|
% - equationname [string] Name of an equation.
|
|
|
|
% - ds1 [string, dseries] Object containing all the variables (exogenous and endogenous)
|
|
|
|
% appearing in the equation, or the name of the dseries object.
|
|
|
|
% - ds0 [string, dseries] Object containing the baseline for all the variables (exogenous
|
|
|
|
% and endogenous) appearing in the equation, or the name of the
|
|
|
|
% dseries object.
|
2017-06-26 22:27:55 +02:00
|
|
|
%
|
|
|
|
% OUTPUTS
|
2017-07-28 12:43:16 +02:00
|
|
|
% none
|
2017-06-26 22:27:55 +02:00
|
|
|
%
|
|
|
|
% SPECIAL REQUIREMENTS
|
2017-07-28 12:43:16 +02:00
|
|
|
% The user must have attached names to the equations using equation
|
|
|
|
% tags. Each equation in the model block must be preceeded with a
|
|
|
|
% tag (see the reference manual). For instance, we should have
|
|
|
|
% something as:
|
|
|
|
%
|
|
|
|
% [name='Phillips curve']
|
|
|
|
% pi = beta*pi(1) + slope*y + lam;
|
2017-06-26 22:27:55 +02:00
|
|
|
|
2019-01-15 10:50:01 +01:00
|
|
|
% Copyright (C) 2017-2019 Dynare Team
|
2017-06-26 22:27:55 +02:00
|
|
|
%
|
|
|
|
% 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
|
|
|
|
2018-07-10 20:15:53 +02:00
|
|
|
jsonfile = [M_.fname filesep() 'model' filesep() 'json' filesep() 'modfile-original.json'];
|
2017-06-26 22:27:55 +02:00
|
|
|
if exist(jsonfile, 'file') ~= 2
|
2017-06-28 21:39:39 +02:00
|
|
|
error('Could not find %s! Please use the json option (See the Dynare invocation section in the reference manual).', jsonfile);
|
2017-06-26 22:27:55 +02:00
|
|
|
end
|
|
|
|
|
2017-07-28 12:43:16 +02:00
|
|
|
% Check the number of input arguments.
|
|
|
|
if nargin>3
|
|
|
|
error('plot_contributions:: Exactly three arguments are required!')
|
|
|
|
end
|
|
|
|
|
|
|
|
% Check the type of the first argument
|
|
|
|
if ~ischar(equationname)
|
|
|
|
error('First argument must be a string.')
|
|
|
|
end
|
|
|
|
|
2018-03-05 12:32:35 +01:00
|
|
|
% Check that the equation name is actually the name of an equation in the model.
|
|
|
|
if ~ismember(equationname, M_.equations_tags(strmatch('name', M_.equations_tags(:,2)),3))
|
|
|
|
error('There is no equation named as %s!', equationname);
|
|
|
|
end
|
|
|
|
|
2017-07-28 12:43:16 +02:00
|
|
|
% Check second argument
|
|
|
|
if ischar(ds1)
|
|
|
|
if ismember(ds1, evalin('caller','who'))
|
|
|
|
ds = evalin('caller', ds1);
|
|
|
|
if isdseries(ds)
|
|
|
|
ds1 = copy(ds); clear ds;
|
|
|
|
else
|
|
|
|
error('plot_contributions:: %s is not a dseries object!', ds1)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
error('plot_contributions:: %s is unknown!', ds1)
|
|
|
|
end
|
|
|
|
else
|
2017-11-10 10:37:16 +01:00
|
|
|
if ~isdseries(ds1)
|
2017-07-28 12:43:16 +02:00
|
|
|
error('plot_contributions:: Second input argument must be a dseries object!')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
% Check third argument
|
|
|
|
if ischar(ds0)
|
|
|
|
if ismember(ds0, evalin('caller','who'))
|
|
|
|
ds = evalin('caller', ds0);
|
|
|
|
if isdseries(ds)
|
|
|
|
ds0 = copy(ds); clear ds;
|
|
|
|
else
|
|
|
|
error('plot_contributions:: %s is not a dseries object!', ds0)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
error('plot_contributions:: %s is unknown!', ds0)
|
|
|
|
end
|
|
|
|
else
|
2017-11-10 10:37:16 +01:00
|
|
|
if ~isdseries(ds0)
|
2017-07-28 12:43:16 +02:00
|
|
|
error('plot_contributions:: Third input argument must be a dseries object!')
|
|
|
|
end
|
|
|
|
end
|
2017-07-05 13:13:33 +02:00
|
|
|
|
2017-06-29 13:29:16 +02:00
|
|
|
% Get equation.
|
2019-05-14 22:12:27 +02:00
|
|
|
[~, jsonmodel] = get_ast(equationname);
|
2018-01-25 15:53:02 +01:00
|
|
|
lhs = jsonmodel{1}.lhs;
|
|
|
|
rhs = jsonmodel{1}.rhs;
|
2017-06-26 22:27:55 +02:00
|
|
|
|
2017-06-29 13:29:16 +02:00
|
|
|
% Get variable and parameter names in the equation.
|
2018-03-05 12:35:49 +01:00
|
|
|
rhs_ = strsplit(rhs,{'+','-','*','/','^','log(','diff(','adl(','exp(','(',')'});
|
|
|
|
rhs_(cellfun(@(x) all(isstrprop(x, 'digit')+isstrprop(x, 'punct')), rhs_)) = []; % Remove numbers
|
2018-01-11 17:10:12 +01:00
|
|
|
pnames = M_.param_names;
|
2017-06-29 13:29:16 +02:00
|
|
|
vnames = setdiff(rhs_, pnames);
|
|
|
|
pnames = setdiff(rhs_, vnames);
|
|
|
|
|
2017-07-06 14:31:15 +02:00
|
|
|
regexprnoleads = cell2mat(strcat('(', vnames, {'\(\d+\))|'}));
|
|
|
|
if ~isempty(regexp(rhs, regexprnoleads(1:end-1), 'match'))
|
|
|
|
error(['plot_contributions: you cannot have leads in equation on line ' lineno ': ' lhs ' = ' rhs]);
|
|
|
|
end
|
|
|
|
|
2017-06-29 13:29:16 +02:00
|
|
|
% Get values for the parameters
|
2018-03-05 12:37:28 +01:00
|
|
|
if ~isempty(pnames)
|
|
|
|
% In case all the parameters are hard coded
|
|
|
|
idp = strmatch(pnames{1}, M_.param_names, 'exact');
|
|
|
|
str = sprintf('%s = M_.params(%d);', pnames{1}, idp);
|
|
|
|
for i=2:length(pnames)
|
|
|
|
idp = strmatch(pnames{i}, M_.param_names, 'exact');
|
|
|
|
str = sprintf('%s %s = M_.params(%d);', str, pnames{i}, idp);
|
|
|
|
end
|
|
|
|
eval(str)
|
2017-06-26 22:27:55 +02:00
|
|
|
end
|
2017-06-29 13:29:16 +02:00
|
|
|
|
|
|
|
% Replace variables with ds.variablename
|
|
|
|
for i = 1:length(vnames)
|
|
|
|
if ismember(vnames{i}, ds1.name) && ismember(vnames{i}, ds0.name)
|
2017-12-14 18:23:52 +01:00
|
|
|
% Match all words with vnames{i}
|
|
|
|
[b, e] = regexp(rhs, sprintf('\\w*%s\\w*', vnames{i}));
|
|
|
|
% Filter out non exact matches (words longer than vnames{i})
|
2018-07-04 08:57:07 +02:00
|
|
|
rid = find(~(e-b>=length(vnames{i})));
|
2017-12-14 18:23:52 +01:00
|
|
|
if ~isempty(rid)
|
|
|
|
b = b(rid);
|
|
|
|
e = e(rid);
|
|
|
|
end
|
|
|
|
% Substitute vnames{i} exact matches by ds.vnames{i}
|
|
|
|
for j=length(rid):-1:1
|
|
|
|
if b(j)>1 && e(j)<length(rhs)
|
|
|
|
rhs = sprintf('%sds.%s%s', rhs(1:b(j)-1), vnames{i}, rhs(e(j)+1:end));
|
|
|
|
elseif isequal(b(j), 1)
|
|
|
|
rhs = sprintf('ds.%s%s', vnames{i}, rhs(e(j)+1:end));
|
|
|
|
elseif isequal(e(j), length(rhs))
|
2018-01-12 12:04:58 +01:00
|
|
|
rhs = sprintf('%sds.%s', rhs(1:b(j)-1), vnames{i});
|
2017-12-14 18:23:52 +01:00
|
|
|
end
|
|
|
|
end
|
2017-06-29 13:29:16 +02:00
|
|
|
else
|
|
|
|
if ismember(vnames{i}, ds1.name)
|
|
|
|
error('Variable %s is not available in the second dseries (baseline paths)!', vnames{i})
|
|
|
|
else
|
|
|
|
error('Variable %s is not available in the first dseries (actual paths)!', vnames{i})
|
|
|
|
end
|
|
|
|
end
|
2017-06-26 22:27:55 +02:00
|
|
|
end
|
|
|
|
|
2017-06-29 16:07:53 +02:00
|
|
|
% Initialize an array for the contributions.
|
|
|
|
contribution = zeros(ds1.nobs, ds1.vobs + 1);
|
|
|
|
|
2017-06-29 14:27:07 +02:00
|
|
|
% Evaluate RHS with all the actual paths.
|
2017-06-29 13:29:16 +02:00
|
|
|
ds = ds1;
|
2017-06-27 10:53:29 +02:00
|
|
|
rhseval = eval(rhs);
|
2018-03-05 12:40:32 +01:00
|
|
|
ds = ds0;
|
|
|
|
rhseval_other = eval(rhs);
|
|
|
|
contribution(:, 1) = rhseval.data - rhseval_other.data;
|
2017-06-27 10:53:29 +02:00
|
|
|
|
2017-06-29 14:27:07 +02:00
|
|
|
% Evaluate RHS with the baseline paths.
|
|
|
|
ds = ds0;
|
|
|
|
rhs0 = eval(rhs);
|
|
|
|
|
2017-06-29 13:29:16 +02:00
|
|
|
% Compute the marginal effect of each variable on the RHS, by evaluating the
|
|
|
|
% RHS with all variables at the Baseline paths except one for which the
|
|
|
|
% actual path is used.
|
|
|
|
for i = 1:length(vnames)
|
|
|
|
ds = ds0; % Set all variable to Baseline paths.
|
|
|
|
ds{vnames{i}} = ds1{vnames{i}};
|
2017-06-29 14:27:07 +02:00
|
|
|
rhsval = eval(rhs)-rhs0;
|
|
|
|
contribution(:, i+1) = rhsval.data;
|
2017-06-26 22:27:55 +02:00
|
|
|
end
|
|
|
|
|
2017-06-29 13:29:16 +02:00
|
|
|
% Create the contributions plot.
|
2017-06-26 22:27:55 +02:00
|
|
|
figure('Name', lhs);
|
2017-06-29 16:07:53 +02:00
|
|
|
hold on
|
|
|
|
cc = contribution(:,2:end);
|
2018-03-05 12:40:32 +01:00
|
|
|
|
|
|
|
% Limit the matrices to what we care about
|
|
|
|
ccneg = cc(:,1:length(vnames)); ccneg(ccneg>=0) = nan;
|
|
|
|
ccpos = cc(:,1:length(vnames)); ccpos(ccpos<0) = nan;
|
|
|
|
H = bar(1:ds.nobs, ccneg, 'stacked');
|
2020-05-29 12:03:25 +02:00
|
|
|
set(gca,'ColorOrderIndex',1);
|
2018-03-05 12:40:32 +01:00
|
|
|
B = bar(1:ds.nobs, ccpos, 'stacked');
|
|
|
|
line_ = plot(1:ds.nobs, contribution(:,1), '-r', 'linewidth', 2);
|
2017-06-29 16:07:53 +02:00
|
|
|
hold off
|
2018-03-05 12:40:32 +01:00
|
|
|
|
|
|
|
lhs = strrep(lhs, '_', '\_');
|
2017-06-29 16:07:53 +02:00
|
|
|
title(sprintf('Decomposition of %s', lhs))
|
2018-03-05 12:40:32 +01:00
|
|
|
|
2017-07-28 12:43:16 +02:00
|
|
|
vnames = strrep(vnames,'_','\_');
|
2018-03-05 12:40:32 +01:00
|
|
|
legend([H, line_], [vnames, lhs], 'location', 'northwest');
|
|
|
|
|