Bug correction (condition info==19 was not used in DsgeLikelihood and DsgeVarLikelihood).

git-svn-id: https://www.dynare.org/svn/dynare/trunk@2667 ac1d8469-bf42-47a9-8791-bf33cf982152
time-shift
stepan 2009-05-12 11:38:06 +00:00
parent 74a2ffcd95
commit ec4b45cc7c
2 changed files with 245 additions and 244 deletions

View File

@ -131,8 +131,8 @@ function [fval,cost_flag,ys,trend_coeff,info] = DsgeLikelihood(xparam1,gend,data
fval = bayestopt_.penalty+1; fval = bayestopt_.penalty+1;
cost_flag = 0; cost_flag = 0;
return return
elseif info(1) == 3 || info(1) == 4 || info(1) == 20 || info(1) == 21 elseif info(1) == 3 || info(1) == 4 || info(1) == 19 || info(1) == 20 || info(1) == 21
fval = bayestopt_.penalty+info(2);%^2; % penalty power raised in DR1.m and resol already. GP July'08 fval = bayestopt_.penalty+info(2);
cost_flag = 0; cost_flag = 0;
return return
end end

View File

@ -1,243 +1,244 @@
function [fval,cost_flag,info,PHI,SIGMAu,iXX,prior] = DsgeVarLikelihood(xparam1,gend) function [fval,cost_flag,info,PHI,SIGMAu,iXX,prior] = DsgeVarLikelihood(xparam1,gend)
% function [fval,cost_flag,info,PHI,SIGMAu,iXX] = DsgeVarLikelihood(xparam1,gend) % function [fval,cost_flag,info,PHI,SIGMAu,iXX] = DsgeVarLikelihood(xparam1,gend)
% Evaluates the posterior kernel of the bvar-dsge model. % Evaluates the posterior kernel of the bvar-dsge model.
% %
% INPUTS % INPUTS
% o xparam1 [double] Vector of model's parameters. % o xparam1 [double] Vector of model's parameters.
% o gend [integer] Number of observations (without conditionning observations for the lags). % o gend [integer] Number of observations (without conditionning observations for the lags).
% %
% OUTPUTS % OUTPUTS
% o fval [double] Value of the posterior kernel at xparam1. % o fval [double] Value of the posterior kernel at xparam1.
% o cost_flag [integer] Zero if the function returns a penalty, one otherwise. % o cost_flag [integer] Zero if the function returns a penalty, one otherwise.
% o info [integer] Vector of informations about the penalty. % o info [integer] Vector of informations about the penalty.
% o PHI [double] Stacked BVAR-DSGE autoregressive matrices (at the mode associated to xparam1). % o PHI [double] Stacked BVAR-DSGE autoregressive matrices (at the mode associated to xparam1).
% o SIGMAu [double] Covariance matrix of the BVAR-DSGE (at the mode associated to xparam1). % o SIGMAu [double] Covariance matrix of the BVAR-DSGE (at the mode associated to xparam1).
% o iXX [double] inv(X'X). % o iXX [double] inv(X'X).
% o prior [double] a matlab structure describing the dsge-var prior. % o prior [double] a matlab structure describing the dsge-var prior.
% %
% SPECIAL REQUIREMENTS % SPECIAL REQUIREMENTS
% None. % None.
% Copyright (C) 2006-2008 Dynare Team % Copyright (C) 2006-2008 Dynare Team
% %
% This file is part of Dynare. % This file is part of Dynare.
% %
% Dynare is free software: you can redistribute it and/or modify % Dynare is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by % it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or % the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version. % (at your option) any later version.
% %
% Dynare is distributed in the hope that it will be useful, % Dynare is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of % but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details. % GNU General Public License for more details.
% %
% You should have received a copy of the GNU General Public License % You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>. % along with Dynare. If not, see <http://www.gnu.org/licenses/>.
global bayestopt_ estim_params_ M_ options_ global bayestopt_ estim_params_ M_ options_
nvx = estim_params_.nvx; nvx = estim_params_.nvx;
nvn = estim_params_.nvn; nvn = estim_params_.nvn;
ncx = estim_params_.ncx; ncx = estim_params_.ncx;
ncn = estim_params_.ncn; ncn = estim_params_.ncn;
np = estim_params_.np; np = estim_params_.np;
nx = nvx+nvn+ncx+ncn+np; nx = nvx+nvn+ncx+ncn+np;
ns = nvx+nvn+ncx+ncn; ns = nvx+nvn+ncx+ncn;
NumberOfObservedVariables = size(options_.varobs,1); NumberOfObservedVariables = size(options_.varobs,1);
NumberOfLags = options_.varlag; NumberOfLags = options_.varlag;
NumberOfParameters = NumberOfObservedVariables*NumberOfLags ; NumberOfParameters = NumberOfObservedVariables*NumberOfLags ;
if ~options_.noconstant if ~options_.noconstant
NumberOfParameters = NumberOfParameters + 1; NumberOfParameters = NumberOfParameters + 1;
end end
mYY = evalin('base', 'mYY'); mYY = evalin('base', 'mYY');
mYX = evalin('base', 'mYX'); mYX = evalin('base', 'mYX');
mXY = evalin('base', 'mXY'); mXY = evalin('base', 'mXY');
mXX = evalin('base', 'mXX'); mXX = evalin('base', 'mXX');
fval = []; fval = [];
cost_flag = 1; cost_flag = 1;
if options_.mode_compute ~= 1 & any(xparam1 < bayestopt_.lb) if options_.mode_compute ~= 1 & any(xparam1 < bayestopt_.lb)
k = find(xparam1 < bayestopt_.lb); k = find(xparam1 < bayestopt_.lb);
fval = bayestopt_.penalty+sum((bayestopt_.lb(k)-xparam1(k)).^2); fval = bayestopt_.penalty+sum((bayestopt_.lb(k)-xparam1(k)).^2);
cost_flag = 0; cost_flag = 0;
info = 41; info = 41;
return; return;
end end
if options_.mode_compute ~= 1 & any(xparam1 > bayestopt_.ub) if options_.mode_compute ~= 1 & any(xparam1 > bayestopt_.ub)
k = find(xparam1 > bayestopt_.ub); k = find(xparam1 > bayestopt_.ub);
fval = bayestopt_.penalty+sum((xparam1(k)-bayestopt_.ub(k)).^2); fval = bayestopt_.penalty+sum((xparam1(k)-bayestopt_.ub(k)).^2);
cost_flag = 0; cost_flag = 0;
info = 42; info = 42;
return; return;
end end
Q = M_.Sigma_e; Q = M_.Sigma_e;
for i=1:estim_params_.nvx for i=1:estim_params_.nvx
k = estim_params_.var_exo(i,1); k = estim_params_.var_exo(i,1);
Q(k,k) = xparam1(i)*xparam1(i); Q(k,k) = xparam1(i)*xparam1(i);
end end
offset = estim_params_.nvx; offset = estim_params_.nvx;
if estim_params_.nvn if estim_params_.nvn
disp('DsgeVarLikelihood :: Measurement errors are implemented!') disp('DsgeVarLikelihood :: Measurement errors are implemented!')
return return
end end
if estim_params_.ncx if estim_params_.ncx
disp('DsgeVarLikelihood :: Correlated structural innovations are not implemented!') disp('DsgeVarLikelihood :: Correlated structural innovations are not implemented!')
return return
end end
M_.params(estim_params_.param_vals(:,1)) = xparam1(offset+1:end); M_.params(estim_params_.param_vals(:,1)) = xparam1(offset+1:end);
M_.Sigma_e = Q; M_.Sigma_e = Q;
%% Weight of the dsge prior: %% Weight of the dsge prior:
dsge_prior_weight = M_.params(strmatch('dsge_prior_weight',M_.param_names)); dsge_prior_weight = M_.params(strmatch('dsge_prior_weight',M_.param_names));
% Is the DSGE prior proper? % Is the DSGE prior proper?
if dsge_prior_weight<(NumberOfParameters+NumberOfObservedVariables)/gend; if dsge_prior_weight<(NumberOfParameters+NumberOfObservedVariables)/gend;
fval = bayestopt_.penalty+abs(gend*dsge_prior_weight-(NumberOfParameters+NumberOfObservedVariables)); fval = bayestopt_.penalty+abs(gend*dsge_prior_weight-(NumberOfParameters+NumberOfObservedVariables));
cost_flag = 0; cost_flag = 0;
info = 51; info = 51;
return; return;
end end
%------------------------------------------------------------------------------ %------------------------------------------------------------------------------
% 2. call model setup & reduction program % 2. call model setup & reduction program
%------------------------------------------------------------------------------ %------------------------------------------------------------------------------
[T,R,SteadyState,info] = dynare_resolve(bayestopt_.restrict_var_list,... [T,R,SteadyState,info] = dynare_resolve(bayestopt_.restrict_var_list,...
bayestopt_.restrict_columns,... bayestopt_.restrict_columns,...
bayestopt_.restrict_aux); bayestopt_.restrict_aux);
if info(1) == 1 | info(1) == 2 | info(1) == 5 if info(1) == 1 || info(1) == 2 || info(1) == 5
fval = bayestopt_.penalty+1; fval = bayestopt_.penalty+1;
cost_flag = 0; cost_flag = 0;
return return
elseif info(1) == 3 | info(1) == 4 | info(1) == 20 elseif info(1) == 3 || info(1) == 4 || info(1) == 19 || info(1) == 20 || info(1) == 21
fval = bayestopt_.penalty+info(2)^2; fval = bayestopt_.penalty+info(2);
cost_flag = 0; cost_flag = 0;
return return
end end
if ~options_.noconstant
if options_.loglinear if ~options_.noconstant
constant = transpose(log(SteadyState(bayestopt_.mfys))); if options_.loglinear
else constant = transpose(log(SteadyState(bayestopt_.mfys)));
constant = transpose(SteadyState(bayestopt_.mfys)); else
end constant = transpose(SteadyState(bayestopt_.mfys));
else end
constant = zeros(1,NumberOfObservedVariables); else
end constant = zeros(1,NumberOfObservedVariables);
if bayestopt_.with_trend == 1 end
disp('DsgeVarLikelihood :: Linear trend is not yet implemented!') if bayestopt_.with_trend == 1
return disp('DsgeVarLikelihood :: Linear trend is not yet implemented!')
end return
end
%------------------------------------------------------------------------------
% 3. theoretical moments (second order) %------------------------------------------------------------------------------
%------------------------------------------------------------------------------ % 3. theoretical moments (second order)
tmp0 = lyapunov_symm(T,R*Q*R',options_.qz_criterium,options_.lyapunov_complex_threshold);% I compute the variance-covariance matrix %------------------------------------------------------------------------------
mf = bayestopt_.mf1; % of the restricted state vector. tmp0 = lyapunov_symm(T,R*Q*R',options_.qz_criterium,options_.lyapunov_complex_threshold);% I compute the variance-covariance matrix
mf = bayestopt_.mf1; % of the restricted state vector.
% Get the non centered second order moments
TheoreticalAutoCovarianceOfTheObservedVariables = ... % Get the non centered second order moments
zeros(NumberOfObservedVariables,NumberOfObservedVariables,NumberOfLags+1); TheoreticalAutoCovarianceOfTheObservedVariables = ...
TheoreticalAutoCovarianceOfTheObservedVariables(:,:,1) = tmp0(mf,mf)+constant'*constant; zeros(NumberOfObservedVariables,NumberOfObservedVariables,NumberOfLags+1);
for lag = 1:NumberOfLags TheoreticalAutoCovarianceOfTheObservedVariables(:,:,1) = tmp0(mf,mf)+constant'*constant;
tmp0 = T*tmp0; for lag = 1:NumberOfLags
TheoreticalAutoCovarianceOfTheObservedVariables(:,:,lag+1) = tmp0(mf,mf) ... tmp0 = T*tmp0;
+ constant'*constant; TheoreticalAutoCovarianceOfTheObservedVariables(:,:,lag+1) = tmp0(mf,mf) ...
end + constant'*constant;
% Build the theoretical "covariance" between Y and X end
GYX = zeros(NumberOfObservedVariables,NumberOfParameters); % Build the theoretical "covariance" between Y and X
for i=1:NumberOfLags GYX = zeros(NumberOfObservedVariables,NumberOfParameters);
GYX(:,(i-1)*NumberOfObservedVariables+1:i*NumberOfObservedVariables) = ... for i=1:NumberOfLags
TheoreticalAutoCovarianceOfTheObservedVariables(:,:,i+1); GYX(:,(i-1)*NumberOfObservedVariables+1:i*NumberOfObservedVariables) = ...
end TheoreticalAutoCovarianceOfTheObservedVariables(:,:,i+1);
if ~options_.noconstant end
GYX(:,end) = constant'; if ~options_.noconstant
end GYX(:,end) = constant';
% Build the theoretical "covariance" between X and X end
GXX = kron(eye(NumberOfLags), ... % Build the theoretical "covariance" between X and X
TheoreticalAutoCovarianceOfTheObservedVariables(:,:,1)); GXX = kron(eye(NumberOfLags), ...
for i = 1:NumberOfLags-1 TheoreticalAutoCovarianceOfTheObservedVariables(:,:,1));
tmp1 = diag(ones(NumberOfLags-i,1),i); for i = 1:NumberOfLags-1
tmp2 = diag(ones(NumberOfLags-i,1),-i); tmp1 = diag(ones(NumberOfLags-i,1),i);
GXX = GXX + kron(tmp1,TheoreticalAutoCovarianceOfTheObservedVariables(:,:,i+1)); tmp2 = diag(ones(NumberOfLags-i,1),-i);
GXX = GXX + kron(tmp2,TheoreticalAutoCovarianceOfTheObservedVariables(:,:,i+1)'); GXX = GXX + kron(tmp1,TheoreticalAutoCovarianceOfTheObservedVariables(:,:,i+1));
end GXX = GXX + kron(tmp2,TheoreticalAutoCovarianceOfTheObservedVariables(:,:,i+1)');
end
if ~options_.noconstant
% Add one row and one column to GXX if ~options_.noconstant
GXX = [GXX , kron(ones(NumberOfLags,1),constant') ; [ kron(ones(1,NumberOfLags),constant) , 1] ]; % Add one row and one column to GXX
end GXX = [GXX , kron(ones(NumberOfLags,1),constant') ; [ kron(ones(1,NumberOfLags),constant) , 1] ];
end
GYY = TheoreticalAutoCovarianceOfTheObservedVariables(:,:,1);
GYY = TheoreticalAutoCovarianceOfTheObservedVariables(:,:,1);
assignin('base','GYY',GYY);
assignin('base','GXX',GXX); assignin('base','GYY',GYY);
assignin('base','GYX',GYX); assignin('base','GXX',GXX);
assignin('base','GYX',GYX);
if ~isinf(dsge_prior_weight)
tmp0 = dsge_prior_weight*gend*TheoreticalAutoCovarianceOfTheObservedVariables(:,:,1) + mYY ; if ~isinf(dsge_prior_weight)
tmp1 = dsge_prior_weight*gend*GYX + mYX; tmp0 = dsge_prior_weight*gend*TheoreticalAutoCovarianceOfTheObservedVariables(:,:,1) + mYY ;
tmp2 = inv(dsge_prior_weight*gend*GXX+mXX); tmp1 = dsge_prior_weight*gend*GYX + mYX;
SIGMAu = tmp0 - tmp1*tmp2*tmp1'; clear('tmp0'); tmp2 = inv(dsge_prior_weight*gend*GXX+mXX);
if ~ispd(SIGMAu) SIGMAu = tmp0 - tmp1*tmp2*tmp1'; clear('tmp0');
v = diag(SIGMAu); if ~ispd(SIGMAu)
k = find(v<0); v = diag(SIGMAu);
fval = bayestopt_.penalty + sum(v(k).^2); k = find(v<0);
info = 52; fval = bayestopt_.penalty + sum(v(k).^2);
cost_flag = 0; info = 52;
return; cost_flag = 0;
end return;
SIGMAu = SIGMAu / (gend*(1+dsge_prior_weight)); end
PHI = tmp2*tmp1'; clear('tmp1'); SIGMAu = SIGMAu / (gend*(1+dsge_prior_weight));
prodlng1 = sum(gammaln(.5*((1+dsge_prior_weight)*gend- ... PHI = tmp2*tmp1'; clear('tmp1');
NumberOfObservedVariables*NumberOfLags ... prodlng1 = sum(gammaln(.5*((1+dsge_prior_weight)*gend- ...
+1-(1:NumberOfObservedVariables)'))); NumberOfObservedVariables*NumberOfLags ...
prodlng2 = sum(gammaln(.5*(dsge_prior_weight*gend- ... +1-(1:NumberOfObservedVariables)')));
NumberOfObservedVariables*NumberOfLags ... prodlng2 = sum(gammaln(.5*(dsge_prior_weight*gend- ...
+1-(1:NumberOfObservedVariables)'))); NumberOfObservedVariables*NumberOfLags ...
lik = .5*NumberOfObservedVariables*log(det(dsge_prior_weight*gend*GXX+mXX)) ... +1-(1:NumberOfObservedVariables)')));
+ .5*((dsge_prior_weight+1)*gend-NumberOfParameters)*log(det((dsge_prior_weight+1)*gend*SIGMAu)) ... lik = .5*NumberOfObservedVariables*log(det(dsge_prior_weight*gend*GXX+mXX)) ...
- .5*NumberOfObservedVariables*log(det(dsge_prior_weight*gend*GXX)) ... + .5*((dsge_prior_weight+1)*gend-NumberOfParameters)*log(det((dsge_prior_weight+1)*gend*SIGMAu)) ...
- .5*(dsge_prior_weight*gend-NumberOfParameters)*log(det(dsge_prior_weight*gend*(GYY-GYX*inv(GXX)*GYX'))) ... - .5*NumberOfObservedVariables*log(det(dsge_prior_weight*gend*GXX)) ...
+ .5*NumberOfObservedVariables*gend*log(2*pi) ... - .5*(dsge_prior_weight*gend-NumberOfParameters)*log(det(dsge_prior_weight*gend*(GYY-GYX*inv(GXX)*GYX'))) ...
- .5*log(2)*NumberOfObservedVariables*((dsge_prior_weight+1)*gend-NumberOfParameters) ... + .5*NumberOfObservedVariables*gend*log(2*pi) ...
+ .5*log(2)*NumberOfObservedVariables*(dsge_prior_weight*gend-NumberOfParameters) ... - .5*log(2)*NumberOfObservedVariables*((dsge_prior_weight+1)*gend-NumberOfParameters) ...
- prodlng1 + prodlng2; + .5*log(2)*NumberOfObservedVariables*(dsge_prior_weight*gend-NumberOfParameters) ...
else - prodlng1 + prodlng2;
iGXX = inv(GXX); else
SIGMAu = GYY - GYX*iGXX*transpose(GYX); iGXX = inv(GXX);
PHI = iGXX*transpose(GYX); SIGMAu = GYY - GYX*iGXX*transpose(GYX);
lik = gend * ( log(det(SIGMAu)) + NumberOfObservedVariables*log(2*pi) + ... PHI = iGXX*transpose(GYX);
trace(inv(SIGMAu)*(mYY - transpose(mYX*PHI) - mYX*PHI + transpose(PHI)*mXX*PHI)/gend)); lik = gend * ( log(det(SIGMAu)) + NumberOfObservedVariables*log(2*pi) + ...
lik = .5*lik;% Minus likelihood trace(inv(SIGMAu)*(mYY - transpose(mYX*PHI) - mYX*PHI + transpose(PHI)*mXX*PHI)/gend));
end lik = .5*lik;% Minus likelihood
end
lnprior = priordens(xparam1,bayestopt_.pshape,bayestopt_.p6,bayestopt_.p7,bayestopt_.p3,bayestopt_.p4);
fval = (lik-lnprior); lnprior = priordens(xparam1,bayestopt_.pshape,bayestopt_.p6,bayestopt_.p7,bayestopt_.p3,bayestopt_.p4);
fval = (lik-lnprior);
if (nargout == 6)
if isinf(dsge_prior_weight) if (nargout == 6)
iXX = iGXX; if isinf(dsge_prior_weight)
else iXX = iGXX;
iXX = tmp2; else
end iXX = tmp2;
end end
end
if (nargout==7)
if isinf(dsge_prior_weight) if (nargout==7)
iXX = iGXX; if isinf(dsge_prior_weight)
else iXX = iGXX;
iXX = tmp2; else
end iXX = tmp2;
iGXX = inv(GXX); end
prior.SIGMAstar = GYY - GYX*iGXX*GYX'; iGXX = inv(GXX);
prior.PHIstar = iGXX*transpose(GYX); prior.SIGMAstar = GYY - GYX*iGXX*GYX';
prior.ArtificialSampleSize = fix(dsge_prior_weight*gend); prior.PHIstar = iGXX*transpose(GYX);
prior.DF = prior.ArtificialSampleSize - NumberOfParameters - NumberOfObservedVariables; prior.ArtificialSampleSize = fix(dsge_prior_weight*gend);
prior.iGXX = iGXX; prior.DF = prior.ArtificialSampleSize - NumberOfParameters - NumberOfObservedVariables;
prior.iGXX = iGXX;
end end