2023-04-13 12:28:26 +02:00
|
|
|
function [endogenousvariables, exogenousvariables] = static_model_inversion(constraints, exogenousvariables, endo_names, exo_names, freeinnovations, DynareModel, DynareOptions, DynareOutput)
|
2019-06-21 11:20:57 +02:00
|
|
|
|
|
|
|
% INPUTS
|
|
|
|
% - constraints [dseries] with N constrained endogenous variables from t1 to t2.
|
|
|
|
% - exogenousvariables [dseries] with Q exogenous variables.
|
|
|
|
% - endo_names [cell] list of endogenous variable names.
|
|
|
|
% - exo_names [cell] list of exogenous variable names.
|
|
|
|
% - freeinstruments [cell] list of exogenous variable names used to control the constrained endogenous variables.
|
|
|
|
%
|
|
|
|
% OUTPUTS
|
|
|
|
% - endogenous [dseries]
|
|
|
|
% - exogenous [dseries]
|
|
|
|
%
|
|
|
|
% REMARKS
|
|
|
|
|
2023-04-13 11:18:34 +02:00
|
|
|
% Copyright © 2019-2023 Dynare Team
|
2019-06-21 11:20:57 +02:00
|
|
|
%
|
|
|
|
% 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
|
2021-06-09 17:33:48 +02:00
|
|
|
% along with Dynare. If not, see <https://www.gnu.org/licenses/>.
|
2019-06-21 11:20:57 +02:00
|
|
|
|
2023-04-13 12:19:36 +02:00
|
|
|
% Get indices for the free innovations.
|
2019-06-21 11:20:57 +02:00
|
|
|
freeinnovations_id = zeros(length(freeinnovations), 1);
|
|
|
|
if length(freeinnovations)<DynareModel.exo_nbr
|
|
|
|
for i=1:length(freeinnovations)
|
|
|
|
freeinnovations_id(i) = find(strcmp(freeinnovations{i}, exo_names));
|
|
|
|
end
|
|
|
|
else
|
|
|
|
freeinnovations_id = transpose(1:length(exo_names));
|
|
|
|
end
|
|
|
|
|
|
|
|
nxfree = length(freeinnovations_id);
|
|
|
|
|
|
|
|
% Get indices for the the controlled and free endogenous variables.
|
|
|
|
controlledendogenousvariables_id = zeros(length(freeinnovations), 1);
|
|
|
|
if length(freeinnovations)<DynareModel.endo_nbr
|
|
|
|
for i=1:length(freeinnovations)
|
|
|
|
controlledendogenousvariables_id(i) = find(strcmp(constraints.name{i}, endo_names));
|
|
|
|
end
|
|
|
|
freeendogenousvariables_id = setdiff(transpose(1:length(endo_names)), controlledendogenousvariables_id);
|
|
|
|
else
|
|
|
|
controlledendogenousvariables_id = transpose(1:length(endo_names));
|
|
|
|
freeendogenousvariables_id = [];
|
|
|
|
end
|
|
|
|
|
|
|
|
nyfree = length(freeendogenousvariables_id);
|
|
|
|
nyctrl = length(controlledendogenousvariables_id);
|
|
|
|
|
|
|
|
% Build structure to be passed to the objective function.
|
|
|
|
ModelInversion.nyfree = nyfree;
|
|
|
|
ModelInversion.nyctrl = nyctrl;
|
|
|
|
ModelInversion.nxfree = nxfree;
|
2023-04-13 12:19:36 +02:00
|
|
|
ModelInversion.y_constrained_id = controlledendogenousvariables_id;
|
|
|
|
ModelInversion.y_free_id = freeendogenousvariables_id;
|
2019-06-21 11:20:57 +02:00
|
|
|
ModelInversion.x_free_id = freeinnovations_id;
|
2023-04-13 12:19:36 +02:00
|
|
|
ModelInversion.J_id = [DynareModel.endo_nbr+ModelInversion.y_free_id ; 3*DynareModel.endo_nbr+ModelInversion.x_free_id];
|
2019-06-21 11:20:57 +02:00
|
|
|
|
2023-04-13 12:19:36 +02:00
|
|
|
% Get function handles to the dynamic model routines.
|
|
|
|
dynamic_resid = str2func([DynareModel.fname '.sparse.dynamic_resid']);
|
|
|
|
dynamic_g1 = str2func([DynareModel.fname '.sparse.dynamic_g1']);
|
2019-06-21 11:20:57 +02:00
|
|
|
|
|
|
|
% Initialization of the returned simulations (endogenous variables).
|
|
|
|
Y = NaN(DynareModel.endo_nbr, nobs(constraints));
|
|
|
|
|
|
|
|
for i=1:nyctrl
|
|
|
|
Y(controlledendogenousvariables_id(i),1:end) = transpose(constraints.data(:,i));
|
|
|
|
end
|
|
|
|
|
|
|
|
% Exogenous variables.
|
|
|
|
X = exogenousvariables.data;
|
|
|
|
|
|
|
|
% Inversion of the model, solvers for the free endogenous and exogenous variables (call a Newton-like algorithm in each period).
|
|
|
|
|
|
|
|
ity = 1;
|
|
|
|
itx = find(exogenousvariables.dates==constraints.dates(1));
|
|
|
|
DynareOptions.solve_algo=4;
|
|
|
|
|
|
|
|
for t = 1:nobs(constraints)
|
|
|
|
% Set the current values of the constrained endogenous variables.
|
|
|
|
ycur = Y(controlledendogenousvariables_id,ity);
|
|
|
|
% Vector z gather the free endogenous variables (initialized with lagged
|
|
|
|
% values) and the free exogenous variables (initialized with 0).
|
|
|
|
z = [Y(freeendogenousvariables_id,ity); zeros(nxfree, 1)];
|
|
|
|
% Solves for z.
|
2023-04-13 11:18:34 +02:00
|
|
|
[z, errorflag, ~, ~, errorcode] = dynare_solve(@static_model_for_inversion, z, DynareOptions.simul.maxit, DynareOptions.dynatol.f, DynareOptions.dynatol.x, ...
|
2023-04-13 12:28:26 +02:00
|
|
|
DynareOptions, dynamic_resid, dynamic_g1, ycur, X(itx, :), DynareModel.params, DynareOutput.steady_state, DynareModel.dynamic_g1_sparse_rowval, DynareModel.dynamic_g1_sparse_colval, DynareModel.dynamic_g1_sparse_colptr, ModelInversion);
|
2023-04-13 12:19:36 +02:00
|
|
|
|
2022-04-09 16:03:37 +02:00
|
|
|
if errorflag
|
|
|
|
error('Enable to solve the system of equations (with error code %i).', errorcode)
|
2019-06-21 11:20:57 +02:00
|
|
|
end
|
|
|
|
% Update the matrix of exogenous variables.
|
|
|
|
X(itx,freeinnovations_id) = z(nyfree+(1:nxfree));
|
|
|
|
% Update the matrix of endogenous variables.
|
|
|
|
if nyfree
|
|
|
|
Y(freeendogenousvariables_id,ity) = z(1:nyfree);
|
|
|
|
end
|
|
|
|
% Increment counters
|
|
|
|
ity = ity+1;
|
|
|
|
itx = itx+1;
|
|
|
|
end
|
|
|
|
|
|
|
|
endogenousvariables = dseries(Y', constraints.dates(1), endo_names);
|
2023-04-13 11:18:34 +02:00
|
|
|
exogenousvariables = dseries(X(find(exogenousvariables.dates==constraints.dates(1))+(0:(nobs(constraints)-1)),:), constraints.dates(1), exo_names);
|
|
|
|
|
|
|
|
|
2023-04-13 12:28:26 +02:00
|
|
|
function [r, J] = static_model_for_inversion(z, dynamic_resid, dynamic_g1, ycur, x, params, steady_state, sparse_rowval, sparse_colval, sparse_colptr, ModelInversion)
|
2023-04-13 12:19:36 +02:00
|
|
|
|
|
|
|
endo_nbr = ModelInversion.nyfree+ModelInversion.nyctrl;
|
2023-04-13 11:18:34 +02:00
|
|
|
|
|
|
|
% Set up y
|
2023-04-13 12:19:36 +02:00
|
|
|
y = NaN(3*endo_nbr, 1);
|
2023-04-13 11:18:34 +02:00
|
|
|
|
2023-04-13 12:19:36 +02:00
|
|
|
y(endo_nbr+ModelInversion.y_constrained_id) = ycur;
|
2023-04-13 11:18:34 +02:00
|
|
|
if ModelInversion.nyfree
|
2023-04-13 12:19:36 +02:00
|
|
|
y(endo_nbr+ModelInversion.y_free_id) = z(1:ModelInversion.nyfree);
|
2023-04-13 11:18:34 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
% Update x
|
|
|
|
x(ModelInversion.x_free_id) = z(ModelInversion.nyfree+(1:ModelInversion.nxfree));
|
|
|
|
|
2023-04-13 12:28:26 +02:00
|
|
|
[r, T_order, T] = dynamic_resid(y, x, params, steady_state);
|
2023-04-13 12:19:36 +02:00
|
|
|
|
2023-04-13 11:18:34 +02:00
|
|
|
if nargout>1
|
2023-04-13 12:28:26 +02:00
|
|
|
Jacobian = dynamic_g1(y, x, params, steady_state, sparse_rowval, ...
|
2023-04-13 12:19:36 +02:00
|
|
|
sparse_colval, sparse_colptr, T_order, T);
|
|
|
|
J = Jacobian(:, ModelInversion.J_id);
|
2023-04-13 11:18:34 +02:00
|
|
|
end
|