2017-11-20 15:27:13 +01:00
|
|
|
function ds = dyn_ols(ds, fitted_names_dict, eqtags)
|
|
|
|
% function ds = dyn_ols(ds, fitted_names_dict, eqtags)
|
2017-10-27 13:07:58 +02:00
|
|
|
% Run OLS on chosen model equations; unlike olseqs, allow for time t
|
|
|
|
% endogenous variables on LHS
|
|
|
|
%
|
|
|
|
% INPUTS
|
2017-11-20 15:27:13 +01:00
|
|
|
% ds [dseries] data
|
2018-01-17 17:19:35 +01:00
|
|
|
% fitted_names_dict [cell] Nx2 or Nx3 cell array to be used in naming fitted
|
|
|
|
% values; first column is the equation tag,
|
2017-11-20 15:27:13 +01:00
|
|
|
% second column is the name of the
|
2018-01-17 17:19:35 +01:00
|
|
|
% associated fitted value, third column
|
|
|
|
% (if it exists) is the function name of
|
|
|
|
% the transformation to perform on the
|
|
|
|
% fitted value.
|
2017-11-20 15:27:13 +01:00
|
|
|
% eqtags [cellstr] names of equation tags to estimate. If empty,
|
|
|
|
% estimate all equations
|
2017-10-27 13:07:58 +02:00
|
|
|
%
|
|
|
|
% OUTPUTS
|
2017-11-20 15:27:13 +01:00
|
|
|
% ds [dseries] data updated with fitted values
|
2017-10-27 13:07:58 +02:00
|
|
|
%
|
|
|
|
% SPECIAL REQUIREMENTS
|
|
|
|
% none
|
|
|
|
|
2018-01-11 17:10:12 +01:00
|
|
|
% Copyright (C) 2017-2018 Dynare Team
|
2017-10-27 13:07:58 +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/>.
|
|
|
|
|
2017-11-17 15:53:37 +01:00
|
|
|
global M_ oo_ options_
|
2017-10-27 13:07:58 +02:00
|
|
|
|
2018-01-16 16:13:38 +01:00
|
|
|
assert(nargin >= 1 && nargin <= 3, 'dyn_ols: takes between 1 and 3 arguments');
|
2017-11-13 16:17:18 +01:00
|
|
|
assert(isdseries(ds), 'dyn_ols: the first argument must be a dseries');
|
2017-11-02 12:06:32 +01:00
|
|
|
|
2017-10-27 13:07:58 +02:00
|
|
|
jsonfile = [M_.fname '_original.json'];
|
|
|
|
if exist(jsonfile, 'file') ~= 2
|
2017-12-12 12:13:14 +01:00
|
|
|
error('Could not find %s! Please use the json=compute option (See the Dynare invocation section in the reference manual).', jsonfile);
|
2017-10-27 13:07:58 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
%% Get Equation(s)
|
|
|
|
jsonmodel = loadjson(jsonfile);
|
|
|
|
jsonmodel = jsonmodel.model;
|
2017-11-20 15:27:13 +01:00
|
|
|
|
2017-11-02 12:06:32 +01:00
|
|
|
if nargin == 1
|
2017-12-07 14:41:22 +01:00
|
|
|
[lhs, rhs, lineno, sample, tags] = getEquationsByTags(jsonmodel);
|
2017-11-20 15:27:13 +01:00
|
|
|
fitted_names_dict = {};
|
2017-11-02 12:06:32 +01:00
|
|
|
else
|
2017-11-20 15:27:13 +01:00
|
|
|
assert(isempty(fitted_names_dict) || ...
|
2018-01-17 17:19:35 +01:00
|
|
|
(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');
|
2017-11-13 16:17:18 +01:00
|
|
|
if nargin == 2
|
2017-12-07 14:41:22 +01:00
|
|
|
[lhs, rhs, lineno, sample, tags] = getEquationsByTags(jsonmodel);
|
2017-11-13 16:17:18 +01:00
|
|
|
else
|
2017-12-07 14:41:22 +01:00
|
|
|
[lhs, rhs, lineno, sample, tags] = getEquationsByTags(jsonmodel, 'name', eqtags);
|
2017-11-13 16:17:18 +01:00
|
|
|
end
|
2017-11-02 12:06:32 +01:00
|
|
|
if isempty(lhs)
|
|
|
|
disp('dyn_ols: Nothing to estimate')
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
2017-10-27 13:07:58 +02:00
|
|
|
|
|
|
|
%% Estimation
|
2018-01-11 17:10:12 +01:00
|
|
|
M_endo_exo_names_trim = [M_.endo_names; M_.exo_names];
|
2017-10-31 16:42:34 +01:00
|
|
|
regex = strjoin(M_endo_exo_names_trim(:,1), '|');
|
2017-11-07 18:17:44 +01:00
|
|
|
mathops = '[\+\*\^\-\/\(\)]';
|
2017-10-27 13:07:58 +02:00
|
|
|
for i = 1:length(lhs)
|
|
|
|
%% Construct regression matrices
|
|
|
|
rhs_ = strsplit(rhs{i}, {'+','-','*','/','^','log(','exp(','(',')'});
|
|
|
|
rhs_(cellfun(@(x) all(isstrprop(x, 'digit')), rhs_)) = [];
|
2018-01-11 17:10:12 +01:00
|
|
|
vnames = setdiff(rhs_, M_.param_names);
|
2017-10-27 13:07:58 +02:00
|
|
|
if ~isempty(regexp(rhs{i}, ...
|
|
|
|
['(' strjoin(vnames, '\\(\\d+\\)|') '\\(\\d+\\))'], ...
|
|
|
|
'once'))
|
|
|
|
error(['dyn_ols: you cannot have leads in equation on line ' ...
|
|
|
|
lineno{i} ': ' lhs{i} ' = ' rhs{i}]);
|
|
|
|
end
|
|
|
|
|
2018-01-11 17:10:12 +01:00
|
|
|
pnames = intersect(rhs_, M_.param_names);
|
2017-10-27 13:07:58 +02:00
|
|
|
vnames = cell(1, length(pnames));
|
2017-11-08 16:49:31 +01:00
|
|
|
splitstrings = cell(length(pnames), 1);
|
2017-10-27 13:07:58 +02:00
|
|
|
X = dseries();
|
|
|
|
for j = 1:length(pnames)
|
2017-10-31 16:42:34 +01:00
|
|
|
createdvar = false;
|
|
|
|
pregex = [...
|
|
|
|
mathops pnames{j} mathops ...
|
|
|
|
'|^' pnames{j} mathops ...
|
|
|
|
'|' mathops pnames{j} '$' ...
|
|
|
|
];
|
|
|
|
[startidx, endidx] = regexp(rhs{i}, pregex, 'start', 'end');
|
|
|
|
assert(length(startidx) == 1);
|
2017-12-07 10:27:04 +01:00
|
|
|
if rhs{i}(startidx) == '*' && rhs{i}(endidx) == '*'
|
|
|
|
vnamesl = getStrMoveLeft(rhs{i}(1:startidx-1));
|
|
|
|
vnamesr = getStrMoveRight(rhs{i}(endidx+1:end));
|
|
|
|
vnames{j} = [vnamesl '*' vnamesr];
|
|
|
|
splitstrings{j} = [vnamesl '*' pnames{j} '*' vnamesr];
|
|
|
|
elseif rhs{i}(startidx) == '*'
|
2017-10-31 16:42:34 +01:00
|
|
|
vnames{j} = getStrMoveLeft(rhs{i}(1:startidx-1));
|
2017-11-08 16:49:31 +01:00
|
|
|
splitstrings{j} = [vnames{j} '*' pnames{j}];
|
2017-10-31 16:42:34 +01:00
|
|
|
elseif rhs{i}(endidx) == '*'
|
|
|
|
vnames{j} = getStrMoveRight(rhs{i}(endidx+1:end));
|
2017-11-08 16:49:31 +01:00
|
|
|
splitstrings{j} = [pnames{j} '*' vnames{j}];
|
2017-11-07 18:17:44 +01:00
|
|
|
if rhs{i}(startidx) == '-'
|
|
|
|
vnames{j} = ['-' vnames{j}];
|
2017-11-08 16:49:31 +01:00
|
|
|
splitstrings{j} = ['-' splitstrings{j}];
|
2017-11-07 18:17:44 +01:00
|
|
|
end
|
2017-10-31 16:42:34 +01:00
|
|
|
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
|
2017-11-08 16:49:31 +01:00
|
|
|
splitstrings{j} = vnames{j};
|
2017-10-27 13:07:58 +02:00
|
|
|
else
|
|
|
|
error('dyn_ols: Shouldn''t arrive here');
|
|
|
|
end
|
2017-10-31 16:42:34 +01:00
|
|
|
if createdvar
|
2017-11-07 18:17:44 +01:00
|
|
|
if rhs{i}(startidx) == '-'
|
|
|
|
Xtmp = dseries(-ones(ds.nobs, 1), ds.firstdate, vnames{j});
|
|
|
|
else
|
|
|
|
Xtmp = dseries(ones(ds.nobs, 1), ds.firstdate, vnames{j});
|
|
|
|
end
|
2017-10-31 16:42:34 +01:00
|
|
|
else
|
|
|
|
Xtmp = eval(regexprep(vnames{j}, regex, 'ds.$&'));
|
|
|
|
Xtmp.rename_(vnames{j});
|
|
|
|
end
|
2017-10-27 13:07:58 +02:00
|
|
|
X = [X Xtmp];
|
|
|
|
end
|
2017-11-08 16:49:31 +01:00
|
|
|
|
2017-12-05 17:44:23 +01:00
|
|
|
lhssub = getRhsToSubFromLhs(ds, rhs{i}, regex, [splitstrings; pnames]);
|
2018-01-11 17:10:12 +01:00
|
|
|
residuals = setdiff(intersect(rhs_, M_.exo_names), ds.name);
|
2017-12-11 14:35:55 +01:00
|
|
|
assert(~isempty(residuals), ['No residuals in equation ' num2str(i)]);
|
|
|
|
assert(length(residuals) == 1, ['More than one residual in equation ' num2str(i)]);
|
2017-12-05 17:44:23 +01:00
|
|
|
|
2017-10-31 16:42:34 +01:00
|
|
|
Y = eval(regexprep(lhs{i}, regex, 'ds.$&'));
|
2017-11-09 16:25:52 +01:00
|
|
|
for j = 1:lhssub.vobs
|
|
|
|
Y = Y - lhssub{j};
|
2017-11-08 16:49:31 +01:00
|
|
|
end
|
2017-10-27 13:07:58 +02:00
|
|
|
|
|
|
|
fp = max(Y.firstobservedperiod, X.firstobservedperiod);
|
|
|
|
lp = min(Y.lastobservedperiod, X.lastobservedperiod);
|
2017-11-20 16:13:39 +01:00
|
|
|
if ~isempty(sample{i})
|
|
|
|
if fp > sample{i}(1) || lp < sample{i}(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);
|
|
|
|
end
|
|
|
|
end
|
2017-10-27 13:07:58 +02:00
|
|
|
|
2017-11-08 16:49:31 +01:00
|
|
|
Y = Y(fp:lp);
|
2017-10-27 13:07:58 +02:00
|
|
|
X = X(fp:lp).data;
|
|
|
|
|
|
|
|
%% Estimation
|
|
|
|
% From LeSage, James P. "Applied Econometrics using MATLAB"
|
|
|
|
[nobs, nvars] = size(X);
|
2017-12-07 14:41:22 +01:00
|
|
|
oo_.ols.(tags{i}).dof = nobs - nvars;
|
2017-10-27 13:07:58 +02:00
|
|
|
|
|
|
|
% Estimated Parameters
|
|
|
|
[q, r] = qr(X, 0);
|
|
|
|
xpxi = (r'*r)\eye(nvars);
|
2017-12-07 14:41:22 +01:00
|
|
|
oo_.ols.(tags{i}).beta = r\(q'*Y.data);
|
2017-10-27 13:07:58 +02:00
|
|
|
for j = 1:length(pnames)
|
2018-01-18 17:22:23 +01:00
|
|
|
M_.params(strcmp(M_.param_names, pnames{j})) = oo_.ols.(tags{i}).beta(j);
|
2017-10-27 13:07:58 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
% Yhat
|
2018-01-17 17:19:35 +01:00
|
|
|
idx = 0;
|
|
|
|
yhatname = [tags{i} '_FIT'];
|
2017-11-13 16:17:18 +01:00
|
|
|
if ~isempty(fitted_names_dict)
|
2018-01-17 17:19:35 +01:00
|
|
|
idx = strcmp(fitted_names_dict(:,1), tags{i});
|
2017-11-13 16:17:18 +01:00
|
|
|
if any(idx)
|
|
|
|
yhatname = fitted_names_dict{idx, 2};
|
|
|
|
end
|
|
|
|
end
|
2017-12-07 14:41:22 +01:00
|
|
|
oo_.ols.(tags{i}).Yhat = dseries(X*oo_.ols.(tags{i}).beta, fp, yhatname);
|
2018-01-17 17:19:35 +01:00
|
|
|
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)']);
|
|
|
|
end
|
2017-10-27 13:07:58 +02:00
|
|
|
|
|
|
|
% Residuals
|
2017-12-07 14:41:22 +01:00
|
|
|
oo_.ols.(tags{i}).resid = Y - oo_.ols.(tags{i}).Yhat;
|
2017-10-27 13:07:58 +02:00
|
|
|
|
2017-11-09 16:27:54 +01:00
|
|
|
% Correct Yhat reported back to user for given
|
|
|
|
for j = 1:lhssub.vobs
|
2017-12-07 14:41:22 +01:00
|
|
|
oo_.ols.(tags{i}).Yhat = oo_.ols.(tags{i}).Yhat + lhssub{j}(fp:lp);
|
2017-11-09 16:27:54 +01:00
|
|
|
end
|
2017-12-07 14:41:22 +01:00
|
|
|
ds = [ds oo_.ols.(tags{i}).Yhat];
|
2017-11-09 16:27:54 +01:00
|
|
|
|
2017-10-27 13:07:58 +02:00
|
|
|
%% Calculate statistics
|
|
|
|
% Estimate for sigma^2
|
2017-12-07 14:41:22 +01:00
|
|
|
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;
|
2017-10-27 13:07:58 +02:00
|
|
|
|
|
|
|
% R^2
|
2017-11-08 16:49:31 +01:00
|
|
|
ym = Y.data - mean(Y);
|
2017-10-27 13:07:58 +02:00
|
|
|
SS_tot = ym'*ym;
|
2017-12-07 14:41:22 +01:00
|
|
|
oo_.ols.(tags{i}).R2 = 1 - SS_res/SS_tot;
|
2017-10-27 13:07:58 +02:00
|
|
|
|
|
|
|
% Adjusted R^2
|
2017-12-07 14:41:22 +01:00
|
|
|
oo_.ols.(tags{i}).adjR2 = oo_.ols.(tags{i}).R2 - (1 - oo_.ols.(tags{i}).R2)*nvars/(oo_.ols.(tags{i}).dof-1);
|
2017-10-27 13:07:58 +02:00
|
|
|
|
|
|
|
% Durbin-Watson
|
2017-12-07 14:41:22 +01:00
|
|
|
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;
|
2017-10-27 13:07:58 +02:00
|
|
|
|
|
|
|
% Standard Error
|
2017-12-07 14:41:22 +01:00
|
|
|
oo_.ols.(tags{i}).stderr = sqrt(oo_.ols.(tags{i}).s2*diag(xpxi));
|
2017-10-27 13:07:58 +02:00
|
|
|
|
|
|
|
% T-Stat
|
2017-12-07 14:41:22 +01:00
|
|
|
oo_.ols.(tags{i}).tstat = oo_.ols.(tags{i}).beta./oo_.ols.(tags{i}).stderr;
|
2017-10-27 13:07:58 +02:00
|
|
|
|
|
|
|
%% Print Output
|
2017-11-17 15:53:37 +01:00
|
|
|
if ~options_.noprint
|
|
|
|
if nargin == 3
|
2017-12-07 14:41:22 +01:00
|
|
|
title = ['OLS Estimation of equation ''' tags{i} ''' [name = ''' tags{i} ''']'];
|
|
|
|
else
|
|
|
|
title = ['OLS Estimation of equation ''' tags{i} ''''];
|
2017-11-17 15:53:37 +01:00
|
|
|
end
|
2017-10-27 13:07:58 +02:00
|
|
|
|
2017-11-17 15:53:37 +01:00
|
|
|
preamble = {sprintf('Dependent Variable: %s', lhs{i}), ...
|
|
|
|
sprintf('No. Independent Variables: %d', nvars), ...
|
|
|
|
sprintf('Observations: %d from %s to %s\n', nobs, fp.char, lp.char)};
|
2017-10-27 13:07:58 +02:00
|
|
|
|
2017-12-07 14:41:22 +01:00
|
|
|
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)};
|
2017-10-27 13:07:58 +02:00
|
|
|
|
2017-11-17 15:53:37 +01:00
|
|
|
dyn_table(title, preamble, afterward, vnames, ...
|
|
|
|
{'Coefficients','t-statistic','Std. Error'}, 4, ...
|
2017-12-07 14:41:22 +01:00
|
|
|
[oo_.ols.(tags{i}).beta oo_.ols.(tags{i}).tstat oo_.ols.(tags{i}).stderr]);
|
2017-11-17 15:53:37 +01:00
|
|
|
end
|
2017-10-27 13:07:58 +02:00
|
|
|
end
|
|
|
|
end
|