Add --log option to dcontrib command.

dcontrib-log
Stéphane Adjemian (Ryûk) 2023-11-30 16:43:53 +01:00
parent dadcd9a2bf
commit edaf938582
Signed by: stepan
GPG Key ID: 295C1FE89E17EB3C
1 changed files with 55 additions and 24 deletions

View File

@ -19,6 +19,7 @@ function dcontrib(varargin)
% --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. % --method followed by cumulate (default) or diff.
% --log returns the variables in logs
% %
% REMARKS % REMARKS
% [1] --baseline and --range are not compatible. % [1] --baseline and --range are not compatible.
@ -53,6 +54,7 @@ function dcontrib(varargin)
disp('--range followed by a dates range') disp('--range followed by a dates range')
disp('--method followed by keywords cumulate or diff') 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]')
disp('--log to return the contributions in logs')
skipline() skipline()
return return
end end
@ -100,6 +102,12 @@ function dcontrib(varargin)
error('dcontrib:: Try reduce lastperiod (<=%s).', char(ds.dates(end))) error('dcontrib:: Try reduce lastperiod (<=%s).', char(ds.dates(end)))
end end
if islog(varargin)
transform = @(x) log(x);
else
transform = @(x) x;
end
% Load baseline (if it makes sense) % Load baseline (if it makes sense)
if isempty(firstperiod) if isempty(firstperiod)
baselinename = getbaselinename(varargin); baselinename = getbaselinename(varargin);
@ -143,12 +151,12 @@ function dcontrib(varargin)
% Compute marginal contributions % Compute marginal contributions
for j=1:length(variables) for j=1:length(variables)
cumulatedcontribs = S.baseline{variables{j}}(firstperiod:lastperiod).data; cumulatedcontribs = S.baseline{variables{j}}(firstperiod:lastperiod).data;
contributions.(variables{j}) = dseries(cumulatedcontribs, firstperiod, 'baseline'); contributions.(variables{j}) = dseries(transform(cumulatedcontribs), firstperiod, 'baseline');
for i=1:xvariables.vobs for i=1:xvariables.vobs
name = xvariables.name{i}; name = xvariables.name{i};
ts = S.(name); ts = S.(name);
data = ts{variables{j}}(firstperiod:lastperiod).data; data = ts{variables{j}}(firstperiod:lastperiod).data;
contributions.(variables{j}) = [contributions.(variables{j}), dseries(data-cumulatedcontribs, firstperiod, name)]; contributions.(variables{j}) = [contributions.(variables{j}), dseries(transform(data)-transform(cumulatedcontribs), firstperiod, name)];
cumulatedcontribs = data; cumulatedcontribs = data;
end end
contributions.(variables{j}) = contributions.(variables{j})(firstperiod:lastperiod); contributions.(variables{j}) = contributions.(variables{j})(firstperiod:lastperiod);
@ -163,12 +171,12 @@ function dcontrib(varargin)
% Compute marginal contributions (removing baseline) % Compute marginal contributions (removing baseline)
for j=1:length(variables) for j=1:length(variables)
cumulatedcontribs = S.baseline{variables{j}}(firstperiod:lastperiod).data; cumulatedcontribs = S.baseline{variables{j}}(firstperiod:lastperiod).data;
contributions.(variables{j}) = dseries(cumulatedcontribs, firstperiod, 'baseline'); contributions.(variables{j}) = dseries(transform(cumulatedcontribs), firstperiod, 'baseline');
for i=1:xvariables.vobs for i=1:xvariables.vobs
name = xvariables.name{i}; name = xvariables.name{i};
ts = S.(name); ts = S.(name);
data = ts{variables{j}}(firstperiod:lastperiod).data; data = ts{variables{j}}(firstperiod:lastperiod).data;
contributions.(variables{j}) = [contributions.(variables{j}), dseries(data-cumulatedcontribs, firstperiod, name)]; contributions.(variables{j}) = [contributions.(variables{j}), dseries(transform(data)-transform(cumulatedcontribs), firstperiod, name)];
end end
contributions.(variables{j}) = contributions.(variables{j})(firstperiod:lastperiod); contributions.(variables{j}) = contributions.(variables{j})(firstperiod:lastperiod);
end end
@ -215,7 +223,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;
@ -318,8 +326,6 @@ function method = getmethod(cellarray)
[~, ~, ~, ~, ~, ~, kpos] = positions(cellarray); [~, ~, ~, ~, ~, ~, kpos] = positions(cellarray);
if isempty(kpos) if isempty(kpos)
method = 'cumulate'; method = 'cumulate';
else else
@ -329,25 +335,44 @@ function method = getmethod(cellarray)
end end
function [mpos, vpos, dpos, rpos, bpos, opos, kpos, indices] = positions(cellarray) function bool = islog(cellarray)
% Return positions of the arguments. % Returns true if the contributions are required in logs.
% %
% INPUTS % INPUTS
% - cellarray [char] 1×n cell array of row char arrays. % - cellarray [char] 1×n cell array of row char arrays.
% %
% OUTPUTS % OUTPUTS
% - mpos [integer] scalar, index for the --model argument. % - method [char] method: 'cumulate' or 'diff'
% - vpos [integer] scalar, index for the --tags arguments.
% - dpos [integer] scalar, index for the --database argument. [~, ~, ~, ~, ~, ~, ~, lpos] = positions(cellarray);
% - rpos [integer] scalar, index for the --range argument.
% - bpos [integer] scalar. index for the --baseline argument. bool = ~isempty(lpos);
% - opos [integer] scalar, index for the --output argument.
% - kpos [integer] scalar, index for the --method argument. end
function [mpos, vpos, dpos, rpos, bpos, opos, kpos, lpos, indices] = positions(cellarray)
% Return positions of the arguments.
%
% INPUTS
% - cellarray [char] 1×n cell array of row char arrays.
%
% OUTPUTS
% - mpos [integer] scalar, index for the --model argument.
% - vpos [integer] scalar, index for the --tags arguments.
% - dpos [integer] scalar, index for the --database argument.
% - rpos [integer] scalar, index for the --range argument.
% - bpos [integer] scalar. index for the --baseline argument.
% - opos [integer] scalar, index for the --output argument.
% - kpos [integer] scalar, index for the --method argument.
% - lpos [integer] scalar, index for the --log option.
% Index for --model argument % Index for --model argument
mpos = find(strcmp('--model', cellarray)); mpos = find(strcmp('--model', cellarray));
if isempty(mpos) if isempty(mpos)
error('dcontrib::positions: --model argument is mandatory.') error('dcontrib::positions: --model argument is mandatory.')
elseif length(mpos)>1 elseif length(mpos)>1
error('dplot::positions: Only one --model argument is allowed.') error('dplot::positions: Only one --model argument is allowed.')
@ -399,7 +424,13 @@ function [mpos, vpos, dpos, rpos, bpos, opos, kpos, indices] = positions(cellarr
error('dplot::positions: Only one --method argument is allowed.') error('dplot::positions: Only one --method argument is allowed.')
end end
% Index for --log option
lpos = find(strcmp('--log', cellarray));
if length(lpos)>1
warning('dplot::positions: There is no point in using --log more than once.')
end
% Sorted vector of indices % Sorted vector of indices
indices = sort([mpos; vpos; dpos; rpos; bpos; opos; kpos]); indices = sort([mpos; vpos; dpos; rpos; bpos; opos; kpos; lpos]);
end end