handle AR(1) with no parameter

time-shift
Houtan Bastani 2019-01-17 15:15:35 +01:00
parent cfd9eff6fb
commit 0badb00687
No known key found for this signature in database
GPG Key ID: 000094FB955BE169
4 changed files with 44 additions and 22 deletions

View File

@ -62,7 +62,9 @@ if overlapping_dates
minlp = min([enddates{:}]);
for i = 1:neqs
Y{i} = Y{i}(maxfp:minlp);
X{i} = X{i}(maxfp:minlp);
if ~isempty(X{i})
X{i} = X{i}(maxfp:minlp);
end
if ~isempty(lhssub{i})
lhssub{i} = lhssub{i}(maxfp:minlp);
end

View File

@ -153,8 +153,13 @@ end
Y = Y - lhssub;
%% Set start and end dates
fp = max(Y.firstobservedperiod, X.firstobservedperiod);
lp = min(Y.lastobservedperiod, X.lastobservedperiod);
fp = Y.firstobservedperiod;
lp = Y.lastobservedperiod;
if ~isempty(X)
% X is empty when AR(1) without parameter is encountered
fp = max(fp, X.firstobservedperiod);
lp = min(lp, X.lastobservedperiod);
end
if ~isempty(lhssub)
fp = max(fp, lhssub.firstobservedperiod);
lp = min(lp, lhssub.lastobservedperiod);
@ -182,7 +187,9 @@ if isfield(jsonmodel, 'tags') ...
end
Y = Y(fp:lp);
X = X(fp:lp);
if ~isempty(X)
X = X(fp:lp);
end
if ~isempty(lhssub)
lhssub = lhssub(fp:lp);
end

View File

@ -40,10 +40,13 @@ if isempty(Y) || ~iscell(Y) || isempty(X) || ~iscell(X) || length(Y) ~= length(X
end
%% Organize output
neqs = length(X);
neqs = length(Y);
nobs = zeros(neqs, 1);
for i = 1:neqs
assert(size(X{i}, 1) == size(Y{i}, 1), 'Y{i} and X{i} must have the same nuber of observations');
if ~isempty(X{i})
% X{i} is empty for AR(1) equations
assert(size(X{i}, 1) == size(Y{i}, 1), 'Y{i} and X{i} must have the same nuber of observations');
end
nobs(i) = size(X{i}, 1);
end
nrows = sum(nobs);
@ -51,23 +54,28 @@ Xmat = dseries([X{1}.data; zeros(nrows-nobs(1), size(X{1}, 2))], X{1}.firstdate,
Yvec = Y{1};
constrained = {};
for i = 2:neqs
to_remove = [];
nr = sum(nobs(1:i-1));
nxcol = size(X{i}, 2);
Xtmp = dseries([zeros(nr, nxcol); X{i}.data; zeros(nrows-nr-nobs(i), nxcol)], X{1}.firstdate, X{i}.name);
for j = 1:length(X{i}.name)
idx = find(strcmp(Xmat.name, X{i}.name{j}));
if ~isempty(idx)
Xmat.(Xmat.name{idx}) = Xmat{idx} + Xtmp{j};
to_remove = [to_remove j];
constrained{end+1} = Xmat.name{idx};
if isempty(X{i})
data = [Xmat.data;zeros(size(Y{i},1), size(Xmat.data, 2))];
Xmat = dseries(data, X{1}.firstdate, Xmat.name);
else
to_remove = [];
nr = sum(nobs(1:i-1));
nxcol = size(X{i}, 2);
Xtmp = dseries([zeros(nr, nxcol); X{i}.data; zeros(nrows-nr-nobs(i), nxcol)], X{1}.firstdate, X{i}.name);
for j = 1:length(X{i}.name)
idx = find(strcmp(Xmat.name, X{i}.name{j}));
if ~isempty(idx)
Xmat.(Xmat.name{idx}) = Xmat{idx} + Xtmp{j};
to_remove = [to_remove j];
constrained{end+1} = Xmat.name{idx};
end
end
for j = length(to_remove):-1:1
Xtmp = Xtmp.remove(Xtmp.name{j});
end
if ~isempty(Xtmp)
Xmat = [Xmat Xtmp];
end
end
for j = length(to_remove):-1:1
Xtmp = Xtmp.remove(Xtmp.name{j});
end
if ~isempty(Xtmp)
Xmat = [Xmat Xtmp];
end
Yvec = dseries([Yvec.data; Y{i}.data], Yvec.firstdate);
end

View File

@ -54,6 +54,11 @@ neqs = length(jsonmodel);
[Y, ~, X] = common_parsing(ds, ast, jsonmodel, true);
clear ast jsonmodel;
nobs = Y{1}.nobs;
[Y, X, constrained] = put_in_sur_form(Y, X);
if nargin == 1 && size(X, 2) ~= M_.param_nbr