File: discretionary_policy_1.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 (125 lines) | stat: -rw-r--r-- 4,548 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
119
120
121
122
123
124
125
function [dr, info, M_, options_, oo_]=discretionary_policy_1(Instruments, M_, options_, oo_)
% Higher-level function for solving discretionary optimal policy
% INPUTS
% - Instruments   [cell]          array containing instrument names
% - M_            [structure]     Matlab's structure describing the model (M_).
% - options_      [structure]     Matlab's structure describing the current options (options_).
% - oo_           [structure]     Matlab's structure containing the results (oo_).
%
% OUTPUTS
% - dr            [structure]     Reduced form model.
% - info          [integer]       scalar or vector, error code.
% - M_            [structure]     Matlab's structure describing the model (M_).
% - options_      [structure]     Matlab's structure describing the current options (options_).
% - oo_           [structure]     Matlab's structure containing the results (oo_).

% Copyright (C) 2007-2020 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/>.

persistent Hold

info = 0;

dr=oo_.dr; %initialize output argument

beta = get_optimal_policy_discount_factor(M_.params, M_.param_names);

%call steady_state_file if present to update parameters
if options_.steadystate_flag
    % explicit steady state file
    [~,M_.params,info] = evaluate_steady_state_file(oo_.steady_state,[oo_.exo_steady_state; oo_.exo_det_steady_state],M_, ...
                                                    options_,false);
    if info(1)
        return;
    end
end
[U,Uy,W] = feval([M_.fname,'.objective.static'],zeros(M_.endo_nbr,1),[], M_.params);
if any(any(isnan(Uy)))
    info = 64 ; %the derivatives of the objective function contain NaN
    return;
end
if any(any(Uy~=0))
    if options_.debug
        non_zero_derivs=find(any(Uy~=0));
        for ii=1:length(non_zero_derivs)
            non_zero_deriv_names{ii,1} = M_.endo_names{non_zero_derivs(ii)};
        end
        disp_string=[non_zero_deriv_names{1,:}];
        for ii=2:size(non_zero_deriv_names,1)
            disp_string=[disp_string,', ',non_zero_deriv_names{ii,:}];
        end
        fprintf('\nThe derivative of the objective function w.r.t. to variable(s) %s is not 0\n',disp_string);
    end
    info = 66;
    return;
end

W=reshape(W,M_.endo_nbr,M_.endo_nbr);

klen = M_.maximum_lag + M_.maximum_lead + 1;
iyv=M_.lead_lag_incidence';
% Find the jacobian
z = repmat(zeros(M_.endo_nbr,1),1,klen);
z = z(nonzeros(iyv)) ;
it_ = M_.maximum_lag + 1 ;

if M_.exo_nbr == 0
    oo_.exo_steady_state = [] ;
end

[junk,jacobia_] = feval([M_.fname '.dynamic'],z, [zeros(size(oo_.exo_simul)) ...
                    oo_.exo_det_simul], M_.params, zeros(M_.endo_nbr,1), it_);
if any(junk~=0)
    info = 65; %the model must be written in deviation form and not have constant terms
    return;
end

Indices={'lag','contemp','lead'};
iter=1;
for j=1:numel(Indices)
    A.(Indices{j})=zeros(M_.orig_eq_nbr,M_.endo_nbr);
    if strcmp(Indices{j},'contemp')||(strcmp(Indices{j},'lag') && M_.maximum_lag)||(strcmp(Indices{j},'lead') && M_.maximum_lead)
        [~,row,col]=find(M_.lead_lag_incidence(iter,:));
        A.(Indices{j})(:,row)=jacobia_(:,col);
        iter=iter+1;
    end
end
B=jacobia_(:,nnz(iyv)+1:end);

%%% MAIN ENGINE %%%

if ~isempty(Hold)
    [H,G,info]=discretionary_policy_engine(A.lag,A.contemp,A.lead,B,W,M_.instr_id,beta,options_.dp.maxit,options_.discretionary_tol,options_.qz_criterium,Hold);
else
    [H,G,info]=discretionary_policy_engine(A.lag,A.contemp,A.lead,B,W,M_.instr_id,beta,options_.dp.maxit,options_.discretionary_tol,options_.qz_criterium);
end

if info
    return
else
    Hold=H; %save previous solution
            % Hold=[]; use this line if persistent command is not used.
end

%write back solution to dr
dr.ys =zeros(M_.endo_nbr,1);
dr=set_state_space(dr,M_,options_);
T=H(dr.order_var,dr.order_var);
dr.ghu=G(dr.order_var,:);
Selection=M_.lead_lag_incidence(1,dr.order_var)>0;%select state variables
dr.ghx=T(:,Selection);
oo_.dr = dr;