Add new option (--method).

Possible values are cumulate (default value) or diff.
kalman-mex
Stéphane Adjemian (Argos) 2023-10-27 11:12:54 +02:00
parent 12cca117bc
commit 33f2456acf
Signed by: stepan
GPG Key ID: A6D44CB9C64CE77B
1 changed files with 77 additions and 20 deletions

View File

@ -18,6 +18,7 @@ function dcontrib(varargin)
% --database dseries object % --database dseries object
% --baseline dseries object (path for the exogenous variables) % --baseline dseries object (path for the exogenous variables)
% --range followed by a dates range % --range followed by a dates range
% --method followed by cumulate (default) or diff.
% %
% REMARKS % REMARKS
% [1] --baseline and --range are not compatible. % [1] --baseline and --range are not compatible.
@ -50,6 +51,7 @@ function dcontrib(varargin)
disp('--database followed by dseries object [mandatory]') disp('--database followed by dseries object [mandatory]')
disp('--baseline followed by dseries object (path for the exogenous variables)') disp('--baseline followed by dseries object (path for the exogenous variables)')
disp('--range followed by a dates range') disp('--range followed by a dates range')
disp('--method followed by keywords cumulate or diff')
disp('--output followed by a name for the structure holding the results [mandatory]') disp('--output followed by a name for the structure holding the results [mandatory]')
skipline() skipline()
return return
@ -115,6 +117,9 @@ function dcontrib(varargin)
baseline = dseries(baseline, firstperiod, M_.exo_names); baseline = dseries(baseline, firstperiod, M_.exo_names);
end end
% get method for computing contributions
method = getmethod(varargin);
% Restrict the observations for the exogenous variables to the pertinent tim range % Restrict the observations for the exogenous variables to the pertinent tim range
xvariables = xvariables(firstperiod:lastperiod); xvariables = xvariables(firstperiod:lastperiod);
@ -126,27 +131,49 @@ function dcontrib(varargin)
% contributions is a dseries object holding the marginal contribution of the baseline and % contributions is a dseries object holding the marginal contribution of the baseline and
% each exogenous variable to endogenous variable z % each exogenous variable to endogenous variable z
% contributions.baseline = S.baseline(firstperiod:lastperiod);
% Add exogenous variables one by one and simulate the model (-> cumulated contributions) switch method
for i=1:xvariables.vobs case 'cumulate'
name = xvariables.name{i}; % Add exogenous variables one by one and simulate the model (-> cumulated contributions)
baseline{name} = xvariables{name};
S.(name) = simul_backward_model(initialconditions, lastperiod-firstperiod+1, baseline);
end
% Compute marginal contributions
for j=1:length(variables)
cumulatedcontribs = S.baseline{variables{j}}(firstperiod:lastperiod).data;
contributions.(variables{j}) = dseries(cumulatedcontribs, firstperiod, 'baseline');
for i=1:xvariables.vobs for i=1:xvariables.vobs
name = xvariables.name{i}; name = xvariables.name{i};
ts = S.(name); baseline{name} = xvariables{name};
data = ts{variables{j}}(firstperiod:lastperiod).data; S.(name) = simul_backward_model(initialconditions, lastperiod-firstperiod+1, baseline);
contributions.(variables{j}) = [contributions.(variables{j}), dseries(data-cumulatedcontribs, firstperiod, name)];
cumulatedcontribs = data;
end end
contributions.(variables{j}) = contributions.(variables{j})(firstperiod:lastperiod); % Compute marginal contributions
for j=1:length(variables)
cumulatedcontribs = S.baseline{variables{j}}(firstperiod:lastperiod).data;
contributions.(variables{j}) = dseries(cumulatedcontribs, firstperiod, 'baseline');
for i=1:xvariables.vobs
name = xvariables.name{i};
ts = S.(name);
data = ts{variables{j}}(firstperiod:lastperiod).data;
contributions.(variables{j}) = [contributions.(variables{j}), dseries(data-cumulatedcontribs, firstperiod, name)];
cumulatedcontribs = data;
end
contributions.(variables{j}) = contributions.(variables{j})(firstperiod:lastperiod);
end
case 'diff'
for i=1:xvariables.vobs
name = xvariables.name{i};
Baseline = baseline;
Baseline{name} = xvariables{name};
S.(name) = simul_backward_model(initialconditions, lastperiod-firstperiod+1, Baseline);
end
% Compute marginal contributions (removing baseline)
for j=1:length(variables)
cumulatedcontribs = S.baseline{variables{j}}(firstperiod:lastperiod).data;
contributions.(variables{j}) = dseries(cumulatedcontribs, firstperiod, 'baseline');
for i=1:xvariables.vobs
name = xvariables.name{i};
ts = S.(name);
data = ts{variables{j}}(firstperiod:lastperiod).data;
contributions.(variables{j}) = [contributions.(variables{j}), dseries(data-cumulatedcontribs, firstperiod, name)];
end
contributions.(variables{j}) = contributions.(variables{j})(firstperiod:lastperiod);
end
otherwise
error('Unknown method (%s)', method)
end end
% Save output in caller workspace % Save output in caller workspace
@ -188,7 +215,7 @@ function eqtags = geteqtags(cellarray)
% OUTPUTS % OUTPUTS
% - eqtags [char] 1×p cell array of row char arrays. % - eqtags [char] 1×p cell array of row char arrays.
[~, vpos, ~, ~, ~, ~, indices] = positions(cellarray); [~, vpos, ~, ~, ~, ~, ~, indices] = positions(cellarray);
lastvalue = indices(find(indices==vpos)+1)-1; lastvalue = indices(find(indices==vpos)+1)-1;
@ -279,7 +306,30 @@ function oname = getoutputname(cellarray)
end end
function [mpos, vpos, dpos, rpos, bpos, opos, indices] = positions(cellarray) function method = getmethod(cellarray)
% Return the method for computing the dynaamic contributions.
%
% INPUTS
% - cellarray [char] 1×n cell array of row char arrays.
%
% OUTPUTS
% - method [char] method: 'cumulate' or 'diff'
[~, ~, ~, ~, ~, ~, kpos] = positions(cellarray);
if isempty(kpos)
method = 'cumulate';
else
method = cellarray{kpos+1};
end
end
function [mpos, vpos, dpos, rpos, bpos, opos, kpos, indices] = positions(cellarray)
% Return positions of the arguments. % Return positions of the arguments.
% %
@ -293,6 +343,7 @@ function [mpos, vpos, dpos, rpos, bpos, opos, indices] = positions(cellarray)
% - rpos [integer] scalar, index for the --range argument. % - rpos [integer] scalar, index for the --range argument.
% - bpos [integer] scalar. index for the --baseline argument. % - bpos [integer] scalar. index for the --baseline argument.
% - opos [integer] scalar, index for the --output argument. % - opos [integer] scalar, index for the --output argument.
% - kpos [integer] scalar, index for the --method argument.
% Index for --model argument % Index for --model argument
mpos = find(strcmp('--model', cellarray)); mpos = find(strcmp('--model', cellarray));
@ -342,7 +393,13 @@ function [mpos, vpos, dpos, rpos, bpos, opos, indices] = positions(cellarray)
error('dplot::positions: Only one --periods argument is allowed.') error('dplot::positions: Only one --periods argument is allowed.')
end end
% Index for --method argument
kpos = find(strcmp('--method', cellarray));
if length(kpos)>1
error('dplot::positions: Only one --method argument is allowed.')
end
% Sorted vector of indices % Sorted vector of indices
indices = sort([mpos; vpos; dpos; rpos; bpos; opos]); indices = sort([mpos; vpos; dpos; rpos; bpos; opos; kpos]);
end end