Commit d8496e68 authored by Houtan Bastani's avatar Houtan Bastani

fix bugs in ols code

parent cd2b14cd
......@@ -65,7 +65,8 @@ end
%% Estimation
M_endo_exo_names_trim = [M_.endo_names; M_.exo_names];
regex = strjoin(M_endo_exo_names_trim(:,1), '|');
[junk, idxs] = sort(cellfun(@length, M_endo_exo_names_trim), 'descend');
regex = strjoin(M_endo_exo_names_trim(idxs), '|');
mathops = '[\+\*\^\-\/\(\)]';
for i = 1:length(jsonmodel)
%% Construct regression matrices
......@@ -137,7 +138,7 @@ for i = 1:length(jsonmodel)
X = [X Xtmp];
end
lhssub = getRhsToSubFromLhs(ds, jsonmodel{i}.rhs, 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)]);
......
function lhssub = getRhsToSubFromLhs(ds, rhs, regex, splits)
%function lhssub = getRhsToSubFromLhs(ds, rhs, regex, splits)
function lhssub = getRhsToSubFromLhs(ds, rhs, regex, splits, pnames)
%function lhssub = getRhsToSubFromLhs(ds, rhs, regex, splits, pnames)
% Helper function that identifies variables on RHS that need to be
% subtracted from LHS of OLS-style equation
%
% INPUTS
% ds [dseries] data
% rhs [string] RHS as a string
% regex [string] regex expressing valid list of variables
% splits [cell array] strings to split out of equation on RHS
% ds [dseries] data
% rhs [string] RHS as a string
% regex [string] regex expressing valid list of variables
% splits [cell string] strings to split out of equation on RHS
% pnames [cell string] parameter names
%
% OUTPUTS
% lhssub [dseries] summed data to subtract from LHS
......@@ -37,7 +38,8 @@ global M_
assert(isdseries(ds), 'The first argument must be a dseries');
assert(ischar(rhs), 'The second argument must be a string');
assert(ischar(regex), 'The third argument must be a string');
assert(iscell(splits), 'The fourth argument must be a cell');
assert(iscellstr(splits), 'The fourth argument must be a cell');
assert(iscellstr(splits), 'The fourth argument must be a cell');
lhssub = dseries();
rhs_ = strsplit(rhs, splits);
......@@ -59,7 +61,7 @@ for j = 1:length(rhs_)
try
lhssub = lhssub + eval(regexprep([minusstr str], regex, 'ds.$&'));
catch
if ~any(strcmp(M_.exo_names, str))
if ~any(strcmp(M_.exo_names, str)) && ~any(strcmp(pnames, str))
error(['getRhsToSubFromLhs: problem evaluating ' minusstr str]);
end
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment