Home > matlab > DsgeVarLikelihood.m

DsgeVarLikelihood

PURPOSE ^

Evaluates the posterior kernel of the bvar-dsge model.

SYNOPSIS ^

function [fval,grad,hess,exit_flag,info,PHI,SIGMAu,iXX,prior] = DsgeVarLikelihood(xparam1,DynareDataset,DynareOptions,Model,EstimatedParameters,BayesInfo,DynareResults)

DESCRIPTION ^

 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).
   o prior         [double]     a matlab structure describing the dsge-var prior.

 SPECIAL REQUIREMENTS
   None.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [fval,grad,hess,exit_flag,info,PHI,SIGMAu,iXX,prior] = DsgeVarLikelihood(xparam1,DynareDataset,DynareOptions,Model,EstimatedParameters,BayesInfo,DynareResults)
0002 % Evaluates the posterior kernel of the bvar-dsge model.
0003 %
0004 % INPUTS
0005 %   o xparam1       [double]     Vector of model's parameters.
0006 %   o gend          [integer]    Number of observations (without conditionning observations for the lags).
0007 %
0008 % OUTPUTS
0009 %   o fval          [double]     Value of the posterior kernel at xparam1.
0010 %   o cost_flag     [integer]    Zero if the function returns a penalty, one otherwise.
0011 %   o info          [integer]    Vector of informations about the penalty.
0012 %   o PHI           [double]     Stacked BVAR-DSGE autoregressive matrices (at the mode associated to xparam1).
0013 %   o SIGMAu        [double]     Covariance matrix of the BVAR-DSGE (at the mode associated to xparam1).
0014 %   o iXX           [double]     inv(X'X).
0015 %   o prior         [double]     a matlab structure describing the dsge-var prior.
0016 %
0017 % SPECIAL REQUIREMENTS
0018 %   None.
0019 
0020 % Copyright (C) 2006-2011 Dynare Team
0021 %
0022 % This file is part of Dynare.
0023 %
0024 % Dynare is free software: you can redistribute it and/or modify
0025 % it under the terms of the GNU General Public License as published by
0026 % the Free Software Foundation, either version 3 of the License, or
0027 % (at your option) any later version.
0028 %
0029 % Dynare is distributed in the hope that it will be useful,
0030 % but WITHOUT ANY WARRANTY; without even the implied warranty of
0031 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0032 % GNU General Public License for more details.
0033 %
0034 % You should have received a copy of the GNU General Public License
0035 % along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
0036 
0037 % Declaration of the persistent variables.
0038 persistent penalty dsge_prior_weight_idx
0039 
0040 grad=[];
0041 hess=[];
0042 exit_flag = [];
0043 info = [];
0044 PHI = [];
0045 SIGMAu = [];
0046 iXX = [];
0047 prior = [];
0048 
0049 % Initialization of the penalty
0050 if ~nargin || isempty(penalty)
0051     penalty = 1e8;
0052     if ~nargin, return, end
0053 end
0054 if nargin==1
0055     penalty = xparam1;
0056     return
0057 end
0058 
0059 % Initialization of of the index for parameter dsge_prior_weight in Model.params.
0060 if isempty(dsge_prior_weight_idx)
0061     dsge_prior_weight_idx = strmatch('dsge_prior_weight',Model.param_names);
0062 end
0063 
0064 % Get the number of estimated (dsge) parameters.
0065 ns = EstimatedParameters.nvx + ...
0066      EstimatedParameters.nvn + ...
0067      EstimatedParameters.ncx + ...
0068      EstimatedParameters.ncn;
0069 nx = ns + EstimatedParameters.np;
0070 
0071 % Get the number of observed variables in the VAR model.
0072 NumberOfObservedVariables = DynareDataset.info.nvobs;
0073 
0074 % Get the number of lags in the VAR model.
0075 NumberOfLags = DynareOptions.dsge_varlag;
0076 
0077 % Get the number of parameters in the VAR model.
0078 NumberOfParameters = NumberOfObservedVariables*NumberOfLags ;
0079 if ~DynareOptions.noconstant
0080     NumberOfParameters = NumberOfParameters + 1;
0081 end
0082 
0083 % Get empirical second order moments for the observed variables.
0084 mYY = evalin('base', 'mYY');
0085 mYX = evalin('base', 'mYX');
0086 mXY = evalin('base', 'mXY');
0087 mXX = evalin('base', 'mXX');
0088 
0089 % Initialize some of the output arguments.
0090 fval = [];
0091 exit_flag = 1;
0092 
0093 % Return, with endogenous penalty, if some dsge-parameters are smaller than the lower bound of the prior domain.
0094 if DynareOptions.mode_compute ~= 1 && any(xparam1 < BayesInfo.lb)
0095     k = find(xparam1 < BayesInfo.lb);
0096     fval = penalty+sum((BayesInfo.lb(k)-xparam1(k)).^2);
0097     exit_flag = 0;
0098     info = 41;
0099     return;
0100 end
0101 
0102 % Return, with endogenous penalty, if some dsge-parameters are greater than the upper bound of the prior domain.
0103 if DynareOptions.mode_compute ~= 1 && any(xparam1 > BayesInfo.ub)
0104     k = find(xparam1 > BayesInfo.ub);
0105     fval = penalty+sum((xparam1(k)-BayesInfo.ub(k)).^2);
0106     exit_flag = 0;
0107     info = 42;
0108     return;
0109 end
0110 
0111 % Get the variance of each structural innovation.
0112 Q = Model.Sigma_e;
0113 for i=1:EstimatedParameters.nvx
0114     k = EstimatedParameters.var_exo(i,1);
0115     Q(k,k) = xparam1(i)*xparam1(i);
0116 end
0117 offset = EstimatedParameters.nvx;
0118 
0119 % Check that the user does not estimate measurment errors.
0120 % TODO Check that the user does not declare non estimated measurement errors...
0121 if EstimatedParameters.nvn
0122     disp('DsgeVarLikelihood :: Measurement errors are not implemented!')
0123     return
0124 end
0125 
0126 % Check that the user does not estimate off diagonal elements in the covariance matrix of the structural innovation.
0127 % TODO Check that Q is a diagonal matrix...
0128 if EstimatedParameters.ncx
0129     disp('DsgeVarLikelihood :: Correlated structural innovations are not implemented!')
0130     return
0131 end
0132 
0133 % Update Model.params and Model.Sigma_e.
0134 Model.params(EstimatedParameters.param_vals(:,1)) = xparam1(offset+1:end);
0135 Model.Sigma_e = Q;
0136 
0137 % Get the weight of the dsge prior.
0138 dsge_prior_weight = Model.params(dsge_prior_weight_idx);
0139 
0140 % Is the dsge prior proper?
0141 if dsge_prior_weight<(NumberOfParameters+NumberOfObservedVariables)/DynareDataset.info.ntobs;
0142     fval = penalty+abs(DynareDataset.info.ntobs*dsge_prior_weight-(NumberOfParameters+NumberOfObservedVariables));
0143     exit_flag = 0;
0144     info = 51;
0145     return
0146 end
0147 
0148 %------------------------------------------------------------------------------
0149 % 2. call model setup & reduction program
0150 %------------------------------------------------------------------------------
0151 
0152 % Solve the Dsge model and get the matrices of the reduced form solution. T and R are the matrices of the
0153 % state equation
0154 [T,R,SteadyState,info,Model,DynareOptions,DynareResults] = dynare_resolve(Model,DynareOptions,DynareResults,'restrict');
0155 
0156 % Return, with endogenous penalty when possible, if dynare_resolve issues an error code (defined in resol).
0157 if info(1) == 1 || info(1) == 2 || info(1) == 5
0158     fval = penalty+1;
0159     info = info(1);
0160     exit_flag = 0;
0161     return
0162 elseif info(1) == 3 || info(1) == 4 || info(1) == 19 || info(1) == 20 || info(1) == 21
0163     fval = penalty+info(2);
0164     info = info(1);
0165     exit_flag = 0;
0166     return
0167 end
0168 
0169 % Define the mean/steady state vector.
0170 if ~DynareOptions.noconstant
0171     if DynareOptions.loglinear
0172         constant = transpose(log(SteadyState(BayesInfo.mfys)));
0173     else
0174         constant = transpose(SteadyState(BayesInfo.mfys));
0175     end
0176 else
0177     constant = zeros(1,NumberOfObservedVariables);
0178 end
0179 
0180 % Dsge-VAR with deterministic trends is not implemented
0181 if BayesInfo.with_trend == 1
0182     error('DsgeVarLikelihood :: Linear trend is not yet implemented!')
0183 end
0184 
0185 %------------------------------------------------------------------------------
0186 % 3. theoretical moments (second order)
0187 %------------------------------------------------------------------------------
0188 
0189 % Compute the theoretical second order moments
0190 tmp0 = lyapunov_symm(T,R*Q*R',DynareOptions.qz_criterium,DynareOptions.lyapunov_complex_threshold);
0191 mf  = BayesInfo.mf1;
0192 
0193 % Get the non centered second order moments
0194 TheoreticalAutoCovarianceOfTheObservedVariables = zeros(NumberOfObservedVariables,NumberOfObservedVariables,NumberOfLags+1);
0195 TheoreticalAutoCovarianceOfTheObservedVariables(:,:,1) = tmp0(mf,mf)+constant'*constant;
0196 for lag = 1:NumberOfLags
0197     tmp0 = T*tmp0;
0198     TheoreticalAutoCovarianceOfTheObservedVariables(:,:,lag+1) = tmp0(mf,mf) + constant'*constant;
0199 end
0200 
0201 % Build the theoretical "covariance" between Y and X
0202 GYX = zeros(NumberOfObservedVariables,NumberOfParameters);
0203 for i=1:NumberOfLags
0204     GYX(:,(i-1)*NumberOfObservedVariables+1:i*NumberOfObservedVariables) = TheoreticalAutoCovarianceOfTheObservedVariables(:,:,i+1);
0205 end
0206 if ~DynareOptions.noconstant
0207     GYX(:,end) = constant';
0208 end
0209 
0210 % Build the theoretical "covariance" between X and X
0211 GXX = kron(eye(NumberOfLags), TheoreticalAutoCovarianceOfTheObservedVariables(:,:,1));
0212 for i = 1:NumberOfLags-1
0213     tmp1 = diag(ones(NumberOfLags-i,1),i);
0214     tmp2 = diag(ones(NumberOfLags-i,1),-i);
0215     GXX = GXX + kron(tmp1,TheoreticalAutoCovarianceOfTheObservedVariables(:,:,i+1));
0216     GXX = GXX + kron(tmp2,TheoreticalAutoCovarianceOfTheObservedVariables(:,:,i+1)');
0217 end
0218 
0219 if ~DynareOptions.noconstant
0220     % Add one row and one column to GXX
0221     GXX = [GXX , kron(ones(NumberOfLags,1),constant') ; [  kron(ones(1,NumberOfLags),constant) , 1] ];
0222 end
0223 
0224 GYY = TheoreticalAutoCovarianceOfTheObservedVariables(:,:,1);
0225 
0226 assignin('base','GYY',GYY);
0227 assignin('base','GXX',GXX);
0228 assignin('base','GYX',GYX);
0229 
0230 if ~isinf(dsge_prior_weight)% Evaluation of the likelihood of the dsge-var model when the dsge prior weight is finite.
0231     tmp0 = dsge_prior_weight*DynareDataset.info.ntobs*TheoreticalAutoCovarianceOfTheObservedVariables(:,:,1) + mYY ;
0232     tmp1 = dsge_prior_weight*DynareDataset.info.ntobs*GYX + mYX;
0233     tmp2 = inv(dsge_prior_weight*DynareDataset.info.ntobs*GXX+mXX);
0234     SIGMAu = tmp0 - tmp1*tmp2*tmp1'; clear('tmp0');
0235     if ~ispd(SIGMAu)
0236         v = diag(SIGMAu);
0237         k = find(v<0);
0238         fval = penalty + sum(v(k).^2);
0239         info = 52;
0240         exit_flag = 0;
0241         return;
0242     end
0243     SIGMAu = SIGMAu / (DynareDataset.info.ntobs*(1+dsge_prior_weight));
0244     PHI = tmp2*tmp1'; clear('tmp1');
0245     prodlng1 = sum(gammaln(.5*((1+dsge_prior_weight)*DynareDataset.info.ntobs- ...
0246                                NumberOfObservedVariables*NumberOfLags ...
0247                                +1-(1:NumberOfObservedVariables)')));
0248     prodlng2 = sum(gammaln(.5*(dsge_prior_weight*DynareDataset.info.ntobs- ...
0249                                NumberOfObservedVariables*NumberOfLags ...
0250                                +1-(1:NumberOfObservedVariables)')));
0251     lik = .5*NumberOfObservedVariables*log(det(dsge_prior_weight*DynareDataset.info.ntobs*GXX+mXX)) ...
0252           + .5*((dsge_prior_weight+1)*DynareDataset.info.ntobs-NumberOfParameters)*log(det((dsge_prior_weight+1)*DynareDataset.info.ntobs*SIGMAu)) ...
0253           - .5*NumberOfObservedVariables*log(det(dsge_prior_weight*DynareDataset.info.ntobs*GXX)) ...
0254           - .5*(dsge_prior_weight*DynareDataset.info.ntobs-NumberOfParameters)*log(det(dsge_prior_weight*DynareDataset.info.ntobs*(GYY-GYX*inv(GXX)*GYX'))) ...
0255           + .5*NumberOfObservedVariables*DynareDataset.info.ntobs*log(2*pi)  ...
0256           - .5*log(2)*NumberOfObservedVariables*((dsge_prior_weight+1)*DynareDataset.info.ntobs-NumberOfParameters) ...
0257           + .5*log(2)*NumberOfObservedVariables*(dsge_prior_weight*DynareDataset.info.ntobs-NumberOfParameters) ...
0258           - prodlng1 + prodlng2;
0259 else% Evaluation of the likelihood of the dsge-var model when the dsge prior weight is infinite.
0260     iGXX = inv(GXX);
0261     SIGMAu = GYY - GYX*iGXX*transpose(GYX);
0262     PHI = iGXX*transpose(GYX);
0263     lik = DynareDataset.info.ntobs * ( log(det(SIGMAu)) + NumberOfObservedVariables*log(2*pi) +  ...
0264                    trace(inv(SIGMAu)*(mYY - transpose(mYX*PHI) - mYX*PHI + transpose(PHI)*mXX*PHI)/DynareDataset.info.ntobs));
0265     lik = .5*lik;% Minus likelihood
0266 end
0267 
0268 % Add the (logged) prior density for the dsge-parameters.
0269 lnprior = priordens(xparam1,BayesInfo.pshape,BayesInfo.p6,BayesInfo.p7,BayesInfo.p3,BayesInfo.p4);
0270 fval = (lik-lnprior);
0271 
0272 if (nargout == 8)
0273     if isinf(dsge_prior_weight)
0274         iXX = iGXX;
0275     else
0276         iXX = tmp2;
0277     end
0278 end
0279 
0280 if (nargout==9)
0281     if isinf(dsge_prior_weight)
0282         iXX = iGXX;
0283     else
0284         iXX = tmp2;
0285     end
0286     iGXX = inv(GXX);
0287     prior.SIGMAstar = GYY - GYX*iGXX*GYX';
0288     prior.PHIstar = iGXX*transpose(GYX);
0289     prior.ArtificialSampleSize = fix(dsge_prior_weight*DynareDataset.info.ntobs);
0290     prior.DF = prior.ArtificialSampleSize - NumberOfParameters - NumberOfObservedVariables;
0291     prior.iGXX = iGXX;
0292 end

Generated on Mon 21-May-2012 02:42:43 by m2html © 2005