diff --git a/matlab/get_companion_matrix.m b/matlab/get_companion_matrix.m index 6fc137b8d1cdf5db9af1da02cfc74effacfe3727..d2ea827a6348f87a75059c2e92b27836428defc4 100644 --- a/matlab/get_companion_matrix.m +++ b/matlab/get_companion_matrix.m @@ -42,7 +42,8 @@ if nargin < 2 end if strcmp(auxiliary_model_type, 'var') - [AR, ~] = feval(sprintf('%s.varmatrices', M_.fname), auxiliary_model_name, M_.params, M_.var.(auxiliary_model_name).structural); + [AR, ~, Constant] = feval(sprintf('%s.varmatrices', M_.fname), auxiliary_model_name, M_.params, M_.var.(auxiliary_model_name).structural); + isconstant = any(abs(Constant)>0); elseif strcmp(auxiliary_model_type, 'trend_component') [AR, A0, A0star] = feval(sprintf('%s.trend_component_ar_a0', M_.fname), auxiliary_model_name, M_.params); else @@ -57,11 +58,17 @@ n = length(M_.(auxiliary_model_type).(auxiliary_model_name).lhs); 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) = AR(:,:,1); + oo_.var.(auxiliary_model_name).CompanionMatrix = zeros(n*p+isconstant); + oo_.var.(auxiliary_model_name).CompanionMatrix(isconstant+(1:n),isconstant+(1:n)) = AR(:,:,1); for i = 2:p - oo_.var.(auxiliary_model_name).CompanionMatrix(1:n,(i-1)*n+(1:n)) = AR(:,:,i); - oo_.var.(auxiliary_model_name).CompanionMatrix((i-1)*n+(1:n),(i-2)*n+(1:n)) = eye(n); + oo_.var.(auxiliary_model_name).CompanionMatrix(isconstant+(1:n),isconstant+(i-1)*n+(1:n)) = AR(:,:,i); + oo_.var.(auxiliary_model_name).CompanionMatrix(isconstant+(i-1)*n+(1:n),isconstant+(i-2)*n+(1:n)) = eye(n); + end + if isconstant + oo_.var.(auxiliary_model_name).CompanionMatrix(1,1) = 1; + for i=1:n + oo_.var.(auxiliary_model_name).CompanionMatrix(1+i,1) = Constant(i); + end end M_.var.(auxiliary_model_name).list_of_variables_in_companion_var = M_.endo_names(M_.var.(auxiliary_model_name).lhs); if nargout diff --git a/preprocessor b/preprocessor index 15d7432105578c749cd5a81b2ab09fe8bf01d1b7..e1f7d8c73556398a774ab55c8dc578c79ebb6c3b 160000 --- a/preprocessor +++ b/preprocessor @@ -1 +1 @@ -Subproject commit 15d7432105578c749cd5a81b2ab09fe8bf01d1b7 +Subproject commit e1f7d8c73556398a774ab55c8dc578c79ebb6c3b diff --git a/tests/var-expectations/11/example1.mod b/tests/var-expectations/11/example1.mod index d5c21ae90d3ecf99be714d64669c0607b6bfd13e..ffd060f8c3e011f4cba9202900644e43136cb983 100644 --- a/tests/var-expectations/11/example1.mod +++ b/tests/var-expectations/11/example1.mod @@ -21,16 +21,17 @@ var_expectation_model(model_name = varexp, expression = diff(log(x)), auxiliary_ model; [ name = 'X' ] -diff(log(x)) = b*diff(z) + a*diff(log(x(-1))) + (1-a)*diff(log(x(-2))) + c*diff(z(-2)) + e_x; +diff(log(x)) = b*diff(z) + a*diff(log(x(-1))) + b*(1-a)*diff(log(x(-2))) + c*diff(z(-2)) + e_x; [ name = 'Z' ] -diff(z) = f*(diff(z(-1))-diff(log(x)))+c*diff(z(-2)) + e_z; +diff(z) = .1 + f*(diff(z(-1))-diff(log(x)))+c*diff(z(-2)) + e_z; [ name = 'Y' ] log(y) = diff(log(x)) + d*log(y(-2)) + e*diff(z(-1)) + e_y; foo = var_expectation(varexp); end; -[ar, a0] = example1.varmatrices('toto', M_.params); +% Evaluate strutural VAR matrices +[ar, a0, const] = example1.varmatrices('toto', M_.params); assert(isequal(diag(a0), ones(3,1)), 'Diagonal of a0 is wrong.') @@ -41,6 +42,19 @@ assert(a0(2,3)==0, 'Element (2,3) in A0 is wrong.') assert(a0(3,1)==f, 'Element (3,1) in A0 is wrong.') assert(a0(3,2)==0, 'Element (3,1) in A0 is wrong.') -assert(isequal(ar(:,:,1), [a 0 0; 0 0 e; 0 0 f]), 'First autoregressive matrix is wrong'); -assert(isequal(ar(:,:,2), [1-a 0 c; 0 d 0; 0 0 c]), 'Second autoregressive matrix is wrong'); +assert(isequal(ar(:,:,1), [a 0 0; 0 0 e; 0 0 f]), 'First autoregressive matrix is wrong') +assert(isequal(ar(:,:,2), [(1-a)*b 0 c; 0 d 0; 0 0 c]), 'Second autoregressive matrix is wrong') +assert(isequal(const, [.0;.0;.1]), 'Constant vector is wrong.') + +% Evaluate reduced form VAR matrices +[AR, ~, CONST] = example1.varmatrices('toto', M_.params, true); + +assert(all(all(abs(AR(:,:,1)-a0\ar(:,:,1))<1e-9)), 'Reduced form is wrong (first lag)') +assert(all(all(abs(AR(:,:,2)-a0\ar(:,:,2))<1e-9)), 'Reduced form is wrong (second lag)') +assert(all(abs(CONST-a0\const)<1e-9), 'Reduced form is wrong (constant)') + +% Test get_companion_matrix when the VAR model has a constant. +get_companion_matrix('toto', 'var'); + +assert(all(all(abs(oo_.var.toto.CompanionMatrix-[1, zeros(1, 6); CONST, AR(:,:,1), AR(:,:,2); zeros(3,1), eye(3), zeros(3)])<1e-9)), 'Companion matrix is wrong')