Home > . > DsgeLikelihood.m

DsgeLikelihood

PURPOSE ^

stephane.adjemian@cepremap.cnrs.fr [09-07-2004]

SYNOPSIS ^

function [fval,cost_flag,ys,trend_coeff,info] = DsgeLikelihood(xparam1,gend,data)

DESCRIPTION ^

 stephane.adjemian@cepremap.cnrs.fr [09-07-2004]

 Adapted from mj_optmumlik.m

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [fval,cost_flag,ys,trend_coeff,info] = DsgeLikelihood(xparam1,gend,data)
0002 % stephane.adjemian@cepremap.cnrs.fr [09-07-2004]
0003 %
0004 % Adapted from mj_optmumlik.m
0005   global bayestopt_ estim_params_ options_ trend_coeff_ M_ oo_ xparam1_test
0006 
0007   fval        = [];
0008   ys        = [];
0009   trend_coeff    = [];
0010   xparam1_test  = xparam1;
0011   cost_flag      = 1;
0012   nobs         = size(options_.varobs,1);
0013   %------------------------------------------------------------------------------
0014   % 1. Get the structural parameters & define penalties
0015   %------------------------------------------------------------------------------
0016   if options_.mode_compute ~= 1 & any(xparam1 < bayestopt_.lb)
0017     k = find(xparam1 < bayestopt_.lb);
0018     fval = bayestopt_.penalty+sum((bayestopt_.lb(k)-xparam1(k)).^2);
0019     cost_flag = 0;
0020     return;
0021   end
0022   if options_.mode_compute ~= 1 & any(xparam1 > bayestopt_.ub)
0023     k = find(xparam1 > bayestopt_.ub);
0024     fval = bayestopt_.penalty+sum((xparam1(k)-bayestopt_.ub(k)).^2);
0025     cost_flag = 0;
0026     return;
0027   end
0028   Q = M_.Sigma_e;
0029   for i=1:estim_params_.nvx
0030     k =estim_params_.var_exo(i,1);
0031     Q(k,k) = xparam1(i)*xparam1(i);
0032   end
0033   offset = estim_params_.nvx;
0034   if estim_params_.nvn
0035     H = zeros(nobs,nobs);
0036     for i=1:estim_params_.nvn
0037       k = estim_params_.var_endo(i,1);
0038       H(k,k) = xparam1(i+offset)*xparam1(i+offset);
0039     end
0040     offset = offset+estim_params_.nvn;
0041   end    
0042   if estim_params_.ncx
0043     for i=1:estim_params_.ncx
0044       k1 =estim_params_.corrx(i,1);
0045       k2 =estim_params_.corrx(i,2);
0046       Q(k1,k2) = xparam1(i+offset)*sqrt(Q(k1,k1)*Q(k2,k2));
0047       Q(k2,k1) = Q(k1,k2);
0048     end
0049     [CholQ,testQ] = chol(Q);
0050     if testQ     %% The variance-covariance matrix of the structural innovations is not definite positive.
0051         %% We have to compute the eigenvalues of this matrix in order to build the penalty.
0052         a = diag(eig(Q));
0053         k = find(a < 0);
0054         if k > 0
0055           fval = bayestopt_.penalty+sum(-a(k));
0056           cost_flag = 0;
0057           return
0058         end
0059     end
0060     offset = offset+estim_params_.ncx;
0061   end
0062   if estim_params_.ncn 
0063     for i=1:estim_params_.ncn
0064       k1 = options_.lgyidx2varobs(estim_params_.corrn(i,1));
0065       k2 = options_.lgyidx2varobs(estim_params_.corrn(i,2));
0066       H(k1,k2) = xparam1(i+offset)*sqrt(H(k1,k1)*H(k2,k2));
0067       H(k2,k1) = H(k1,k2);
0068     end
0069     [CholH,testH] = chol(H);
0070     if testH
0071       a = diag(eig(H));
0072       k = find(a < 0);
0073       if k > 0
0074     fval = bayestopt_.penalty+sum(-a(k));
0075     cost_flag = 0;
0076     return
0077       end
0078     end
0079     offset = offset+estim_params_.ncn;
0080   end
0081   M_.params(estim_params_.param_vals(:,1)) = xparam1(offset+1:end);  
0082   % for i=1:estim_params_.np
0083   %  M_.params(estim_params_.param_vals(i,1)) = xparam1(i+offset);
0084   %end
0085   M_.Sigma_e = Q;
0086   %------------------------------------------------------------------------------
0087   % 2. call model setup & reduction program
0088   %------------------------------------------------------------------------------
0089   [T,R,SteadyState,info] = dynare_resolve;
0090   rs = bayestopt_.restrict_state;
0091   if info(1) == 1 | info(1) == 2 | info(1) == 5
0092     fval = bayestopt_.penalty+1;
0093     cost_flag = 0;
0094     return
0095   elseif info(1) == 3 | info(1) == 4 | info(1) == 20
0096     fval = bayestopt_.penalty+info(2)^2;
0097     cost_flag = 0;
0098     return
0099   end
0100   T = T(rs,rs);
0101   R = R(rs,:);
0102   bayestopt_.mf = bayestopt_.mf1;
0103   if options_.loglinear == 1
0104     constant = log(SteadyState(bayestopt_.mfys));
0105   else
0106     constant = SteadyState(bayestopt_.mfys);
0107   end
0108   if bayestopt_.with_trend == 1
0109     trend_coeff = zeros(nobs,1);
0110     for i=1:nobs
0111       trend_coeff(i) = evalin('base',bayestopt_.trend_coeff{i});
0112     end
0113     trend = repmat(constant,1,gend)+trend_coeff*[1:gend];
0114   else
0115     trend = repmat(constant,1,gend);
0116   end
0117   start = options_.presample+1;
0118   np    = size(T,1);
0119   mf    = bayestopt_.mf;
0120   %------------------------------------------------------------------------------
0121   % 3. Initial condition of the Kalman filter
0122   %------------------------------------------------------------------------------
0123   if options_.lik_init == 1        % Kalman filter
0124     Pstar = lyapunov_symm(T,R*Q*transpose(R));
0125     Pinf    = [];
0126   elseif options_.lik_init == 2    % Old Diffuse Kalman filter
0127     Pstar = 10*eye(np);
0128     Pinf    = [];
0129   elseif options_.lik_init == 3    % Diffuse Kalman filter
0130     Pstar = zeros(np,np);
0131     ivs = bayestopt_.i_T_var_stable;
0132     Pstar(ivs,ivs) = lyapunov_symm(T(ivs,ivs),R(ivs,:)*Q* ...
0133                    transpose(R(ivs,:)));
0134     Pinf  = bayestopt_.Pinf;
0135     % by M. Ratto
0136     RR=T(:,find(~ismember([1:np],ivs)));
0137     i=find(abs(RR)>1.e-10);
0138     R0=zeros(size(RR));
0139     R0(i)=sign(RR(i));
0140     Pinf=R0*R0';
0141     % by M. Ratto
0142   end
0143   %------------------------------------------------------------------------------
0144   % 4. Likelihood evaluation
0145   %------------------------------------------------------------------------------
0146   if estim_params_.nvn
0147     if options_.kalman_algo == 1
0148       LIK = DiffuseLikelihoodH1(T,R,Q,H,Pinf,Pstar,data,trend,start);
0149       if isinf(LIK) & ~estim_params_.ncn %% The univariate approach considered here doesn't
0150                      %%    apply when H has some off-diagonal elements.
0151         LIK = DiffuseLikelihoodH3(T,R,Q,H,Pinf,Pstar,data,trend,start);
0152       elseif isinf(LIK) & estim_params_.ncn
0153     LIK = DiffuseLikelihoodH3corr(T,R,Q,H,Pinf,Pstar,data,trend,start);
0154       end
0155     elseif options_.kalman_algo == 3
0156       if ~estim_params_.ncn %% The univariate approach considered here doesn't
0157                 %%    apply when H has some off-diagonal elements.
0158         LIK = DiffuseLikelihoodH3(T,R,Q,H,Pinf,Pstar,data,trend,start);
0159       else
0160     LIK = DiffuseLikelihoodH3corr(T,R,Q,H,Pinf,Pstar,data,trend,start);
0161       end    
0162     end      
0163   else
0164     if options_.kalman_algo == 1
0165        nv = size(bayestopt_.Z,1);
0166        LIK = kalman_filter(bayestopt_.Z,zeros(nv,nv),T,R,Q,data,zeros(size(T,1),1),Pstar,'u');
0167 %      LIK = DiffuseLikelihood1(T,R,Q,Pinf,Pstar,data,trend,start);
0168       if isinf(LIK)
0169     LIK = DiffuseLikelihood3(T,R,Q,Pinf,Pstar,data,trend,start);
0170       end
0171     elseif options_.kalman_algo == 3
0172       LIK = DiffuseLikelihood3(T,R,Q,Pinf,Pstar,data,trend,start);
0173     end     
0174   end
0175   if imag(LIK) ~= 0
0176     likelihood = bayestopt_.penalty;
0177   else
0178     likelihood = LIK;
0179   end
0180   % ------------------------------------------------------------------------------
0181   % Adds prior if necessary
0182   % ------------------------------------------------------------------------------
0183   lnprior = priordens(xparam1,bayestopt_.pshape,bayestopt_.p1,bayestopt_.p2,bayestopt_.p3,bayestopt_.p4);
0184   fval    = (likelihood-lnprior);

Generated on Fri 16-Jun-2006 09:09:06 by m2html © 2003