Commit 5d2a077a authored by Sébastien Villemot's avatar Sébastien Villemot

Merge branch 'remove_kstate' into 'master'

Remove kstate in dyn_second_order_solver

See merge request !1656
parents 83f809e0 052d3047
Pipeline #1602 passed with stages
in 72 minutes and 39 seconds
function dr = dyn_second_order_solver(jacobia,hessian_mat,dr,M_,threads_BC) function dr = dyn_second_order_solver(jacobia,hessian_mat,dr,M,threads_BC)
%@info: %@info:
%! @deftypefn {Function File} {@var{dr} =} dyn_second_order_solver (@var{jacobia},@var{hessian_mat},@var{dr},@var{M_},@var{threads_BC}) %! @deftypefn {Function File} {@var{dr} =} dyn_second_order_solver (@var{jacobia},@var{hessian_mat},@var{dr},@var{M_},@var{threads_BC})
%! @anchor{dyn_second_order_solver} %! @anchor{dyn_second_order_solver}
%! @sp 1 %! @sp 1
%! Computes the second order reduced form of the DSGE model %! Computes the second order reduced form of the DSGE model, for details please refer to
%! * Juillard and Kamenik (2004): Solving Stochastic Dynamic Equilibrium Models: A k-Order Perturbation Approach
%! * Kamenik (2005) - Solving SDGE Models: A New Algorithm for the Sylvester Equation
%! Note that this function makes use of the fact that Dynare internally transforms the model
%! so that there is only one lead and one lag on endogenous variables and, in the case of a stochastic model,
%! no leads/lags on exogenous variables. See the manual for more details.
% Auxiliary variables
%! @sp 2 %! @sp 2
%! @strong{Inputs} %! @strong{Inputs}
%! @sp 1 %! @sp 1
...@@ -30,7 +36,7 @@ function dr = dyn_second_order_solver(jacobia,hessian_mat,dr,M_,threads_BC) ...@@ -30,7 +36,7 @@ function dr = dyn_second_order_solver(jacobia,hessian_mat,dr,M_,threads_BC)
%! @end deftypefn %! @end deftypefn
%@eod: %@eod:
% Copyright (C) 2001-2017 Dynare Team % Copyright (C) 2001-2019 Dynare Team
% %
% This file is part of Dynare. % This file is part of Dynare.
% %
...@@ -51,130 +57,75 @@ dr.ghxx = []; ...@@ -51,130 +57,75 @@ dr.ghxx = [];
dr.ghuu = []; dr.ghuu = [];
dr.ghxu = []; dr.ghxu = [];
dr.ghs2 = []; dr.ghs2 = [];
Gy = dr.Gy;
kstate = dr.kstate; k1 = nonzeros(M.lead_lag_incidence(:,dr.order_var)');
nstatic = M_.nstatic; kk1 = [k1; length(k1)+(1:M.exo_nbr+M.exo_det_nbr)'];
nfwrd = M_.nfwrd; nk = size(kk1,1);
nspred = M_.nspred; kk2 = reshape(1:nk^2,nk,nk);
nboth = M_.nboth; ic = [ M.nstatic+(1:M.nspred) M.endo_nbr+(1:size(dr.ghx,2)-M.nspred) ]';
nsfwrd = M_.nsfwrd;
order_var = dr.order_var; klag = M.lead_lag_incidence(1,dr.order_var); %columns are in DR order
nd = size(kstate,1); kcurr = M.lead_lag_incidence(2,dr.order_var); %columns are in DR order
lead_lag_incidence = M_.lead_lag_incidence; klead = M.lead_lag_incidence(3,dr.order_var); %columns are in DR order
np = nd - nsfwrd; %% ghxx
A = zeros(M.endo_nbr,M.endo_nbr);
k1 = nonzeros(lead_lag_incidence(:,order_var)'); A(:,kcurr~=0) = jacobia(:,nonzeros(kcurr));
kk = [k1; length(k1)+(1:M_.exo_nbr+M_.exo_det_nbr)']; A(:,ic) = A(:,ic) + jacobia(:,nonzeros(klead))*dr.ghx(klead~=0,:);
nk = size(kk,1); B = zeros(M.endo_nbr,M.endo_nbr);
kk1 = reshape([1:nk^2],nk,nk); B(:,M.nstatic+M.npred+1:end) = jacobia(:,nonzeros(klead));
kk1 = kk1(kk,kk); C = dr.ghx(ic,:);
% reordering second order derivatives zx = [eye(length(ic));
hessian_mat = hessian_mat(:,kk1(:)); dr.ghx(kcurr~=0,:);
dr.ghx(klead~=0,:)*dr.ghx(ic,:);
zx = zeros(np,np); zeros(M.exo_nbr,length(ic));
zu=zeros(np,M_.exo_nbr); zeros(M.exo_det_nbr,length(ic))];
zx(1:np,:)=eye(np); zu = [zeros(length(ic),M.exo_nbr);
k0 = [1:M_.endo_nbr]; dr.ghu(kcurr~=0,:);
gx1 = dr.ghx; dr.ghx(klead~=0,:)*dr.ghu(ic,:);
hu = dr.ghu(nstatic+[1:nspred],:); eye(M.exo_nbr);
k0 = find(lead_lag_incidence(M_.maximum_endo_lag+1,order_var)'); zeros(M.exo_det_nbr,M.exo_nbr)];
zx = [zx; gx1(k0,:)]; [rhs, err] = sparse_hessian_times_B_kronecker_C(hessian_mat(:,kk2(kk1,kk1)),zx,threads_BC); %hessian_mat: reordering to DR order
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_mat,zx,threads_BC);
mexErrCheck('sparse_hessian_times_B_kronecker_C', err); mexErrCheck('sparse_hessian_times_B_kronecker_C', err);
rhs = -rhs; rhs = -rhs;
[err, dr.ghxx] = gensylv(2,A,B,C,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;
[~,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); mexErrCheck('gensylv', err);
%ghxu
%% ghxu
%rhs %rhs
hu = dr.ghu(nstatic+1:nstatic+nspred,:); [rhs, err] = sparse_hessian_times_B_kronecker_C(hessian_mat(:,kk2(kk1,kk1)),zx,zu,threads_BC); %hessian_mat: reordering to DR order
[rhs, err] = sparse_hessian_times_B_kronecker_C(hessian_mat,zx,zu,threads_BC);
mexErrCheck('sparse_hessian_times_B_kronecker_C', err); mexErrCheck('sparse_hessian_times_B_kronecker_C', err);
[abcOut,err] = A_times_B_kronecker_C(dr.ghxx, dr.ghx(ic,:), dr.ghu(ic,:));
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);
mexErrCheck('A_times_B_kronecker_C', err); mexErrCheck('A_times_B_kronecker_C', err);
B1 = B*abcOut; rhs = -rhs-B*abcOut;
rhs = -[rhs; zeros(n-M_.endo_nbr,size(rhs,2))]-B1;
%lhs %lhs
dr.ghxu = A\rhs; dr.ghxu = A\rhs;
%ghuu %% ghuu
%rhs %rhs
[rhs, err] = sparse_hessian_times_B_kronecker_C(hessian_mat,zu,threads_BC); [rhs, err] = sparse_hessian_times_B_kronecker_C(hessian_mat(:,kk2(kk1,kk1)),zu,threads_BC); %hessian_mat: reordering to DR order
mexErrCheck('sparse_hessian_times_B_kronecker_C', err); mexErrCheck('sparse_hessian_times_B_kronecker_C', err);
[B1, err] = A_times_B_kronecker_C(B*dr.ghxx,dr.ghu(ic,:));
[B1, err] = A_times_B_kronecker_C(B*dr.ghxx,hu1);
mexErrCheck('A_times_B_kronecker_C', err); mexErrCheck('A_times_B_kronecker_C', err);
rhs = -[rhs; zeros(n-M_.endo_nbr,size(rhs,2))]-B1; rhs = -rhs-B1;
%lhs %lhs
dr.ghuu = A\rhs; dr.ghuu = A\rhs;
% dr.ghs2 %% ghs2
% derivatives of F with respect to forward variables % derivatives of F with respect to forward variables
% reordering predetermined variables in diminishing lag order O1 = zeros(M.endo_nbr,M.nstatic);
O1 = zeros(M_.endo_nbr,nstatic); O2 = zeros(M.endo_nbr,M.nfwrd);
O2 = zeros(M_.endo_nbr,M_.endo_nbr-nstatic-nspred); LHS = zeros(M.endo_nbr,M.endo_nbr);
LHS = zeros(M_.endo_nbr,M_.endo_nbr); LHS(:,kcurr~=0) = jacobia(:,nonzeros(kcurr));
LHS(:,k0) = jacobia(:,nonzeros(lead_lag_incidence(M_.maximum_endo_lag+1,order_var))); RHS = zeros(M.endo_nbr,M.exo_nbr^2);
RHS = zeros(M_.endo_nbr,M_.exo_nbr^2); E = eye(M.endo_nbr);
gu = dr.ghu; [B1, err] = sparse_hessian_times_B_kronecker_C(hessian_mat(:,kk2(nonzeros(klead),nonzeros(klead))), dr.ghu(klead~=0,:),threads_BC); %hessian_mat:focus only on forward variables and reorder to DR order
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],:);
[~,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_mat(:,kh(k3,k3)),gu(k2a,:),threads_BC);
mexErrCheck('sparse_hessian_times_B_kronecker_C', err); mexErrCheck('sparse_hessian_times_B_kronecker_C', err);
RHS = RHS + jacobia(:,k2)*guu(k2a,:)+B1; RHS = RHS + jacobia(:,nonzeros(klead))*dr.ghuu(klead~=0,:)+B1;
% LHS % LHS
LHS = LHS + jacobia(:,k2)*(E(k2a,:)+[O1(k2a,:) dr.ghx(k2a,:)*H O2(k2a,:)]); LHS = LHS + jacobia(:,nonzeros(klead))*(E(klead~=0,:)+[O1(klead~=0,:) dr.ghx(klead~=0,:) O2(klead~=0,:)]);
RHS = RHS*M.Sigma_e(:);
RHS = RHS*M_.Sigma_e(:);
dr.fuu = RHS; dr.fuu = RHS;
%RHS = -RHS-dr.fbias;
RHS = -RHS; RHS = -RHS;
dr.ghs2 = LHS\RHS; dr.ghs2 = LHS\RHS;
% deterministic exogenous variables
if M_.exo_det_nbr > 0
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment