diff --git a/matlab/ols/common_parsing.m b/matlab/ols/common_parsing.m index e5a06f59b..3fc3d459c 100644 --- a/matlab/ols/common_parsing.m +++ b/matlab/ols/common_parsing.m @@ -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 diff --git a/matlab/ols/parse_ols_style_equation.m b/matlab/ols/parse_ols_style_equation.m index 10c7cc040..b623a1a65 100644 --- a/matlab/ols/parse_ols_style_equation.m +++ b/matlab/ols/parse_ols_style_equation.m @@ -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 diff --git a/matlab/ols/put_in_sur_form.m b/matlab/ols/put_in_sur_form.m index 87efa6665..0afe6f6af 100644 --- a/matlab/ols/put_in_sur_form.m +++ b/matlab/ols/put_in_sur_form.m @@ -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 diff --git a/matlab/ols/sur.m b/matlab/ols/sur.m index af5bb77c1..6c69b45ec 100644 --- a/matlab/ols/sur.m +++ b/matlab/ols/sur.m @@ -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