From 507b56a05533d07b181d59086559e8224fed3aaa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Adjemian=20=28Scylla=29?= Date: Fri, 2 May 2014 17:52:54 +0200 Subject: [PATCH] Added lead/lag incidence table. Also check that the ranges of the dseries objects appearing in the recursive expression are compatible with the range of the loop. --- matlab/utilities/dseries/from.m | 86 +++++++++++++++++++++++++++------ 1 file changed, 71 insertions(+), 15 deletions(-) diff --git a/matlab/utilities/dseries/from.m b/matlab/utilities/dseries/from.m index 234610853..4fa830917 100644 --- a/matlab/utilities/dseries/from.m +++ b/matlab/utilities/dseries/from.m @@ -62,35 +62,91 @@ end EXPRESSION = char([varargin{5:end}]); % Get all the variables involved in the recursive expression. -variables = regexpi(EXPRESSION, '\w*\(t\)|\w*\(t\-\d\)|\w*\(t\+\d\)','match'); +variables = unique(regexpi(EXPRESSION, '\w*\(t\)|\w*\(t\-\d\)|\w*\(t\+\d\)','match')); -% Remove the time indices. +% Build an incidence table (max lag/lead for each variable) +% +% Column 1: Name of the variable. +% Column 2: Maximum lag order. +% Column 3: Equal to 1 if the variable appears at the current period, 0 otherwise. +% Column 4: Maximum lead order. +leadlagtable = cell(0,4); for i=1:length(variables) + current = ~isempty(regexpi(variables{i},'\(t\)')); + lag = ~isempty(regexpi(variables{i},'\(t\-\d\)')); + lead = ~isempty(regexpi(variables{i},'\(t\+\d\)')); start = regexpi(variables{i},'\(t\)|\(t\-\d\)|\(t\+\d\)'); + index = variables{i}(start:end); variables(i) = {variables{i}(1:start-1)}; + if isempty(leadlagtable) + leadlagtable(1,1) = {variables{i}}; + if current + leadlagtable(1,3) = {1}; + else + leadlagtable(1,3) = {0}; + end + if lag + tmp = regexpi(index,'\d','match'); + leadlagtable(1,2) = {str2num(tmp{1})}; + else + leadlagtable(1,2) = {0}; + end + if lead + tmp = regexpi(index,'\d','match'); + leadlagtable(1,4) = {str2num(tmp{1})}; + else + leadlagtable(1,4) = {0}; + end + else + linea = strmatch(variables{i},leadlagtable(:,1)); + if isempty(linea) + linea = size(leadlagtable,1)+1; + leadlagtable(linea,1) = {variables{i}}; + leadlagtable(linea,2) = {0}; + leadlagtable(linea,3) = {0}; + leadlagtable(linea,4) = {0}; + end + if current + leadlagtable(linea,3) = {1}; + end + if lag + tmp = regexpi(index,'\d','match'); + leadlagtable(linea,2) = {max(str2num(tmp{1}),leadlagtable{linea,2})}; + end + if lead + tmp = regexpi(index,'\d','match'); + leadlagtable(linea,4) = {max(str2num(tmp{1}),leadlagtable{linea,4})}; + end + end end -% Remove duplicates. -variables = unique(variables); +% Set the number of variables +number_of_variables = size(leadlagtable,1); -% Test that all the involved variables are available dseries objects. Also check -% that these time series are defined over the time range given by d1 and d2. -for i=1:length(variables) +% Test that all the involved variables are available dseries objects. Also check that +% these time series are defined over the time range given by d1 and d2 (taking care of +% the lags and leads). +for i=1:number_of_variables + current_variable = leadlagtable{i,1}; try - var = evalin('caller',variables{i}); + var = evalin('caller',current_variable); catch - error(['dseries::from: Variable ' variables{i} ' is unknown!']) + error(['dseries::from: Variable ' current_variable ' is unknown!']) end if ~isdseries(var) - error(['dseries::from: Variable ' variables{i} ' is not a dseries object!']) + error(['dseries::from: Variable ' current_variable ' is not a dseries object!']) else - if d1var.dates(end) - error(sprintf('dseries::from: Last date in variable %s is anterior to %s!\n Check the terminal date of the loop.', variables{i}, char(d2))) + if d2>var.dates(end)-leadlagtable{i,4} + msg = sprintf('dseries::from: Last date of the loop (%s) is inconsistent with %s''s range!\n',char(d2),current_variable); + msg = [msg, sprintf(' Last date should be less than or equal to %s.',char(var.dates(end)-leadlagtable{i,4}))]; + error(msg) end - eval(sprintf('%s = var;',variables{i})); + eval(sprintf('%s = var;',current_variable)); end end