2008-03-10 17:08:02 +01:00
|
|
|
function [fval,cost_flag,info,PHI,SIGMAu,iXX,prior] = DsgeVarLikelihood(xparam1,gend)
|
2008-03-03 11:26:09 +01:00
|
|
|
% function [fval,cost_flag,info,PHI,SIGMAu,iXX] = DsgeVarLikelihood(xparam1,gend)
|
2007-10-03 16:40:43 +02:00
|
|
|
% Evaluates the posterior kernel of the bvar-dsge model.
|
|
|
|
%
|
|
|
|
% INPUTS
|
|
|
|
% o xparam1 [double] Vector of model's parameters.
|
|
|
|
% o gend [integer] Number of observations (without conditionning observations for the lags).
|
|
|
|
%
|
|
|
|
% OUTPUTS
|
|
|
|
% o fval [double] Value of the posterior kernel at xparam1.
|
|
|
|
% o cost_flag [integer] Zero if the function returns a penalty, one otherwise.
|
|
|
|
% o info [integer] Vector of informations about the penalty.
|
|
|
|
% 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 iXX [double] inv(X'X).
|
2008-03-10 17:08:02 +01:00
|
|
|
% o prior [double] a matlab structure describing the dsge-var prior.
|
2007-10-03 16:40:43 +02:00
|
|
|
%
|
|
|
|
% SPECIAL REQUIREMENTS
|
|
|
|
% None.
|
|
|
|
%
|
2008-03-03 11:26:09 +01:00
|
|
|
% part of DYNARE, copyright Dynare Team (2006-2008)
|
2007-10-03 16:40:43 +02:00
|
|
|
% Gnu Public License.
|
2007-01-26 15:50:30 +01:00
|
|
|
global bayestopt_ estim_params_ M_ options_
|
2006-09-13 14:39:23 +02:00
|
|
|
|
|
|
|
nvx = estim_params_.nvx;
|
|
|
|
nvn = estim_params_.nvn;
|
|
|
|
ncx = estim_params_.ncx;
|
|
|
|
ncn = estim_params_.ncn;
|
|
|
|
np = estim_params_.np;
|
|
|
|
nx = nvx+nvn+ncx+ncn+np;
|
|
|
|
ns = nvx+nvn+ncx+ncn;
|
|
|
|
|
2007-07-25 15:25:38 +02:00
|
|
|
NumberOfObservedVariables = size(options_.varobs,1);
|
|
|
|
NumberOfLags = options_.varlag;
|
2007-07-26 10:50:38 +02:00
|
|
|
NumberOfParameters = NumberOfObservedVariables*NumberOfLags ;
|
2007-12-07 17:31:15 +01:00
|
|
|
if ~options_.noconstant
|
|
|
|
NumberOfParameters = NumberOfParameters + 1;
|
|
|
|
end
|
2007-07-25 15:25:38 +02:00
|
|
|
|
2006-09-13 14:39:23 +02:00
|
|
|
mYY = evalin('base', 'mYY');
|
|
|
|
mYX = evalin('base', 'mYX');
|
|
|
|
mXY = evalin('base', 'mXY');
|
|
|
|
mXX = evalin('base', 'mXX');
|
|
|
|
|
|
|
|
fval = [];
|
2007-10-03 16:40:43 +02:00
|
|
|
cost_flag = 1;
|
2006-09-13 14:39:23 +02:00
|
|
|
|
|
|
|
if options_.mode_compute ~= 1 & any(xparam1 < bayestopt_.lb)
|
2007-01-26 15:50:30 +01:00
|
|
|
k = find(xparam1 < bayestopt_.lb);
|
|
|
|
fval = bayestopt_.penalty*min(1e3,exp(sum(bayestopt_.lb(k)-xparam1(k))));
|
2007-07-25 15:25:38 +02:00
|
|
|
info = 41;
|
2007-01-26 15:50:30 +01:00
|
|
|
cost_flag = 0;
|
|
|
|
return;
|
2006-09-13 14:39:23 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
if options_.mode_compute ~= 1 & any(xparam1 > bayestopt_.ub)
|
2007-01-26 15:50:30 +01:00
|
|
|
k = find(xparam1 > bayestopt_.ub);
|
2007-07-25 15:25:38 +02:00
|
|
|
fval = bayestopt_.penalty*min(1e3,exp(sum(xparam1(k)- bayestopt_.ub(k))));
|
|
|
|
info = 42;
|
2007-01-26 15:50:30 +01:00
|
|
|
cost_flag = 0;
|
|
|
|
return;
|
2006-09-13 14:39:23 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
Q = M_.Sigma_e;
|
|
|
|
for i=1:estim_params_.nvx
|
2007-01-26 15:50:30 +01:00
|
|
|
k = estim_params_.var_exo(i,1);
|
|
|
|
Q(k,k) = xparam1(i)*xparam1(i);
|
2006-09-13 14:39:23 +02:00
|
|
|
end
|
|
|
|
offset = estim_params_.nvx;
|
|
|
|
if estim_params_.nvn
|
2007-10-03 16:40:43 +02:00
|
|
|
disp('DsgeVarLikelihood :: Measurement errors are implemented!')
|
2007-01-26 15:50:30 +01:00
|
|
|
return
|
2006-09-13 14:39:23 +02:00
|
|
|
end
|
|
|
|
if estim_params_.ncx
|
2007-10-03 16:40:43 +02:00
|
|
|
disp('DsgeVarLikelihood :: Correlated structural innovations are not implemented!')
|
2007-01-26 15:50:30 +01:00
|
|
|
return
|
2006-09-13 14:39:23 +02:00
|
|
|
end
|
2007-07-25 15:25:38 +02:00
|
|
|
|
2006-09-13 14:39:23 +02:00
|
|
|
M_.params(estim_params_.param_vals(:,1)) = xparam1(offset+1:end);
|
|
|
|
M_.Sigma_e = Q;
|
2007-07-25 15:25:38 +02:00
|
|
|
|
2006-09-13 14:39:23 +02:00
|
|
|
%% Weight of the dsge prior:
|
|
|
|
dsge_prior_weight = M_.params(strmatch('dsge_prior_weight',M_.param_names));
|
2007-10-03 16:40:43 +02:00
|
|
|
% Is the DSGE prior proper?
|
2007-07-26 10:50:38 +02:00
|
|
|
if dsge_prior_weight<(NumberOfParameters+NumberOfObservedVariables)/gend;
|
|
|
|
fval = bayestopt_.penalty*min(1e3,(NumberOfParameters+NumberOfObservedVariables)/gend-dsge_prior_weight);
|
2007-11-14 18:42:18 +01:00
|
|
|
info = 51;
|
2007-07-25 15:25:38 +02:00
|
|
|
cost_flag = 0;
|
|
|
|
return;
|
|
|
|
end
|
2007-07-06 16:43:00 +02:00
|
|
|
|
2007-10-03 16:40:43 +02:00
|
|
|
|
2006-09-13 14:39:23 +02:00
|
|
|
%------------------------------------------------------------------------------
|
|
|
|
% 2. call model setup & reduction program
|
|
|
|
%------------------------------------------------------------------------------
|
2006-09-22 17:58:43 +02:00
|
|
|
[T,R,SteadyState,info] = dynare_resolve(bayestopt_.restrict_var_list,...
|
|
|
|
bayestopt_.restrict_columns,...
|
|
|
|
bayestopt_.restrict_aux);
|
2006-09-13 14:39:23 +02:00
|
|
|
if info(1) == 1 | info(1) == 2 | info(1) == 5
|
2007-01-26 15:50:30 +01:00
|
|
|
fval = bayestopt_.penalty;
|
|
|
|
cost_flag = 0;
|
|
|
|
return
|
2006-09-13 14:39:23 +02:00
|
|
|
elseif info(1) == 3 | info(1) == 4 | info(1) == 20
|
2007-01-26 15:50:30 +01:00
|
|
|
fval = bayestopt_.penalty*min(1e3,exp(info(2)));
|
|
|
|
cost_flag = 0;
|
|
|
|
return
|
2006-09-13 14:39:23 +02:00
|
|
|
end
|
2007-11-14 18:42:18 +01:00
|
|
|
if ~options_.noconstant
|
|
|
|
if options_.loglinear
|
|
|
|
constant = transpose(log(SteadyState(bayestopt_.mfys)));
|
|
|
|
else
|
|
|
|
constant = transpose(SteadyState(bayestopt_.mfys));
|
2007-12-07 17:31:15 +01:00
|
|
|
end
|
2006-09-13 14:39:23 +02:00
|
|
|
else
|
2007-11-14 18:42:18 +01:00
|
|
|
constant = zeros(1,NumberOfObservedVariables);
|
2006-09-13 14:39:23 +02:00
|
|
|
end
|
|
|
|
if bayestopt_.with_trend == 1
|
2007-01-26 15:50:30 +01:00
|
|
|
disp('DsgeVarLikelihood :: Linear trend is not yet implemented!')
|
|
|
|
return
|
2006-09-13 14:39:23 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
%------------------------------------------------------------------------------
|
2007-11-14 18:42:18 +01:00
|
|
|
% 3. theoretical moments (second order)
|
2006-09-13 14:39:23 +02:00
|
|
|
%------------------------------------------------------------------------------
|
2007-10-03 16:40:43 +02:00
|
|
|
tmp0 = lyapunov_symm(T,R*Q*R');% I compute the variance-covariance matrix
|
2007-11-14 18:42:18 +01:00
|
|
|
mf = bayestopt_.mf1; % of the restricted state vector.
|
2007-07-06 16:43:00 +02:00
|
|
|
|
2007-11-14 18:42:18 +01:00
|
|
|
% Get the non centered second order moments
|
2006-09-13 14:39:23 +02:00
|
|
|
TheoreticalAutoCovarianceOfTheObservedVariables = ...
|
|
|
|
zeros(NumberOfObservedVariables,NumberOfObservedVariables,NumberOfLags+1);
|
2007-11-14 18:42:18 +01:00
|
|
|
TheoreticalAutoCovarianceOfTheObservedVariables(:,:,1) = tmp0(mf,mf)+constant'*constant;
|
2006-09-13 14:39:23 +02:00
|
|
|
for lag = 1:NumberOfLags
|
2007-10-03 16:40:43 +02:00
|
|
|
tmp0 = T*tmp0;
|
2007-11-14 18:42:18 +01:00
|
|
|
TheoreticalAutoCovarianceOfTheObservedVariables(:,:,lag+1) = tmp0(mf,mf) ...
|
|
|
|
+ constant'*constant;
|
2006-09-13 14:39:23 +02:00
|
|
|
end
|
2007-11-14 18:42:18 +01:00
|
|
|
% Build the theoretical "covariance" between Y and X
|
2007-07-26 10:50:38 +02:00
|
|
|
GYX = zeros(NumberOfObservedVariables,NumberOfParameters);
|
2006-09-13 14:39:23 +02:00
|
|
|
for i=1:NumberOfLags
|
|
|
|
GYX(:,(i-1)*NumberOfObservedVariables+1:i*NumberOfObservedVariables) = ...
|
|
|
|
TheoreticalAutoCovarianceOfTheObservedVariables(:,:,i+1);
|
|
|
|
end
|
2007-11-14 18:42:18 +01:00
|
|
|
if ~options_.noconstant
|
|
|
|
GYX(:,end) = constant';
|
|
|
|
end
|
|
|
|
% Build the theoretical "covariance" between X and X
|
2006-09-13 14:39:23 +02:00
|
|
|
GXX = kron(eye(NumberOfLags), ...
|
|
|
|
TheoreticalAutoCovarianceOfTheObservedVariables(:,:,1));
|
|
|
|
for i = 1:NumberOfLags-1
|
2007-11-14 18:42:18 +01:00
|
|
|
tmp1 = diag(ones(NumberOfLags-i,1),i);
|
|
|
|
tmp2 = diag(ones(NumberOfLags-i,1),-i);
|
|
|
|
GXX = GXX + kron(tmp1,TheoreticalAutoCovarianceOfTheObservedVariables(:,:,i+1));
|
|
|
|
GXX = GXX + kron(tmp2,TheoreticalAutoCovarianceOfTheObservedVariables(:,:,i+1)');
|
|
|
|
end
|
|
|
|
if ~options_.noconstant
|
|
|
|
% Add one row and one column to GXX
|
2007-12-07 17:31:15 +01:00
|
|
|
GXX = [GXX , kron(ones(NumberOfLags,1),constant') ; [ kron(ones(1,NumberOfLags),constant) , 1] ];
|
2006-09-13 14:39:23 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
GYY = TheoreticalAutoCovarianceOfTheObservedVariables(:,:,1);
|
|
|
|
|
|
|
|
assignin('base','GYY',GYY);
|
|
|
|
assignin('base','GXX',GXX);
|
|
|
|
assignin('base','GYX',GYX);
|
|
|
|
|
2007-11-14 18:42:18 +01:00
|
|
|
if ~isinf(dsge_prior_weight)
|
2007-10-03 16:40:43 +02:00
|
|
|
tmp0 = dsge_prior_weight*gend*TheoreticalAutoCovarianceOfTheObservedVariables(:,:,1) + mYY ;
|
2006-09-13 14:39:23 +02:00
|
|
|
tmp1 = dsge_prior_weight*gend*GYX + mYX;
|
|
|
|
tmp2 = inv(dsge_prior_weight*gend*GXX+mXX);
|
2007-10-03 16:40:43 +02:00
|
|
|
SIGMAu = tmp0 - tmp1*tmp2*tmp1'; clear('tmp0');
|
2007-07-25 15:25:38 +02:00
|
|
|
if ~ispd(SIGMAu)
|
|
|
|
v = diag(SIGMAu);
|
|
|
|
k = find(v<0);
|
|
|
|
fval = bayestopt_.penalty*min(1e3,exp(abs(v(k))));
|
2007-07-26 10:21:54 +02:00
|
|
|
info = 52;
|
2007-07-25 15:25:38 +02:00
|
|
|
cost_flag = 0;
|
2007-10-03 16:40:43 +02:00
|
|
|
return;
|
2007-07-25 15:25:38 +02:00
|
|
|
end
|
2007-10-03 16:40:43 +02:00
|
|
|
SIGMAu = SIGMAu / (gend*(1+dsge_prior_weight));
|
|
|
|
PHI = tmp2*tmp1'; clear('tmp1');
|
2006-09-13 14:39:23 +02:00
|
|
|
prodlng1 = sum(gammaln(.5*((1+dsge_prior_weight)*gend- ...
|
|
|
|
NumberOfObservedVariables*NumberOfLags ...
|
|
|
|
+1-(1:NumberOfObservedVariables)')));
|
|
|
|
prodlng2 = sum(gammaln(.5*(dsge_prior_weight*gend- ...
|
|
|
|
NumberOfObservedVariables*NumberOfLags ...
|
|
|
|
+1-(1:NumberOfObservedVariables)')));
|
|
|
|
lik = .5*NumberOfObservedVariables*log(det(dsge_prior_weight*gend*GXX+mXX)) ...
|
2007-07-26 10:50:38 +02:00
|
|
|
+ .5*((dsge_prior_weight+1)*gend-NumberOfParameters)*log(det((dsge_prior_weight+1)*gend*SIGMAu)) ...
|
2006-09-13 14:39:23 +02:00
|
|
|
- .5*NumberOfObservedVariables*log(det(dsge_prior_weight*gend*GXX)) ...
|
2007-07-26 10:50:38 +02:00
|
|
|
- .5*(dsge_prior_weight*gend-NumberOfParameters)*log(det(dsge_prior_weight*gend*(GYY-GYX*inv(GXX)*GYX'))) ...
|
2006-09-13 14:39:23 +02:00
|
|
|
+ .5*NumberOfObservedVariables*gend*log(2*pi) ...
|
2007-07-26 10:50:38 +02:00
|
|
|
- .5*log(2)*NumberOfObservedVariables*((dsge_prior_weight+1)*gend-NumberOfParameters) ...
|
|
|
|
+ .5*log(2)*NumberOfObservedVariables*(dsge_prior_weight*gend-NumberOfParameters) ...
|
2006-09-13 14:39:23 +02:00
|
|
|
- prodlng1 + prodlng2;
|
2007-10-03 16:40:43 +02:00
|
|
|
else
|
|
|
|
iGXX = inv(GXX);
|
|
|
|
SIGMAu = GYY - GYX*iGXX*transpose(GYX);
|
|
|
|
PHI = iGXX*transpose(GYX);
|
|
|
|
lik = gend * ( log(det(SIGMAu)) + NumberOfObservedVariables*log(2*pi) + ...
|
|
|
|
trace(inv(SIGMAu)*(mYY - transpose(mYX*PHI) - mYX*PHI + transpose(PHI)*mXX*PHI)/gend));
|
|
|
|
lik = .5*lik;% Minus likelihood
|
2006-09-13 14:39:23 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
lnprior = priordens(xparam1,bayestopt_.pshape,bayestopt_.p1,bayestopt_.p2,bayestopt_.p3,bayestopt_.p4);
|
|
|
|
fval = (lik-lnprior);
|
2008-03-18 11:28:53 +01:00
|
|
|
|
2007-10-03 16:40:43 +02:00
|
|
|
if (nargout == 6)
|
|
|
|
if isinf(dsge_prior_weight)
|
|
|
|
iXX = iGXX;
|
|
|
|
else
|
|
|
|
iXX = tmp2;
|
|
|
|
end
|
2008-03-10 17:08:02 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
if (nargout==7)
|
2008-03-27 10:19:38 +01:00
|
|
|
if isinf(dsge_prior_weight)
|
|
|
|
iXX = iGXX;
|
|
|
|
else
|
|
|
|
iXX = tmp2;
|
|
|
|
end
|
|
|
|
iGXX = inv(GXX);
|
2008-03-10 17:08:02 +01:00
|
|
|
prior.SIGMAstar = GYY - GYX*iGXX*GYX';
|
|
|
|
prior.PHIstar = iGXX*transpose(GYX);
|
2008-03-18 11:28:53 +01:00
|
|
|
prior.ArtificialSampleSize = fix(dsge_prior_weight*gend);
|
2008-03-10 17:08:02 +01:00
|
|
|
prior.DF = prior.ArtificialSampleSize - NumberOfParameters - NumberOfObservedVariables;
|
|
|
|
prior.iGXX = iGXX;
|
2007-10-03 16:40:43 +02:00
|
|
|
end
|