1function [endogenousvariables, exogenousvariables] = model_inversion(constraints, ...
2                                                  exogenousvariables, ...
3                                                  initialconditions, DynareModel, DynareOptions, DynareOutput)
4
5% INPUTS
6% - constraints         [dseries]        with N constrained endogenous variables from t1 to t2.
7% - exogenousvariables  [dseries]        with Q exogenous variables.
8% - initialconditions   [dseries]        with M endogenous variables starting before t1 (M initialcond must contain at least the state variables).
9% - DynareModel         [struct]         M_, Dynare global structure containing informations related to the model.
10% - DynareOptions       [struct]         options_, Dynare global structure containing all the options.
11%
12% OUTPUTS
13% - endogenous          [dseries]
14% - exogenous           [dseries]
15%
16% REMARKS
17
18% Copyright (C) 2018-2019 Dynare Team
19%
20% This file is part of Dynare.
21%
22% Dynare is free software: you can redistribute it and/or modify
23% it under the terms of the GNU General Public License as published by
24% the Free Software Foundation, either version 3 of the License, or
25% (at your option) any later version.
26%
27% Dynare is distributed in the hope that it will be useful,
28% but WITHOUT ANY WARRANTY; without even the implied warranty of
29% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30% GNU General Public License for more details.
31%
32% You should have received a copy of the GNU General Public License
33% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
34
35if ~isequal(nargin, 6)
36    error('model_inversion: This routine require six input arguments!')
37end
38
39if ~isdseries(constraints)
40    error('model_inversion: First input argument must be a dseries object!')
41end
42
43if ~isdseries(exogenousvariables)
44    error('model_inversion: Second input argument must be a dseries object!')
45end
46
47if ~isempty(initialconditions) && ~isdseries(initialconditions)
48    error('model_inversion: Third input argument must be a dseries object!')
49end
50
51if ~isstruct(DynareModel)
52    error('model_inversion: Last input argument must be structures (M_)!')
53end
54
55% Set range where the endogenous variables are constrained.
56crange = constraints.dates;
57
58% Check that the number of instruments match the number of constrained endogenous variables.
59instruments = exogenousvariables(crange);
60freeinnovations = instruments.name(find(all(isnan(instruments))));
61if ~isequal(length(freeinnovations), constraints.vobs)
62    error('The number of instruments must be equal to the number of constrained variables!')
63end
64
65% Check if some of the exogenous variables are given.
66observed_exogenous_variables_flag = false;
67if exogenousvariables.vobs>constraints.vobs
68    observed_exogenous_variables_flag = true;
69end
70
71if DynareModel.maximum_lag
72    % Add auxiliary variables in initialconditions object.
73    initialconditions = checkdatabase(initialconditions, DynareModel, true, false);
74end
75
76% Get the list of endogenous and exogenous variables.
77endo_names = DynareModel.endo_names;
78exo_names = DynareModel.exo_names;
79
80% Use specidalized routine if the model is backward looking.
81if ~DynareModel.maximum_lead
82    if DynareModel.maximum_lag
83        [endogenousvariables, exogenousvariables] = ...
84            backward_model_inversion(constraints, exogenousvariables, initialconditions, ...
85                                     endo_names, exo_names, freeinnovations, ...
86                                     DynareModel, DynareOptions, DynareOutput);
87    else
88        [endogenousvariables, exogenousvariables] = ...
89            static_model_inversion(constraints, exogenousvariables, ...
90                                   endo_names, exo_names, freeinnovations, ...
91                                   DynareModel, DynareOptions, DynareOutput);
92    end
93    return
94end
95
96% Initialize fplan
97fplan = init_plan(crange);
98
99% Set the exogenous observed variables.
100if observed_exogenous_variables_flag
101    list_of_observed_exogenous_variables = setdiff(exo_names, freeinnovations);
102    observed_exogenous_variables = exogenousvariables{list_of_observed_exogenous_variables{:}};
103    for i=1:length(list_of_observed_exogenous_variables)
104        fplan = basic_plan(fplan, list_of_observed_exogenous_variables{i}, ...
105                           'surprise', crange, observed_exogenous_variables{list_of_observed_exogenous_variables{i}}.data(2:length(crange)+1));
106    end
107end
108
109% Set constrained path for the endogenous variables.
110for i = 1:constraints.vobs
111    fplan = flip_plan(fplan, constraints.name{i}, freeinnovations{i}, 'surprise', crange, transpose(constraints.data(:,i)));
112end
113
114% Identify the innovations (model inversion)
115f = det_cond_forecast(fplan, initialconditions, crange);
116
117endogenousvariables = f{endo_names{:}};
118exogenousvariables = f{exo_names{:}};