get_ar_ec_matrices: changes for introduction of trend_component_model statement

time-shift
Houtan Bastani 2018-08-20 10:24:41 +02:00
parent 7c541b3bba
commit b27267da9b
1 changed files with 55 additions and 33 deletions

View File

@ -1,17 +1,23 @@
function get_ar_ec_matrices(var_model_name) function get_ar_ec_matrices(model_name, model_type)
%function get_ar_ec_matrices(var_model_name) %function get_ar_ec_matrices(model_name, model_type)
% %
% Returns the autoregressive matrix associated with the VAR specified by % Returns the autoregressive matrix associated with the auxiliary model specified by
% var_model_name. Output is stored in cellarray oo_.var.(var_model_name).ar, % model_name. Output is stored in cellarray oo_.(model_type).(model_name).ar,
% with oo_.var.(var_model_name).ar(:,:,i) being the AR matrix at time t-i. Each % with oo_.(model_type).(model_name).ar(:,:,i) being the AR matrix at time t-i. Each
% AR matrix is stored with rows and columns organized by the ordering of the % AR matrix is stored with rows and columns organized by the ordering of the
% equation tags found in M_.var.(var_model_name).eqtags. % equation tags found in M_.(model_type).(model_name).eqtags.
% oo_.var.(var_model_name).ec contains those entries that are not % oo_.(model_type).(model_name).ec contains those entries that are not
% autoregressive. % autoregressive.
% %
% INPUTS % INPUTS
% %
% var_model_name [string] the name of the VAR model % model_name [string] the name of the auxiliary model
% model_type [string] the type of the auxiliary model ('var' or
% 'trend_component'. If not passed, the
% value is set by the function; if a 'var'
% subfield is found, that is used. Otherwise
% 'trend_component' is used (if it exists as
% a subfield of M_.
% %
% OUTPUTS % OUTPUTS
% %
@ -36,13 +42,29 @@ function get_ar_ec_matrices(var_model_name)
global M_ oo_ global M_ oo_
%% Check inputs and initialize output %% Check inputs
assert(nargin == 1, 'This function requires one argument'); assert(nargin <= 2, 'This function requires one or two arguments');
assert(~isempty(var_model_name) && ischar(var_model_name), ... assert(~isempty(model_name) && ischar(model_name), ...
'The sole argument must be a non-empty string'); 'The first argument must be a non-empty string');
if ~isfield(M_.var, var_model_name)
error(['Could not find ' var_model_name ' in M_.var. ' ... if nargin < 2
'First declare it via the var_model statement.']); model_type = 'var';
if ~(isfield(M_, model_type) && isfield(M_.(model_type), model_name))
model_type = 'trend_component';
if ~(isfield(M_, model_type) && isfield(M_.(model_type), model_name))
error(['Could not find ' model_name ' in M_.var or ' ...
'M_.trend_component. First declare it via the var_model ' ...
'or trend_component_model statement.']);
end
end
else
assert(~isempty(model_type) && ischar(model_type), ...
'If provided, the second argument must be a non-empty string');
if ~(isfield(M_, model_type) && isfield(M_.(model_type), model_name))
error(['Could not find M_.' model_type '.' model_name ...
'. First declare it via the var_model or ' ...
'trend_component_model statement.']);
end
end end
%% Call Dynamic Function %% Call Dynamic Function
@ -54,10 +76,10 @@ end
1); 1);
% Choose rows of Jacobian based on equation tags % Choose rows of Jacobian based on equation tags
ntags = length(M_.var.(var_model_name).eqtags); ntags = length(M_.(model_type).(model_name).eqtags);
g1rows = zeros(ntags, 1); g1rows = zeros(ntags, 1);
for i = 1:ntags for i = 1:ntags
idxs = strcmp(M_.equations_tags(:, 3), M_.var.(var_model_name).eqtags{i}); idxs = strcmp(M_.equations_tags(:, 3), M_.(model_type).(model_name).eqtags{i});
if any(idxs) if any(idxs)
g1rows(i) = M_.equations_tags{idxs, 1}; g1rows(i) = M_.equations_tags{idxs, 1};
end end
@ -68,20 +90,20 @@ g1 = -1 * g1(g1rows, :);
if rows(M_.lead_lag_incidence) == 3 if rows(M_.lead_lag_incidence) == 3
idxs = M_.lead_lag_incidence(3, M_.lead_lag_incidence(3, :) ~= 0); idxs = M_.lead_lag_incidence(3, M_.lead_lag_incidence(3, :) ~= 0);
assert(~any(any(g1(g1rows, idxs))), ... assert(~any(any(g1(g1rows, idxs))), ...
['You cannot have leads in the equations specified by ' strjoin(M_.var.(var_model_name).eqtags, ',')]); ['You cannot have leads in the equations specified by ' strjoin(M_.(model_type).(model_name).eqtags, ',')]);
end end
%% Organize AR & EC matrices %% Organize AR & EC matrices
assert(length(M_.var.(var_model_name).lhs) == rows(g1)); assert(length(M_.(model_type).(model_name).lhs) == rows(g1));
% Find RHS vars for AR & EC matrices % Find RHS vars for AR & EC matrices
ecRhsVars = []; ecRhsVars = [];
lhs = M_.var.(var_model_name).lhs; lhs = M_.(model_type).(model_name).lhs;
rhsvars = cell(length(lhs), 1); rhsvars = cell(length(lhs), 1);
for i = 1:length(M_.var.(var_model_name).rhs.vars_at_eq) for i = 1:length(M_.(model_type).(model_name).rhs.vars_at_eq)
vars = M_.var.(var_model_name).rhs.vars_at_eq{i}.var; vars = M_.(model_type).(model_name).rhs.vars_at_eq{i}.var;
rhsvars{i}.vars = vars; rhsvars{i}.vars = vars;
rhsvars{i}.lags = M_.var.(var_model_name).rhs.vars_at_eq{i}.lag; rhsvars{i}.lags = M_.(model_type).(model_name).rhs.vars_at_eq{i}.lag;
rhsvars{i}.arRhsIdxs = []; rhsvars{i}.arRhsIdxs = [];
rhsvars{i}.ecRhsIdxs = []; rhsvars{i}.ecRhsIdxs = [];
for j = 1:length(vars) for j = 1:length(vars)
@ -113,10 +135,10 @@ for i = 1:length(M_.var.(var_model_name).rhs.vars_at_eq)
end end
% Initialize matrices % Initialize matrices
oo_.var.(var_model_name).ar = zeros(length(lhs), length(lhs), M_.var.(var_model_name).max_lag); oo_.(model_type).(model_name).ar = zeros(length(lhs), length(lhs), M_.(model_type).(model_name).max_lag);
oo_.var.(var_model_name).ec = zeros(length(lhs), length(ecRhsVars), M_.var.(var_model_name).max_lag); oo_.(model_type).(model_name).ec = zeros(length(lhs), length(ecRhsVars), M_.(model_type).(model_name).max_lag);
oo_.var.(var_model_name).ar_idx = lhs; oo_.(model_type).(model_name).ar_idx = lhs;
oo_.var.(var_model_name).ec_idx = ecRhsVars; oo_.(model_type).(model_name).ec_idx = ecRhsVars;
% Fill matrices % Fill matrices
for i = 1:length(lhs) for i = 1:length(lhs)
@ -131,16 +153,16 @@ for i = 1:length(lhs)
if rhsvars{i}.arRhsIdxs(j) > 0 if rhsvars{i}.arRhsIdxs(j) > 0
% Fill AR % Fill AR
[lag, ndiffs] = findLagForVar(var, -rhsvars{i}.lags(j), 0, lhs); [lag, ndiffs] = findLagForVar(var, -rhsvars{i}.lags(j), 0, lhs);
oo_.var.(var_model_name).ar(i, rhsvars{i}.arRhsIdxs(j), lag) = ... oo_.(model_type).(model_name).ar(i, rhsvars{i}.arRhsIdxs(j), lag) = ...
oo_.var.(var_model_name).ar(i, rhsvars{i}.arRhsIdxs(j), lag) + g1(i, g1col); oo_.(model_type).(model_name).ar(i, rhsvars{i}.arRhsIdxs(j), lag) + g1(i, g1col);
elseif rhsvars{i}.ecRhsIdxs(j) > 0 elseif rhsvars{i}.ecRhsIdxs(j) > 0
% Fill EC % Fill EC
[lag, ndiffs] = findLagForVar(var, -rhsvars{i}.lags(j), 0, ecRhsVars); [lag, ndiffs] = findLagForVar(var, -rhsvars{i}.lags(j), 0, ecRhsVars);
if size(oo_.var.(var_model_name).ec, 3) < lag if size(oo_.(model_type).(model_name).ec, 3) < lag
oo_.var.(var_model_name).ec(i, rhsvars{i}.ecRhsIdxs(j), lag) = 0; oo_.(model_type).(model_name).ec(i, rhsvars{i}.ecRhsIdxs(j), lag) = 0;
end end
oo_.var.(var_model_name).ec(i, rhsvars{i}.ecRhsIdxs(j), lag) = ... oo_.(model_type).(model_name).ec(i, rhsvars{i}.ecRhsIdxs(j), lag) = ...
oo_.var.(var_model_name).ec(i, rhsvars{i}.ecRhsIdxs(j), lag) + g1(i, g1col); oo_.(model_type).(model_name).ec(i, rhsvars{i}.ecRhsIdxs(j), lag) + g1(i, g1col);
else else
error('Shouldn''t arrive here'); error('Shouldn''t arrive here');
end end