common parsing: small fixes

time-shift
Houtan Bastani 2019-01-11 17:27:42 +01:00
parent 934a22c3a7
commit 1e41235b6b
No known key found for this signature in database
GPG Key ID: 000094FB955BE169
2 changed files with 4 additions and 148 deletions

View File

@ -62,7 +62,7 @@ enddates = cell(length(ast), 1);
neqs = length(ast);
for i = 1:neqs
%% Parse equation i
[Y{i}, lhssub{i}, X{i}] = parse_ols_style_equation(ds, ast{i}, jsonmodel{i}.line);
[Y{i}, lhssub{i}, X{i}] = parse_ols_style_equation(ds, ast{i});
%% Set start and end dates
if overlapping_dates
@ -81,148 +81,4 @@ if overlapping_dates
end
end
end
return
%%
M_endo_exo_names_trim = [M_.endo_names; M_.exo_names];
[junk, idxs] = sort(cellfun(@length, M_endo_exo_names_trim), 'descend');
regex = strjoin(M_endo_exo_names_trim(idxs), '|');
mathops = '[\+\*\^\-\/]';
params = cell(length(jsonmodel),1);
vars = cell(length(jsonmodel),1);
pbeta = {};
Y = [];
X = [];
startidxs = zeros(length(jsonmodel), 1);
startdates = cell(length(jsonmodel), 1);
enddates = cell(length(jsonmodel), 1);
residnames = cell(length(jsonmodel), 1);
surpidxs = zeros(M_.param_nbr, 1);
surpidx = 0;
surconstrainedparams = [];
for i = 1:length(jsonmodel)
rhs_ = strsplit(jsonmodel{i}.rhs, {'+','-','*','/','^','log(','ln(','log10(','exp(','(',')','diff('});
rhs_(cellfun(@(x) all(isstrprop(x, 'digit')), rhs_)) = [];
vnames = setdiff(rhs_, M_.param_names);
if ~isempty(regexp(jsonmodel{i}.rhs, ...
['(' strjoin(vnames, '\\(\\d+\\)|') '\\(\\d+\\))'], 'once'))
error(['you cannot have leads in equation on line ' ...
jsonmodel{i}.line ': ' jsonmodel{i}.lhs ' = ' jsonmodel{i}.rhs]);
end
% Find parameters and associated variables
pnames = intersect(rhs_, M_.param_names);
pidxs = zeros(length(pnames), 1);
vnames = cell(1, length(pnames));
splitstrings = cell(length(pnames), 1);
xjdata = dseries;
dropvname = true(1,length(pnames));
for j = 1:length(pnames)
createdvar = false;
idx = find(strcmp(pbeta, pnames{j}));
if isempty(idx)
pbeta = [pbeta; pnames{j}];
pidxs(j) = length(pbeta);
surpidx = surpidx + 1;
surpidxs(surpidx, 1) = find(strcmp(pnames{j}, M_.param_names));
else
pidxs(j) = idx;
surconstrainedparams = [surconstrainedparams idx];
dropvname(j) = false;
end
pregex = [...
mathops pnames{j} mathops ...
'|^' pnames{j} mathops ...
'|' mathops pnames{j} '$' ...
];
[startidx, endidx] = regexp(jsonmodel{i}.rhs, pregex, 'start', 'end');
assert(length(startidx) == 1);
if jsonmodel{i}.rhs(startidx) == '*' && jsonmodel{i}.rhs(endidx) == '*'
vnames{j} = [getStrMoveLeft(jsonmodel{i}.rhs(1:startidx-1)) '*' ...
getStrMoveRight(jsonmodel{i}.rhs(endidx+1:end))];
elseif jsonmodel{i}.rhs(startidx) == '*'
vnames{j} = getStrMoveLeft(jsonmodel{i}.rhs(1:startidx-1));
splitstrings{j} = [vnames{j} '*' pnames{j}];
elseif jsonmodel{i}.rhs(endidx) == '*'
vnames{j} = getStrMoveRight(jsonmodel{i}.rhs(endidx+1:end));
splitstrings{j} = [pnames{j} '*' vnames{j}];
if jsonmodel{i}.rhs(startidx) == '-'
vnames{j} = ['-' vnames{j}];
splitstrings{j} = ['-' splitstrings{j}];
end
elseif jsonmodel{i}.rhs(startidx) == '+' ...
|| jsonmodel{i}.rhs(startidx) == '-' ...
|| jsonmodel{i}.rhs(endidx) == '+' ...
|| jsonmodel{i}.rhs(endidx) == '-'
% intercept
createdvar = true;
if any(strcmp(M_endo_exo_names_trim, 'intercept'))
[~, vnames{j}] = fileparts(tempname);
vnames{j} = ['intercept_' vnames{j}];
assert(~any(strcmp(M_endo_exo_names_trim, vnames{j})));
else
vnames{j} = 'intercept';
end
splitstrings{j} = vnames{j};
else
error('Shouldn''t arrive here');
end
if createdvar
xjdatatmp = dseries(ones(ds.nobs, 1), ds.firstdate, vnames{j});
else
xjdatatmp = eval(regexprep(vnames{j}, regex, 'ds.$&'));
xjdatatmp.rename_(vnames{j});
end
xjdatatmp.rename_(num2str(j));
xjdata = [xjdata xjdatatmp];
end
if ~all(dropvname)
vnames = vnames(dropvname);
end
lhssub = getRhsToSubFromLhs(ds, jsonmodel{i}.rhs, regex, splitstrings, pnames);
residnames{i} = setdiff(intersect(rhs_, M_.exo_names), ds.name);
assert(~isempty(residnames{i}), ['No residuals in equation ' num2str(i)]);
assert(length(residnames{i}) == 1, ['More than one residual in equation ' num2str(i)]);
params{i} = pnames;
vars{i} = vnames;
ydata = eval(regexprep(jsonmodel{i}.lhs, regex, 'ds.$&'));
if ~isempty(lhssub)
ydata = ydata - lhssub;
end
if isempty(xjdata)
% AR(1) case
fp = ydata.firstobservedperiod;
lp = ydata.lastobservedperiod;
startidxs(i) = length(Y) + 1;
startdates{i} = fp;
enddates{i} = lp;
Y(startidxs(i):startidxs(i)+lp-fp, 1) = ydata(fp:lp).data;
if columns(X) == 0
X(startidxs(i):startidxs(i)+lp-fp, :) = zeros(ydata(fp:lp).nobs, 1);
else
X(startidxs(i):startidxs(i)+lp-fp, :) = zeros(ydata(fp:lp).nobs, columns(X));
end
else
fp = max(ydata.firstobservedperiod, xjdata.firstobservedperiod);
lp = min(ydata.lastobservedperiod, xjdata.lastobservedperiod);
startidxs(i) = length(Y) + 1;
startdates{i} = fp;
enddates{i} = lp;
Y(startidxs(i):startidxs(i)+lp-fp, 1) = ydata(fp:lp).data;
X(startidxs(i):startidxs(i)+lp-fp, pidxs) = xjdata(fp:lp).data;
end
end
surpidxs = surpidxs(1:surpidx);
end

View File

@ -1,4 +1,4 @@
function [Y, lhssub, X] = parse_ols_style_equation(ds, ast, line)
function [Y, lhssub, X] = parse_ols_style_equation(ds, ast)
%function X = parse_ols_style_equation()
% Run OLS on chosen model equations; unlike olseqs, allow for time t
% endogenous variables on LHS
@ -6,7 +6,6 @@ function [Y, lhssub, X] = parse_ols_style_equation(ds, ast, line)
% INPUTS
% ds [dseries] data
% ast [struct] AST representing the equation to be parsed
% line [int] equation line number
%
% OUTPUTS
% Y [dseries] LHS of the equation (with lhssub subtracted)
@ -33,7 +32,7 @@ function [Y, lhssub, X] = parse_ols_style_equation(ds, ast, line)
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
if nargin ~= 3
if nargin ~= 2
error('parse_ols_style_equation takes 3 arguments')
end
@ -45,6 +44,7 @@ if ~isstruct(ast) || length(ast) ~= 1
error('ast must be a celength must be equal to 1');
end
line = ast.line;
if ~strcmp(ast.AST.node_type, 'BinaryOpNode') ...
|| ~strcmp(ast.AST.op, '=')
ols_error('expecting equation with equal sign', line);