Adapted matlab code to new TREND_COMPONENT auxiliary model.

time-shift
Stéphane Adjemia (Scylla) 2018-08-21 21:06:07 +02:00
parent 38b2593e31
commit 4a0b3ffe7e
4 changed files with 195 additions and 185 deletions

View File

@ -44,21 +44,21 @@ end
pacmodel = DynareModel.pac.(pacname);
% Get the name of the associated VAR model and test its existence.
if ~isfield(DynareModel.var, pacmodel.var_model_name)
error('Unknown VAR (%s) in PAC model (%s)!', pacmodel.var_model_name, pacname)
if ~isfield(DynareModel.(pacmodel.auxiliary_model_type), pacmodel.auxiliary_model_name)
error('Unknown auxiliary model (%s) in PAC model (%s)!', pacmodel.auxiliary_model_name, pacname)
end
varmodel = DynareModel.var.(pacmodel.var_model_name);
varmodel = DynareModel.(pacmodel.auxiliary_model_type).(pacmodel.auxiliary_model_name);
% Check that we have the values of the VAR matrices.
if ~isfield(DynareOutput.var, pacmodel.var_model_name)
error('VAR model %s has to be estimated first!', pacmodel.var_model_name)
if ~isfield(DynareOutput.(pacmodel.auxiliary_model_type), pacmodel.auxiliary_model_name)
error('Auxiliary model %s has to be estimated first!', pacmodel.auxiliary_model_name)
end
varcalib = DynareOutput.var.(pacmodel.var_model_name);
varcalib = DynareOutput.(pacmodel.auxiliary_model_type).(pacmodel.auxiliary_model_name);
if ~isfield(varcalib, 'CompanionMatrix') || any(isnan(varcalib.CompanionMatrix(:)))
error('VAR model %s has to be estimated first.', pacmodel.var_model_name)
error('Auxiliary model %s has to be estimated first.', pacmodel.auxiliary_model_name)
end
% Build the vector of PAC parameters (ECM parameter + autoregressive parameters).
@ -86,12 +86,18 @@ if isempty(id)
end
end
if varmodel.nonstationary(id)
idns = id;
ids = [];
if isequal(pacmodel.auxiliary_model_type, 'var')
if varmodel.nonstationary(id)
idns = id;
ids = [];
else
idns = [];
ids = id;
end
else
idns = [];
ids = id;
% Trend component model is assumed.
ids = [];
idns = id;
end
% Get the value of the discount factor.

43
matlab/+pac/initialize.m Normal file
View File

@ -0,0 +1,43 @@
function initialize(pacmodel)
% Initialization of a PAC model.
%
% INPUTS
% - pacmodel [string] Name of the pac model.
%
% OUTPUTS
% None
% Copyright (C) 2018 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_
auxiliary_model_name = M_.pac.(pacmodel).auxiliary_model_name;
if isfield(M_, 'var') && isfield(M_.var, auxiliary_model_name)
auxiliary_model_type = 'var';
elseif isfield(M_, 'trend_component') && isfield(M_.trend_component, auxiliary_model_name)
auxiliary_model_type = 'trend_component';
else
error('Unknown type of auxiliary model.')
end
M_.pac.(pacmodel).auxiliary_model_type = auxiliary_model_type;
get_companion_matrix(auxiliary_model_name, auxiliary_model_type);

View File

@ -1,11 +1,11 @@
function get_companion_matrix(var_model_name, pac_model_name)
%function get_companion_matrix(var_model_name)
function get_companion_matrix(auxiliary_model_name, auxiliary_model_type)
% Gets the companion matrix associated with the var specified by
% var_model_name. Output stored in cellarray oo_.var.(var_model_name).H.
% Gets the companion VAR representation of a PAC auxiliary model.
% Depending on the nature of this auxiliary model the output is
% saved in oo_.{var,trend_component}.(auxiliary_model_name).H
%
% INPUTS
% - var_model_name [string] the name of the VAR model
% - auxiliary_model_name [string] the name of the auxiliary model
%
% OUTPUTS
% - None
@ -29,185 +29,146 @@ function get_companion_matrix(var_model_name, pac_model_name)
global oo_ M_
get_ar_ec_matrices(var_model_name);
% Get the number of lags
p = size(oo_.var.(var_model_name).ar, 3);
% Get the number of variables
n = length(oo_.var.(var_model_name).ar(:,:,1));
% If not used in a PAC equation
if nargin<2
oo_.var.(var_model_name).CompanionMatrix = zeros(n*p);
oo_.var.(var_model_name).CompanionMatrix(1:n,1:n) = oo_.var.(var_model_name).ar(:,:,1);
if p>1
for i=2:p
oo_.var.(var_model_name).CompanionMatrix(1:n,(i-1)*n+(1:n)) = oo_.var.(var_model_name).ar(:,:,i);
oo_.var.(var_model_name).CompanionMatrix((i-1)*n+(1:n),(i-2)*n+(1:n)) = eye(n);
end
if isfield(M_, 'var') && isfield(M_.var, auxiliary_model_name)
auxiliary_model_type = 'var';
elseif isfield(M_, 'trend_component') && isfield(M_.trend_component, auxiliary_model_name)
auxiliary_model_type = 'trend_component';
else
error('Unknown type of auxiliary model.')
end
M_.var.(var_model_name).list_of_variables_in_companion_var = M_.endo_names(M_.var.(var_model_name).lhs);
return
end
get_ar_ec_matrices(auxiliary_model_name, auxiliary_model_type);
if all(~oo_.var.(var_model_name).ec(:))
% The auxiliary model is a VAR model.
M_.pac.(pac_model_name).auxmodel = 'var';
if isempty(M_.pac.(pac_model_name).undiff_eqtags)
% Build the companion matrix (standard VAR)
oo_.var.(var_model_name).CompanionMatrix = zeros(n*p);
oo_.var.(var_model_name).CompanionMatrix(1:n,1:n) = oo_.var.(var_model_name).ar(:,:,1);
if p>1
for i=2:p
oo_.var.(var_model_name).CompanionMatrix(1:n,(i-1)*n+(1:n)) = oo_.var.(var_model_name).ar(:,:,i);
oo_.var.(var_model_name).CompanionMatrix((i-1)*n+(1:n),(i-2)*n+(1:n)) = eye(n);
end
end
M_.var.(var_model_name).list_of_variables_in_companion_var = M_.endo_names(M_.var.(var_model_name).lhs);
else
error('You should not use undiff option in this model!')
end
else
% The auxiliary model is a VECM model.
M_.pac.(pac_model_name).auxmodel = 'vecm';
if ~isempty(M_.pac.(pac_model_name).undiff_eqtags)
% REMARK It is assumed that the equations with undiff option are the
% ECM equations. By complementarity, the other equations are
% the trends appearing in the error correction terms. We
% assume that the model can be cast in the following form:
%
% Δ Xₜ = A₀ (Xₜ₋₁ - Zₜ₋₁) + Σᵢ₌₁ᵖ Aᵢ Δ Xₜ₋ᵢ + ϵₜ
%
% Zₜ = Zₜ₋₁ + ηₜ
%
% We first recast the equation into this representation, and
% we rewrite the model in levels (we integrate the first set
% of equations) to rewrite the model as a VAR(1) model. Let
% Yₜ = [Xₜ; Zₜ] be the vertical concatenation of vectors
% Xₜ (variables with EC) and Zₜ (trends). We have
%
% Yₜ = Σᵢ₌₁ᵖ⁺¹ Bᵢ Yₜ₋ᵢ + [εₜ; ηₜ]
%
% with
%
% B₁ = [I+Λ+A₁, -Λ; 0, I]
%
% Bᵢ = [Aᵢ-Aᵢ₋₁, 0; 0, 0] for i = 2,…, p
% and
% Bₚ₊₁ = -[Aₚ, 0; 0, 0]
%
% where the dimensions of I and 0 matrices can easily be
% deduced from the number of EC and trend equations.
%
% Get the indices of the equations with error correction terms.
m = length(M_.pac.(pac_model_name).undiff_eqtags);
q = length(M_.var.(var_model_name).eqn)-m;
ecm_eqnums = zeros(m, 1);
ecm_eqnums_in_auxiliary_model = zeros(m, 1);
trend_eqnums = zeros(q, 1);
trend_eqnums_in_auxiliary_model = zeros(q, 1);
% EC equations in the order of M_.pac.(pac_model_name).undiff_eqtags{i}
ecm = zeros(m, 1);
for i = 1:m
ecm(i) = get_equation_number_by_tag(M_.pac.(pac_model_name).undiff_eqtags{i});
end
% Trend equations
trends = setdiff(M_.var.(var_model_name).eqn(:), ecm);
i1 = 1;
i2 = 1;
for i=1:m+q
% Only EC or trend equations are allowed in this model.
if ismember(M_.var.(var_model_name).eqn(i), ecm)
ecm_eqnums(i1) = M_.var.(var_model_name).eqn(i);
ecm_eqnums_in_auxiliary_model(i1) = i;
i1 = i1 + 1;
else
trend_eqnums(i2) = M_.var.(var_model_name).eqn(i);
trend_eqnums_in_auxiliary_model(i2) = i;
i2 = i2 + 1;
end
end
% Check that the lhs of candidate ecm equations are at least first differences.
difference_orders_in_error_correction_eq = zeros(m, 1);
for i=1:m
difference_orders_in_error_correction_eq(i) = get_difference_order(M_.var.(var_model_name).lhs(ecm_eqnums_in_auxiliary_model(i)));
end
if any(~difference_orders_in_error_correction_eq)
error('Model %s is not a VECM model! LHS variables should be in difference', var_model_name)
end
% Get the trend variables indices (lhs variables in trend equations).
[~, id_trend_in_var, ~] = intersect(M_.var.(var_model_name).eqn, trend_eqnums);
trend_variables = reshape(M_.var.(var_model_name).lhs(id_trend_in_var), q, 1);
% Get the rhs variables in trend equations.
trend_autoregressive_variables = zeros(q, 1);
for i=1:q
% Check that there is only one variable on the rhs and update trend_autoregressive_variables.
v = M_.var.(var_model_name).rhs.vars_at_eq{id_trend_in_var(i)}.var;
if ~(length(v)==1)
error('A trend equation (%s) must have only one variable on the RHS!', M_.var.(var_model_name).eqtags{trend_eqnums(i)})
end
trend_autoregressive_variables(i) = v;
% Check that the variables on lhs and rhs have the same difference orders.
if get_difference_order(trend_variables(i))~=get_difference_order(trend_autoregressive_variables(i))
error('In a trend equation (%s) LHS and RHS variables must have the same difference orders!', M_.var.(var_model_name).eqtags{trend_eqnums(i)})
end
% Check that the trend equation is autoregressive.
if isdiff(v)
if ~M_.aux_vars(get_aux_variable_id(v)).type==9
error('In a trend equation (%s) RHS variable must be lagged LHS variable!', M_.var.(var_model_name).eqtags{trend_eqnums(i)})
else
if M_.aux_vars(get_aux_variable_id(v)).orig_index~=trend_variables(i)
error('In a trend equation (%s) RHS variable must be lagged LHS variable!', M_.var.(var_model_name).eqtags{trend_eqnums(i)})
end
end
else
if get_aux_variable_id(v) && M_.aux_vars(get_aux_variable_id(v)).endo_index~=trend_variables(i)
error('In a trend equation (%s) RHS variable must be lagged LHS variable!', M_.var.(var_model_name).eqtags{trend_eqnums(i)})
end
end
end
% Get the EC matrix (the EC term is assumend to be in t-1).
%
% TODO: Check that the EC term is the difference between the
% endogenous variable and the trend variable.
%
A0 = oo_.var.(var_model_name).ec(ecm_eqnums_in_auxiliary_model,:,1);
% Get the AR matrices.
AR = oo_.var.(var_model_name).ar(ecm_eqnums_in_auxiliary_model,ecm_eqnums_in_auxiliary_model,:);
% Build B matrices (VAR in levels)
B(ecm_eqnums_in_auxiliary_model,ecm_eqnums_in_auxiliary_model,1) = eye(m)+A0+AR(:,:,1);
B(ecm_eqnums_in_auxiliary_model,trend_eqnums_in_auxiliary_model) = -A0;
B(trend_eqnums_in_auxiliary_model,trend_eqnums_in_auxiliary_model) = eye(q);
% Get the number of lags
p = size(oo_.(auxiliary_model_type).(auxiliary_model_name).ar, 3);
% Get the number of variables
n = length(oo_.(auxiliary_model_type).(auxiliary_model_name).ar(:,:,1));
switch auxiliary_model_type
case 'var'
oo_.var.(auxiliary_model_name).CompanionMatrix = zeros(n*p);
oo_.var.(auxiliary_model_name).CompanionMatrix(1:n,1:n) = oo_.var.(auxiliary_model_name).ar(:,:,1);
if p>1
for i=2:p
B(ecm_eqnums_in_auxiliary_model,ecm_eqnums_in_auxiliary_model,i) = AR(:,:,i)-AR(:,:,i-1);
oo_.var.(auxiliary_model_name).CompanionMatrix(1:n,(i-1)*n+(1:n)) = oo_.var.(auxiliary_model_name).ar(:,:,i);
oo_.var.(auxiliary_model_name).CompanionMatrix((i-1)*n+(1:n),(i-2)*n+(1:n)) = eye(n);
end
B(ecm_eqnums_in_auxiliary_model,ecm_eqnums_in_auxiliary_model,p+1) = -AR(:,:,p);
% Write Companion matrix
oo_.var.(var_model_name).CompanionMatrix = zeros(size(B, 1)*size(B, 3));
for i=1:p
oo_.var.(var_model_name).CompanionMatrix(1:n, (i-1)*n+(1:n)) = B(:,:,i);
oo_.var.(var_model_name).CompanionMatrix(i*n+(1:n),(i-1)*n+(1:n)) = eye(n);
end
M_.var.(auxiliary_model_name).list_of_variables_in_companion_var = M_.endo_names(M_.var.(auxiliary_model_name).lhs);
case 'trend_component'
% Get number of trends.
q = sum(M_.trend_component.(auxiliary_model_name).trends);
% Get the number of equations with error correction.
m = n-q;
% Get the indices of trend and EC equations in the auxiliary model.
trend_eqnums_in_auxiliary_model = find(M_.trend_component.(auxiliary_model_name).trends);
ecm_eqnums_in_auxiliary_model = find(~M_.trend_component.(auxiliary_model_name).trends);
% Get the indices of trend equations in model.
trend_eqnums = M_.trend_component.(auxiliary_model_name).trend_eqn;
% REMARK It is assumed that the non trend equations are the error correction
% equations. We assume that the model can be cast in the following form:
%
% Δ Xₜ₋₁ = A₀ (Xₜ₋₁ - Zₜ₋₁) + Σᵢ₌₁ᵖ Aᵢ Δ Xₜ₋ᵢ + ϵₜ
%
% Zₜ = Zₜ₋₁ + ηₜ
%
% We first recast the equation into this representation, and
% we rewrite the model in levels (we integrate the first set
% of equations) to rewrite the model as a VAR(1) model. Let
% Yₜ = [Xₜ; Zₜ] be the vertical concatenation of vectors
% Xₜ (variables with EC) and Zₜ (trends). We have
%
% Yₜ = Σᵢ₌₁ᵖ⁺¹ Bᵢ Yₜ₋ᵢ + [εₜ; ηₜ]
%
% with
%
% B₁ = [I+Λ+A₁, -Λ; 0, I]
%
% Bᵢ = [Aᵢ-Aᵢ₋₁, 0; 0, 0] for i = 2,…, p
% and
% Bₚ₊₁ = -[Aₚ, 0; 0, 0]
%
% where the dimensions of I and 0 matrices can easily be
% deduced from the number of EC and trend equations.
% Check that the lhs of candidate ecm equations are at least first differences.
difference_orders_in_error_correction_eq = zeros(m, 1);
for i=1:m
difference_orders_in_error_correction_eq(i) = get_difference_order(M_.trend_component.(auxiliary_model_name).lhs(ecm_eqnums_in_auxiliary_model(i)));
end
if any(~difference_orders_in_error_correction_eq)
error('Model %s is not a Trend component model! LHS variables should be in difference', auxiliary_model_name)
end
% Get the trend variables indices (lhs variables in trend equations).
[~, id_trend_in_var, ~] = intersect(M_.trend_component.(auxiliary_model_name).eqn, trend_eqnums);
trend_variables = reshape(M_.trend_component.(auxiliary_model_name).lhs(id_trend_in_var), q, 1);
% Get the rhs variables in trend equations.
trend_autoregressive_variables = zeros(q, 1);
for i=1:q
% Check that there is only one variable on the rhs and update trend_autoregressive_variables.
v = M_.trend_component.(auxiliary_model_name).rhs.vars_at_eq{id_trend_in_var(i)}.var;
if length(v)~=1
error('A trend equation (%s) must have only one variable on the RHS!', M_.trend_component.(auxiliary_model_name).eqtags{trend_eqnums(i)})
end
oo_.var.(var_model_name).CompanionMatrix(1:n, p*n+(1:n)) = B(:,:,p+1);
M_.var.(var_model_name).list_of_variables_in_companion_var = M_.endo_names(M_.var.(var_model_name).lhs);
variables_rewritten_in_levels = M_.var.(var_model_name).list_of_variables_in_companion_var(ecm_eqnums_in_auxiliary_model);
for i=1:m
id = get_aux_variable_id(variables_rewritten_in_levels{i});
if id
auxinfo = M_.aux_vars(id);
if auxinfo.type==8
M_.var.(var_model_name).list_of_variables_in_companion_var(ecm_eqnums_in_auxiliary_model(i)) = ...
{M_.endo_names{auxinfo.orig_index}};
else
error('This is a bug. Please contact the Dynare Team.')
trend_autoregressive_variables(i) = v;
% Check that the variables on lhs and rhs have the same difference orders.
if get_difference_order(trend_variables(i))~=get_difference_order(trend_autoregressive_variables(i))
error('In a trend equation (%s) LHS and RHS variables must have the same difference orders!', M_.trend_component.(auxiliary_model_name).eqtags{trend_eqnums(i)})
end
% Check that the trend equation is autoregressive.
if isdiff(v)
if ~M_.aux_vars(get_aux_variable_id(v)).type==9
error('In a trend equation (%s) RHS variable must be lagged LHS variable!', M_.trend_component.(auxiliary_model_name).eqtags{trend_eqnums(i)})
else
if M_.aux_vars(get_aux_variable_id(v)).orig_index~=trend_variables(i)
error('In a trend equation (%s) RHS variable must be lagged LHS variable!', M_.trend_component.(auxiliary_model_name).eqtags{trend_eqnums(i)})
end
end
else
if get_aux_variable_id(v) && M_.aux_vars(get_aux_variable_id(v)).endo_index~=trend_variables(i)
error('In a trend equation (%s) RHS variable must be lagged LHS variable!', M_.trend_component.(auxiliary_model_name).eqtags{trend_eqnums(i)})
end
end
end
% Get the EC matrix (the EC term is assumend to be in t-1).
%
% TODO: Check that the EC term is the difference between the
% endogenous variable and the trend variable.
%
A0 = oo_.trend_component.(auxiliary_model_name).ec(ecm_eqnums_in_auxiliary_model,:,1);
% Get the AR matrices.
AR = oo_.trend_component.(auxiliary_model_name).ar(ecm_eqnums_in_auxiliary_model,ecm_eqnums_in_auxiliary_model,:);
% Build B matrices (VAR in levels)
B(ecm_eqnums_in_auxiliary_model,ecm_eqnums_in_auxiliary_model,1) = eye(m)+A0+AR(:,:,1);
B(ecm_eqnums_in_auxiliary_model,trend_eqnums_in_auxiliary_model) = -A0;
B(trend_eqnums_in_auxiliary_model,trend_eqnums_in_auxiliary_model) = eye(q);
for i=2:p
B(ecm_eqnums_in_auxiliary_model,ecm_eqnums_in_auxiliary_model,i) = AR(:,:,i)-AR(:,:,i-1);
end
B(ecm_eqnums_in_auxiliary_model,ecm_eqnums_in_auxiliary_model,p+1) = -AR(:,:,p);
% Write Companion matrix
oo_.trend_component.(auxiliary_model_name).CompanionMatrix = zeros(size(B, 1)*size(B, 3));
for i=1:p
oo_.trend_component.(auxiliary_model_name).CompanionMatrix(1:n, (i-1)*n+(1:n)) = B(:,:,i);
oo_.trend_component.(auxiliary_model_name).CompanionMatrix(i*n+(1:n),(i-1)*n+(1:n)) = eye(n);
end
oo_.trend_component.(auxiliary_model_name).CompanionMatrix(1:n, p*n+(1:n)) = B(:,:,p+1);
M_.trend_component.(auxiliary_model_name).list_of_variables_in_companion_var = M_.endo_names(M_.trend_component.(auxiliary_model_name).lhs);
variables_rewritten_in_levels = M_.trend_component.(auxiliary_model_name).list_of_variables_in_companion_var(ecm_eqnums_in_auxiliary_model);
for i=1:m
id = get_aux_variable_id(variables_rewritten_in_levels{i});
if id
auxinfo = M_.aux_vars(id);
if auxinfo.type==8
M_.trend_component.(auxiliary_model_name).list_of_variables_in_companion_var(ecm_eqnums_in_auxiliary_model(i)) = ...
{M_.endo_names{auxinfo.orig_index}};
else
error('This is a bug. Please contact the Dynare Team.')
end
else
error('This is a bug. Please contact the Dynare Team.')
end
else
error('It is not possible to cast the VECM model in a companion representation! Use undiff option.')
end
end

View File

@ -63,7 +63,7 @@ if nargout>1
switch auxmodel
case 'var'
h1 = A_1*A_b*(kron(iota(m, m)'*inv(eye(m)-G), H')*(tmp\kron(iota(m, m), iota(n, idns))));
case 'vecm'
case 'trend_component'
h1 = A_1*A_b*(kron(iota(m, m)'*inv(eye(m)-G), (H'-eye(length(H))))*(tmp\kron(iota(m, m), iota(n, idns))));
end
end