Commit ef1cf5f0 authored by Stéphane Adjemian (Charybdis)'s avatar Stéphane Adjemian (Charybdis)

Merge remote-tracking branch 'marco/master'

parents a9839333 9d595340
function [AHess] = AHessian(T,R,Q,H,P,Y,DT,DYss,DOm,DH,DP,start,mf,kalman_tol,riccati_tol)
% function [AHess] = AHessian(T,R,Q,H,P,Y,DT,DYss,DOm,DH,DP,start,mf,kalman_tol,riccati_tol)
function [AHess, DLIK, LIK] = AHessian(T,R,Q,H,P,Y,DT,DYss,DOm,DH,DP,start,mf,kalman_tol,riccati_tol)
% function [AHess, DLIK, LIK] = AHessian(T,R,Q,H,P,Y,DT,DYss,DOm,DH,DP,start,mf,kalman_tol,riccati_tol)
%
% computes the asymptotic hessian matrix of the log-likelihood function of
% a state space model (notation as in kalman_filter.m in DYNARE
......@@ -28,6 +28,7 @@ function [AHess] = AHessian(T,R,Q,H,P,Y,DT,DYss,DOm,DH,DP,start,mf,kalman_tol,ri
k = size(DT,3); % number of structural parameters
smpl = size(Y,2); % Sample size.
pp = size(Y,1); % Maximum number of observed variables.
mm = size(T,2); % Number of state variables.
a = zeros(mm,1); % State vector.
Om = R*Q*transpose(R); % Variance of R times the vector of structural innovations.
......@@ -36,6 +37,11 @@ function [AHess] = AHessian(T,R,Q,H,P,Y,DT,DYss,DOm,DH,DP,start,mf,kalman_tol,ri
notsteady = 1; % Steady state flag.
F_singular = 1;
lik = zeros(smpl,1); % Initialization of the vector gathering the densities.
LIK = Inf; % Default value of the log likelihood.
if nargout > 1,
DLIK = zeros(k,1); % Initialization of the score.
end
AHess = zeros(k,k); % Initialization of the Hessian
Da = zeros(mm,k); % State vector.
Dv = zeros(length(mf),k);
......@@ -59,17 +65,21 @@ function [AHess] = AHessian(T,R,Q,H,P,Y,DT,DYss,DOm,DH,DP,start,mf,kalman_tol,ri
F_singular = 0;
iF = inv(F);
K = P(:,mf)*iF;
lik(t) = log(det(F))+transpose(v)*iF*v;
[DK,DF,DP1] = computeDKalman(T,DT,DOm,P,DP,DH,mf,iF,K);
for ii = 1:k
Dv(:,ii) = -Da(mf,ii) - DYss(mf,ii);
Da(:,ii) = DT(:,:,ii)*(a+K*v) + T*(Da(:,ii)+DK(:,:,ii)*v + K*Dv(:,ii));
if t>=start && nargout > 1
DLIK(ii,1) = DLIK(ii,1) + trace( iF*DF(:,:,ii) ) + 2*Dv(:,ii)'*iF*v - v'*(iF*DF(:,:,ii)*iF)*v;
end
end
vecDPmf = reshape(DP(mf,mf,:),[],k);
iPmf = inv(P(mf,mf));
% iPmf = inv(P(mf,mf));
if t>=start
AHess = AHess + Dv'*iPmf*Dv + .5*(vecDPmf' * kron(iPmf,iPmf) * vecDPmf);
AHess = AHess + Dv'*iF*Dv + .5*(vecDPmf' * kron(iF,iF) * vecDPmf);
end
a = T*(a+K*v);
P = T*(P-K*P(mf,:))*transpose(T)+Om;
......@@ -92,13 +102,23 @@ function [AHess] = AHessian(T,R,Q,H,P,Y,DT,DYss,DOm,DH,DP,start,mf,kalman_tol,ri
for ii = 1:k
Dv(:,ii) = -Da(mf,ii)-DYss(mf,ii);
Da(:,ii) = DT(:,:,ii)*(a+K*v) + T*(Da(:,ii)+DK(:,:,ii)*v + K*Dv(:,ii));
if t>=start && nargout >1
DLIK(ii,1) = DLIK(ii,1) + trace( iF*DF(:,:,ii) ) + 2*Dv(:,ii)'*iF*v - v'*(iF*DF(:,:,ii)*iF)*v;
end
end
if t>=start
AHess = AHess + Dv'*iPmf*Dv;
AHess = AHess + Dv'*iF*Dv;
end
a = T*(a+K*v);
lik(t) = transpose(v)*iF*v;
end
AHess = AHess + .5*(smpl+t0-1)*(vecDPmf' * kron(iF,iF) * vecDPmf);
if nargout > 1
for ii = 1:k
% DLIK(ii,1) = DLIK(ii,1) + (smpl-t0+1)*trace( iF*DF(:,:,ii) );
end
end
AHess = AHess + .5*(smpl+t0-1)*(vecDPmf' * kron(iPmf,iPmf) * vecDPmf);
lik(t0:smpl) = lik(t0:smpl) + log(det(F));
% for ii = 1:k;
% for jj = 1:ii
% H(ii,jj) = trace(iPmf*(.5*DP(mf,mf,ii)*iPmf*DP(mf,mf,jj) + Dv(:,ii)*Dv(:,jj)'));
......@@ -107,6 +127,13 @@ function [AHess] = AHessian(T,R,Q,H,P,Y,DT,DYss,DOm,DH,DP,start,mf,kalman_tol,ri
end
AHess = -AHess;
if nargout > 1,
DLIK = DLIK/2;
end
% adding log-likelihhod constants
lik = (lik + pp*log(2*pi))/2;
LIK = sum(lik(start:end)); % Minus the log-likelihood.
% end of main function
function [DK,DF,DP1] = computeDKalman(T,DT,DOm,P,DP,DH,mf,iF,K)
......
......@@ -439,9 +439,10 @@ end
if analytic_derivation
no_DLIK = 0;
full_Hess = 0;
DLIK = [];
AHess = [];
if nargin<7 || isempty(derivatives_info)
if nargin<8 || isempty(derivatives_info)
[A,B,nou,nou,Model,DynareOptions,DynareResults] = dynare_resolve(Model,DynareOptions,DynareResults);
if ~isempty(EstimatedParameters.var_exo)
indexo=EstimatedParameters.var_exo(:,1);
......@@ -453,12 +454,25 @@ if analytic_derivation
else
indparam=[];
end
[dum, DT, DOm, DYss] = getH(A,B,Model,DynareResults,0,indparam,indexo);
if full_Hess,
[dum, DT, DOm, DYss, dum2, D2T, D2Om, D2Yss] = getH(A, B, Model,DynareResults,0,indparam,indexo);
else
[dum, DT, DOm, DYss] = getH(A, B, Model,DynareResults,0,indparam,indexo);
end
else
DT = derivatives_info.DT;
DOm = derivatives_info.DOm;
DYss = derivatives_info.DYss;
if isfield(derivatives_info,'no_DLIK')
if isfield(derivatives_info,'full_Hess'),
full_Hess = derivatives_info.full_Hess;
end
if full_Hess,
D2T = derivatives_info.D2T;
D2Om = derivatives_info.D2Om;
D2Yss = derivatives_info.D2Yss;
end
if isfield(derivatives_info,'no_DLIK'),
no_DLIK = derivatives_info.no_DLIK;
end
clear('derivatives_info');
......@@ -471,6 +485,17 @@ if analytic_derivation
DH=zeros([size(H),length(xparam1)]);
DQ=zeros([size(Q),length(xparam1)]);
DP=zeros([size(T),length(xparam1)]);
if full_Hess,
for j=1:size(D2Yss,1),
tmp(j,:,:) = blkdiag(zeros(offset,offset), squeeze(D2Yss(j,:,:)));
end
D2Yss = tmp;
D2T = D2T(iv,iv,:,:);
D2Om = D2Om(iv,iv,:,:);
D2Yss = D2Yss(iv,:,:);
D2H=zeros([size(H),length(xparam1),length(xparam1)]);
D2P=zeros([size(T),length(xparam1),length(xparam1)]);
end
for i=1:EstimatedParameters.nvx
k =EstimatedParameters.var_exo(i,1);
DQ(k,k,i) = 2*sqrt(Q(k,k));
......@@ -478,11 +503,23 @@ if analytic_derivation
kk = find(abs(dum) < 1e-12);
dum(kk) = 0;
DP(:,:,i)=dum;
if full_Hess
for j=1:i,
dum = lyapunov_symm(T,D2Om(:,:,i,j),DynareOptions.qz_criterium,DynareOptions.lyapunov_complex_threshold);
kk = (abs(dum) < 1e-12);
dum(kk) = 0;
D2P(:,:,i,j)=dum;
D2P(:,:,j,i)=dum;
end
end
end
offset = EstimatedParameters.nvx;
for i=1:EstimatedParameters.nvn
k = EstimatedParameters.var_endo(i,1);
DH(k,k,i+offset) = 2*sqrt(H(k,k));
if full_Hess
D2H(k,k,i+offset,i+offset) = 2;
end
end
offset = offset + EstimatedParameters.nvn;
for j=1:EstimatedParameters.np
......@@ -490,6 +527,21 @@ if analytic_derivation
kk = find(abs(dum) < 1e-12);
dum(kk) = 0;
DP(:,:,j+offset)=dum;
if full_Hess
DTj = DT(:,:,j+offset);
DPj = dum;
for i=1:j,
DTi = DT(:,:,i+offset);
DPi = DP(:,:,i+offset);
D2Tij = D2T(:,:,i,j);
D2Omij = D2Om(:,:,i,j);
tmp = D2Tij*Pstar*T' + T*Pstar*D2Tij' + DTi*DPj*T' + DTj*DPi*T' + T*DPj*DTi' + T*DPi*DTj' + DTi*Pstar*DTj' + DTj*Pstar*DTi' + D2Omij;
dum = lyapunov_symm(T,tmp,DynareOptions.qz_criterium,DynareOptions.lyapunov_complex_threshold);
dum(abs(dum)<1.e-12) = 0;
D2P(:,:,i+offset,j+offset) = dum;
D2P(:,:,j+offset,i+offset) = dum;
end
end
end
end
......@@ -515,6 +567,10 @@ if ((kalman_algo==1) || (kalman_algo==3))% Multivariate Kalman Filter
end
if nargout==11
[AHess] = AHessian(T,R,Q,H,Pstar,Y,DT,DYss,DOm,DH,DP,start,Z,kalman_tol,riccati_tol);
if full_Hess,
Hess = get_Hessian(T,R,Q,H,Pstar,Y,DT,DYss,DOm,DH,DP,D2T,D2Yss,D2Om,D2H,D2P,start,Z,kalman_tol,riccati_tol);
Hess0 = getHessian(Y,T,DT,D2T, R*Q*transpose(R),DOm,D2Om,Z,DYss,D2Yss);
end
end
end
else
......@@ -587,7 +643,19 @@ end
% ------------------------------------------------------------------------------
% 5. Adds prior if necessary
% ------------------------------------------------------------------------------
lnprior = priordens(xparam1,BayesInfo.pshape,BayesInfo.p6,BayesInfo.p7,BayesInfo.p3,BayesInfo.p4);
if analytic_derivation
if full_Hess,
[lnprior, dlnprior, d2lnprior] = priordens(xparam1,BayesInfo.pshape,BayesInfo.p6,BayesInfo.p7,BayesInfo.p3,BayesInfo.p4);
AHess = Hess + d2lnprior;
else
[lnprior, dlnprior] = priordens(xparam1,BayesInfo.pshape,BayesInfo.p6,BayesInfo.p7,BayesInfo.p3,BayesInfo.p4);
end
if no_DLIK==0
DLIK = DLIK - dlnprior';
end
else
lnprior = priordens(xparam1,BayesInfo.pshape,BayesInfo.p6,BayesInfo.p7,BayesInfo.p3,BayesInfo.p4);
end
fval = (likelihood-lnprior);
% Update DynareOptions.kalman_algo.
......
function [fval,llik,cost_flag,ys,trend_coeff,info] = DsgeLikelihood_hh(xparam1,DynareDataset,DynareOptions,Model,EstimatedParameters,BayesInfo,DynareResults)
% function [fval,cost_flag,ys,trend_coeff,info] = DsgeLikelihood(xparam1,gend,data,data_index,number_of_observations,no_more_missing_observations)
% function [fval,llik,cost_flag,ys,trend_coeff,info] = DsgeLikelihood_hh(xparam1,DynareDataset,DynareOptions,Model,EstimatedParameters,BayesInfo,DynareResults)
% Evaluates the posterior kernel of a dsge model.
%
% INPUTS
......@@ -11,6 +11,7 @@ function [fval,llik,cost_flag,ys,trend_coeff,info] = DsgeLikelihood_hh(xparam1,D
% no_more_missing_observations [integer]
% OUTPUTS
% fval : value of the posterior kernel at xparam1.
% llik : probabilities at each time point
% cost_flag : zero if the function returns a penalty, one otherwise.
% ys : steady state of original endogenous variables
% trend_coeff :
......@@ -52,11 +53,14 @@ if nargin==1
return
end
fval = [];
ys = [];
trend_coeff = [];
cost_flag = 1;
% Initialization of the returned variables and others...
fval = [];
ys = [];
trend_coeff = [];
cost_flag = 1;
llik=NaN;
info = 0;
singularity_flag = 0;
if DynareOptions.block == 1
error('DsgeLikelihood_hh:: This routine (called if mode_compute==5) is not compatible with the block option!')
......@@ -159,8 +163,6 @@ end
Model.Sigma_e = Q;
Model.H = H;
%------------------------------------------------------------------------------
% 2. call model setup & reduction program
%------------------------------------------------------------------------------
......@@ -223,8 +225,31 @@ Y = DynareDataset.data-trend;
%------------------------------------------------------------------------------
% 3. Initial condition of the Kalman filter
%------------------------------------------------------------------------------
kalman_algo = DynareOptions.kalman_algo;
% resetting measurement errors covariance matrix for univariate filters
if (kalman_algo == 2) || (kalman_algo == 4)
if isequal(H,0)
H = zeros(nobs,1);
mmm = mm;
else
if all(all(abs(H-diag(diag(H)))<1e-14))% ie, the covariance matrix is diagonal...
H = diag(H);
mmm = mm;
else
Z = [Z, eye(pp)];
T = blkdiag(T,zeros(pp));
Q = blkdiag(Q,H);
R = blkdiag(R,eye(pp));
Pstar = blkdiag(Pstar,H);
Pinf = blckdiag(Pinf,zeros(pp));
H = zeros(nobs,1);
mmm = mm+pp;
end
end
end
diffuse_periods = 0;
switch DynareOptions.lik_init
case 1% Standard initialization with the steady state of the state equation.
......@@ -271,20 +296,32 @@ switch DynareOptions.lik_init
if isinf(dLIK)
% Go to univariate diffuse filter if singularity problem.
kalman_algo = 4;
singularity_flag = 1;
end
end
if (kalman_algo==4)
% Univariate Diffuse Kalman Filter
if no_correlation_flag
mmm = mm;
else
Z = [Z, eye(pp)];
T = blkdiag(T,zeros(pp));
Q = blkdiag(Q,H);
R = blkdiag(R,eye(pp));
Pstar = blkdiag(Pstar,H);
Pinf = blckdiag(Pinf,zeros(pp));
mmm = mm+pp;
if singularity_flag
if isequal(H,0)
H = zeros(nobs,1);
mmm = mm;
else
if all(all(abs(H-diag(diag(H)))<1e-14))% ie, the covariance matrix is diagonal...
H = diag(H);
mmm = mm;
else
Z = [Z, eye(pp)];
T = blkdiag(T,zeros(pp));
Q = blkdiag(Q,H);
R = blkdiag(R,eye(pp));
Pstar = blkdiag(Pstar,H);
Pinf = blckdiag(Pinf,zeros(pp));
H = zeros(nobs,1);
mmm = mm+pp;
end
end
% no need to test again for correlation elements
singularity_flag = 0;
end
[dLIK,dlik,a,Pstar] = univariate_kalman_filter_d(DynareDataset.missing.aindex,DynareDataset.missing.number_of_observations,DynareDataset.missing.no_more_missing_observations, ...
Y, 1, size(Y,2), ...
......@@ -294,13 +331,13 @@ switch DynareOptions.lik_init
diffuse_periods = length(dlik);
end
case 4% Start from the solution of the Riccati equation.
if kalman_algo ~= 2
if kalman_algo ~= 2
kalman_algo = 1;
end
if isequal(H,0)
[err,Pstar] = kalman_steady_state(transpose(T),R*Q*transpose(R),transpose(build_selection_matrix(mf,np,length(mf))));
[err,Pstar] = kalman_steady_state(transpose(T),R*Q*transpose(R),transpose(build_selection_matrix(Z,np,length(Z))));
else
[err,Pstar] = kalman_steady_state(transpose(T),R*Q*transpose(R),transpose(build_selection_matrix(mf,np,length(mf))),H);
[err,Pstar] = kalman_steady_state(transpose(T),R*Q*transpose(R),transpose(build_selection_matrix(Z,np,length(Z))),H);
end
if err
disp(['DsgeLikelihood:: I am not able to solve the Riccati equation, so I switch to lik_init=1!']);
......@@ -316,8 +353,6 @@ end
% 4. Likelihood evaluation
%------------------------------------------------------------------------------
singularity_flag = 0;
if ((kalman_algo==1) || (kalman_algo==3))% Multivariate Kalman Filter
if no_missing_data_flag
[LIK,lik] = kalman_filter(Y,diffuse_periods+1,size(Y,2), ...
......@@ -333,6 +368,11 @@ if ((kalman_algo==1) || (kalman_algo==3))% Multivariate Kalman Filter
T,Q,R,H,Z,mm,pp,rr,Zflag,diffuse_periods);
end
if isinf(LIK)
if kalman_algo == 1
kalman_algo = 2;
else
kalman_algo = 4;
end
singularity_flag = 1;
else
if DynareOptions.lik_init==3
......@@ -342,21 +382,30 @@ if ((kalman_algo==1) || (kalman_algo==3))% Multivariate Kalman Filter
end
end
if ( (singularity_flag) || (kalman_algo==2) || (kalman_algo==4) )% Univariate Kalman Filter
if ( singularity_flag || (kalman_algo==2) || (kalman_algo==4) )
% Univariate Kalman Filter
% resetting measurement error covariance matrix when necessary %
if singularity_flag
if no_correlation
if isequal(H,0)
H = zeros(nobs,1);
mmm = mm;
else
Z = [Z, eye(pp)];
T = blkdiag(T,zeros(pp));
Q = blkdiag(Q,H);
R = blkdiag(R,eye(pp));
Pstar = blkdiag(Pstar,H);
Pinf = blckdiag(Pinf,zeros(pp));
mmm = mm+pp;
a = [a; zeros(pp,1)];
if all(all(abs(H-diag(diag(H)))<1e-14))% ie, the covariance matrix is diagonal...
H = diag(H);
mmm = mm;
else
Z = [Z, eye(pp)];
T = blkdiag(T,zeros(pp));
Q = blkdiag(Q,H);
R = blkdiag(R,eye(pp));
Pstar = blkdiag(Pstar,H);
Pinf = blckdiag(Pinf,zeros(pp));
H = zeros(nobs,1);
mmm = mm+pp;
end
end
end
[LIK,lik] = univariate_kalman_filter(DynareDataset.missing.aindex,DynareDataset.missing.number_of_observations,DynareDataset.missing.no_more_missing_observations,Y,diffuse_periods+1,size(Y,2), ...
a,Pstar, ...
DynareOptions.kalman_tol, ...
......
......@@ -188,7 +188,7 @@ if any(idemoments.ino),
iweak = length(find(idemoments.jweak_pair(:,j)));
if iweak,
[jx,jy]=find(jmap_pair==j);
jstore=[jstore jx(1) jy(1)]';
jstore=[jstore' jx(1) jy(1)]';
if SampleSize > 1
disp([' [',name{jx(1)},',',name{jy(1)},'] are PAIRWISE collinear (with tol = 1.e-10) for ',num2str((iweak)/SampleSize*100),'% of MC runs!' ])
else
......
......@@ -353,7 +353,7 @@ if ~options_.mh_posterior_mode_estimation && options_.cova_compute
end
if options_.mode_check == 1 && ~options_.mh_posterior_mode_estimation
mode_check('objective_function',xparam1,hh,dataset_,options_,M_,estim_params_,bayestopt_,oo_);
mode_check(objective_function,xparam1,hh,dataset_,options_,M_,estim_params_,bayestopt_,oo_);
end
if ~options_.mh_posterior_mode_estimation
......
......@@ -41,6 +41,8 @@ function [ys,params1,check] = evaluate_steady_state_file(ys_init,exo_ss,params,f
if steadystate_flag == 1
% old format
assignin('base','tmp_00_',params);
evalin('base','M_.params=tmp_00_; clear(''tmp_00_'')');
h_steadystate = str2func([fname '_steadystate']);
[ys,check] = h_steadystate(ys_init, exo_ss);
params1 = evalin('base','M_.params');
......
function [H, dA, dOm, Hss, gp, info] = getH(A, B, M_,oo_,kronflag,indx,indexo)
function [H, dA, dOm, Hss, gp, d2A, d2Om, H2ss] = getH(A, B, M_,oo_,kronflag,indx,indexo)
% computes derivative of reduced form linear model w.r.t. deep params
%
......@@ -31,10 +31,29 @@ yy0=oo_.dr.ys(I);
% yy0 = [ yy0; oo_.dr.ys(find(M_.lead_lag_incidence(j,:)))];
% end
dyssdtheta=zeros(length(oo_.dr.ys),M_.param_nbr);
df = feval([M_.fname,'_params_derivs'],yy0, oo_.exo_steady_state', ...
M_.params, oo_.dr.ys, 1, dyssdtheta);
[residual, gg1] = feval([M_.fname,'_static'],oo_.dr.ys, oo_.exo_steady_state', M_.params);
if nargout>5,
[residual, gg1, gg2] = feval([M_.fname,'_static'],oo_.dr.ys, oo_.exo_steady_state', M_.params);
[df, gp, d2f] = feval([M_.fname,'_params_derivs'],yy0, oo_.exo_steady_state', ...
M_.params, oo_.dr.ys, 1, dyssdtheta);
d2f = get_all_resid_2nd_derivs(d2f,length(oo_.dr.ys),M_.param_nbr);
d2f = d2f(:,indx,indx);
if isempty(find(gg2)),
for j=1:length(indx),
d2yssdtheta(:,:,j) = -gg1\d2f(:,:,j);
end
else
gam = d2f*0;
for j=1:length(indx),
d2yssdtheta(:,:,j) = -gg1\(d2f(:,:,j)+gam(:,:,j));
end
end
else
[residual, gg1] = feval([M_.fname,'_static'],oo_.dr.ys, oo_.exo_steady_state', M_.params);
df = feval([M_.fname,'_params_derivs'],yy0, oo_.exo_steady_state', ...
M_.params, oo_.dr.ys, 1, dyssdtheta);
end
dyssdtheta = -gg1\df;
if any(any(isnan(dyssdtheta))),
[U,T] = schur(gg1);
global options_
......@@ -45,15 +64,29 @@ if any(any(isnan(dyssdtheta))),
[U,T] = ordschur(U,T,e1);
T = T(k+1:end,k+1:end);
dyssdtheta = -U(:,k+1:end)*(T\U(:,k+1:end)')*df;
if nargout>5,
for j=1:length(indx),
d2yssdtheta(:,:,j) = -U(:,k+1:end)*(T\U(:,k+1:end)')*d2f(:,:,j);
end
end
end
if nargout>5,
[df, gp, d2f, gpp] = feval([M_.fname,'_params_derivs'],yy0, oo_.exo_steady_state', ...
M_.params, oo_.dr.ys, 1, dyssdtheta);
H2ss = d2yssdtheta(oo_.dr.order_var,:,:);
[residual, g1, g2, g3] = feval([M_.fname,'_dynamic'],yy0, oo_.exo_steady_state', ...
M_.params, oo_.dr.ys, 1);
else
[df, gp] = feval([M_.fname,'_params_derivs'],yy0, oo_.exo_steady_state', ...
M_.params, oo_.dr.ys, 1, dyssdtheta);
[residual, g1, g2 ] = feval([M_.fname,'_dynamic'],yy0, oo_.exo_steady_state', ...
M_.params, oo_.dr.ys, 1);
end
Hss = dyssdtheta(oo_.dr.order_var,indx);
dyssdtheta = dyssdtheta(I,:);
[nr, nc]=size(g2);
[m, nelem]=size(g1);
nc = sqrt(nc);
ns = max(max(M_.lead_lag_incidence));
gp2 = gp*0;
......@@ -73,19 +106,9 @@ end
gp = gp+gp2;
gp = gp(:,:,indx);
param_nbr = length(indx);
% order_var = [oo_.dr.order_var; ...
% [size(oo_dr.ghx,2)+1:size(oo_dr.ghx,2)+size(oo_.dr.transition_auxiliary_variables,1)]' ];
% [A(order_var,order_var),B(order_var,:)]=dynare_resolve;
% [A,B,ys,info]=dynare_resolve;
% if info(1) > 0
% H = [];
% A0 = [];
% B0 = [];
% dA = [];
% dOm = [];
% return
% end
if nargout>5,
param_nbr_2 = param_nbr*(param_nbr+1)/2;
end
m = size(A,1);
n = size(B,2);
......@@ -93,9 +116,15 @@ n = size(B,2);
klen = M_.maximum_endo_lag + M_.maximum_endo_lead + 1;
k1 = M_.lead_lag_incidence(find([1:klen] ~= M_.maximum_endo_lag+1),:);
a = g1(:,nonzeros(k1'));
da = gp(:,nonzeros(k1'),:);
k11 = M_.lead_lag_incidence(find([1:klen] ~= M_.maximum_endo_lag+1),:);
a = g1(:,nonzeros(k11'));
da = gp(:,nonzeros(k11'),:);
if nargout > 5,
nelem = size(g1,2);
g22 = get_all_2nd_derivs(gpp,m,nelem,M_.param_nbr);
g22 = g22(:,:,indx,indx);
d2a = g22(:,nonzeros(k11'),:,:);
end
kstate = oo_.dr.kstate;
GAM1 = zeros(M_.endo_nbr,M_.endo_nbr);
......@@ -103,22 +132,26 @@ Dg1 = zeros(M_.endo_nbr,M_.endo_nbr,param_nbr);
% nf = find(M_.lead_lag_incidence(M_.maximum_endo_lag+2,:));
% GAM1(:, nf) = -g1(:,M_.lead_lag_incidence(M_.maximum_endo_lag+2,nf));
k = find(kstate(:,2) == M_.maximum_endo_lag+2 & kstate(:,3));
GAM1(:, kstate(k,1)) = -a(:,kstate(k,3));
Dg1(:, kstate(k,1), :) = -da(:,kstate(k,3),:);
k = find(kstate(:,2) > M_.maximum_endo_lag+2 & kstate(:,3));
kk = find(kstate(:,2) > M_.maximum_endo_lag+2 & ~kstate(:,3));
nauxe = 0;
if ~isempty(k),
ax(:,k)= -a(:,kstate(k,3));
ax(:,kk)= 0;
dax(:,k,:)= -da(:,kstate(k,3),:);
dax(:,kk,:)= 0;
nauxe=size(ax,2);
GAM1 = [ax GAM1];
Dg1 = cat(2,dax,Dg1);
clear ax
k1 = find(kstate(:,2) == M_.maximum_endo_lag+2 & kstate(:,3));
GAM1(:, kstate(k1,1)) = -a(:,kstate(k1,3));
Dg1(:, kstate(k1,1), :) = -da(:,kstate(k1,3),:);
if nargout > 5,
D2g1 = zeros(M_.endo_nbr,M_.endo_nbr,param_nbr,param_nbr);
D2g1(:, kstate(k1,1), :, :) = -d2a(:,kstate(k1,3),:,:);
end
% k = find(kstate(:,2) > M_.maximum_endo_lag+2 & kstate(:,3));
% kk = find(kstate(:,2) > M_.maximum_endo_lag+2 & ~kstate(:,3));
% nauxe = 0;
% if ~isempty(k),
% ax(:,k)= -a(:,kstate(k,3));
% ax(:,kk)= 0;
% dax(:,k,:)= -da(:,kstate(k,3),:);
% dax(:,kk,:)= 0;
% nauxe=size(ax,2);
% GAM1 = [ax GAM1];
% Dg1 = cat(2,dax,Dg1);
% clear ax
% end
[junk,cols_b,cols_j] = find(M_.lead_lag_incidence(M_.maximum_endo_lag+1, ...
......@@ -127,84 +160,95 @@ GAM0 = zeros(M_.endo_nbr,M_.endo_nbr);
Dg0 = zeros(M_.endo_nbr,M_.endo_nbr,param_nbr);
GAM0(:,cols_b) = g1(:,cols_j);
Dg0(:,cols_b,:) = gp(:,cols_j,:);
if nargout > 5,
D2g0 = zeros(M_.endo_nbr,M_.endo_nbr,param_nbr,param_nbr);
D2g0(:, cols_b, :, :) = g22(:,cols_j,:,:);
end
% GAM0 = g1(:,M_.lead_lag_incidence(M_.maximum_endo_lag+1,:));
k = find(kstate(:,2) == M_.maximum_endo_lag+1 & kstate(:,4));
k2 = find(kstate(:,2) == M_.maximum_endo_lag+1 & kstate(:,4));
GAM2 = zeros(M_.endo_nbr,M_.endo_nbr);
Dg2 = zeros(M_.endo_nbr,M_.endo_nbr,param_nbr);