diff --git a/matlab/get_companion_matrix.m b/matlab/get_companion_matrix.m index 35a17020d..7450c4fc4 100644 --- a/matlab/get_companion_matrix.m +++ b/matlab/get_companion_matrix.m @@ -90,18 +90,27 @@ else q = length(M_.var.(var_model_name).eqn)-m; ecm_eqnums = zeros(m, 1); ecm_eqnums_in_auxiliary_model = zeros(m, 1); - for i=1:m - number = get_equation_number_by_tag(M_.pac.(pac_model_name).undiff_eqtags{i}); - if number>0 - ecm_eqnums(i) = number; - ide = find(M_.var.(var_model_name).eqn==number); - if isempty(ide) - error('(eq = %s, eq_in_aux_model = %s) This is most likely a bug. Please contact the DynareTeam.', int2str(number), int2str(ide)) - else - ecm_eqnums_in_auxiliary_model(i) = ide; - end + 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 - error('%s is not declared as an equation in the model block!', M_.pac.(pac_model_name).undiff_eqtag{i}) + 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. @@ -112,20 +121,9 @@ else 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 indices of the trend equations. - trend_eqnums = transpose(setdiff(M_.var.(var_model_name).eqn,ecm_eqnums)); - trend_eqnums_in_auxiliary_model = zeros(q, 1); - for i=1:q - idt = find(M_.var.(var_model_name).eqn==trend_eqnums(i)); - if isempty(idt) - error('(eq = %s, eq_in_aux_model = %s) This is most likely a bug. Please contact the DynareTeam.', int2str(trend_eqnums(i)), int2str(idt)) - else - trend_eqnums_in_auxiliary_model(i) = idt; - end - 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 = M_.var.(var_model_name).lhs(id_trend_in_var); + 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