dyn_ramsey_static.m 6.24 KB
Newer Older
1
function [steady_state,params,check] = dyn_ramsey_static(ys_init,M,options_,oo)
2

3
% function  [steady_state,params,check] = dyn_ramsey_static_(x)
4
5
6
% Computes the static first order conditions for optimal policy
%
% INPUTS
7
%    x:         vector of endogenous variables or instruments
8
9
10
11
12
13
14
15
16
%
% OUTPUTS
%    resids:    residuals of non linear equations
%    rJ:        Jacobian
%    mult:      Lagrangian multipliers
%
% SPECIAL REQUIREMENTS
%    none

17
% Copyright (C) 2003-2015 Dynare Team
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
%
% 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/>.
33
34
35
36


params = M.params;
check = 0;
37
options_.steadystate.nocheck = 1; %disable checking because Lagrange multipliers are not accounted for in evaluate_steady_state_file
38
% dyn_ramsey_static_1 is a subfunction
39
40
nl_func = @(x) dyn_ramsey_static_1(x,M,options_,oo);

41
% check_static_model is a subfunction
42
43
if check_static_model(ys_init,M,options_,oo) && ~options_.steadystate_flag
    steady_state = ys_init;
44
45
    return
elseif options_.steadystate_flag
46
    k_inst = [];
MichelJuillard's avatar
MichelJuillard committed
47
    inst_nbr = size(options_.instruments,1);
48
49
50
51
52
    for i = 1:inst_nbr
        k_inst = [k_inst; strmatch(options_.instruments(i,:), ...
                                   M.endo_names,'exact')];
    end
    if inst_nbr == 1
53
        inst_val = csolve(nl_func,ys_init(k_inst),'',options_.solve_tolf,100); %solve for instrument, using univariate solver, starting at initial value for instrument
54
    else
55
        [inst_val,info1] = dynare_solve(nl_func,ys_init(k_inst),0); %solve for instrument, using multivariate solver, starting at initial value for instrument
56
    end
57
    ys_init(k_inst) = inst_val;
58
    exo_ss = [oo.exo_steady_state oo.exo_det_steady_state];
59
60
    [xx,params,check] = evaluate_steady_state_file(ys_init,exo_ss,M,options_); %run steady state file again to update parameters
    [junk,junk,steady_state] = nl_func(inst_val); %compute and return steady state
61
else
62
    n_var = M.orig_endo_nbr;
63
    xx = oo.steady_state(1:n_var);
64
    [xx,check] = dynare_solve(nl_func,xx,0);
MichelJuillard's avatar
MichelJuillard committed
65
    [junk,junk,steady_state] = nl_func(xx);
66
67
68
69
end



70
function [resids,rJ,steady_state] = dyn_ramsey_static_1(x,M,options_,oo)
71
72
73
resids = [];
rJ = [];
mult = [];
74
75

% recovering usefull fields
76
params = M.params;
77
endo_nbr = M.endo_nbr;
78
endo_names = M.endo_names;
79
orig_endo_nbr = M.orig_endo_nbr;
80
81
aux_vars_type = [M.aux_vars.type];
orig_endo_aux_nbr = orig_endo_nbr + min(find(aux_vars_type == 6)) - 1; 
82
orig_eq_nbr = M.orig_eq_nbr;
83
inst_nbr = orig_endo_aux_nbr - orig_eq_nbr;
84
% indices of Lagrange multipliers
85
fname = M.fname;
86
87
88
89
90
91


if options_.steadystate_flag
    k_inst = [];
    instruments = options_.instruments;
    for i = 1:size(instruments,1)
92
        k_inst = [k_inst; strmatch(instruments(i,:), ...
93
                                   endo_names,'exact')];
94
    end
95
96
    ys_init=zeros(size(oo.steady_state)); %create starting vector for steady state computation as only instrument value is handed over
    ys_init(k_inst) = x; %set instrument, the only value required for steady state computation, to current value
Houtan Bastani's avatar
Houtan Bastani committed
97
    [x,params,check] = evaluate_steady_state_file(ys_init,... %returned x now has size endo_nbr as opposed to input size of n_instruments
98
                                                  [oo.exo_steady_state; ...
99
100
                                                  oo.exo_det_steady_state], ...
                                                  M,options_);
101
end
MichelJuillard's avatar
MichelJuillard committed
102

103
104
105
106
107
xx = zeros(endo_nbr,1); %initialize steady state vector
xx(1:M.orig_endo_nbr) = x(1:M.orig_endo_nbr); %set values of original endogenous variables based on steady state file or initial value

% setting steady state of auxiliary variables that depends on original endogenous variables
if any([M.aux_vars.type] ~= 6) %auxiliary variables other than multipliers
108
109
110
111
112
    needs_set_auxiliary_variables = 1;
    fh = str2func([M.fname '_set_auxiliary_variables']);
    s_a_v_func = @(z) fh(z,... 
                         [oo.exo_steady_state,...
                        oo.exo_det_steady_state],...
113
114
                         params);
    xx = s_a_v_func(xx);
115
116
117
else
    needs_set_auxiliary_variables = 0;
end
118
119

% value and Jacobian of objective function
120
ex = zeros(1,M.exo_nbr);
121
[U,Uy,Uyy] = feval([fname '_objective_static'],x,ex, params);
122
123
Uyy = reshape(Uyy,endo_nbr,endo_nbr);

124
125
% set multipliers and auxiliary variables that
% depends on multipliers to 0 to compute residuals
126
if (options_.bytecode)
127
   [chck, res, junk] = bytecode('static',xx,[oo.exo_steady_state oo.exo_det_steady_state], ...
128
               params, 'evaluate');
129
130
   fJ = junk.g1;
else
131
   [res,fJ] = feval([fname '_static'],xx,[oo.exo_steady_state oo.exo_det_steady_state], ...
132
               params);
133
end
134
135
136
% index of multipliers and corresponding equations
% the auxiliary variables before the Lagrange multipliers are treated
% as ordinary endogenous variables
137
138
aux_eq = [1:orig_endo_aux_nbr, orig_endo_aux_nbr+orig_eq_nbr+1:size(fJ,1)];
A = fJ(aux_eq,orig_endo_aux_nbr+1:end);
139
y = res(aux_eq);
140
mult = -A\y;
141

142
resids1 = y+A*mult;
MichelJuillard's avatar
MichelJuillard committed
143
144
145
146
if inst_nbr == 1
    r1 = sqrt(resids1'*resids1);
else
    [q,r,e] = qr([A y]');
147
148
    k = size(A,1)+(1-inst_nbr:0);
    r1 = r(end,k)';
MichelJuillard's avatar
MichelJuillard committed
149
end
150
if options_.steadystate_flag
MichelJuillard's avatar
MichelJuillard committed
151
    resids = r1;
152
else
153
    resids = [res(orig_endo_nbr+(1:orig_endo_nbr-inst_nbr)); r1];
154
155
end
rJ = [];
156
157
158
159
160
161
162
163
164
if needs_set_auxiliary_variables
    steady_state = s_a_v_func([xx(1:orig_endo_aux_nbr); mult]);
else
    steady_state = [xx(1:orig_endo_aux_nbr); mult];
end

function result = check_static_model(ys,M,options_,oo)
result = false;
if (options_.bytecode)
165
    [chck, res, junk] = bytecode('static',ys,[oo.exo_steady_state oo.exo_det_steady_state], ...
166
167
                                 M.params, 'evaluate'); 
else
168
    res = feval([M.fname '_static'],ys,[oo.exo_steady_state oo.exo_det_steady_state], ...
169
170
171
172
173
174
                M.params);
end
if norm(res) < options_.solve_tolf
    result = true;
end