simplify getEquationsByTags

time-shift
Houtan Bastani 2018-01-19 12:51:51 +01:00
parent c17f648b39
commit c42311de12
7 changed files with 158 additions and 200 deletions

View File

@ -1,24 +1,23 @@
function [lhs, rhs, linenum, sample, tagvalue] = getEquationsByTags(jsonmodel, varargin)
%function [lhs, rhs, linenum, sample] = getEquationByTag(jsonmodel, varargin)
% Return the lhs, rhs of an equation and the line it was defined
% on given its tag
function [jsonmodel] = getEquationsByTags(jsonmodel, tagname, tagvalue)
%function [jsonmodel] = getEquationsByTags(jsonmodel, tagname, tagvalue)
% Return the jsonmodel structure with the matching tags
%
% INPUTS
% jsonmodel [string] JSON representation of model block
% varargin [string or cellstring arrays] tagname and tagvalue for
% eqs to get
% jsonmodel [string] JSON representation of model block
% tagname [string] The name of the tag whos values are to
% be selected
% tagvalue [string] The values to be selected for the
% provided tagname
%
% OUTPUTS
% lhs [cellstring array] left hand side of eq
% rhs [cellstring array] right hand side of eq
% linenum [cellstring array] eq line in .mod file
% sample [cell array of dates] sample range
% tagvalue [cellstring array] tags associated with equations
% jsonmodel [string] JSON representation of model block,
% with equations removed that don't match
% eqtags
%
% SPECIAL REQUIREMENTS
% none
% Copyright (C) 2017 Dynare Team
% Copyright (C) 2017-2018 Dynare Team
%
% This file is part of Dynare.
%
@ -35,39 +34,9 @@ function [lhs, rhs, linenum, sample, tagvalue] = getEquationsByTags(jsonmodel, v
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
assert(nargin == 1 || nargin == 3, 'Incorrect number of arguments passed to getEquationsByTags');
if nargin == 1
lhs = cell(1, length(jsonmodel));
rhs = cell(1, length(jsonmodel));
linenum = cell(1, length(jsonmodel));
sample = cell(1, length(jsonmodel));
tagvalue = cell(1, length(jsonmodel));
for i=1:length(jsonmodel)
lhs{i} = jsonmodel{i}.lhs;
rhs{i} = jsonmodel{i}.rhs;
linenum{i} = jsonmodel{i}.line;
if isfield(jsonmodel{i}, 'tags') && ...
isfield(jsonmodel{i}.tags, 'name')
tagvalue{i} = jsonmodel{i}.tags.('name');
else
tagvalue{i} = ['eq_line_no_' num2str(linenum{i})];
end
if isfield(jsonmodel{i}, 'tags')
if isfield(jsonmodel{i}.tags, 'sample')
tmp = strsplit(jsonmodel{i}.tags.sample, ':');
sample{i} = dates(tmp{1}):dates(tmp{2});
end
else
tagvalue{i} = ['eq_line_no_' num2str(linenum{i})];
end
end
return
end
tagname = varargin{1};
tagvalue = varargin{2};
assert(nargin == 3, 'Incorrect number of arguments passed to getEquationsByTags');
assert(iscell(jsonmodel) && ~isempty(jsonmodel), ...
'the first argument must be a cell array of structs');
assert(ischar(tagname), 'Tag name must be a string');
assert(ischar(tagvalue) || iscell(tagvalue), 'Tag value must be a string or a cell string array');
@ -75,39 +44,22 @@ if ischar(tagvalue)
tagvalue = {tagvalue};
end
lhs = cell(1, length(tagvalue));
rhs = cell(1, length(tagvalue));
linenum = cell(1, length(tagvalue));
sample = cell(1, length(tagvalue));
idx2rm = [];
idx2keep = [];
for j = 1:length(tagvalue)
orig_len_idx2keep = length(idx2keep);
for i=1:length(jsonmodel)
assert(isstruct(jsonmodel{i}), 'Every entry in jsonmodel must be a struct');
if isfield(jsonmodel{i}, 'tags') && ...
isfield(jsonmodel{i}.tags, tagname) && ...
strcmp(jsonmodel{i}.tags.(tagname), tagvalue{j})
lhs{j} = jsonmodel{i}.lhs;
rhs{j} = jsonmodel{i}.rhs;
linenum{j} = jsonmodel{i}.line;
if isfield(jsonmodel{i}.tags, 'sample')
tmp = strsplit(jsonmodel{i}.tags.sample, ':');
sample{j} = dates(tmp{1}):dates(tmp{2});
end
if ~any(cellfun(@isempty, lhs))
return
end
idx2keep = [idx2keep; i];
break
end
end
if isempty(rhs{j})
warning(['getEquationsByTags: No equation tag found by the name of ''' tagvalue{j} ''''])
idx2rm = [idx2rm j];
if length(idx2keep) == orig_len_idx2keep
warning(['getEquationsByTags: no equation tag found by the name of ''' tagvalue{j} ''''])
end
end
if ~isempty(idx2rm)
lhs(:,idx2rm) = [];
rhs(:,idx2rm) = [];
linenum(:,idx2rm) = [];
sample(:,idx2rm) = [];
tagvalue(:,idx2rm) = [];
assert(~isempty(idx2keep), 'getEquationsByTags: no equations selected');
jsonmodel = jsonmodel(unique(idx2keep, 'stable'));
end
end

View File

@ -80,10 +80,10 @@ if exist(jsonfile, 'file') ~= 2
end
jsonmodel = loadjson(jsonfile);
jsonmodel = jsonmodel.model;
lhs = getEquationsByTags(jsonmodel, 'name', M_.var.(var_model_name).eqtags);
jsonmodel = getEquationsByTags(jsonmodel, 'name', M_.var.(var_model_name).eqtags);
lhsidxs = zeros(ntags, 1);
for i = 1:ntags
idxs = strcmp(M_.endo_names, lhs{i});
idxs = strcmp(M_.endo_names, jsonmodel{i}.lhs);
if any(idxs)
lhsidxs(i) = find(idxs);
continue

View File

@ -53,38 +53,30 @@ jsonmodel = loadjson(jsonfile);
jsonmodel = jsonmodel.model;
if nargin == 1
[lhs, rhs, lineno, sample, tags] = getEquationsByTags(jsonmodel);
fitted_names_dict = {};
else
elseif nargin == 2
assert(isempty(fitted_names_dict) || ...
(iscell(fitted_names_dict) && ...
(size(fitted_names_dict, 2) == 2 || size(fitted_names_dict, 2) == 3)), ...
'dyn_ols: the second argument must be an Nx2 or Nx3 cell array');
if nargin == 2
[lhs, rhs, lineno, sample, tags] = getEquationsByTags(jsonmodel);
else
[lhs, rhs, lineno, sample, tags] = getEquationsByTags(jsonmodel, 'name', eqtags);
end
if isempty(lhs)
disp('dyn_ols: Nothing to estimate')
return
end
elseif nargin == 3
jsonmodel = getEquationsByTags(jsonmodel, 'name', eqtags);
end
%% Estimation
M_endo_exo_names_trim = [M_.endo_names; M_.exo_names];
regex = strjoin(M_endo_exo_names_trim(:,1), '|');
mathops = '[\+\*\^\-\/\(\)]';
for i = 1:length(lhs)
for i = 1:length(jsonmodel)
%% Construct regression matrices
rhs_ = strsplit(rhs{i}, {'+','-','*','/','^','log(','exp(','(',')'});
rhs_ = strsplit(jsonmodel{i}.rhs, {'+','-','*','/','^','log(','exp(','(',')'});
rhs_(cellfun(@(x) all(isstrprop(x, 'digit')), rhs_)) = [];
vnames = setdiff(rhs_, M_.param_names);
if ~isempty(regexp(rhs{i}, ...
if ~isempty(regexp(jsonmodel{i}.rhs, ...
['(' strjoin(vnames, '\\(\\d+\\)|') '\\(\\d+\\))'], ...
'once'))
error(['dyn_ols: you cannot have leads in equation on line ' ...
lineno{i} ': ' lhs{i} ' = ' rhs{i}]);
jsonmodel{i}.line ': ' jsonmodel{i}.lhs ' = ' jsonmodel{i}.rhs]);
end
pnames = intersect(rhs_, M_.param_names);
@ -98,27 +90,27 @@ for i = 1:length(lhs)
'|^' pnames{j} mathops ...
'|' mathops pnames{j} '$' ...
];
[startidx, endidx] = regexp(rhs{i}, pregex, 'start', 'end');
[startidx, endidx] = regexp(jsonmodel{i}.rhs, pregex, 'start', 'end');
assert(length(startidx) == 1);
if rhs{i}(startidx) == '*' && rhs{i}(endidx) == '*'
vnamesl = getStrMoveLeft(rhs{i}(1:startidx-1));
vnamesr = getStrMoveRight(rhs{i}(endidx+1:end));
if jsonmodel{i}.rhs(startidx) == '*' && jsonmodel{i}.rhs(endidx) == '*'
vnamesl = getStrMoveLeft(jsonmodel{i}.rhs(1:startidx-1));
vnamesr = getStrMoveRight(jsonmodel{i}.rhs(endidx+1:end));
vnames{j} = [vnamesl '*' vnamesr];
splitstrings{j} = [vnamesl '*' pnames{j} '*' vnamesr];
elseif rhs{i}(startidx) == '*'
vnames{j} = getStrMoveLeft(rhs{i}(1:startidx-1));
elseif jsonmodel{i}.rhs(startidx) == '*'
vnames{j} = getStrMoveLeft(jsonmodel{i}.rhs(1:startidx-1));
splitstrings{j} = [vnames{j} '*' pnames{j}];
elseif rhs{i}(endidx) == '*'
vnames{j} = getStrMoveRight(rhs{i}(endidx+1:end));
elseif jsonmodel{i}.rhs(endidx) == '*'
vnames{j} = getStrMoveRight(jsonmodel{i}.rhs(endidx+1:end));
splitstrings{j} = [pnames{j} '*' vnames{j}];
if rhs{i}(startidx) == '-'
if jsonmodel{i}.rhs(startidx) == '-'
vnames{j} = ['-' vnames{j}];
splitstrings{j} = ['-' splitstrings{j}];
end
elseif rhs{i}(startidx) == '+' ...
|| rhs{i}(startidx) == '-' ...
|| rhs{i}(endidx) == '+' ...
|| rhs{i}(endidx) == '-'
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'))
@ -133,7 +125,7 @@ for i = 1:length(lhs)
error('dyn_ols: Shouldn''t arrive here');
end
if createdvar
if rhs{i}(startidx) == '-'
if jsonmodel{i}.rhs(startidx) == '-'
Xtmp = dseries(-ones(ds.nobs, 1), ds.firstdate, vnames{j});
else
Xtmp = dseries(ones(ds.nobs, 1), ds.firstdate, vnames{j});
@ -145,113 +137,120 @@ for i = 1:length(lhs)
X = [X Xtmp];
end
lhssub = getRhsToSubFromLhs(ds, rhs{i}, regex, [splitstrings; pnames]);
lhssub = getRhsToSubFromLhs(ds, jsonmodel{i}.rhs, regex, [splitstrings; pnames]);
residuals = setdiff(intersect(rhs_, M_.exo_names), ds.name);
assert(~isempty(residuals), ['No residuals in equation ' num2str(i)]);
assert(length(residuals) == 1, ['More than one residual in equation ' num2str(i)]);
Y = eval(regexprep(lhs{i}, regex, 'ds.$&'));
Y = eval(regexprep(jsonmodel{i}.lhs, regex, 'ds.$&'));
for j = 1:lhssub.vobs
Y = Y - lhssub{j};
end
fp = max(Y.firstobservedperiod, X.firstobservedperiod);
lp = min(Y.lastobservedperiod, X.lastobservedperiod);
if ~isempty(sample{i})
if fp > sample{i}(1) || lp < sample{i}(end)
if isfield(jsonmodel{i}, 'sample') && ~isempty(jsonmodel{i}.sample)
if fp > jsonmodel{i}.sample(1) || lp < jsonmodel{i}.sample(end)
warning(['The sample over which you want to estimate contains NaNs. '...
'Adjusting estimation range to be: ' fp.char ' to ' lp.char])
else
fp = sample{i}(1);
lp = sample{i}(end);
fp = jsonmodel{i}.sample(1);
lp = jsonmodel{i}.sample(end);
end
end
Y = Y(fp:lp);
X = X(fp:lp).data;
if isfield(jsonmodel{i}, 'tags') && ...
isfield(jsonmodel{i}.tags, 'name')
tag = jsonmodel{i}.tags.('name');
else
tag = ['eq_line_no_' num2str(jsonmodel{i}.line)];
end
%% Estimation
% From LeSage, James P. "Applied Econometrics using MATLAB"
[nobs, nvars] = size(X);
oo_.ols.(tags{i}).dof = nobs - nvars;
oo_.ols.(tag).dof = nobs - nvars;
% Estimated Parameters
[q, r] = qr(X, 0);
xpxi = (r'*r)\eye(nvars);
oo_.ols.(tags{i}).beta = r\(q'*Y.data);
oo_.ols.(tag).beta = r\(q'*Y.data);
for j = 1:length(pnames)
M_.params(strcmp(M_.param_names, pnames{j})) = oo_.ols.(tags{i}).beta(j);
M_.params(strcmp(M_.param_names, pnames{j})) = oo_.ols.(tag).beta(j);
end
% Yhat
idx = 0;
yhatname = [tags{i} '_FIT'];
yhatname = [tag '_FIT'];
if ~isempty(fitted_names_dict)
idx = strcmp(fitted_names_dict(:,1), tags{i});
idx = strcmp(fitted_names_dict(:,1), tag);
if any(idx)
yhatname = fitted_names_dict{idx, 2};
end
end
oo_.ols.(tags{i}).Yhat = dseries(X*oo_.ols.(tags{i}).beta, fp, yhatname);
oo_.ols.(tag).Yhat = dseries(X*oo_.ols.(tag).beta, fp, yhatname);
if any(idx) ...
&& length(fitted_names_dict(idx, :)) == 3 ...
&& ~isempty(fitted_names_dict{idx, 3})
oo_.ols.(tags{i}).Yhat = ...
eval([fitted_names_dict{idx, 3} '(oo_.ols.(tags{' num2str(i) '}).Yhat)']);
oo_.ols.(tag).Yhat = ...
feval(fitted_names_dict{idx, 3}, oo_.ols.(tag).Yhat);
end
% Residuals
oo_.ols.(tags{i}).resid = Y - oo_.ols.(tags{i}).Yhat;
oo_.ols.(tag).resid = Y - oo_.ols.(tag).Yhat;
% Correct Yhat reported back to user for given
for j = 1:lhssub.vobs
oo_.ols.(tags{i}).Yhat = oo_.ols.(tags{i}).Yhat + lhssub{j}(fp:lp);
oo_.ols.(tag).Yhat = oo_.ols.(tag).Yhat + lhssub{j}(fp:lp);
end
ds = [ds oo_.ols.(tags{i}).Yhat];
ds = [ds oo_.ols.(tag).Yhat];
%% Calculate statistics
% Estimate for sigma^2
SS_res = oo_.ols.(tags{i}).resid.data'*oo_.ols.(tags{i}).resid.data;
oo_.ols.(tags{i}).s2 = SS_res/oo_.ols.(tags{i}).dof;
SS_res = oo_.ols.(tag).resid.data'*oo_.ols.(tag).resid.data;
oo_.ols.(tag).s2 = SS_res/oo_.ols.(tag).dof;
% R^2
ym = Y.data - mean(Y);
SS_tot = ym'*ym;
oo_.ols.(tags{i}).R2 = 1 - SS_res/SS_tot;
oo_.ols.(tag).R2 = 1 - SS_res/SS_tot;
% Adjusted R^2
oo_.ols.(tags{i}).adjR2 = oo_.ols.(tags{i}).R2 - (1 - oo_.ols.(tags{i}).R2)*nvars/(oo_.ols.(tags{i}).dof-1);
oo_.ols.(tag).adjR2 = oo_.ols.(tag).R2 - (1 - oo_.ols.(tag).R2)*nvars/(oo_.ols.(tag).dof-1);
% Durbin-Watson
ediff = oo_.ols.(tags{i}).resid.data(2:nobs) - oo_.ols.(tags{i}).resid.data(1:nobs-1);
oo_.ols.(tags{i}).dw = (ediff'*ediff)/SS_res;
ediff = oo_.ols.(tag).resid.data(2:nobs) - oo_.ols.(tag).resid.data(1:nobs-1);
oo_.ols.(tag).dw = (ediff'*ediff)/SS_res;
% Standard Error
oo_.ols.(tags{i}).stderr = sqrt(oo_.ols.(tags{i}).s2*diag(xpxi));
oo_.ols.(tag).stderr = sqrt(oo_.ols.(tag).s2*diag(xpxi));
% T-Stat
oo_.ols.(tags{i}).tstat = oo_.ols.(tags{i}).beta./oo_.ols.(tags{i}).stderr;
oo_.ols.(tag).tstat = oo_.ols.(tag).beta./oo_.ols.(tag).stderr;
%% Print Output
if ~options_.noprint
if nargin == 3
title = ['OLS Estimation of equation ''' tags{i} ''' [name = ''' tags{i} ''']'];
title = ['OLS Estimation of equation ''' tag ''' [name = ''' tag ''']'];
else
title = ['OLS Estimation of equation ''' tags{i} ''''];
title = ['OLS Estimation of equation ''' tag ''''];
end
preamble = {sprintf('Dependent Variable: %s', lhs{i}), ...
preamble = {sprintf('Dependent Variable: %s', jsonmodel{i}.lhs), ...
sprintf('No. Independent Variables: %d', nvars), ...
sprintf('Observations: %d from %s to %s\n', nobs, fp.char, lp.char)};
afterward = {sprintf('R^2: %f', oo_.ols.(tags{i}).R2), ...
sprintf('R^2 Adjusted: %f', oo_.ols.(tags{i}).adjR2), ...
sprintf('s^2: %f', oo_.ols.(tags{i}).s2), ...
sprintf('Durbin-Watson: %f', oo_.ols.(tags{i}).dw)};
afterward = {sprintf('R^2: %f', oo_.ols.(tag).R2), ...
sprintf('R^2 Adjusted: %f', oo_.ols.(tag).adjR2), ...
sprintf('s^2: %f', oo_.ols.(tag).s2), ...
sprintf('Durbin-Watson: %f', oo_.ols.(tag).dw)};
dyn_table(title, preamble, afterward, vnames, ...
{'Coefficients','t-statistic','Std. Error'}, 4, ...
[oo_.ols.(tags{i}).beta oo_.ols.(tags{i}).tstat oo_.ols.(tags{i}).stderr]);
[oo_.ols.(tag).beta oo_.ols.(tag).tstat oo_.ols.(tag).stderr]);
end
end
end

View File

@ -58,13 +58,18 @@ for i = 1:length(param_regex)
beta_idx = strcmp(pbeta, strrep(param_regex{i}, '*', oo_.pooled_fgls.country_name));
assigned_idxs = assigned_idxs | beta_idx;
value = oo_.pooled_fgls.beta(beta_idx);
assert(~isempty(value));
M_.params(~cellfun(@isempty, regexp(M_.param_names, ...
strrep(param_regex{i}, '*', regexcountries)))) = value;
if isempty(eqtags)
assert(~isempty(value));
end
if ~isempty(value)
M_.params(~cellfun(@isempty, regexp(M_.param_names, ...
strrep(param_regex{i}, '*', regexcountries)))) = value;
end
end
idxs = find(assigned_idxs == 0);
values = oo_.pooled_fgls.beta(idxs);
names = pbeta(idxs);
assert(length(values) == length(names));
for i = 1:length(idxs)
M_.params(strcmp(M_.param_names, names{i})) = values(i);
end

View File

@ -70,9 +70,9 @@ end
jsonmodel = loadjson(jsonfile);
jsonmodel = jsonmodel.model;
if nargin < 5
[lhs, rhs, lineno] = getEquationsByTags(jsonmodel);
eqtags ={};
else
[lhs, rhs, lineno] = getEquationsByTags(jsonmodel, 'name', eqtags);
jsonmodel = getEquationsByTags(jsonmodel, 'name', eqtags);
end
%% Replace parameter names in equations
@ -82,12 +82,14 @@ param_regex_idx = false(length(param_regex), 1);
for i = 1:length(param_regex)
splitp = strsplit(param_regex{i}, '*');
assert(length(splitp) >= 2);
rhstmp = regexprep(rhs, ...
strjoin(splitp, regexcountries), ...
strjoin(splitp, country_name));
if length(intersect(rhs, rhstmp)) ~= length(rhs)
rhs = rhstmp;
param_regex_idx(i) = true;
for j = 1:length(jsonmodel)
rhstmp = regexprep(jsonmodel{j}.rhs, ...
strjoin(splitp, regexcountries), ...
strjoin(splitp, country_name));
if length(intersect(jsonmodel{j}.rhs, rhstmp)) ~= length(jsonmodel{j}.rhs)
jsonmodel{j}.rhs = rhstmp;
param_regex_idx(i) = true;
end
end
end
param_regex = param_regex(param_regex_idx);
@ -101,18 +103,18 @@ end
%% Find parameters and variable names in every equation & Setup estimation matrices
[X, Y, startdates, enddates, startidxs, residnames, pbeta, vars, surpidxs, surconstrainedparams] = ...
pooled_sur_common(ds, lhs, rhs, lineno);
pooled_sur_common(ds, jsonmodel);
if overlapping_dates
maxfp = max([startdates{:}]);
minlp = min([enddates{:}]);
nobs = minlp - maxfp;
newY = zeros(nobs*length(lhs), 1);
newX = zeros(nobs*length(lhs), columns(X));
newY = zeros(nobs*length(jsonmodel), 1);
newX = zeros(nobs*length(jsonmodel), columns(X));
newstartidxs = zeros(size(startidxs));
newstartidxs(1) = 1;
for i = 1:length(lhs)
if i == length(lhs)
for i = 1:length(jsonmodel)
if i == length(jsonmodel)
yds = dseries(Y(startidxs(i):end), startdates{i});
xds = dseries(X(startidxs(i):end, :), startdates{i});
else
@ -121,7 +123,7 @@ if overlapping_dates
end
newY(newstartidxs(i):newstartidxs(i) + nobs, 1) = yds(maxfp:minlp).data;
newX(newstartidxs(i):newstartidxs(i) + nobs, :) = xds(maxfp:minlp, :).data;
if i ~= length(lhs)
if i ~= length(jsonmodel)
newstartidxs(i+1) = newstartidxs(i) + nobs + 1;
end
end
@ -152,9 +154,13 @@ for i = 1:length(param_regex)
beta_idx = strcmp(pbeta, strrep(param_regex{i}, '*', country_name));
assigned_idxs = assigned_idxs | beta_idx;
value = oo_.(save_structure_name).beta(beta_idx);
assert(~isempty(value));
M_.params(~cellfun(@isempty, regexp(M_.param_names, ...
strrep(param_regex{i}, '*', regexcountries)))) = value;
if isempty(eqtags)
assert(~isempty(value));
end
if ~isempty(value)
M_.params(~cellfun(@isempty, regexp(M_.param_names, ...
strrep(param_regex{i}, '*', regexcountries)))) = value;
end
end
idxs = find(assigned_idxs == 0);
values = oo_.(save_structure_name).beta(idxs);
@ -165,8 +171,8 @@ for i = 1:length(idxs)
end
residuals = Y - X * oo_.(save_structure_name).beta;
for i = 1:length(lhs)
if i == length(lhs)
for i = 1:length(jsonmodel)
if i == length(jsonmodel)
oo_.(save_structure_name).resid.(residnames{i}{:}) = residuals(startidxs(i):end);
else
oo_.(save_structure_name).resid.(residnames{i}{:}) = residuals(startidxs(i):startidxs(i+1)-1);

View File

@ -1,13 +1,11 @@
function [X, Y, startdates, enddates, startidxs, residnames, pbeta, vars, surpidxs, surconstrainedparams] = pooled_sur_common(ds, lhs, rhs, lineno)
%function [X, Y, startdates, enddates, startidxs, residnames, pbeta, vars, surpidxs, surconstrainedparams] = pooled_sur_common(ds, lhs, rhs, lineno)
function [X, Y, startdates, enddates, startidxs, residnames, pbeta, vars, surpidxs, surconstrainedparams] = pooled_sur_common(ds, jsonmodel)
%function [X, Y, startdates, enddates, startidxs, residnames, pbeta, vars, surpidxs, surconstrainedparams] = pooled_sur_common(ds, jsonmodel)
%
% Code common to sur.m and pooled_ols.m
%
% INPUTS
% ds [dseries] dataset
% lhs [cellstr] LHS of equations
% rhs [cellstr] RHS of equations
% lineno [cellstr] line number of equations
% jsonmodel [string] JSON representation of model block
%
% OUTPUTS
% X [matrix] regressors
@ -53,26 +51,26 @@ global M_
M_endo_exo_names_trim = [M_.endo_names; M_.exo_names];
regex = strjoin(M_endo_exo_names_trim(:,1), '|');
mathops = '[\+\*\^\-\/]';
params = cell(length(rhs),1);
vars = cell(length(rhs),1);
params = cell(length(jsonmodel),1);
vars = cell(length(jsonmodel),1);
pbeta = {};
Y = [];
X = [];
startidxs = zeros(length(lhs), 1);
startdates = cell(length(lhs), 1);
enddates = cell(length(lhs), 1);
residnames = cell(length(lhs), 1);
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(lhs)
rhs_ = strsplit(rhs{i}, {'+','-','*','/','^','log(','ln(','log10(','exp(','(',')','diff('});
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(rhs{i}, ...
if ~isempty(regexp(jsonmodel{i}.rhs, ...
['(' strjoin(vnames, '\\(\\d+\\)|') '\\(\\d+\\))'], 'once'))
error(['pooled_ols: you cannot have leads in equation on line ' ...
lineno{i} ': ' lhs{i} ' = ' rhs{i}]);
jsonmodel{i}.line ': ' jsonmodel{i}.lhs ' = ' jsonmodel{i}.rhs]);
end
% Find parameters and associated variables
@ -101,25 +99,25 @@ for i = 1:length(lhs)
'|^' pnames{j} mathops ...
'|' mathops pnames{j} '$' ...
];
[startidx, endidx] = regexp(rhs{i}, pregex, 'start', 'end');
[startidx, endidx] = regexp(jsonmodel{i}.rhs, pregex, 'start', 'end');
assert(length(startidx) == 1);
if rhs{i}(startidx) == '*' && rhs{i}(endidx) == '*'
vnames{j} = [getStrMoveLeft(rhs{i}(1:startidx-1)) '*' ...
getStrMoveRight(rhs{i}(endidx+1:end))];
elseif rhs{i}(startidx) == '*'
vnames{j} = getStrMoveLeft(rhs{i}(1: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 rhs{i}(endidx) == '*'
vnames{j} = getStrMoveRight(rhs{i}(endidx+1:end));
elseif jsonmodel{i}.rhs(endidx) == '*'
vnames{j} = getStrMoveRight(jsonmodel{i}.rhs(endidx+1:end));
splitstrings{j} = [pnames{j} '*' vnames{j}];
if rhs{i}(startidx) == '-'
if jsonmodel{i}.rhs(startidx) == '-'
vnames{j} = ['-' vnames{j}];
splitstrings{j} = ['-' splitstrings{j}];
end
elseif rhs{i}(startidx) == '+' ...
|| rhs{i}(startidx) == '-' ...
|| rhs{i}(endidx) == '+' ...
|| rhs{i}(endidx) == '-'
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'))
@ -146,7 +144,7 @@ for i = 1:length(lhs)
vnames = vnames(dropvname);
end
lhssub = getRhsToSubFromLhs(ds, rhs{i}, regex, [splitstrings; pnames]);
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)]);
@ -155,7 +153,7 @@ for i = 1:length(lhs)
params{i} = pnames;
vars{i} = vnames;
ydata = eval(regexprep(lhs{i}, regex, 'ds.$&'));
ydata = eval(regexprep(jsonmodel{i}.lhs, regex, 'ds.$&'));
for j = 1:lhssub.vobs
ydata = ydata - lhssub{j};
end

View File

@ -44,15 +44,13 @@ end
jsonmodel = loadjson(jsonfile);
jsonmodel = jsonmodel.model;
if nargin == 1
[lhs, rhs, lineno] = getEquationsByTags(jsonmodel);
else
[lhs, rhs, lineno] = getEquationsByTags(jsonmodel, 'name', eqtags);
if nargin == 2
jsonmodel = getEquationsByTags(jsonmodel, 'name', eqtags);
end
%% Find parameters and variable names in equations and setup estimation matrices
[X, Y, startdates, enddates, startidxs, residnames, pbeta, vars, pidxs, surconstrainedparams] = ...
pooled_sur_common(ds, lhs, rhs, lineno);
pooled_sur_common(ds, jsonmodel);
if nargin == 1 && size(X, 2) ~= M_.param_nbr
warning(['Not all parameters were used in model: ' ...
@ -63,11 +61,11 @@ end
maxfp = max([startdates{:}]);
minlp = min([enddates{:}]);
nobs = minlp - maxfp;
newY = zeros(nobs*length(lhs), 1);
newX = zeros(nobs*length(lhs), columns(X));
newY = zeros(nobs*length(jsonmodel), 1);
newX = zeros(nobs*length(jsonmodel), columns(X));
lastidx = 1;
for i = 1:length(lhs)
if i == length(lhs)
for i = 1:length(jsonmodel)
if i == length(jsonmodel)
yds = dseries(Y(startidxs(i):end), startdates{i});
xds = dseries(X(startidxs(i):end, :), startdates{i});
else
@ -76,7 +74,7 @@ for i = 1:length(lhs)
end
newY(lastidx:lastidx + nobs, 1) = yds(maxfp:minlp).data;
newX(lastidx:lastidx + nobs, :) = xds(maxfp:minlp, :).data;
if i ~= length(lhs)
if i ~= length(jsonmodel)
lastidx = lastidx + nobs + 1;
end
end
@ -88,7 +86,7 @@ if strcmp(st(1).name, 'surgibbs')
varargout{2} = pidxs;
varargout{3} = newX;
varargout{4} = newY;
varargout{5} = length(lhs);
varargout{5} = length(jsonmodel);
return
end
@ -101,7 +99,7 @@ oo_.sur.dof = length(maxfp:minlp);
[q, r] = qr(X, 0);
xpxi = (r'*r)\eye(size(X, 2));
resid = Y - X * (r\(q'*Y));
resid = reshape(resid, oo_.sur.dof, length(lhs));
resid = reshape(resid, oo_.sur.dof, length(jsonmodel));
M_.Sigma_e = resid'*resid/oo_.sur.dof;
kLeye = kron(chol(inv(M_.Sigma_e)), eye(oo_.sur.dof));
@ -141,7 +139,7 @@ oo_.sur.tstat = oo_.sur.beta./oo_.sur.stderr;
%% Print Output
if ~options_.noprint
preamble = {sprintf('Dependent Variable: %s', lhs{i}), ...
preamble = {sprintf('Dependent Variable: %s', jsonmodel{i}.lhs), ...
sprintf('No. Independent Variables: %d', M_.param_nbr), ...
sprintf('Observations: %d', oo_.sur.dof)};