pooled_ols: bring over improvement recently made to dyn_ols

time-shift
Houtan Bastani 2017-11-02 14:15:58 +01:00
parent 788910ecd1
commit e0b051314b
1 changed files with 86 additions and 39 deletions

View File

@ -46,7 +46,7 @@ end
assert(~isempty(param_common) && iscellstr(param_common), 'The second argument must be a cellstr');
assert(~isempty(param_regex) && iscellstr(param_regex), 'The third argument must be a cellstr');
jsonfile = [M_.fname '.json'];
jsonfile = [M_.fname '_original.json'];
if exist(jsonfile, 'file') ~= 2
error('Could not find %s! Please use the json=parse option (See the Dynare invocation section in the reference manual).', jsonfile);
end
@ -68,22 +68,11 @@ for i = 1:length(param_regex)
end
%% Find parameters and variable names in every equation & Setup estimation matrices
regexpr1 = ...
['(diff\(\w+(\(\W?\w+\))?\))\*$' ...
'|' '\((\w+(\(\W?\w+\))?(\W?\w+(\(\W?\w+\))?)*)\)\*$' ...
'|' '(\w+(\(\W?\w+\))?)\*$' ...
];
regexpr2 = ...
['^\*(diff\(\w+(\(\W?\w+\))?\))' ...
'|' '^\*\((\w+(\(\W?\w+\))?(\W?\w+(\(\W?\w+\))?)*)' ...
'|' '^\*(\w+(\(\W?\w+\))?)'
];
M_endo_names_trim = cellfun(@strtrim, num2cell(M_.endo_names(:,:),2), 'Uniform', 0);
regex = ['(?<chb>^|(\-|\(|+|\*|\/|\^))(?<var>' ...
strjoin(M_endo_names_trim, '|') ...
')(?<cha>$|(\)\-|\(|+|\*|\/|\^))'];
M_endo_exo_names_trim = cellfun(@strtrim, ...
[num2cell(M_.endo_names(:,:),2) ; num2cell(M_.exo_names(:,:),2)], ...
'Uniform', 0);
regex = strjoin(M_endo_exo_names_trim(:,1), '|');
mathops = '[\+\*\^\-\/]';
params = cell(length(rhs),1);
vars = cell(length(rhs),1);
pbeta = {};
@ -99,7 +88,6 @@ for i = 1:length(lhs)
error(['pooled_ols: you cannot have leads in equation on line ' ...
lineno{i} ': ' lhs{i} ' = ' rhs{i}]);
end
assert(numel(intersect(rhs_, cellstr(M_.exo_names))) == 1);
% Find parameters and associated variables
pnames = intersect(rhs_, cellstr(M_.param_names));
@ -107,31 +95,55 @@ for i = 1:length(lhs)
vnames = cell(1, length(pnames));
xjdata = dseries;
for j = 1:length(pnames)
idx = find(strcmp(pbeta, pnames(j,:)));
createdvar = false;
idx = find(strcmp(pbeta, pnames{j}));
if isempty(idx)
pbeta = [pbeta; pnames(j,:)];
pbeta = [pbeta; pnames{j}];
pidxs(j) = length(pbeta);
else
pidxs(j) = idx;
end
rhs_split = strsplit(rhs{i}, pnames{j});
assert(length(rhs_split) == 2);
if ~isempty(rhs_split{1}) && rhs_split{1}(end) == '*'
vnames(j) = regexp(rhs_split{1}, regexpr1, 'tokens');
elseif ~isempty(rhs_split{2}) && rhs_split{2}(1) == '*'
vnames(j) = regexp(rhs_split{2}, regexpr2, 'tokens');
pregex = [...
mathops pnames{j} mathops ...
'|^' pnames{j} mathops ...
'|' mathops pnames{j} '$' ...
];
[startidx, endidx] = regexp(rhs{i}, pregex, 'start', 'end');
assert(length(startidx) == 1);
if rhs{i}(startidx) == '*'
vnames{j} = getStrMoveLeft(rhs{i}(1:startidx-1));
elseif rhs{i}(endidx) == '*'
vnames{j} = getStrMoveRight(rhs{i}(endidx+1:end));
elseif rhs{i}(startidx) == '+' ...
|| rhs{i}(startidx) == '-' ...
|| rhs{i}(endidx) == '+' ...
|| rhs{i}(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
else
error('pooled_ols: Shouldn''t arrive here');
end
xjdatatmp = getdata(ds, regex, vnames{j}{:});
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
params{i} = pnames;
vars{i} = [vnames{:}];
ydata = getdata(ds, regex, lhs{i});
ydata = eval(regexprep(lhs{i}, regex, 'ds.$&'));
fp = max(ydata.firstobservedperiod, xjdata.firstobservedperiod);
lp = min(ydata.lastobservedperiod, xjdata.lastobservedperiod);
@ -167,19 +179,54 @@ for i = 1:length(idxs)
end
end
function retval = getdata(ds, regex, ser)
if strncmp(ser, 'diff', 4)
ser = ser(6:end-1);
lagidx = strfind(ser, '(');
if isempty(lagidx)
retval = ds{ser} - ds{ser}(-1);
function retval = getStrMoveLeft(str)
mathops = '[\+\*\^\-\/]';
if str(end) ~= ')'
mathidxs = regexp(str, mathops);
if isempty(mathidxs)
retval = str;
else
lag = str2double(ser(lagidx+1:strfind(ser, ')')-1));
assert(lag < 0);
ser = ser(1:lagidx-1);
retval = ds{ser}(lag) - ds{ser}(lag-1);
retval = str(max(mathidxs)+1:end);
end
else
retval = eval(regexprep(ser, regex, '$<chb>ds.$<var>$<cha>'));
closedidxs = strfind(str, ')');
closedidxs = [(length(closedidxs):-1:1)' closedidxs'];
openidxs = strfind(str, '(');
openidxs = [(length(openidxs):-1:1)' openidxs'];
assert(rows(closedidxs) == rows(openidxs));
for i = rows(openidxs):-1:1
openparenidx = find(openidxs(i, 2) < closedidxs(:, 2), 1, 'first');
if openidxs(i, 1) == closedidxs(openparenidx, 1)
break;
end
end
retval = str(max(regexp(str(1:openidxs(openparenidx,2)), mathops))+1:closedidxs(end));
end
end
function retval = getStrMoveRight(str)
mathops = '[\+\*\^\-\/]';
mathidxs = regexp(str, mathops);
openidxs = strfind(str, '(');
if isempty(openidxs) ...
|| (~isempty(mathidxs) ...
&& min(mathidxs) < min(openidxs))
if isempty(mathidxs)
retval = str;
else
retval = str(1:min(mathidxs)-1);
end
else
openidxs = [(1:length(openidxs))' openidxs'];
closedidxs = strfind(str, ')');
closedidxs = [(1:length(closedidxs))' closedidxs'];
assert(length(openidxs) == length(closedidxs));
for i = 1:length(closedidxs)
closedparenidx = sum(openidxs(:, 2) < closedidxs(i, 2));
if openidxs(closedparenidx, 1) == closedidxs(i, 1)
break;
end
end
retval = str(1:closedidxs(closedparenidx, 2));
end
end