2018-01-17 12:57:58 +01:00
|
|
|
function varargout = pooled_ols(ds, param_common, param_regex, overlapping_dates, eqtags)
|
2018-01-17 12:50:22 +01:00
|
|
|
% function pooled_ols(ds, param_common, param_regex, overlapping_dates, eqtags)
|
2017-10-23 15:55:01 +02:00
|
|
|
% Run Pooled OLS
|
|
|
|
% Apply parameter values found to corresponding parameter values in the
|
|
|
|
% other blocks of the model
|
|
|
|
%
|
|
|
|
% INPUTS
|
2017-11-15 12:36:12 +01:00
|
|
|
% ds [dseries] data to use in estimation
|
|
|
|
% param_common [cellstr] List of values to insert into param_regex,
|
|
|
|
% e.g. country codes {'FR', 'DE', 'IT'}
|
|
|
|
% param_regex [cellstr] Where '*' should be replaced by the first
|
|
|
|
% value in param_common
|
|
|
|
% overlapping_dates [bool] if the dates across the equations should
|
|
|
|
% overlap
|
2018-01-17 11:56:47 +01:00
|
|
|
% eqtags [cellstr] names of equation tags to estimate. If empty,
|
|
|
|
% estimate all equations
|
2017-10-23 15:55:01 +02:00
|
|
|
%
|
|
|
|
% OUTPUTS
|
|
|
|
% none
|
|
|
|
%
|
|
|
|
% SPECIAL REQUIREMENTS
|
2017-12-12 12:13:14 +01:00
|
|
|
% dynare must be run with the option: json=compute
|
2017-10-23 15:55:01 +02:00
|
|
|
|
2019-01-14 15:23:30 +01:00
|
|
|
% Copyright (C) 2017-2019 Dynare Team
|
2017-10-23 15:55:01 +02:00
|
|
|
%
|
|
|
|
% This file is part of Dynare.
|
|
|
|
%
|
|
|
|
% Dynare is free software: you can redistribute it and/or modify
|
|
|
|
% it under the terms of the GNU General Public License as published by
|
|
|
|
% the Free Software Foundation, either version 3 of the License, or
|
|
|
|
% (at your option) any later version.
|
|
|
|
%
|
|
|
|
% Dynare is distributed in the hope that it will be useful,
|
|
|
|
% but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
% GNU General Public License for more details.
|
|
|
|
%
|
|
|
|
% You should have received a copy of the GNU General Public License
|
|
|
|
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
global M_ oo_
|
|
|
|
|
2017-12-12 12:13:14 +01:00
|
|
|
%% Check input arguments
|
2017-11-02 12:34:11 +01:00
|
|
|
assert(~isempty(ds) && isdseries(ds), 'The first argument must be a dseries');
|
2017-11-15 12:36:12 +01:00
|
|
|
|
2017-11-02 12:36:00 +01:00
|
|
|
if isempty(param_common) && isempty(param_regex)
|
|
|
|
disp('Performing OLS instead of Pooled OLS...')
|
2018-01-17 11:56:47 +01:00
|
|
|
if nargin < 6
|
|
|
|
dyn_ols(ds);
|
|
|
|
else
|
|
|
|
dyn_ols(ds, {}, eqtags);
|
|
|
|
end
|
2019-01-14 15:23:30 +01:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
if nargin < 5
|
|
|
|
eqtags = {};
|
2017-11-02 12:36:00 +01:00
|
|
|
end
|
2019-01-14 15:23:30 +01:00
|
|
|
|
2017-11-02 12:34:11 +01:00
|
|
|
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');
|
2017-10-27 12:20:16 +02:00
|
|
|
|
2017-11-15 12:36:12 +01:00
|
|
|
if nargin < 4
|
|
|
|
overlapping_dates = false;
|
|
|
|
else
|
|
|
|
assert(islogical(overlapping_dates) && length(overlapping_dates) == 1, 'The fourth argument must be a bool');
|
|
|
|
end
|
|
|
|
|
2019-01-14 15:23:30 +01:00
|
|
|
%% Get Equation(s)
|
|
|
|
[ast, jsonmodel] = get_ast_jsonmodel(eqtags);
|
|
|
|
neqs = length(jsonmodel);
|
2017-10-23 15:55:01 +02:00
|
|
|
|
|
|
|
%% Replace parameter names in equations
|
|
|
|
country_name = param_common{1};
|
|
|
|
regexcountries = ['(' strjoin(param_common(2:end),'|') ')'];
|
2019-01-14 15:23:30 +01:00
|
|
|
ast = replace_parameters(ast, country_name, regexcountries, param_regex);
|
2017-10-23 15:55:01 +02:00
|
|
|
|
2019-01-14 15:23:30 +01:00
|
|
|
%% Handle FGLS
|
2018-01-17 12:50:22 +01:00
|
|
|
st = dbstack(1);
|
|
|
|
save_structure_name = 'pooled_ols';
|
|
|
|
if strcmp(st(1).name, 'pooled_fgls')
|
2018-01-17 12:57:58 +01:00
|
|
|
varargout{1} = param_regex;
|
2018-01-17 12:50:22 +01:00
|
|
|
save_structure_name = 'pooled_fgls';
|
|
|
|
end
|
|
|
|
|
2017-10-23 15:55:01 +02:00
|
|
|
%% Find parameters and variable names in every equation & Setup estimation matrices
|
2019-01-14 15:23:30 +01:00
|
|
|
[Y, ~, X] = common_parsing(ds, ast, jsonmodel, overlapping_dates);
|
|
|
|
clear ast jsonmodel;
|
|
|
|
nobs = Y{1}.nobs;
|
2019-01-14 15:22:02 +01:00
|
|
|
fp = Y{1}.firstobservedperiod;
|
|
|
|
for i = 2:length(Y)
|
|
|
|
if Y{i}.firstobservedperiod < fp
|
|
|
|
fp = Y{i}.firstobservedperiod;
|
|
|
|
end
|
|
|
|
nobs = nobs + Y{i}.nobs;
|
|
|
|
end
|
2019-01-14 15:23:30 +01:00
|
|
|
[Y, X] = put_in_sur_form(Y, X);
|
|
|
|
|
|
|
|
%% Save
|
2019-01-14 15:22:02 +01:00
|
|
|
oo_.(save_structure_name).sample_range = fp:fp+nobs;
|
2019-01-14 15:23:30 +01:00
|
|
|
%oo_.(save_structure_name).residnames = residnames;
|
|
|
|
oo_.(save_structure_name).Y = Y.data;
|
|
|
|
oo_.(save_structure_name).X = X.data;
|
|
|
|
oo_.(save_structure_name).pbeta = X.name;
|
|
|
|
oo_.(save_structure_name).country_name = country_name;
|
2017-11-15 12:36:12 +01:00
|
|
|
|
2017-10-23 15:55:01 +02:00
|
|
|
%% Estimation
|
|
|
|
% Estimated Parameters
|
2019-01-14 15:23:30 +01:00
|
|
|
[q, r] = qr(X.data, 0);
|
|
|
|
oo_.(save_structure_name).beta = r\(q'*Y.data);
|
2017-10-23 15:55:01 +02:00
|
|
|
|
2017-11-21 15:23:12 +01:00
|
|
|
if strcmp(st(1).name, 'pooled_fgls')
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2017-10-23 15:55:01 +02:00
|
|
|
% Assign parameter values back to parameters using param_regex & param_common
|
|
|
|
regexcountries = ['(' strjoin(param_common(1:end),'|') ')'];
|
2019-01-14 15:23:30 +01:00
|
|
|
assigned_idxs = false(size(X.name));
|
2017-10-23 15:55:01 +02:00
|
|
|
for i = 1:length(param_regex)
|
2019-01-14 15:23:30 +01:00
|
|
|
beta_idx = strcmp(X.name, strrep(param_regex{i}, '*', country_name));
|
2017-10-23 15:55:01 +02:00
|
|
|
assigned_idxs = assigned_idxs | beta_idx;
|
2017-11-15 12:36:12 +01:00
|
|
|
value = oo_.(save_structure_name).beta(beta_idx);
|
2018-01-19 12:51:51 +01:00
|
|
|
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
|
2017-10-23 15:55:01 +02:00
|
|
|
end
|
|
|
|
idxs = find(assigned_idxs == 0);
|
2017-11-15 12:36:12 +01:00
|
|
|
values = oo_.(save_structure_name).beta(idxs);
|
2019-01-14 15:23:30 +01:00
|
|
|
names = X.name(idxs);
|
2017-10-23 15:55:01 +02:00
|
|
|
assert(length(values) == length(names));
|
|
|
|
for i = 1:length(idxs)
|
2018-01-11 17:10:12 +01:00
|
|
|
M_.params(strcmp(M_.param_names, names{i})) = values(i);
|
2017-10-23 15:55:01 +02:00
|
|
|
end
|
2017-11-07 14:58:11 +01:00
|
|
|
|
2019-01-14 14:57:48 +01:00
|
|
|
% residuals = Y.data - X.data * oo_.(save_structure_name).beta;
|
|
|
|
% for i = 1:neqs
|
|
|
|
% if i == neqs
|
|
|
|
% 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);
|
|
|
|
% end
|
|
|
|
% oo_.(save_structure_name).varcovar.(['eq' num2str(i)]) = oo_.(save_structure_name).resid.(residnames{i}{:})*oo_.(save_structure_name).resid.(residnames{i}{:})';
|
|
|
|
% idx = find(strcmp(residnames{i}{:}, M_.exo_names));
|
|
|
|
% M_.Sigma_e(idx, idx) = var(oo_.(save_structure_name).resid.(residnames{i}{:}));
|
|
|
|
% end
|
2017-10-23 15:55:01 +02:00
|
|
|
end
|
2019-01-14 15:23:30 +01:00
|
|
|
|
|
|
|
function ast = replace_parameters(ast, country_name, regexcountries, param_regex)
|
|
|
|
for i = 1:length(ast)
|
|
|
|
ast{i}.AST = replace_parameters_recursive(ast{i}.AST, country_name, regexcountries, param_regex);
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function node = replace_parameters_recursive(node, country_name, regexcountries, param_regex)
|
|
|
|
if strcmp(node.node_type, 'VariableNode')
|
|
|
|
if strcmp(node.type, 'parameter')
|
|
|
|
for i = 1:length(param_regex)
|
|
|
|
splitp = strsplit(param_regex{i}, '*');
|
|
|
|
assert(length(splitp) >= 2);
|
|
|
|
tmp = regexprep(node.name, strjoin(splitp, regexcountries), strjoin(splitp, country_name));
|
|
|
|
if ~strcmp(tmp, node.name)
|
|
|
|
node.name = tmp;
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
elseif strcmp(node.node_type, 'UnaryOpNode')
|
|
|
|
node.arg = replace_parameters_recursive(node.arg, country_name, regexcountries, param_regex);
|
|
|
|
elseif strcmp(node.node_type, 'BinaryOpNode')
|
|
|
|
node.arg1 = replace_parameters_recursive(node.arg1, country_name, regexcountries, param_regex);
|
|
|
|
node.arg2 = replace_parameters_recursive(node.arg2, country_name, regexcountries, param_regex);
|
|
|
|
end
|
|
|
|
end
|