Automagically increase the number of observations in the assigned variable if needed.

So that the following is possible

 y = dseries([0],'1990Q1','y');
 e = dseries(randn(1000,1),'1990Q1','e');
 from 1990Q2 to 2239Q4 do y(t) = .5*y(t-1) + e(t) - .5*e(t-1)*y(t-1) +.1*y(t-1)^2

Initially y has only one observation (which is mandatory because y
depends on its first lag), the routine extends the number of
observations so that y.dates(end)==2239Q4

Note that the exogenous variables are not adjusted, they must be
defined (leads/lags included) between the first and last dates of the
from-to-do syntax.
time-shift
Stéphane Adjemian (Charybdis) 2014-06-12 15:15:04 +02:00
parent 02d410fe08
commit 08ea66057c
1 changed files with 59 additions and 41 deletions

View File

@ -86,6 +86,17 @@ end
% Build the recursive expression.
EXPRESSION = char([varargin{5:end}]);
% Check that the expression is an assignment
equal_id = strfind(EXPRESSION,'=');
if isempty(equal_id)
error('dseries::from: Wrong syntax! The expression following the DO keyword must be an assignment (missing equal symbol).')
end
% Issue ann error message if the user attempts to do more than one assignment.
if ~isequal(length(equal_id),1)
error('dseries::from: Not yet implemented! Only one assignment is allowed in the FROM-TO-DO statement.')
end
% Get all the variables involved in the recursive expression.
variables = unique(regexpi(EXPRESSION, '\w*\(t\)|\w*\(t\-\d\)|\w*\(t\+\d\)|\w*\.\w*\(t\)|\w*\.\w*\(t\-\d\)|\w*\.\w*\(t\+\d\)','match'));
@ -221,20 +232,19 @@ for i=1:number_of_variables
error(msg)
end
if d2>var.dates(end)-leadlagtable{i,4}
% The first variable should be the assigned variable (will be tested later)
if ~isassignedvariable(leadlagtable{i,1},EXPRESSION)
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)
else
var = [var; dseries(NaN((d2-var.dates(end)),1),var.dates(end)+1:d2,var.name)];
end
end
eval(sprintf('%s = var;',current_variable));
end
end
% Check that the recursion is assigning something to a variable
equal_id = strfind(EXPRESSION,'=');
if isempty(equal_id)
error('dseries::from: Wrong syntax! The expression following the DO keyword must be an assignment (missing equal symbol).')
end
if isequal(length(equal_id),1)
% Get the name of the assigned variable (with time index)
assignedvariablename = regexpi(EXPRESSION(1:equal_id-1), '\w*\(t\)|\w*\(t\-\d\)|\w*\(t\+\d\)|\w*\.\w*\(t\)|\w*\.\w*\(t\-\d\)|\w*\.\w*\(t\+\d\)','match');
if isempty(assignedvariablename)
@ -267,9 +277,6 @@ if isequal(length(equal_id),1)
error(sprintf('dseries::from: On the righthand side, the endogenous variable, %s, must be indexed by %s at most.',assignedvariablename,num2index(indum-1)))
end
end
else
error('dseries::from: Not yet implemented! Only one assignment is allowed in the FROM-TO-DO statement.')
end
% Put all the variables in a unique dseries object.
list_of_variables = leadlagtable{1,1};
@ -404,3 +411,14 @@ function id = num2index(i)
else
id = ['(t+' int2str(i) ')'];
end
function i = isassignedvariable(var,expr)
idv = strfind(expr,var);
idq = strfind(expr,'=');
if ~isempty(idv)
if idv(1)<idq
i = 1;
return
end
end
i = 0;