2018-09-28 16:09:50 +02:00
|
|
|
function varargout = dyn_ols(ds, fitted_names_dict, eqtags)
|
|
|
|
% function varargout = 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
|
2018-09-28 16:09:50 +02:00
|
|
|
% varargout{1} [dseries] data updated with fitted values (if not
|
|
|
|
% called from olsgibbs)
|
2017-10-27 13:07:58 +02:00
|
|
|
%
|
|
|
|
% SPECIAL REQUIREMENTS
|
|
|
|
% none
|
|
|
|
|
2019-01-10 14:53:07 +01:00
|
|
|
% Copyright (C) 2017-2019 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
|
|
|
|
2018-09-14 15:09:02 +02:00
|
|
|
jsonfile = [M_.fname filesep() 'model' filesep() 'json' filesep() 'modfile-original.json'];
|
2017-10-27 13:07:58 +02:00
|
|
|
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);
|
2018-11-23 16:21:11 +01:00
|
|
|
ast = jsonmodel.abstract_syntax_tree;
|
2017-10-27 13:07:58 +02:00
|
|
|
jsonmodel = jsonmodel.model;
|
2017-11-20 15:27:13 +01:00
|
|
|
|
2017-11-02 12:06:32 +01:00
|
|
|
if nargin == 1
|
2017-11-20 15:27:13 +01:00
|
|
|
fitted_names_dict = {};
|
2018-01-19 12:51:51 +01:00
|
|
|
elseif nargin == 2
|
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');
|
2018-01-19 12:51:51 +01:00
|
|
|
elseif nargin == 3
|
2018-11-23 16:21:11 +01:00
|
|
|
ast = getEquationsByTags(ast, 'name', eqtags);
|
2018-01-19 12:51:51 +01:00
|
|
|
jsonmodel = getEquationsByTags(jsonmodel, 'name', eqtags);
|
2017-11-02 12:06:32 +01:00
|
|
|
end
|
2017-10-27 13:07:58 +02:00
|
|
|
|
2019-01-10 14:53:07 +01:00
|
|
|
%% Check to see if called from Gibbs
|
|
|
|
st = dbstack(1);
|
|
|
|
varargout = cell(1, 1);
|
|
|
|
called_from_olsgibbs = false;
|
|
|
|
if strcmp(st(1).name, 'olsgibbs')
|
|
|
|
varargout = cell(1, 4);
|
|
|
|
called_from_olsgibbs = true;
|
|
|
|
assert(length(ast) == 1);
|
|
|
|
end
|
2018-11-23 16:21:11 +01:00
|
|
|
|
2019-01-10 14:53:07 +01:00
|
|
|
%% Loop over equations
|
2018-11-23 16:21:11 +01:00
|
|
|
for i = 1:length(ast)
|
2019-01-10 14:53:07 +01:00
|
|
|
%% Parse equation i
|
|
|
|
[Y, lhssub, X] = parse_ols_style_equation(ds, ast{i}, jsonmodel{i}.line);
|
2017-10-27 13:07:58 +02:00
|
|
|
|
2018-11-23 16:21:11 +01:00
|
|
|
%% Set dates
|
2017-10-27 13:07:58 +02:00
|
|
|
fp = max(Y.firstobservedperiod, X.firstobservedperiod);
|
|
|
|
lp = min(Y.lastobservedperiod, X.lastobservedperiod);
|
2018-11-23 16:21:11 +01:00
|
|
|
if ~isempty(lhssub)
|
|
|
|
fp = max(fp, lhssub.firstobservedperiod);
|
|
|
|
lp = min(lp, lhssub.lastobservedperiod);
|
|
|
|
end
|
2018-01-30 10:30:50 +01:00
|
|
|
if isfield(jsonmodel{i}, 'tags') ...
|
|
|
|
&& isfield(jsonmodel{i}.tags, 'sample') ...
|
|
|
|
&& ~isempty(jsonmodel{i}.tags.sample)
|
2018-01-24 17:25:17 +01:00
|
|
|
colon_idx = strfind(jsonmodel{i}.tags.sample, ':');
|
|
|
|
fsd = dates(jsonmodel{i}.tags.sample(1:colon_idx-1));
|
|
|
|
lsd = dates(jsonmodel{i}.tags.sample(colon_idx+1:end));
|
|
|
|
if fp > fsd
|
2017-11-20 16:13:39 +01:00
|
|
|
warning(['The sample over which you want to estimate contains NaNs. '...
|
2018-01-24 17:25:17 +01:00
|
|
|
'Adjusting estimation range to begin on: ' fp.char])
|
2017-11-20 16:13:39 +01:00
|
|
|
else
|
2018-01-24 17:25:17 +01:00
|
|
|
fp = fsd;
|
|
|
|
end
|
|
|
|
if lp < lsd
|
2018-11-23 16:21:11 +01:00
|
|
|
warning(['The sample over which you want to estimate contains NaNs. '...
|
2018-01-24 17:25:17 +01:00
|
|
|
'Adjusting estimation range to end on: ' lp.char])
|
|
|
|
else
|
|
|
|
lp = lsd;
|
2017-11-20 16:13:39 +01:00
|
|
|
end
|
|
|
|
end
|
2017-10-27 13:07:58 +02:00
|
|
|
|
2017-11-08 16:49:31 +01:00
|
|
|
Y = Y(fp:lp);
|
2018-02-20 14:33:56 +01:00
|
|
|
if ~isempty(lhssub)
|
|
|
|
lhssub = lhssub(fp:lp);
|
|
|
|
end
|
2018-11-23 16:21:11 +01:00
|
|
|
pnames = X.name;
|
|
|
|
X = X(fp:lp).data;
|
2018-01-19 12:51:51 +01:00
|
|
|
|
2018-09-28 16:09:50 +02:00
|
|
|
[nobs, nvars] = size(X);
|
|
|
|
if called_from_olsgibbs
|
|
|
|
varargout{1} = nobs;
|
|
|
|
varargout{2} = pnames;
|
|
|
|
varargout{3} = X;
|
|
|
|
varargout{4} = Y.data;
|
2018-10-06 16:08:28 +02:00
|
|
|
varargout{5} = jsonmodel;
|
|
|
|
varargout{6} = fp;
|
|
|
|
varargout{7} = lp;
|
2018-09-28 16:09:50 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2018-11-23 16:21:11 +01:00
|
|
|
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
|
|
|
|
|
2017-10-27 13:07:58 +02:00
|
|
|
%% Estimation
|
|
|
|
% From LeSage, James P. "Applied Econometrics using MATLAB"
|
2018-01-19 12:51:51 +01:00
|
|
|
oo_.ols.(tag).dof = nobs - nvars;
|
2017-10-27 13:07:58 +02:00
|
|
|
|
|
|
|
% Estimated Parameters
|
|
|
|
[q, r] = qr(X, 0);
|
|
|
|
xpxi = (r'*r)\eye(nvars);
|
2018-01-19 12:51:51 +01:00
|
|
|
oo_.ols.(tag).beta = r\(q'*Y.data);
|
2018-03-09 13:22:43 +01:00
|
|
|
oo_.ols.(tag).param_idxs = zeros(length(pnames), 1);
|
2017-10-27 13:07:58 +02:00
|
|
|
for j = 1:length(pnames)
|
2018-11-23 16:21:11 +01:00
|
|
|
if ~strcmp(pnames{j}, 'intercept')
|
|
|
|
oo_.ols.(tag).param_idxs(j) = find(strcmp(M_.param_names, pnames{j}));
|
|
|
|
M_.params(oo_.ols.(tag).param_idxs(j)) = oo_.ols.(tag).beta(j);
|
|
|
|
end
|
2017-10-27 13:07:58 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
% Yhat
|
2018-01-17 17:19:35 +01:00
|
|
|
idx = 0;
|
2018-01-19 12:51:51 +01:00
|
|
|
yhatname = [tag '_FIT'];
|
2017-11-13 16:17:18 +01:00
|
|
|
if ~isempty(fitted_names_dict)
|
2018-01-19 12:51:51 +01:00
|
|
|
idx = strcmp(fitted_names_dict(:,1), tag);
|
2017-11-13 16:17:18 +01:00
|
|
|
if any(idx)
|
|
|
|
yhatname = fitted_names_dict{idx, 2};
|
|
|
|
end
|
|
|
|
end
|
2018-01-19 12:51:51 +01:00
|
|
|
oo_.ols.(tag).Yhat = dseries(X*oo_.ols.(tag).beta, fp, yhatname);
|
2017-10-27 13:07:58 +02:00
|
|
|
|
|
|
|
% Residuals
|
2018-01-19 12:51:51 +01:00
|
|
|
oo_.ols.(tag).resid = Y - oo_.ols.(tag).Yhat;
|
2017-10-27 13:07:58 +02:00
|
|
|
|
2018-01-29 16:26:46 +01:00
|
|
|
% Correct Yhat reported back to user
|
2018-11-23 16:21:11 +01:00
|
|
|
Y = Y + lhssub;
|
|
|
|
oo_.ols.(tag).Yhat = oo_.ols.(tag).Yhat + lhssub;
|
|
|
|
|
2018-01-29 16:26:46 +01:00
|
|
|
% Apply correcting function for Yhat if it was passed
|
|
|
|
if any(idx) ...
|
|
|
|
&& length(fitted_names_dict(idx, :)) == 3 ...
|
|
|
|
&& ~isempty(fitted_names_dict{idx, 3})
|
|
|
|
oo_.ols.(tag).Yhat = ...
|
|
|
|
feval(fitted_names_dict{idx, 3}, oo_.ols.(tag).Yhat);
|
|
|
|
end
|
2018-01-19 12:51:51 +01:00
|
|
|
ds = [ds oo_.ols.(tag).Yhat];
|
2017-11-09 16:27:54 +01:00
|
|
|
|
2017-10-27 13:07:58 +02:00
|
|
|
%% Calculate statistics
|
|
|
|
% Estimate for sigma^2
|
2018-01-19 12:51:51 +01:00
|
|
|
SS_res = oo_.ols.(tag).resid.data'*oo_.ols.(tag).resid.data;
|
|
|
|
oo_.ols.(tag).s2 = SS_res/oo_.ols.(tag).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;
|
2018-01-19 12:51:51 +01:00
|
|
|
oo_.ols.(tag).R2 = 1 - SS_res/SS_tot;
|
2017-10-27 13:07:58 +02:00
|
|
|
|
|
|
|
% Adjusted R^2
|
2018-02-20 15:23:38 +01:00
|
|
|
oo_.ols.(tag).adjR2 = oo_.ols.(tag).R2 - (1 - oo_.ols.(tag).R2)*(nvars-1)/(oo_.ols.(tag).dof);
|
2017-10-27 13:07:58 +02:00
|
|
|
|
|
|
|
% Durbin-Watson
|
2018-01-19 12:51:51 +01:00
|
|
|
ediff = oo_.ols.(tag).resid.data(2:nobs) - oo_.ols.(tag).resid.data(1:nobs-1);
|
|
|
|
oo_.ols.(tag).dw = (ediff'*ediff)/SS_res;
|
2017-10-27 13:07:58 +02:00
|
|
|
|
|
|
|
% Standard Error
|
2018-01-19 12:51:51 +01:00
|
|
|
oo_.ols.(tag).stderr = sqrt(oo_.ols.(tag).s2*diag(xpxi));
|
2017-10-27 13:07:58 +02:00
|
|
|
|
|
|
|
% T-Stat
|
2018-01-19 12:51:51 +01:00
|
|
|
oo_.ols.(tag).tstat = oo_.ols.(tag).beta./oo_.ols.(tag).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
|
2018-01-19 12:51:51 +01:00
|
|
|
title = ['OLS Estimation of equation ''' tag ''' [name = ''' tag ''']'];
|
2017-12-07 14:41:22 +01:00
|
|
|
else
|
2018-01-19 12:51:51 +01:00
|
|
|
title = ['OLS Estimation of equation ''' tag ''''];
|
2017-11-17 15:53:37 +01:00
|
|
|
end
|
2017-10-27 13:07:58 +02:00
|
|
|
|
2018-01-19 12:51:51 +01:00
|
|
|
preamble = {sprintf('Dependent Variable: %s', jsonmodel{i}.lhs), ...
|
2017-11-17 15:53:37 +01:00
|
|
|
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
|
|
|
|
2018-01-19 12:51:51 +01:00
|
|
|
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)};
|
2017-10-27 13:07:58 +02:00
|
|
|
|
2018-11-23 16:21:11 +01:00
|
|
|
dyn_table(title, preamble, afterward, pnames, ...
|
|
|
|
{'Estimates','t-statistic','Std. Error'}, 4, ...
|
2018-01-19 12:51:51 +01:00
|
|
|
[oo_.ols.(tag).beta oo_.ols.(tag).tstat oo_.ols.(tag).stderr]);
|
2017-11-17 15:53:37 +01:00
|
|
|
end
|
2018-11-23 16:21:11 +01:00
|
|
|
end
|
|
|
|
|
2019-01-10 14:53:07 +01:00
|
|
|
%% Set return value
|
|
|
|
varargout{1} = ds;
|
2017-10-27 13:07:58 +02:00
|
|
|
end
|