File: dyn_second_order_solver.m

package info (click to toggle)
dynare 4.4.3-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 41,356 kB
  • ctags: 15,842
  • sloc: cpp: 77,029; ansic: 29,056; pascal: 13,241; sh: 4,811; objc: 3,061; yacc: 3,013; makefile: 1,479; lex: 1,258; python: 162; lisp: 54; xml: 8
file content (182 lines) | stat: -rw-r--r-- 6,261 bytes parent folder | download | duplicates (2)
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
function dr = dyn_second_order_solver(jacobia,hessian,dr,M_,threads_ABC,threads_BC)

%@info:
%! @deftypefn {Function File} {@var{dr} =} dyn_second_order_solver (@var{jacobia},@var{hessian},@var{dr},@var{M_},@var{threads_ABC},@var{threads_BC})
%! @anchor{dyn_second_order_solver}
%! @sp 1
%! Computes the second order reduced form of the DSGE model
%! @sp 2
%! @strong{Inputs}
%! @sp 1
%! @table @ @var
%! @item jacobia
%! Matrix containing the Jacobian of the model
%! @item hessian
%! Matrix containing the second order derivatives of the model
%! @item dr
%! Matlab's structure describing the reduced form solution of the model.
%! @item M_
%! Matlab's structure describing the model (initialized by @code{dynare}).
%! @item threads_ABC
%! Integer controlling number of threads in A_times_B_kronecker_C
%! @item threads_BC
%! Integer controlling number of threads in sparse_hessian_times_B_kronecker_C
%! @end table
%! @sp 2
%! @strong{Outputs}
%! @sp 1
%! @table @ @var
%! @item dr
%! Matlab's structure describing the reduced form solution of the model.
%! @end table
%! @end deftypefn
%@eod:

% Copyright (C) 2001-2012 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/>.
    
    dr.ghxx = [];
    dr.ghuu = [];
    dr.ghxu = [];
    dr.ghs2 = [];
    Gy = dr.Gy;
    
    kstate = dr.kstate;
    nstatic = M_.nstatic;
    nfwrd = M_.nfwrd;
    nspred = M_.nspred;
    nboth = M_.nboth;
    nsfwrd = M_.nsfwrd;
    order_var = dr.order_var;
    nd = size(kstate,1);
    lead_lag_incidence = M_.lead_lag_incidence;

    np = nd - nsfwrd;

    k1 = nonzeros(lead_lag_incidence(:,order_var)');
    kk = [k1; length(k1)+(1:M_.exo_nbr+M_.exo_det_nbr)'];
    nk = size(kk,1);
    kk1 = reshape([1:nk^2],nk,nk);
    kk1 = kk1(kk,kk);
    % reordering second order derivatives
    hessian = hessian(:,kk1(:));

    zx = zeros(np,np);
    zu=zeros(np,M_.exo_nbr);
    zx(1:np,:)=eye(np);
    k0 = [1:M_.endo_nbr];
    gx1 = dr.ghx;
    hu = dr.ghu(nstatic+[1:nspred],:);
    k0 = find(lead_lag_incidence(M_.maximum_endo_lag+1,order_var)');
    zx = [zx; gx1(k0,:)];
    zu = [zu; dr.ghu(k0,:)];
    k1 = find(lead_lag_incidence(M_.maximum_endo_lag+2,order_var)');
    zu = [zu; gx1(k1,:)*hu];
    zx = [zx; gx1(k1,:)*Gy];
    zx=[zx; zeros(M_.exo_nbr,np);zeros(M_.exo_det_nbr,np)];
    zu=[zu; eye(M_.exo_nbr);zeros(M_.exo_det_nbr,M_.exo_nbr)];
    [nrzx,nczx] = size(zx);

    [rhs, err] = sparse_hessian_times_B_kronecker_C(hessian,zx,threads_BC);
    mexErrCheck('sparse_hessian_times_B_kronecker_C', err);
    rhs = -rhs;

    %lhs
    n = M_.endo_nbr+sum(kstate(:,2) > M_.maximum_endo_lag+1 & kstate(:,2) < M_.maximum_endo_lag+M_.maximum_endo_lead+1);
    A = zeros(M_.endo_nbr,M_.endo_nbr);
    B = zeros(M_.endo_nbr,M_.endo_nbr);
    A(:,k0) = jacobia(:,nonzeros(lead_lag_incidence(M_.maximum_endo_lag+1,order_var)));
    % variables with the highest lead
    k1 = find(kstate(:,2) == M_.maximum_endo_lag+2);
    % Jacobian with respect to the variables with the highest lead
    fyp = jacobia(:,kstate(k1,3)+nnz(M_.lead_lag_incidence(M_.maximum_endo_lag+1,:)));
    B(:,nstatic+M_.npred+1:end) = fyp;
    [junk,k1,k2] = find(M_.lead_lag_incidence(M_.maximum_endo_lag+M_.maximum_endo_lead+1,order_var));
    A(1:M_.endo_nbr,nstatic+1:nstatic+nspred)=...
        A(1:M_.endo_nbr,nstatic+[1:nspred])+fyp*gx1(k1,1:nspred);
    C = Gy;
    D = [rhs; zeros(n-M_.endo_nbr,size(rhs,2))];


    [err, dr.ghxx] = gensylv(2,A,B,C,D);
    mexErrCheck('gensylv', err);

    %ghxu
    %rhs
    hu = dr.ghu(nstatic+1:nstatic+nspred,:);
    [rhs, err] = sparse_hessian_times_B_kronecker_C(hessian,zx,zu,threads_BC);
    mexErrCheck('sparse_hessian_times_B_kronecker_C', err);

    hu1 = [hu;zeros(np-nspred,M_.exo_nbr)];
    [nrhx,nchx] = size(Gy);
    [nrhu1,nchu1] = size(hu1);

    [abcOut,err] = A_times_B_kronecker_C(dr.ghxx,Gy,hu1,threads_ABC);
    mexErrCheck('A_times_B_kronecker_C', err);
    B1 = B*abcOut;
    rhs = -[rhs; zeros(n-M_.endo_nbr,size(rhs,2))]-B1;


    %lhs
    dr.ghxu = A\rhs;

    %ghuu
    %rhs
    [rhs, err] = sparse_hessian_times_B_kronecker_C(hessian,zu,threads_BC);
    mexErrCheck('sparse_hessian_times_B_kronecker_C', err);

    [B1, err] = A_times_B_kronecker_C(B*dr.ghxx,hu1,threads_ABC);
    mexErrCheck('A_times_B_kronecker_C', err);
    rhs = -[rhs; zeros(n-M_.endo_nbr,size(rhs,2))]-B1;

    %lhs
    dr.ghuu = A\rhs;

    % dr.ghs2
    % derivatives of F with respect to forward variables
    % reordering predetermined variables in diminishing lag order
    O1 = zeros(M_.endo_nbr,nstatic);
    O2 = zeros(M_.endo_nbr,M_.endo_nbr-nstatic-nspred);
    LHS = zeros(M_.endo_nbr,M_.endo_nbr);
    LHS(:,k0) = jacobia(:,nonzeros(lead_lag_incidence(M_.maximum_endo_lag+1,order_var)));
    RHS = zeros(M_.endo_nbr,M_.exo_nbr^2);
    gu = dr.ghu; 
    guu = dr.ghuu; 
    E = eye(M_.endo_nbr);
    kh = reshape([1:nk^2],nk,nk);
    kp = sum(kstate(:,2) <= M_.maximum_endo_lag+1);
    E1 = [eye(nspred); zeros(kp-nspred,nspred)];
    H = E1;
    hxx = dr.ghxx(nstatic+[1:nspred],:);
    [junk,k2a,k2] = find(M_.lead_lag_incidence(M_.maximum_endo_lag+2,order_var));
    k3 = nnz(M_.lead_lag_incidence(1:M_.maximum_endo_lag+1,:))+(1:M_.nsfwrd)';
    [B1, err] = sparse_hessian_times_B_kronecker_C(hessian(:,kh(k3,k3)),gu(k2a,:),threads_BC);
    mexErrCheck('sparse_hessian_times_B_kronecker_C', err);
    RHS = RHS + jacobia(:,k2)*guu(k2a,:)+B1;

    % LHS
    LHS = LHS + jacobia(:,k2)*(E(k2a,:)+[O1(k2a,:) dr.ghx(k2a,:)*H O2(k2a,:)]);
    
    RHS = RHS*M_.Sigma_e(:);
    dr.fuu = RHS;
    %RHS = -RHS-dr.fbias;
    RHS = -RHS;
    dr.ghs2 = LHS\RHS;

    % deterministic exogenous variables
    if M_.exo_det_nbr > 0
    end