0001 function [ide_hess, ide_moments, ide_model, ide_lre, derivatives_info, info] = identification_analysis(params,indx,indexo,options_ident,data_info, prior_exist, name_tex, init)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045 global oo_ M_ options_ bayestopt_ estim_params_
0046 persistent indH indJJ indLRE
0047
0048 nparam=length(params);
0049 np=length(indx);
0050 offset=nparam-np;
0051 set_all_parameters(params);
0052
0053 nlags = options_ident.ar;
0054 useautocorr = options_ident.useautocorr;
0055 advanced = options_ident.advanced;
0056 replic = options_ident.replic;
0057 periods = options_ident.periods;
0058 max_dim_cova_group = options_ident.max_dim_cova_group;
0059 normalize_jacobians = options_ident.normalize_jacobians;
0060 [I,J]=find(M_.lead_lag_incidence');
0061
0062 ide_hess = struct();
0063 ide_moments = struct();
0064 ide_model = struct();
0065 ide_lre = struct();
0066 derivatives_info = struct();
0067
0068 [A,B,ys,info,M_,options_,oo_] = dynare_resolve(M_,options_,oo_);
0069 if info(1)==0,
0070 oo0=oo_;
0071 tau=[oo_.dr.ys(oo_.dr.order_var); vec(A); dyn_vech(B*M_.Sigma_e*B')];
0072 yy0=oo_.dr.ys(I);
0073 [residual, g1 ] = feval([M_.fname,'_dynamic'],yy0, ...
0074 oo_.exo_steady_state', M_.params, ...
0075 oo_.dr.ys, 1);
0076 vg1 = [oo_.dr.ys(oo_.dr.order_var); vec(g1)];
0077
0078 [JJ, H, gam, gp, dA, dOm, dYss] = getJJ(A, B, M_,oo0,options_,0,indx,indexo,bayestopt_.mf2,nlags,useautocorr);
0079 derivatives_info.DT=dA;
0080 derivatives_info.DOm=dOm;
0081 derivatives_info.DYss=dYss;
0082 if init,
0083 indJJ = (find(max(abs(JJ'))>1.e-8));
0084 while length(indJJ)<nparam && nlags<10,
0085 disp('The number of moments with non-zero derivative is smaller than the number of parameters')
0086 disp(['Try increasing ar = ', int2str(nlags+1)])
0087 nlags=nlags+1;
0088 [JJ, H, gam, gp, dA, dOm, dYss] = getJJ(A, B, M_,oo0,options_,0,indx,indexo,bayestopt_.mf2,nlags,useautocorr);
0089 derivatives_info.DT=dA;
0090 derivatives_info.DOm=dOm;
0091 derivatives_info.DYss=dYss;
0092 evalin('caller',['options_ident.ar=',int2str(nlags),';']);
0093 end
0094 if length(indJJ)<nparam,
0095 disp('The number of moments with non-zero derivative is smaller than the number of parameters')
0096 disp('up to 10 lags: check your model')
0097 disp('Either further increase ar or reduce the list of estimated parameters')
0098 error('IDETooManyParams',''),
0099 end
0100 indH = (find(max(abs(H'))>1.e-8));
0101 indLRE = (find(max(abs(gp'))>1.e-8));
0102 end
0103 TAU(:,1)=tau(indH);
0104 LRE(:,1)=vg1(indLRE);
0105 GAM(:,1)=gam(indJJ);
0106 siJ = (JJ(indJJ,:));
0107 siH = (H(indH,:));
0108 siLRE = (gp(indLRE,:));
0109 ide_strength_J=NaN(1,nparam);
0110 ide_strength_J_prior=NaN(1,nparam);
0111 if init,
0112 normaliz = abs(params);
0113 if prior_exist,
0114 if ~isempty(estim_params_.var_exo),
0115 normaliz1 = estim_params_.var_exo(:,7);
0116 else
0117 normaliz1=[];
0118 end
0119 if ~isempty(estim_params_.param_vals),
0120 normaliz1 = [normaliz1; estim_params_.param_vals(:,7)]';
0121 end
0122
0123 normaliz1(isinf(normaliz1)) = 1;
0124
0125 else
0126 normaliz1 = NaN(1,nparam);
0127 end
0128 try,
0129 options_.irf = 0;
0130 options_.noprint = 1;
0131 options_.order = 1;
0132 options_.periods = data_info.info.ntobs+100;
0133 options_.kalman_algo = 1;
0134 options_.analytic_derivation = -2;
0135 info = stoch_simul(options_.varobs);
0136 data_info.data=oo_.endo_simul(options_.varobs_id,100+1:end);
0137
0138 derivatives_info.no_DLIK=1;
0139 [fval,DLIK,AHess,cost_flag,ys,trend_coeff,info,M_,options_,bayestopt_,oo_] = dsge_likelihood(params',data_info,options_,M_,estim_params_,bayestopt_,oo_,derivatives_info);
0140
0141 AHess=-AHess;
0142 if min(eig(AHess))<0,
0143 error('Analytic Hessian is not positive semi-definite!')
0144 end
0145
0146 ide_hess.AHess= AHess;
0147 deltaM = sqrt(diag(AHess));
0148 iflag=any((deltaM.*deltaM)==0);
0149 tildaM = AHess./((deltaM)*(deltaM'));
0150 if iflag || rank(AHess)>rank(tildaM),
0151 [ide_hess.cond, ide_hess.ind0, ide_hess.indno, ide_hess.ino, ide_hess.Mco, ide_hess.Pco] = identification_checks(AHess, 1);
0152 else
0153 [ide_hess.cond, ide_hess.ind0, ide_hess.indno, ide_hess.ino, ide_hess.Mco, ide_hess.Pco] = identification_checks(tildaM, 1);
0154 end
0155 indok = find(max(ide_hess.indno,[],1)==0);
0156 cparam(indok,indok) = inv(AHess(indok,indok));
0157 normaliz(indok) = sqrt(diag(cparam(indok,indok)))';
0158 cmm = NaN(size(siJ,1),size(siJ,1));
0159 ind1=find(ide_hess.ind0);
0160 cmm = siJ(:,ind1)*((AHess(ind1,ind1))\siJ(:,ind1)');
0161 temp1=((AHess(ind1,ind1))\siH(:,ind1)');
0162 diag_chh=sum(siH(:,ind1)'.*temp1)';
0163
0164 ind1=ind1(ind1>offset);
0165 clre = siLRE(:,ind1-offset)*((AHess(ind1,ind1))\siLRE(:,ind1-offset)');
0166 rhoM=sqrt(1./diag(inv(tildaM(indok,indok))));
0167
0168 flag_score=1;
0169 catch,
0170 replic = max([replic, length(indJJ)*3]);
0171 cmm = simulated_moment_uncertainty(indJJ, periods, replic);
0172
0173
0174 sd=sqrt(diag(cmm));
0175 cc=cmm./(sd*sd');
0176 ix=[];
0177 for jc=1:length(cmm),
0178 jcheck=find(abs(cc(:,jc))>(1-1.e-6));
0179 ix=[ix; jcheck(jcheck>jc)];
0180 end
0181 iy=find(~ismember([1:length(cmm)],ix));
0182 indJJ=indJJ(iy);
0183 GAM=GAM(iy);
0184 cmm=cmm(iy,iy);
0185 siJ = (JJ(indJJ,:));
0186 MIM=siJ'*(cmm\siJ);
0187 ide_hess.AHess= MIM;
0188 deltaM = sqrt(diag(MIM));
0189 iflag=any((deltaM.*deltaM)==0);
0190 tildaM = MIM./((deltaM)*(deltaM'));
0191 if iflag || rank(MIM)>rank(tildaM),
0192 [ide_hess.cond, ide_hess.ind0, ide_hess.indno, ide_hess.ino, ide_hess.Mco, ide_hess.Pco] = identification_checks(MIM, 1);
0193 else
0194 [ide_hess.cond, ide_hess.ind0, ide_hess.indno, ide_hess.ino, ide_hess.Mco, ide_hess.Pco] = identification_checks(tildaM, 1);
0195 end
0196 indok = find(max(ide_hess.indno,[],1)==0);
0197
0198
0199 ind1=find(ide_hess.ind0);
0200 temp1=((MIM(ind1,ind1))\siH(:,ind1)');
0201 diag_chh=sum(siH(:,ind1)'.*temp1)';
0202
0203 ind1=ind1(ind1>offset);
0204 clre = siLRE(:,ind1-offset)*((MIM(ind1,ind1))\siLRE(:,ind1-offset)');
0205 if ~isempty(indok),
0206 rhoM(indok)=sqrt(1./diag(inv(tildaM(indok,indok))));
0207 normaliz(indok) = (sqrt(diag(inv(tildaM(indok,indok))))./deltaM(indok))';
0208 end
0209
0210 flag_score=0;
0211 end
0212 ide_strength_J(indok) = (1./(normaliz(indok)'./abs(params(indok)')));
0213 ide_strength_J_prior(indok) = (1./(normaliz(indok)'./normaliz1(indok)'));
0214 ide_strength_J(params==0)=ide_strength_J_prior(params==0);
0215 deltaM_prior = deltaM.*abs(normaliz1');
0216 deltaM = deltaM.*abs(params');
0217 deltaM(params==0)=deltaM_prior(params==0);
0218 quant = siJ./repmat(sqrt(diag(cmm)),1,nparam);
0219 siJnorm = vnorm(quant).*normaliz1;
0220
0221 quant=[];
0222
0223
0224
0225
0226
0227 quant = siH./repmat(sqrt(diag_chh),1,nparam);
0228 siHnorm = vnorm(quant).*normaliz1;
0229
0230 quant=[];
0231
0232
0233
0234
0235 quant = siLRE./repmat(sqrt(diag(clre)),1,np);
0236 siLREnorm = vnorm(quant).*normaliz1(offset+1:end);
0237
0238 ide_hess.ide_strength_J=ide_strength_J;
0239 ide_hess.ide_strength_J_prior=ide_strength_J_prior;
0240 ide_hess.deltaM=deltaM;
0241 ide_hess.deltaM_prior=deltaM_prior;
0242 ide_moments.siJnorm=siJnorm;
0243 ide_model.siHnorm=siHnorm;
0244 ide_lre.siLREnorm=siLREnorm;
0245 ide_hess.flag_score=flag_score;
0246 end,
0247 if normalize_jacobians,
0248 normH = max(abs(siH)')';
0249 normH = normH(:,ones(nparam,1));
0250 normJ = max(abs(siJ)')';
0251 normJ = normJ(:,ones(nparam,1));
0252 normLRE = max(abs(siLRE)')';
0253 normLRE = normLRE(:,ones(size(gp,2),1));
0254 else
0255 normH = 1;
0256 normJ = 1;
0257 normLRE = 1;
0258 end
0259 ide_moments.indJJ=indJJ;
0260 ide_model.indH=indH;
0261 ide_lre.indLRE=indLRE;
0262 ide_moments.siJ=siJ;
0263 ide_model.siH=siH;
0264 ide_lre.siLRE=siLRE;
0265 ide_moments.GAM=GAM;
0266 ide_model.TAU=TAU;
0267 ide_lre.LRE=LRE;
0268
0269
0270
0271
0272
0273
0274
0275
0276 [ide_moments.cond, ide_moments.ind0, ide_moments.indno, ide_moments.ino, ide_moments.Mco, ide_moments.Pco, ide_moments.jweak, ide_moments.jweak_pair] = ...
0277 identification_checks(JJ(indJJ,:)./normJ, 0);
0278 [ide_model.cond, ide_model.ind0, ide_model.indno, ide_model.ino, ide_model.Mco, ide_model.Pco, ide_model.jweak, ide_model.jweak_pair] = ...
0279 identification_checks(H(indH,:)./normH, 0);
0280 [ide_lre.cond, ide_lre.ind0, ide_lre.indno, ide_lre.ino, ide_lre.Mco, ide_lre.Pco, ide_lre.jweak, ide_lre.jweak_pair] = ...
0281 identification_checks(gp(indLRE,:)./normLRE, 0);
0282 normJ=1;
0283 [U, S, V]=svd(JJ(indJJ,:)./normJ,0);
0284 S=diag(S);
0285 if nparam>8
0286 ide_moments.S = S([1:4, end-3:end]);
0287 ide_moments.V = V(:,[1:4, end-3:end]);
0288 else
0289 ide_moments.S = S;
0290 ide_moments.V = V;
0291 end
0292
0293 indok = find(max(ide_moments.indno,[],1)==0);
0294 if advanced,
0295 [ide_moments.pars, ide_moments.cosnJ] = ident_bruteforce(JJ(indJJ,:)./normJ,max_dim_cova_group,options_.TeX,name_tex);
0296 end
0297 end