File: model_inversion.m

package info (click to toggle)
dynare 4.6.3-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 74,896 kB
  • sloc: cpp: 98,057; ansic: 28,929; pascal: 13,844; sh: 5,947; objc: 4,236; yacc: 4,215; makefile: 2,583; lex: 1,534; fortran: 877; python: 647; ruby: 291; lisp: 152; xml: 22
file content (118 lines) | stat: -rw-r--r-- 4,842 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
function [endogenousvariables, exogenousvariables] = model_inversion(constraints, ...
                                                  exogenousvariables, ...
                                                  initialconditions, DynareModel, DynareOptions, DynareOutput)

% INPUTS
% - constraints         [dseries]        with N constrained endogenous variables from t1 to t2.
% - exogenousvariables  [dseries]        with Q exogenous variables.
% - initialconditions   [dseries]        with M endogenous variables starting before t1 (M initialcond must contain at least the state variables).
% - DynareModel         [struct]         M_, Dynare global structure containing informations related to the model.
% - DynareOptions       [struct]         options_, Dynare global structure containing all the options.
%
% OUTPUTS
% - endogenous          [dseries]
% - exogenous           [dseries]
%
% REMARKS

% Copyright (C) 2018-2019 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/>.

if ~isequal(nargin, 6)
    error('model_inversion: This routine require six input arguments!')
end

if ~isdseries(constraints)
    error('model_inversion: First input argument must be a dseries object!')
end

if ~isdseries(exogenousvariables)
    error('model_inversion: Second input argument must be a dseries object!')
end

if ~isempty(initialconditions) && ~isdseries(initialconditions)
    error('model_inversion: Third input argument must be a dseries object!')
end

if ~isstruct(DynareModel)
    error('model_inversion: Last input argument must be structures (M_)!')
end

% Set range where the endogenous variables are constrained.
crange = constraints.dates;

% Check that the number of instruments match the number of constrained endogenous variables.
instruments = exogenousvariables(crange);
freeinnovations = instruments.name(find(all(isnan(instruments))));
if ~isequal(length(freeinnovations), constraints.vobs)
    error('The number of instruments must be equal to the number of constrained variables!')
end

% Check if some of the exogenous variables are given.
observed_exogenous_variables_flag = false;
if exogenousvariables.vobs>constraints.vobs
    observed_exogenous_variables_flag = true;
end

if DynareModel.maximum_lag
    % Add auxiliary variables in initialconditions object.
    initialconditions = checkdatabase(initialconditions, DynareModel, true, false);
end

% Get the list of endogenous and exogenous variables.
endo_names = DynareModel.endo_names;
exo_names = DynareModel.exo_names;

% Use specidalized routine if the model is backward looking.
if ~DynareModel.maximum_lead
    if DynareModel.maximum_lag
        [endogenousvariables, exogenousvariables] = ...
            backward_model_inversion(constraints, exogenousvariables, initialconditions, ...
                                     endo_names, exo_names, freeinnovations, ...
                                     DynareModel, DynareOptions, DynareOutput);
    else
        [endogenousvariables, exogenousvariables] = ...
            static_model_inversion(constraints, exogenousvariables, ...
                                   endo_names, exo_names, freeinnovations, ...
                                   DynareModel, DynareOptions, DynareOutput);
    end
    return
end

% Initialize fplan
fplan = init_plan(crange);

% Set the exogenous observed variables.
if observed_exogenous_variables_flag
    list_of_observed_exogenous_variables = setdiff(exo_names, freeinnovations);
    observed_exogenous_variables = exogenousvariables{list_of_observed_exogenous_variables{:}};
    for i=1:length(list_of_observed_exogenous_variables)
        fplan = basic_plan(fplan, list_of_observed_exogenous_variables{i}, ...
                           'surprise', crange, observed_exogenous_variables{list_of_observed_exogenous_variables{i}}.data(2:length(crange)+1));
    end
end

% Set constrained path for the endogenous variables.
for i = 1:constraints.vobs
    fplan = flip_plan(fplan, constraints.name{i}, freeinnovations{i}, 'surprise', crange, transpose(constraints.data(:,i)));
end

% Identify the innovations (model inversion)
f = det_cond_forecast(fplan, initialconditions, crange);

endogenousvariables = f{endo_names{:}};
exogenousvariables = f{exo_names{:}};