parse residual information

time-shift
Houtan Bastani 2019-01-14 15:43:29 +01:00
parent 72081e640e
commit e4dc7c403e
No known key found for this signature in database
GPG Key ID: 000094FB955BE169
3 changed files with 25 additions and 28 deletions

View File

@ -1,5 +1,5 @@
function [Y, lhssub, X, startdates, enddates] = common_parsing(ds, ast, jsonmodel, overlapping_dates)
%function [Y, lhssub, X, startdates, enddates] = common_parsing(ds, ast, jsonmodel, overlapping_dates)
function [Y, lhssub, X, startdates, enddates, residnames] = common_parsing(ds, ast, jsonmodel, overlapping_dates)
%function [Y, lhssub, X, startdates, enddates, residnames] = common_parsing(ds, ast, jsonmodel, overlapping_dates)
%
% Code common to sur.m and pooled_ols.m
%
@ -20,14 +20,6 @@ function [Y, lhssub, X, startdates, enddates] = common_parsing(ds, ast, jsonmode
% startidxs [vector] rows corresponding to each
% equation's observations
% residnames [cell array] name of residual in each equation
% pbeta [cell array] parameter names corresponding to
% columns of X
% vars [cell array] variable names corresponding to
% parameters
% surpidxs [vector] indexes in M_.params associated with
% columns of X
% surconstrainedparams [vector] indexes of parameters that were
% constrained
%
% SPECIAL REQUIREMENTS
% none
@ -54,6 +46,7 @@ function [Y, lhssub, X, startdates, enddates] = common_parsing(ds, ast, jsonmode
Y = cell(length(ast), 1);
lhssub = cell(length(ast), 1);
X = cell(length(ast), 1);
residnames = cell(length(ast), 1);
startdates = cell(length(ast), 1);
enddates = cell(length(ast), 1);
@ -61,7 +54,7 @@ enddates = cell(length(ast), 1);
neqs = length(ast);
for i = 1:neqs
%% Parse equation i
[Y{i}, lhssub{i}, X{i}] = parse_ols_style_equation(ds, ast{i});
[Y{i}, lhssub{i}, X{i}, residnames{i}] = parse_ols_style_equation(ds, ast{i});
%% Set start and end dates
[startdates{i}, enddates{i}] = get_ols_start_end_dates(Y{i}, lhssub{i}, X{i}, jsonmodel{i});

View File

@ -1,4 +1,4 @@
function [Y, lhssub, X] = parse_ols_style_equation(ds, ast)
function [Y, lhssub, X, residual] = parse_ols_style_equation(ds, ast)
%function X = parse_ols_style_equation()
% Run OLS on chosen model equations; unlike olseqs, allow for time t
% endogenous variables on LHS
@ -11,6 +11,7 @@ function [Y, lhssub, X] = parse_ols_style_equation(ds, ast)
% Y [dseries] LHS of the equation (with lhssub subtracted)
% lhssub [dseries] RHS subtracted from LHS
% X [dseries] RHS of the equation
% residual [string] name of residual in equation
%
% SPECIAL REQUIREMENTS
% none

View File

@ -84,21 +84,22 @@ if strcmp(st(1).name, 'pooled_fgls')
end
%% Find parameters and variable names in every equation & Setup estimation matrices
[Y, ~, X] = common_parsing(ds, ast, jsonmodel, overlapping_dates);
[Y, ~, X, ~, ~, residnames] = common_parsing(ds, ast, jsonmodel, overlapping_dates);
clear ast jsonmodel;
nobs = Y{1}.nobs;
nobs = zeros(length(Y), 1);
nobs(1) = Y{1}.nobs;
fp = Y{1}.firstobservedperiod;
for i = 2:length(Y)
if Y{i}.firstobservedperiod < fp
fp = Y{i}.firstobservedperiod;
end
nobs = nobs + Y{i}.nobs;
nobs(i) = Y{i}.nobs;
end
[Y, X] = put_in_sur_form(Y, X);
%% Save
oo_.(save_structure_name).sample_range = fp:fp+nobs;
%oo_.(save_structure_name).residnames = residnames;
oo_.(save_structure_name).sample_range = fp:fp+sum(nobs);
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;
@ -136,17 +137,19 @@ for i = 1:length(idxs)
M_.params(strcmp(M_.param_names, names{i})) = values(i);
end
% 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
residuals = Y.data - X.data * oo_.(save_structure_name).beta;
for i = 1:neqs
if i == 1
oo_.(save_structure_name).resid.(residnames{i}) = residuals(1:nobs(1));
elseif i == neqs
oo_.(save_structure_name).resid.(residnames{i}) = residuals(sum(nobs(1:i-1))+1:end);
else
oo_.(save_structure_name).resid.(residnames{i}) = residuals(sum(nobs(1:i-1))+1:sum(nobs(1:i)));
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
end
function ast = replace_parameters(ast, country_name, regexcountries, param_regex)