SUR with test

time-shift
Houtan Bastani 2017-11-17 14:41:27 +01:00
parent 0301029a17
commit 3cd4e732d3
4 changed files with 415 additions and 196 deletions

228
matlab/ols/sur.m Normal file
View File

@ -0,0 +1,228 @@
function sur(ds)
% function sur(ds)
% Seemingly Unrelated Regressions
%
% INPUTS
% ds [dseries] data to use in estimation
%
% OUTPUTS
% none
%
% SPECIAL REQUIREMENTS
% dynare must be run with the option: json=parse
% Copyright (C) 2017 Dynare Team
%
% 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_ options_
%% Check input argument
assert(~isempty(ds) && isdseries(ds), 'The first argument must be a dseries');
%% Read 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
jsonmodel = loadjson(jsonfile);
jsonmodel = jsonmodel.model;
[lhs, rhs, lineno] = getEquationsByTags(jsonmodel);
%% Find parameters and variable names in equations and setup estimation matrices
M_exo_names_trim = cellstr(M_.exo_names);
M_endo_exo_names_trim = [cellstr(M_.endo_names); M_exo_names_trim];
M_param_names_trim = cellstr(M_.param_names);
regex = strjoin(M_endo_exo_names_trim(:,1), '|');
mathops = '[\+\*\^\-\/]';
params = cell(length(rhs),1);
vars = cell(length(rhs),1);
Y = [];
X = [];
startidxs = zeros(length(lhs), 1);
startdates = cell(length(lhs), 1);
enddates = cell(length(lhs), 1);
residnames = cell(length(lhs), 1);
pidxs = zeros(M_.param_nbr, 1);
pidx = 0;
vnamesall = {};
for i = 1:length(lhs)
rhs_ = strsplit(rhs{i}, {'+','-','*','/','^','log(','ln(','log10(','exp(','(',')','diff('});
rhs_(cellfun(@(x) all(isstrprop(x, 'digit')), rhs_)) = [];
vnames = setdiff(rhs_, M_param_names_trim);
if ~isempty(regexp(rhs{i}, ...
['(' strjoin(vnames, '\\(\\d+\\)|') '\\(\\d+\\))'], ...
'once'))
error(['sur1: you cannot have leads in equation on line ' ...
lineno{i} ': ' lhs{i} ' = ' rhs{i}]);
end
% Find parameters and associated variables
pnames = intersect(rhs_, M_param_names_trim);
vnames = cell(1, length(pnames));
xjdata = dseries;
for j = 1:length(pnames)
pidx = pidx + 1;
pidxs(pidx, 1) = find(strcmp(pnames{j}, M_param_names_trim));
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);
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('sur1: Shouldn''t arrive here');
end
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
residuals = intersect(rhs_, cellstr(M_.exo_names));
for j = 1:length(residuals)
if any(strcmp(residuals{j}, vnames))
residuals{j} = [];
end
end
idx = ~cellfun(@isempty, residuals);
assert(sum(idx) == 1, ['More than one residual in equation ' num2str(i)]);
residnames{i} = residuals{idx};
params{i} = pnames;
vars{i} = vnames;
ydata = eval(regexprep(lhs{i}, regex, 'ds.$&'));
fp = max(ydata.firstobservedperiod, xjdata.firstobservedperiod);
lp = min(ydata.lastobservedperiod, xjdata.lastobservedperiod);
startidxs(i) = length(Y) + 1;
startdates{i} = fp;
enddates{i} = lp;
Y(startidxs(i):startidxs(i)+lp-fp, 1) = ydata(fp:lp).data;
X(startidxs(i):startidxs(i)+lp-fp, end+1:end+size(xjdata(fp:lp).data,2)) = xjdata(fp:lp).data;
end
assert(size(X, 2) == M_.param_nbr, 'Not all parameters were used in model');
%% Force equations to have the same sample range
maxfp = max([startdates{:}]);
minlp = min([enddates{:}]);
nobs = minlp - maxfp;
newY = zeros(nobs*length(lhs), 1);
newX = zeros(nobs*length(lhs), columns(X));
lastidx = 1;
for i = 1:length(lhs)
if i == length(lhs)
yds = dseries(Y(startidxs(i):end), startdates{i});
xds = dseries(X(startidxs(i):end, :), startdates{i});
else
yds = dseries(Y(startidxs(i):startidxs(i+1)-1), startdates{i});
xds = dseries(X(startidxs(i):startidxs(i+1)-1, :), startdates{i});
end
newY(lastidx:lastidx + nobs, 1) = yds(maxfp:minlp).data;
newX(lastidx:lastidx + nobs, :) = xds(maxfp:minlp, :).data;
if i ~= length(lhs)
lastidx = lastidx + nobs + 1;
end
end
Y = newY;
X = newX;
%% Estimation
% Estimated Parameters
oo_.sur.dof = length(maxfp:minlp);
[q, r] = qr(X, 0);
xpxi = (r'*r)\eye(M_.param_nbr);
resid = Y - X * (r\(q'*Y));
resid = reshape(resid, oo_.sur.dof, length(lhs));
M_.Sigma_e = resid'*resid/oo_.sur.dof;
kLeye = kron(inv(M_.Sigma_e), eye(oo_.sur.dof));
[q, r] = qr(kLeye*X, 0);
oo_.sur.beta = r\(q'*kLeye*Y);
M_.params(pidxs, 1) = oo_.sur.beta;
% Yhat
oo_.sur.Yhat = X * oo_.sur.beta;
% Residuals
oo_.sur.resid = Y - oo_.sur.Yhat;
%% Calculate statistics
% Estimate for sigma^2
SS_res = oo_.sur.resid'*oo_.sur.resid;
oo_.sur.s2 = SS_res/oo_.sur.dof;
% R^2
ym = Y - mean(Y);
SS_tot = ym'*ym;
oo_.sur.R2 = 1 - SS_res/SS_tot;
% Adjusted R^2
oo_.sur.adjR2 = oo_.sur.R2 - (1 - oo_.sur.R2)*M_.param_nbr/(oo_.sur.dof - 1);
% Durbin-Watson
ediff = oo_.sur.resid(2:oo_.sur.dof) - oo_.sur.resid(1:oo_.sur.dof - 1);
oo_.sur.dw = (ediff'*ediff)/SS_res;
% Standard Error
oo_.sur.stderr = sqrt(oo_.sur.s2*diag(xpxi));
% T-Stat
oo_.sur.tstat = oo_.sur.beta./oo_.sur.stderr;
%% Print Output
if ~options_.noprint
preamble = {sprintf('Dependent Variable: %s', lhs{i}), ...
sprintf('No. Independent Variables: %d', M_.param_nbr), ...
sprintf('Observations: %d', oo_.sur.dof)};
afterward = {sprintf('R^2: %f', oo_.sur.R2), ...
sprintf('R^2 Adjusted: %f', oo_.sur.adjR2), ...
sprintf('s^2: %f', oo_.sur.s2), ...
sprintf('Durbin-Watson: %f', oo_.sur.dw)};
dyn_table('SUR Estimation', preamble, afterward, [vars{:}], ...
{'Coefficients','t-statistic','Std. Error'}, 4, ...
[oo_.sur.beta oo_.sur.tstat oo_.sur.stderr]);
end
end

View File

@ -1,196 +0,0 @@
function varargout = sur(ds, varargin)
%function varargout = sur(ds, varargin)
% Run a Seemingly Unrelated Regression on the provided equations
%
% INPUTS
% ds [dseries] data
%
% OUTPUTS
% varargout [cell array] contains the common work between sur and
% surgibbs
%
% SPECIAL REQUIREMENTS
% none
% Copyright (C) 2017 Dynare Team
%
% 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_
%% Check input
assert(nargin == 1 || nargin == 3, 'Incorrect number of arguments passed to sur');
jsonfile = [M_.fname '_original.json'];
if exist(jsonfile, 'file') ~= 2
error('Could not find %s! Please use the json option (See the Dynare invocation section in the reference manual).', jsonfile);
end
%% Get Equations
jsonmodel = loadjson(jsonfile);
jsonmodel = jsonmodel.model;
[lhs, rhs, lineno] = getEquationsByTags(jsonmodel, varargin{:});
m = length(lhs);
if m <= 1
error('SUR estimation requires the selection of at least two equations')
end
%% Construct regression matrices
Y = dseries();
Xi = cell(m, 1);
pnamesall = [];
vwlagsall = [];
for i = 1:m
Y = [Y ds{lhs{i}}];
rhs_ = strsplit(rhs{i}, {'+','-','*','/','^','log(','exp(','(',')'});
rhs_(cellfun(@(x) all(isstrprop(x, 'digit')), rhs_)) = [];
vnames = setdiff(rhs_, cellstr(M_.param_names));
regexprnoleads = cell2mat(strcat('(', vnames, {'\(\d+\))|'}));
if ~isempty(regexp(rhs{i}, regexprnoleads(1:end-1), 'match'))
error(['sur: you cannot have leads in equation on line ' ...
lineno{i} ': ' lhs{i} ' = ' rhs{i}]);
end
regexpr = cell2mat(strcat('(', vnames, {'\(-\d+\))|'}));
vwlags = regexp(rhs{i}, regexpr(1:end-1), 'match');
% Find parameters
pnames = cell(1, length(vwlags));
for j = 1:length(vwlags)
regexmatch = regexp(rhs{i}, ['(\w*\*?)?' strrep(strrep(vwlags{j}, '(', '\('), ')', '\)') '(\*?\w*)?'], 'match');
regexmatch = strsplit(regexmatch{:}, '*');
assert(length(regexmatch) == 2);
if strcmp(vwlags{j}, regexmatch{1})
pnames{j} = regexmatch{2};
else
pnames{j} = regexmatch{1};
end
end
pnamesall = [pnamesall pnames];
vwlagsall = [vwlagsall vwlags];
Xi{i} = cellfun(@eval, strcat('ds.', vwlags), 'UniformOutput', false);
end
fp = Y.firstobservedperiod;
lp = Y.lastobservedperiod;
for i = 1:m
X = dseries();
for j = 1:length(Xi{i})
X = [X dseries(Xi{i}{j}.data, Xi{i}{j}.dates, ['V' num2str(i) num2str(j)])];
end
Xi{i} = X;
fp = max(fp, X.firstobservedperiod);
lp = min(lp, X.lastobservedperiod);
end
Y = Y(fp:lp).data(:);
X = [];
for i = 1:m
Xi{i} = Xi{i}(fp:lp).data;
ind = size(X);
X(ind(1)+1:ind(1)+size(Xi{i}, 1), ind(2)+1:ind(2)+size(Xi{i},2)) = Xi{i};
end
%% Estimation
nobs = length(fp:lp);
nvars = size(X, 2);
[q, r] = qr(X, 0);
xpxi = (r'*r)\eye(nvars);
resid = Y - X * (r\(q'*Y));
resid = reshape(resid, nobs, m);
s2 = resid'*resid/nobs;
tmp = kron(inv(s2), eye(nobs));
beta = (X'*tmp*X)\X'*tmp*Y;
% if called from surgibbs, return common work
st = dbstack(1);
if strcmp(st(1).name, 'surgibbs')
varargout{1} = nobs;
varargout{2} = nvars;
varargout{3} = pnamesall;
varargout{4} = beta;
varargout{5} = X;
varargout{6} = Y;
varargout{7} = m;
return
end
oo_.sur.s2 = s2;
oo_.sur.beta = beta;
for j = 1:length(pnamesall)
M_.params(strmatch(pnamesall{j}, M_.param_names, 'exact')) = oo_.sur.beta(j);
end
% Yhat
oo_.sur.Yhat = X * oo_.sur.beta;
% Residuals
oo_.sur.resid = Y - oo_.sur.Yhat;
%% Calculate statistics
oo_.sur.dof = nobs;
% Estimate for sigma^2
SS_res = oo_.sur.resid'*oo_.sur.resid;
oo_.sur.s2 = SS_res/oo_.sur.dof;
% R^2
ym = Y - mean(Y);
SS_tot = ym'*ym;
oo_.sur.R2 = 1 - SS_res/SS_tot;
% Adjusted R^2
oo_.sur.adjR2 = oo_.sur.R2 - (1 - oo_.sur.R2)*nvars/(oo_.sur.dof-1);
% Durbin-Watson
ediff = oo_.sur.resid(2:nobs) - oo_.sur.resid(1:nobs-1);
oo_.sur.dw = (ediff'*ediff)/SS_res;
% Standard Error
oo_.sur.stderr = sqrt(oo_.sur.s2*diag(xpxi));
% T-Stat
oo_.sur.tstat = oo_.sur.beta./oo_.sur.stderr;
%% Print Output
title = sprintf('SUR Estimation');
if nargin == 1
title = [title sprintf(' of all equations')];
else
title = [title s(' [%s = {', varargin{1})];
for i = 1:length(varargin{2})
if i ~= 1
title = [title sprintf(', ')];
end
title = [title sprintf('%s', varargin{2}{i})];
end
title = [title sprintf('}]')];
end
preamble = {sprintf('Dependent Variable: %s', lhs{i}), ...
sprintf('No. Independent Variables: %d', nvars), ...
sprintf('Observations: %d', nobs)};
afterward = {sprintf('R^2: %f', oo_.sur.R2), ...
sprintf('R^2 Adjusted: %f', oo_.sur.adjR2), ...
sprintf('s^2: %f', oo_.sur.s2), ...
sprintf('Durbin-Watson: %f', oo_.sur.dw)};
dyn_table(title, preamble, afterward, vwlagsall, ...
{'Coefficients','t-statistic','Std. Error'}, 4, ...
[oo_.sur.beta oo_.sur.tstat oo_.sur.stderr]);
end

View File

@ -0,0 +1,151 @@
// --+ options: json=compute +--
/* REMARK
** ------
**
** You need to have the first line on top of the mod file. The options defined on this line are passed
** to the dynare command (you can add other options, separated by spaces or commas). The option defined
** here is mandatory for the decomposition. It forces Dynare to output another representation of the
** model in JSON file (additionaly to the matlab files) which is used here to manipulate the equations.
*/
var
U2_Q_YED
U2_G_YER
U2_STN
U2_ESTN
U2_EHIC
DE_Q_YED
DE_G_YER
DE_EHIC
;
varexo
res_U2_Q_YED
res_U2_G_YER
res_U2_STN
res_U2_ESTN
res_U2_EHIC
res_DE_Q_YED
res_DE_G_YER
res_DE_EHIC
;
parameters
u2_q_yed_ecm_u2_q_yed_L1
u2_q_yed_ecm_u2_stn_L1
u2_q_yed_u2_g_yer_L1
u2_q_yed_u2_stn_L1
u2_g_yer_ecm_u2_q_yed_L1
u2_g_yer_ecm_u2_stn_L1
u2_g_yer_u2_q_yed_L1
u2_g_yer_u2_g_yer_L1
u2_g_yer_u2_stn_L1
u2_stn_ecm_u2_q_yed_L1
u2_stn_ecm_u2_stn_L1
u2_stn_u2_q_yed_L1
u2_stn_u2_g_yer_L1
u2_estn_u2_estn_L1
u2_ehic_u2_ehic_L1
de_q_yed_ecm_de_q_yed_L1
de_q_yed_ecm_u2_stn_L1
de_q_yed_de_g_yer_L1
de_q_yed_u2_stn_L1
de_g_yer_ecm_de_q_yed_L1
de_g_yer_ecm_u2_stn_L1
de_g_yer_de_q_yed_L1
de_g_yer_de_g_yer_L1
de_g_yer_u2_stn_L1
de_ehic_de_ehic_L1
;
u2_q_yed_ecm_u2_q_yed_L1 = -0.82237516589315 ;
u2_q_yed_ecm_u2_stn_L1 = -0.323715338568976 ;
u2_q_yed_u2_g_yer_L1 = 0.0401361895021084 ;
u2_q_yed_u2_stn_L1 = 0.058397703958446 ;
u2_g_yer_ecm_u2_q_yed_L1 = 0.0189896046977421 ;
u2_g_yer_ecm_u2_stn_L1 = -0.109597659887432 ;
u2_g_yer_u2_q_yed_L1 = 0.0037667967632025 ;
u2_g_yer_u2_g_yer_L1 = 0.480506381923644 ;
u2_g_yer_u2_stn_L1 = -0.0722359286123494 ;
u2_stn_ecm_u2_q_yed_L1 = -0.0438500662608356 ;
u2_stn_ecm_u2_stn_L1 = -0.153283917138772 ;
u2_stn_u2_q_yed_L1 = 0.0328744983772825 ;
u2_stn_u2_g_yer_L1 = 0.292121949736756 ;
u2_estn_u2_estn_L1 = 1 ;
u2_ehic_u2_ehic_L1 = 1 ;
de_q_yed_ecm_de_q_yed_L1 = -0.822375165893149 ;
de_q_yed_ecm_u2_stn_L1 = -0.323715338568977 ;
de_q_yed_de_g_yer_L1 = 0.0401361895021082 ;
de_q_yed_u2_stn_L1 = 0.0583977039584461 ;
de_g_yer_ecm_de_q_yed_L1 = 0.0189896046977422 ;
de_g_yer_ecm_u2_stn_L1 = -0.109597659887433 ;
de_g_yer_de_q_yed_L1 = 0.00376679676320256;
de_g_yer_de_g_yer_L1 = 0.480506381923643 ;
de_g_yer_u2_stn_L1 = -0.0722359286123494 ;
de_ehic_de_ehic_L1 = 1 ;
model(linear);
diff(U2_Q_YED) = u2_q_yed_ecm_u2_q_yed_L1 * (U2_Q_YED(-1) - U2_EHIC(-1))
+ u2_q_yed_ecm_u2_stn_L1 * (U2_STN(-1) - U2_ESTN(-1))
+ u2_q_yed_u2_g_yer_L1 * diff(U2_G_YER(-1))
+ u2_q_yed_u2_stn_L1 * diff(U2_STN(-1))
+ res_U2_Q_YED ;
diff(U2_G_YER) = u2_g_yer_ecm_u2_q_yed_L1 * (U2_Q_YED(-1) - U2_EHIC(-1))
+ u2_g_yer_ecm_u2_stn_L1 * (U2_STN(-1) - U2_ESTN(-1))
+ u2_g_yer_u2_q_yed_L1 * diff(U2_Q_YED(-1))
+ u2_g_yer_u2_g_yer_L1 * diff(U2_G_YER(-1))
+ u2_g_yer_u2_stn_L1 * diff(U2_STN(-1))
+ res_U2_G_YER ;
diff(U2_STN) = u2_stn_ecm_u2_q_yed_L1 * (U2_Q_YED(-1) - U2_EHIC(-1))
+ u2_stn_ecm_u2_stn_L1 * (U2_STN(-1) - U2_ESTN(-1))
+ u2_stn_u2_q_yed_L1 * diff(U2_Q_YED(-1))
+ u2_stn_u2_g_yer_L1 * diff(U2_G_YER(-1))
+ res_U2_STN ;
U2_ESTN = u2_estn_u2_estn_L1 * U2_ESTN(-1)
+ res_U2_ESTN ;
U2_EHIC = u2_ehic_u2_ehic_L1 * U2_EHIC(-1)
+ res_U2_EHIC ;
diff(DE_Q_YED) = de_q_yed_ecm_de_q_yed_L1 * (DE_Q_YED(-1) - DE_EHIC(-1))
+ de_q_yed_ecm_u2_stn_L1 * (U2_STN(-1) - U2_ESTN(-1))
+ de_q_yed_de_g_yer_L1 * diff(DE_G_YER(-1))
+ de_q_yed_u2_stn_L1 * diff(U2_STN(-1))
+ res_DE_Q_YED ;
diff(DE_G_YER) = de_g_yer_ecm_de_q_yed_L1 * (DE_Q_YED(-1) - DE_EHIC(-1))
+ de_g_yer_ecm_u2_stn_L1 * (U2_STN(-1) - U2_ESTN(-1))
+ de_g_yer_de_q_yed_L1 * diff(DE_Q_YED(-1))
+ de_g_yer_de_g_yer_L1 * diff(DE_G_YER(-1))
+ de_g_yer_u2_stn_L1 * diff(U2_STN(-1))
+ res_DE_G_YER ;
DE_EHIC = de_ehic_de_ehic_L1 * DE_EHIC(-1)
+ res_DE_EHIC ;
end;
shocks;
var res_U2_Q_YED = 0.005;
var res_U2_G_YER = 0.005;
var res_U2_STN = 0.005;
var res_U2_ESTN = 0.005;
var res_U2_EHIC = 0.005;
var res_DE_Q_YED = 0.005;
var res_DE_G_YER = 0.005;
var res_DE_EHIC = 0.005;
end;

View File

@ -0,0 +1,36 @@
close all
dynare panel_var_diff_NB_simulation_test.mod;
NSIMS = 1000;
options_.noprint = 1;
calibrated_values = M_.params;
Sigma_e = M_.Sigma_e;
options_.bnlms.set_dynare_seed_to_default = false;
M_endo_names_trim = cellstr(M_.endo_names);
nparampool = length(M_.params);
BETA = zeros(NSIMS, nparampool);
for i=1:NSIMS
i
firstobs = rand(3, length(M_endo_names_trim));
M_.params = calibrated_values;
M_.Sigma_e = Sigma_e;
simdata = simul_backward_model(dseries(firstobs, dates('1995Q1'), M_endo_names_trim), 10000);
simdata = simdata(simdata.dates(5001:6000));
sur(simdata);
BETA(i, :) = M_.params';
end
mean(BETA)' - calibrated_values
for i=1:nparampool
figure
hold on
title(strrep(M_.param_names(i,:), '_', '\_'));
histogram(BETA(:,i),50);
line([calibrated_values(i) calibrated_values(i)], [0 NSIMS/10], 'LineWidth', 2, 'Color', 'r');
hold off
end