Home > matlab > dsge_posterior_kernel.m

dsge_posterior_kernel

PURPOSE ^

function [fval,cost_flag,ys,trend_coeff,info] = dsge_posterior_kernel(xparam1,gend,data,data_index,number_of_observations,no_more_missing_observations)

SYNOPSIS ^

function [fval,cost_flag,ys,trend_coeff,info] = dsge_posterior_kernel(xparam1,gend,data,data_index,number_of_observations,no_more_missing_observations)

DESCRIPTION ^

 function [fval,cost_flag,ys,trend_coeff,info] = dsge_posterior_kernel(xparam1,gend,data,data_index,number_of_observations,no_more_missing_observations)
 Evaluates the posterior kernel of a dsge model. 
 
 INPUTS 
   xparam1                        [double]   vector of model parameters.
   gend                           [integer]  scalar specifying the number of observations.
   data                           [double]   matrix of data
   data_index                     [cell]     cell of column vectors
   number_of_observations         [integer]
   no_more_missing_observations   [integer] 
 OUTPUTS 
   fval        :     value of the posterior kernel at xparam1.
   cost_flag   :     zero if the function returns a penalty, one otherwise.
   ys          :     steady state of original endogenous variables
   trend_coeff :
   info        :     vector of informations about the penalty:
                     41: one (many) parameter(s) do(es) not satisfied the lower bound
                     42: one (many) parameter(s) do(es) not satisfied the upper bound
               
 SPECIAL REQUIREMENTS

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [fval,cost_flag,ys,trend_coeff,info] = dsge_posterior_kernel(xparam1,gend,data,data_index,number_of_observations,no_more_missing_observations)
0002 % function [fval,cost_flag,ys,trend_coeff,info] = dsge_posterior_kernel(xparam1,gend,data,data_index,number_of_observations,no_more_missing_observations)
0003 % Evaluates the posterior kernel of a dsge model.
0004 %
0005 % INPUTS
0006 %   xparam1                        [double]   vector of model parameters.
0007 %   gend                           [integer]  scalar specifying the number of observations.
0008 %   data                           [double]   matrix of data
0009 %   data_index                     [cell]     cell of column vectors
0010 %   number_of_observations         [integer]
0011 %   no_more_missing_observations   [integer]
0012 % OUTPUTS
0013 %   fval        :     value of the posterior kernel at xparam1.
0014 %   cost_flag   :     zero if the function returns a penalty, one otherwise.
0015 %   ys          :     steady state of original endogenous variables
0016 %   trend_coeff :
0017 %   info        :     vector of informations about the penalty:
0018 %                     41: one (many) parameter(s) do(es) not satisfied the lower bound
0019 %                     42: one (many) parameter(s) do(es) not satisfied the upper bound
0020 %
0021 % SPECIAL REQUIREMENTS
0022 %
0023 
0024 % Copyright (C) 2004-2011 Dynare Team
0025 %
0026 % This file is part of Dynare.
0027 %
0028 % Dynare is free software: you can redistribute it and/or modify
0029 % it under the terms of the GNU General Public License as published by
0030 % the Free Software Foundation, either version 3 of the License, or
0031 % (at your option) any later version.
0032 %
0033 % Dynare is distributed in the hope that it will be useful,
0034 % but WITHOUT ANY WARRANTY; without even the implied warranty of
0035 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0036 % GNU General Public License for more details.
0037 %
0038 % You should have received a copy of the GNU General Public License
0039 % along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
0040 
0041 global bayestopt_ estim_params_ options_ trend_coeff_ M_ oo_
0042 fval            = [];
0043 ys              = [];
0044 trend_coeff     = [];
0045 cost_flag       = 1;
0046 nobs            = size(options_.varobs,1);
0047 %------------------------------------------------------------------------------
0048 % 1. Get the structural parameters & define penalties
0049 %------------------------------------------------------------------------------
0050 if ~isequal(options_.mode_compute,1) && any(xparam1 < bayestopt_.lb)
0051     k = find(xparam1 < bayestopt_.lb);
0052     fval = bayestopt_.penalty+sum((bayestopt_.lb(k)-xparam1(k)).^2);
0053     cost_flag = 0;
0054     info = 41;
0055     return;
0056 end
0057 if ~isequal(options_.mode_compute,1) && any(xparam1 > bayestopt_.ub)
0058     k = find(xparam1 > bayestopt_.ub);
0059     fval = bayestopt_.penalty+sum((xparam1(k)-bayestopt_.ub(k)).^2);
0060     cost_flag = 0;
0061     info = 42;
0062     return;
0063 end
0064 Q = M_.Sigma_e;
0065 H = M_.H;
0066 for i=1:estim_params_.nvx
0067     k =estim_params_.var_exo(i,1);
0068     Q(k,k) = xparam1(i)*xparam1(i);
0069 end
0070 offset = estim_params_.nvx;
0071 if estim_params_.nvn
0072     for i=1:estim_params_.nvn
0073         k = estim_params_.var_endo(i,1);
0074         H(k,k) = xparam1(i+offset)*xparam1(i+offset);
0075     end
0076     offset = offset+estim_params_.nvn;
0077 end     
0078 if estim_params_.ncx
0079     for i=1:estim_params_.ncx
0080         k1 =estim_params_.corrx(i,1);
0081         k2 =estim_params_.corrx(i,2);
0082         Q(k1,k2) = xparam1(i+offset)*sqrt(Q(k1,k1)*Q(k2,k2));
0083         Q(k2,k1) = Q(k1,k2);
0084     end
0085     [CholQ,testQ] = chol(Q);
0086     if testQ    %% The variance-covariance matrix of the structural innovations is not definite positive.
0087         %% We have to compute the eigenvalues of this matrix in order to build the penalty.
0088         a = diag(eig(Q));
0089         k = find(a < 0);
0090         if k > 0
0091             fval = bayestopt_.penalty+sum(-a(k));
0092             cost_flag = 0;
0093             info = 43;
0094             return
0095         end
0096     end
0097     offset = offset+estim_params_.ncx;
0098 end
0099 if estim_params_.ncn 
0100     for i=1:estim_params_.ncn
0101         k1 = options_.lgyidx2varobs(estim_params_.corrn(i,1));
0102         k2 = options_.lgyidx2varobs(estim_params_.corrn(i,2));
0103         H(k1,k2) = xparam1(i+offset)*sqrt(H(k1,k1)*H(k2,k2));
0104         H(k2,k1) = H(k1,k2);
0105     end
0106     [CholH,testH] = chol(H);
0107     if testH
0108         a = diag(eig(H));
0109         k = find(a < 0);
0110         if k > 0
0111             fval = bayestopt_.penalty+sum(-a(k));
0112             cost_flag = 0;
0113             info = 44;
0114             return
0115         end
0116     end
0117     offset = offset+estim_params_.ncn;
0118 end
0119 if estim_params_.np > 0
0120     M_.params(estim_params_.param_vals(:,1)) = xparam1(offset+1:end);
0121 end
0122 M_.Sigma_e = Q;
0123 M_.H = H;
0124 %------------------------------------------------------------------------------
0125 % 2. call model setup & reduction program
0126 %------------------------------------------------------------------------------
0127 [T,R,SteadyState,info,M_,options_,oo_] = dynare_resolve(M_,options_,oo_);
0128 if info(1) == 1 || info(1) == 2 || info(1) == 5
0129     fval = bayestopt_.penalty+1;
0130     cost_flag = 0;
0131     return
0132 elseif info(1) == 3 || info(1) == 4 || info(1) == 20
0133     fval = bayestopt_.penalty+info(2);%^2; % penalty power raised in DR1.m and resol already. GP July'08
0134     cost_flag = 0;
0135     return
0136 end
0137 bayestopt_.mf = bayestopt_.mf1;
0138 if ~options_.noconstant
0139     if options_.loglinear == 1
0140         constant = log(SteadyState(bayestopt_.mfys));
0141     else
0142         constant = SteadyState(bayestopt_.mfys);
0143     end
0144 else
0145     constant = zeros(nobs,1);
0146 end
0147 if bayestopt_.with_trend == 1
0148     trend_coeff = zeros(nobs,1);
0149     t = options_.trend_coeffs;
0150     for i=1:length(t)
0151         if ~isempty(t{i})
0152             trend_coeff(i) = evalin('base',t{i});
0153         end
0154     end
0155     trend = repmat(constant,1,gend)+trend_coeff*[1:gend];
0156 else
0157     trend = repmat(constant,1,gend);
0158 end
0159 start = options_.presample+1;
0160 np    = size(T,1);
0161 mf    = bayestopt_.mf;
0162 no_missing_data_flag = (number_of_observations==gend*nobs);
0163 %------------------------------------------------------------------------------
0164 % 3. Initial condition of the Kalman filter
0165 %------------------------------------------------------------------------------
0166 kalman_algo = options_.kalman_algo;
0167 if options_.lik_init == 1               % Kalman filter
0168     if kalman_algo ~= 2
0169         kalman_algo = 1;
0170     end
0171     Pstar = lyapunov_symm(T,R*Q*R',options_.qz_criterium,options_.lyapunov_complex_threshold);
0172     Pinf        = [];
0173 elseif options_.lik_init == 2   % Old Diffuse Kalman filter
0174     if kalman_algo ~= 2
0175         kalman_algo = 1;
0176     end
0177     Pstar = 10*eye(np);
0178     Pinf = [];
0179 elseif options_.lik_init == 3   % Diffuse Kalman filter
0180     if kalman_algo ~= 4
0181         kalman_algo = 3;
0182     end
0183     [QT,ST] = schur(T);
0184     e1 = abs(ordeig(ST)) > 2-options_.qz_criterium;
0185     [QT,ST] = ordschur(QT,ST,e1);
0186     k = find(abs(ordeig(ST)) > 2-options_.qz_criterium);
0187     nk = length(k);
0188     nk1 = nk+1;
0189     Pinf = zeros(np,np);
0190     Pinf(1:nk,1:nk) = eye(nk);
0191     Pstar = zeros(np,np);
0192     B = QT'*R*Q*R'*QT;
0193     for i=np:-1:nk+2
0194         if ST(i,i-1) == 0
0195             if i == np
0196                 c = zeros(np-nk,1);
0197             else
0198                 c = ST(nk1:i,:)*(Pstar(:,i+1:end)*ST(i,i+1:end)')+...
0199                     ST(i,i)*ST(nk1:i,i+1:end)*Pstar(i+1:end,i);
0200             end
0201             q = eye(i-nk)-ST(nk1:i,nk1:i)*ST(i,i);
0202             Pstar(nk1:i,i) = q\(B(nk1:i,i)+c);
0203             Pstar(i,nk1:i-1) = Pstar(nk1:i-1,i)';
0204         else
0205             if i == np
0206                 c = zeros(np-nk,1);
0207                 c1 = zeros(np-nk,1);
0208             else
0209                 c = ST(nk1:i,:)*(Pstar(:,i+1:end)*ST(i,i+1:end)')+...
0210                     ST(i,i)*ST(nk1:i,i+1:end)*Pstar(i+1:end,i)+...
0211                     ST(i,i-1)*ST(nk1:i,i+1:end)*Pstar(i+1:end,i-1);
0212                 c1 = ST(nk1:i,:)*(Pstar(:,i+1:end)*ST(i-1,i+1:end)')+...
0213                      ST(i-1,i-1)*ST(nk1:i,i+1:end)*Pstar(i+1:end,i-1)+...
0214                      ST(i-1,i)*ST(nk1:i,i+1:end)*Pstar(i+1:end,i);
0215             end
0216             q = [eye(i-nk)-ST(nk1:i,nk1:i)*ST(i,i) -ST(nk1:i,nk1:i)*ST(i,i-1);...
0217                  -ST(nk1:i,nk1:i)*ST(i-1,i) eye(i-nk)-ST(nk1:i,nk1:i)*ST(i-1,i-1)];
0218             z =  q\[B(nk1:i,i)+c;B(nk1:i,i-1)+c1];
0219             Pstar(nk1:i,i) = z(1:(i-nk));
0220             Pstar(nk1:i,i-1) = z(i-nk+1:end);
0221             Pstar(i,nk1:i-1) = Pstar(nk1:i-1,i)';
0222             Pstar(i-1,nk1:i-2) = Pstar(nk1:i-2,i-1)';
0223             i = i - 1;
0224         end
0225     end
0226     if i == nk+2
0227         c = ST(nk+1,:)*(Pstar(:,nk+2:end)*ST(nk1,nk+2:end)')+ST(nk1,nk1)*ST(nk1,nk+2:end)*Pstar(nk+2:end,nk1);
0228         Pstar(nk1,nk1)=(B(nk1,nk1)+c)/(1-ST(nk1,nk1)*ST(nk1,nk1));
0229     end
0230     Z = QT(mf,:);
0231     R1 = QT'*R;
0232     [QQ,RR,EE] = qr(Z*ST(:,1:nk),0);
0233     k = find(abs(diag(RR)) < 1e-8);
0234     if length(k) > 0
0235         k1 = EE(:,k);
0236         dd =ones(nk,1);
0237         dd(k1) = zeros(length(k1),1);
0238         Pinf(1:nk,1:nk) = diag(dd);
0239     end
0240 end
0241 if kalman_algo == 2
0242     no_correlation_flag = 1;
0243     if length(H)==1
0244         H = zeros(nobs,1);
0245     else
0246         if all(all(abs(H-diag(diag(H)))<1e-14))% ie, the covariance matrix is diagonal...
0247             H = diag(H);
0248         else
0249             no_correlation_flag = 0;
0250         end
0251     end
0252 end
0253 kalman_tol = options_.kalman_tol;
0254 riccati_tol = options_.riccati_tol;
0255 mf = bayestopt_.mf1;
0256 Y   = data-trend;
0257 %------------------------------------------------------------------------------
0258 % 4. Likelihood evaluation
0259 %------------------------------------------------------------------------------
0260 if (kalman_algo==1)% Multivariate Kalman Filter
0261     if no_missing_data_flag
0262         LIK = kalman_filter(T,R,Q,H,Pstar,Y,start,mf,kalman_tol,riccati_tol); 
0263     else
0264         LIK = ...
0265             missing_observations_kalman_filter(T,R,Q,H,Pstar,Y,start,mf,kalman_tol,riccati_tol, ...
0266                                                data_index,number_of_observations,no_more_missing_observations);
0267     end
0268     if isinf(LIK)
0269         kalman_algo = 2;
0270     end
0271 end
0272 if (kalman_algo==2)% Univariate Kalman Filter
0273     if no_correlation_flag
0274         LIK = univariate_kalman_filter(T,R,Q,H,Pstar,Y,start,mf,kalman_tol,riccati_tol,data_index,number_of_observations,no_more_missing_observations);
0275     else
0276         LIK = univariate_kalman_filter_corr(T,R,Q,H,Pstar,Y,start,mf,kalman_tol,riccati_tol,data_index,number_of_observations,no_more_missing_observations);
0277     end
0278 end
0279 if (kalman_algo==3)% Multivariate Diffuse Kalman Filter
0280     if no_missing_data_flag
0281         LIK = diffuse_kalman_filter(ST,R1,Q,H,Pinf,Pstar,Y,start,Z,kalman_tol,riccati_tol);
0282     else
0283         LIK = missing_observations_diffuse_kalman_filter(ST,R1,Q,H,Pinf,Pstar,Y,start,Z,kalman_tol,riccati_tol,...
0284                                                          data_index,number_of_observations,no_more_missing_observations);
0285     end
0286     if isinf(LIK)
0287         kalman_algo = 4;
0288     end
0289 end
0290 if (kalman_algo==4)% Univariate Diffuse Kalman Filter
0291     if no_correlation_flag
0292         LIK = univariate_diffuse_kalman_filter(ST,R1,Q,H,Pinf,Pstar,Y,start,Z,kalman_tol,riccati_tol,...
0293                                                data_index,number_of_observations,no_more_missing_observations);
0294     else
0295         LIK = univariate_diffuse_kalman_filter_corr(ST,R1,Q,H,Pinf,Pstar,Y,start,Z,kalman_tol,riccati_tol,...
0296                                                     data_index,number_of_observations,no_more_missing_observations);
0297     end
0298 end
0299 if imag(LIK) ~= 0
0300     likelihood = bayestopt_.penalty;
0301 else
0302     likelihood = LIK;
0303 end
0304 % ------------------------------------------------------------------------------
0305 % Adds prior if necessary
0306 % ------------------------------------------------------------------------------
0307 lnprior = priordens(xparam1,bayestopt_.pshape,bayestopt_.p6,bayestopt_.p7,bayestopt_.p3,bayestopt_.p4);
0308 fval    = (likelihood-lnprior);
0309 options_.kalman_algo = kalman_algo;

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