Added use of G Anderson AIM - see Readme.txt in the new AIM subdirectory.
DR1 tries to invoke AIM if options_.useAIM == 1 is set. for start, options_.useAIM = 0 is set by default in global_initialization.m so that system uses mjdgges by default. If AIM is to be used, options_.useAIM = 1 needs to be set either in the model <>.mod file, before invoking, estimate and/or stoch_simul, or by issuing appropriate command for estimate and/or stoch_simul. However, if other exceptions occur and aimcode is higher than 5, the system resets options_.useAIM = 0 tries to use mjdgges instead. GP July '08 git-svn-id: https://www.dynare.org/svn/dynare/dynare_v4@1967 ac1d8469-bf42-47a9-8791-bf33cf982152time-shift
parent
bee92a9714
commit
827261166c
317
matlab/dr1.m
317
matlab/dr1.m
|
@ -209,17 +209,36 @@ function [dr,info,M_,options_,oo_] = dr1(dr,task,M_,options_,oo_)
|
||||||
b = jacobia_(:,k0);
|
b = jacobia_(:,k0);
|
||||||
|
|
||||||
if M_.maximum_endo_lead == 0; % backward models
|
if M_.maximum_endo_lead == 0; % backward models
|
||||||
a = jacobia_(:,nonzeros(k1'));
|
% If required, try Gary Anderson and G Moore AIM solver if not
|
||||||
dr.ghx = zeros(size(a));
|
% check only and if 1st order (added by GP July'08)
|
||||||
m = 0;
|
if (options_.useAIM == 1) && (task == 0) && (options_.order == 1)
|
||||||
for i=M_.maximum_endo_lag:-1:1
|
try
|
||||||
k = nonzeros(M_.lead_lag_incidence(i,order_var));
|
[dr,aimcode]=dynAIMsolver1(jacobia_,M_,dr);
|
||||||
dr.ghx(:,m+[1:length(k)]) = -b\a(:,k);
|
if aimcode ~=1
|
||||||
m = m+length(k);
|
info(1) = aimcode;
|
||||||
end
|
info(2) = 1.0e+8;
|
||||||
if M_.exo_nbr
|
return
|
||||||
dr.ghu = -b\jacobia_(:,nz+1:end);
|
end
|
||||||
end
|
catch
|
||||||
|
%warning('Problem with using AIM solver - Using Dynare solver instead');
|
||||||
|
disp('Problem with using AIM solver - Using Dynare solver instead');
|
||||||
|
options_.useAIM = 0; % and try mjdgges instead
|
||||||
|
end
|
||||||
|
end % end if useAIM and...
|
||||||
|
%else use original Dynare solver
|
||||||
|
if ~((options_.useAIM == 1) && (task == 0) && (options_.order == 1))
|
||||||
|
a = jacobia_(:,nonzeros(k1'));
|
||||||
|
dr.ghx = zeros(size(a));
|
||||||
|
m = 0;
|
||||||
|
for i=M_.maximum_endo_lag:-1:1
|
||||||
|
k = nonzeros(M_.lead_lag_incidence(i,order_var));
|
||||||
|
dr.ghx(:,m+[1:length(k)]) = -b\a(:,k);
|
||||||
|
m = m+length(k);
|
||||||
|
end
|
||||||
|
if M_.exo_nbr
|
||||||
|
dr.ghu = -b\jacobia_(:,nz+1:end);
|
||||||
|
end
|
||||||
|
end % if not use AIM or not...
|
||||||
dr.eigval = eig(transition_matrix(dr));
|
dr.eigval = eig(transition_matrix(dr));
|
||||||
dr.rank = 0;
|
dr.rank = 0;
|
||||||
if any(abs(dr.eigval) > options_.qz_criterium)
|
if any(abs(dr.eigval) > options_.qz_criterium)
|
||||||
|
@ -239,121 +258,177 @@ function [dr,info,M_,options_,oo_] = dr1(dr,task,M_,options_,oo_)
|
||||||
else
|
else
|
||||||
aa = jacobia_;
|
aa = jacobia_;
|
||||||
end
|
end
|
||||||
a = aa(:,nonzeros(k1'));
|
|
||||||
b = aa(:,k0);
|
|
||||||
b10 = b(1:nstatic,1:nstatic);
|
|
||||||
b11 = b(1:nstatic,nstatic+1:end);
|
|
||||||
b2 = b(nstatic+1:end,nstatic+1:end);
|
|
||||||
if any(isinf(a(:)))
|
|
||||||
info = 1;
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
% buildind D and E
|
% If required, try Gary Anderson and G Moore AIM solver, that is, if not check
|
||||||
d = zeros(nd,nd) ;
|
% only and if 1st order (added by GP July'08)
|
||||||
e = d ;
|
if (options_.useAIM == 1) && (task == 0) && (options_.order == 1)
|
||||||
|
try
|
||||||
|
[dr,aimcode]=dynAIMsolver1(aa,M_,dr);
|
||||||
|
|
||||||
k = find(kstate(:,2) >= M_.maximum_endo_lag+2 & kstate(:,3));
|
% reuse some of the bypassed code and tests that may be needed
|
||||||
d(1:sdyn,k) = a(nstatic+1:end,kstate(k,3)) ;
|
|
||||||
k1 = find(kstate(:,2) == M_.maximum_endo_lag+2);
|
|
||||||
e(1:sdyn,k1) = -b2(:,kstate(k1,1)-nstatic);
|
|
||||||
k = find(kstate(:,2) <= M_.maximum_endo_lag+1 & kstate(:,4));
|
|
||||||
e(1:sdyn,k) = -a(nstatic+1:end,kstate(k,4)) ;
|
|
||||||
k2 = find(kstate(:,2) == M_.maximum_endo_lag+1);
|
|
||||||
k2 = k2(~ismember(kstate(k2,1),kstate(k1,1)));
|
|
||||||
d(1:sdyn,k2) = b2(:,kstate(k2,1)-nstatic);
|
|
||||||
|
|
||||||
if ~isempty(kad)
|
if aimcode ~=1
|
||||||
for j = 1:size(kad,1)
|
info(1) = aimcode;
|
||||||
d(sdyn+j,kad(j)) = 1 ;
|
info(2) = 1.0e+8;
|
||||||
e(sdyn+j,kae(j)) = 1 ;
|
return
|
||||||
|
end
|
||||||
|
[A,B] =transition_matrix(dr);
|
||||||
|
dr.eigval = eig(A);
|
||||||
|
% if any(abs(dr.eigval) > options_.qz_criterium)
|
||||||
|
% temp = sort(abs(dr.eigval));
|
||||||
|
% nba = nnz(abs(dr.eigval) > options_.qz_criterium);
|
||||||
|
% temp = temp(nd-nba+1:nd)-1-options_.qz_criterium;
|
||||||
|
% info(1) = 3;
|
||||||
|
% info(2) = temp'*temp;
|
||||||
|
% return
|
||||||
|
% end
|
||||||
|
sdim = sum( abs(dr.eigval) < options_.qz_criterium );
|
||||||
|
nba = nd-sdim;
|
||||||
|
|
||||||
|
nyf = sum(kstate(:,2) > M_.maximum_endo_lag+1);
|
||||||
|
if nba ~= nyf
|
||||||
|
temp = sort(abs(dr.eigval));
|
||||||
|
if nba > nyf
|
||||||
|
temp = temp(nd-nba+1:nd-nyf)-1-options_.qz_criterium;
|
||||||
|
info(1) = 3
|
||||||
|
elseif nba < nyf;
|
||||||
|
temp = temp(nd-nyf+1:nd-nba)-1-options_.qz_criterium;
|
||||||
|
info(1) = 4
|
||||||
|
end
|
||||||
|
info(2) = temp'*temp;
|
||||||
|
return
|
||||||
|
end
|
||||||
|
catch
|
||||||
|
%warning('Problem with using AIM solver - Using Dynare solver instead');
|
||||||
|
disp('Problem with using AIM solver - Using Dynare solver instead');
|
||||||
|
options_.useAIM = 0; % and then try mjdgges instead
|
||||||
end
|
end
|
||||||
end
|
end % end if useAIM and...
|
||||||
|
%else % use original Dynare solver
|
||||||
[ss,tt,w,sdim,dr.eigval,info1] = mjdgges(e,d,options_.qz_criterium);
|
if ~((options_.useAIM == 1)&& (task == 0) && (options_.order == 1)) % || isempty(options_.useAIM)
|
||||||
|
a = aa(:,nonzeros(k1'));
|
||||||
if info1
|
b = aa(:,k0);
|
||||||
info(1) = 2;
|
b10 = b(1:nstatic,1:nstatic);
|
||||||
info(2) = info1;
|
b11 = b(1:nstatic,nstatic+1:end);
|
||||||
return
|
b2 = b(nstatic+1:end,nstatic+1:end);
|
||||||
end
|
if any(isinf(a(:)))
|
||||||
|
info = 1;
|
||||||
nba = nd-sdim;
|
return
|
||||||
|
|
||||||
nyf = sum(kstate(:,2) > M_.maximum_endo_lag+1);
|
|
||||||
|
|
||||||
if task == 1
|
|
||||||
dr.rank = rank(w(1:nyf,nd-nyf+1:end));
|
|
||||||
% Under Octave, eig(A,B) doesn't exist, and
|
|
||||||
% lambda = qz(A,B) won't return infinite eigenvalues
|
|
||||||
if ~exist('OCTAVE_VERSION')
|
|
||||||
dr.eigval = eig(e,d);
|
|
||||||
end
|
end
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
if nba ~= nyf
|
% buildind D and E
|
||||||
temp = sort(abs(dr.eigval));
|
d = zeros(nd,nd) ;
|
||||||
if nba > nyf
|
e = d ;
|
||||||
temp = temp(nd-nba+1:nd-nyf)-1-options_.qz_criterium;
|
|
||||||
info(1) = 3;
|
k = find(kstate(:,2) >= M_.maximum_endo_lag+2 & kstate(:,3));
|
||||||
elseif nba < nyf;
|
d(1:sdyn,k) = a(nstatic+1:end,kstate(k,3)) ;
|
||||||
temp = temp(nd-nyf+1:nd-nba)-1-options_.qz_criterium;
|
k1 = find(kstate(:,2) == M_.maximum_endo_lag+2);
|
||||||
info(1) = 4;
|
e(1:sdyn,k1) = -b2(:,kstate(k1,1)-nstatic);
|
||||||
|
k = find(kstate(:,2) <= M_.maximum_endo_lag+1 & kstate(:,4));
|
||||||
|
e(1:sdyn,k) = -a(nstatic+1:end,kstate(k,4)) ;
|
||||||
|
k2 = find(kstate(:,2) == M_.maximum_endo_lag+1);
|
||||||
|
k2 = k2(~ismember(kstate(k2,1),kstate(k1,1)));
|
||||||
|
d(1:sdyn,k2) = b2(:,kstate(k2,1)-nstatic);
|
||||||
|
|
||||||
|
if ~isempty(kad)
|
||||||
|
for j = 1:size(kad,1)
|
||||||
|
d(sdyn+j,kad(j)) = 1 ;
|
||||||
|
e(sdyn+j,kae(j)) = 1 ;
|
||||||
|
end
|
||||||
end
|
end
|
||||||
info(2) = temp'*temp;
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
np = nd - nyf;
|
% 1) if mjdgges.dll (or .mexw32 or ....) doesn't exit,
|
||||||
n2 = np + 1;
|
% matlab/qz is added to the path. There exists now qz/mjdgges.m that
|
||||||
n3 = nyf;
|
% contains the calls to the old Sims code
|
||||||
n4 = n3 + 1;
|
% 2) In global_initialization.m, if mjdgges.m is visible exist(...)==2,
|
||||||
% derivatives with respect to dynamic state variables
|
% this means that the DLL isn't avaiable and use_qzdiv is set to 1
|
||||||
% forward variables
|
|
||||||
w1 =w(1:n3,n2:nd);
|
|
||||||
if condest(w1) > 1e9;
|
|
||||||
info(1) = 5;
|
|
||||||
info(2) = condest(w1);
|
|
||||||
return;
|
|
||||||
else
|
|
||||||
gx = -w1'\w(n4:nd,n2:nd)';
|
|
||||||
end
|
|
||||||
|
|
||||||
% predetermined variables
|
[ss,tt,w,sdim,dr.eigval,info1] = mjdgges(e,d,options_.qz_criterium);
|
||||||
hx = w(1:n3,1:np)'*gx+w(n4:nd,1:np)';
|
|
||||||
hx = (tt(1:np,1:np)*hx)\(ss(1:np,1:np)*hx);
|
|
||||||
|
|
||||||
k1 = find(kstate(n4:nd,2) == M_.maximum_endo_lag+1);
|
if info1
|
||||||
k2 = find(kstate(1:n3,2) == M_.maximum_endo_lag+2);
|
info(1) = 2;
|
||||||
dr.ghx = [hx(k1,:); gx(k2(nboth+1:end),:)];
|
info(2) = info1;
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
%lead variables actually present in the model
|
nba = nd-sdim;
|
||||||
j3 = nonzeros(kstate(:,3));
|
|
||||||
j4 = find(kstate(:,3));
|
nyf = sum(kstate(:,2) > M_.maximum_endo_lag+1);
|
||||||
% derivatives with respect to exogenous variables
|
|
||||||
if M_.exo_nbr
|
if task == 1
|
||||||
fu = aa(:,nz+(1:M_.exo_nbr));
|
dr.rank = rank(w(1:nyf,nd-nyf+1:end));
|
||||||
a1 = b;
|
% Under Octave, eig(A,B) doesn't exist, and
|
||||||
aa1 = [];
|
% lambda = qz(A,B) won't return infinite eigenvalues
|
||||||
|
if ~exist('OCTAVE_VERSION')
|
||||||
|
dr.eigval = eig(e,d);
|
||||||
|
end
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
if nba ~= nyf
|
||||||
|
temp = sort(abs(dr.eigval));
|
||||||
|
if nba > nyf
|
||||||
|
temp = temp(nd-nba+1:nd-nyf)-1-options_.qz_criterium;
|
||||||
|
info(1) = 3
|
||||||
|
elseif nba < nyf;
|
||||||
|
temp = temp(nd-nyf+1:nd-nba)-1-options_.qz_criterium;
|
||||||
|
info(1) = 4;
|
||||||
|
end
|
||||||
|
info(2) = temp'*temp;
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
np = nd - nyf;
|
||||||
|
n2 = np + 1;
|
||||||
|
n3 = nyf;
|
||||||
|
n4 = n3 + 1;
|
||||||
|
% derivatives with respect to dynamic state variables
|
||||||
|
% forward variables
|
||||||
|
w1 =w(1:n3,n2:nd);
|
||||||
|
if condest(w1) > 1e9;
|
||||||
|
info(1) = 5;
|
||||||
|
info(2) = condest(w1);
|
||||||
|
return;
|
||||||
|
else
|
||||||
|
gx = -w1'\w(n4:nd,n2:nd)';
|
||||||
|
end
|
||||||
|
|
||||||
|
% predetermined variables
|
||||||
|
hx = w(1:n3,1:np)'*gx+w(n4:nd,1:np)';
|
||||||
|
hx = (tt(1:np,1:np)*hx)\(ss(1:np,1:np)*hx);
|
||||||
|
|
||||||
|
k1 = find(kstate(n4:nd,2) == M_.maximum_endo_lag+1);
|
||||||
|
k2 = find(kstate(1:n3,2) == M_.maximum_endo_lag+2);
|
||||||
|
dr.ghx = [hx(k1,:); gx(k2(nboth+1:end),:)];
|
||||||
|
|
||||||
|
%lead variables actually present in the model
|
||||||
|
j3 = nonzeros(kstate(:,3));
|
||||||
|
j4 = find(kstate(:,3));
|
||||||
|
% derivatives with respect to exogenous variables
|
||||||
|
if M_.exo_nbr
|
||||||
|
fu = aa(:,nz+(1:M_.exo_nbr));
|
||||||
|
a1 = b;
|
||||||
|
aa1 = [];
|
||||||
|
if nstatic > 0
|
||||||
|
aa1 = a1(:,1:nstatic);
|
||||||
|
end
|
||||||
|
dr.ghu = -[aa1 a(:,j3)*gx(j4,1:npred)+a1(:,nstatic+1:nstatic+ ...
|
||||||
|
npred) a1(:,nstatic+npred+1:end)]\fu;
|
||||||
|
else
|
||||||
|
dr.ghu = [];
|
||||||
|
end
|
||||||
|
|
||||||
|
% static variables
|
||||||
if nstatic > 0
|
if nstatic > 0
|
||||||
aa1 = a1(:,1:nstatic);
|
temp = -a(1:nstatic,j3)*gx(j4,:)*hx;
|
||||||
|
j5 = find(kstate(n4:nd,4));
|
||||||
|
temp(:,j5) = temp(:,j5)-a(1:nstatic,nonzeros(kstate(:,4)));
|
||||||
|
temp = b10\(temp-b11*dr.ghx);
|
||||||
|
dr.ghx = [temp; dr.ghx];
|
||||||
|
temp = [];
|
||||||
end
|
end
|
||||||
dr.ghu = -[aa1 a(:,j3)*gx(j4,1:npred)+a1(:,nstatic+1:nstatic+ ...
|
end % if not use AIM and ....
|
||||||
npred) a1(:,nstatic+npred+1:end)]\fu;
|
% End of if... and if not... main AIM Blocks, continue as per usual...
|
||||||
else
|
|
||||||
dr.ghu = [];
|
|
||||||
end
|
|
||||||
|
|
||||||
% static variables
|
|
||||||
if nstatic > 0
|
|
||||||
temp = -a(1:nstatic,j3)*gx(j4,:)*hx;
|
|
||||||
j5 = find(kstate(n4:nd,4));
|
|
||||||
temp(:,j5) = temp(:,j5)-a(1:nstatic,nonzeros(kstate(:,4)));
|
|
||||||
temp = b10\(temp-b11*dr.ghx);
|
|
||||||
dr.ghx = [temp; dr.ghx];
|
|
||||||
temp = [];
|
|
||||||
end
|
|
||||||
|
|
||||||
if options_.loglinear == 1
|
if options_.loglinear == 1
|
||||||
k = find(dr.kstate(:,2) <= M_.maximum_endo_lag+1);
|
k = find(dr.kstate(:,2) <= M_.maximum_endo_lag+1);
|
||||||
|
@ -365,13 +440,15 @@ function [dr,info,M_,options_,oo_] = dr1(dr,task,M_,options_,oo_)
|
||||||
dr.ghu = repmat(1./dr.ys(k1),1,size(dr.ghu,2)).*dr.ghu;
|
dr.ghu = repmat(1./dr.ys(k1),1,size(dr.ghu,2)).*dr.ghu;
|
||||||
end
|
end
|
||||||
|
|
||||||
%% Necessary when using Sims' routines for QZ
|
if ~((options_.useAIM == 1) && (options_.order == 1)) % if not use AIM ...
|
||||||
if options_.use_qzdiv
|
%% Necessary when using Sims' routines for QZ
|
||||||
gx = real(gx);
|
if options_.use_qzdiv
|
||||||
hx = real(hx);
|
gx = real(gx);
|
||||||
dr.ghx = real(dr.ghx);
|
hx = real(hx);
|
||||||
dr.ghu = real(dr.ghu);
|
dr.ghx = real(dr.ghx);
|
||||||
end
|
dr.ghu = real(dr.ghu);
|
||||||
|
end
|
||||||
|
end % if not use AIM
|
||||||
|
|
||||||
%exogenous deterministic variables
|
%exogenous deterministic variables
|
||||||
if M_.exo_det_nbr > 0
|
if M_.exo_det_nbr > 0
|
||||||
|
|
Loading…
Reference in New Issue