0001 function x0 = stab_map_(Nsam, fload, ksstat, alpha2, prepSA, pprior, ilptau, OutputDirectoryName)
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
0046
0047
0048
0049
0050
0051
0052 global bayestopt_ estim_params_ options_ oo_ M_
0053
0054 dr_ = oo_.dr;
0055 if isfield(dr_,'ghx'),
0056 ys_ = oo_.dr.ys;
0057 nspred = size(dr_.ghx,2);
0058 nboth = dr_.nboth;
0059 nfwrd = dr_.nfwrd;
0060 end
0061 fname_ = M_.fname;
0062
0063 nshock = estim_params_.nvx;
0064 nshock = nshock + estim_params_.nvn;
0065 nshock = nshock + estim_params_.ncx;
0066 nshock = nshock + estim_params_.ncn;
0067
0068
0069 if nargin==0,
0070 Nsam=2000;
0071 end
0072 if nargin<2,
0073 fload=0;
0074 end
0075 if nargin<3,
0076 ksstat=0.1;
0077 end
0078 if nargin<4,
0079 alpha2=0.3;
0080 end
0081 if nargin<5,
0082 prepSA=0;
0083 end
0084 if nargin<6,
0085 pprior=1;
0086 end
0087 if nargin<7,
0088 ilptau=1;
0089 end
0090 if nargin<8,
0091 OutputDirectoryName='';
0092 end
0093
0094 options_.periods=0;
0095 options_.nomoments=1;
0096 options_.irf=0;
0097 options_.noprint=1;
0098
0099 if fload==0 | nargin<2 | isempty(fload),
0100 if prepSA
0101 T=zeros(size(dr_.ghx,1),size(dr_.ghx,2)+size(dr_.ghu,2),Nsam/2);
0102 end
0103
0104
0105 if estim_params_.np<52 & ilptau,
0106 [lpmat] = lptauSEQ(Nsam,estim_params_.np);
0107 if estim_params_.np>30
0108 for j=1:estim_params_.np,
0109 lpmat(:,j)=lpmat(randperm(Nsam),j);
0110 end
0111 end
0112 else
0113
0114 for j=1:estim_params_.np,
0115 lpmat(:,j) = randperm(Nsam)'./(Nsam+1);
0116 end
0117 end
0118
0119 if pprior,
0120 for j=1:nshock,
0121 lpmat0(:,j) = randperm(Nsam)'./(Nsam+1);
0122 lpmat0(:,j)=lpmat0(:,j).*(bayestopt_.ub(j)-bayestopt_.lb(j))+bayestopt_.lb(j);
0123 end
0124 for j=1:estim_params_.np,
0125 lpmat(:,j)=lpmat(:,j).*(bayestopt_.ub(j+nshock)-bayestopt_.lb(j+nshock))+bayestopt_.lb(j+nshock);
0126 end
0127 else
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147
0148
0149
0150
0151
0152
0153
0154
0155
0156
0157
0158 eval(['load ' options_.mode_file ';']');
0159 d = chol(inv(hh));
0160 lp=randn(Nsam*2,nshock+estim_params_.np)*d+kron(ones(Nsam*2,1),xparam1');
0161 for j=1:Nsam*2,
0162 lnprior(j) = any(lp(j,:)'<=bayestopt_.lb | lp(j,:)'>=bayestopt_.ub);
0163 end
0164 ireal=[1:2*Nsam];
0165 ireal=ireal(find(lnprior==0));
0166 lp=lp(ireal,:);
0167 Nsam=min(Nsam, length(ireal));
0168 lpmat0=lp(1:Nsam,1:nshock);
0169 lpmat=lp(1:Nsam,nshock+1:end);
0170 clear lp lnprior ireal;
0171 end
0172
0173 h = waitbar(0,'Please wait...');
0174 istable=[1:Nsam];
0175 jstab=0;
0176 iunstable=[1:Nsam];
0177 iindeterm=zeros(1,Nsam);
0178 iwrong=zeros(1,Nsam);
0179 for j=1:Nsam,
0180 M_.params(estim_params_.param_vals(:,1)) = lpmat(j,:)';
0181 stoch_simul([]);
0182 dr_ = oo_.dr;
0183 if isfield(dr_,'ghx'),
0184 egg(:,j) = sort(dr_.eigval);
0185 iunstable(j)=0;
0186 if prepSA
0187 jstab=jstab+1;
0188 T(:,:,jstab) = [dr_.ghx dr_.ghu];
0189 end
0190 if ~exist('nspred'),
0191 nspred = size(dr_.ghx,2);
0192 nboth = dr_.nboth;
0193 nfwrd = dr_.nfwrd;
0194 end
0195 else
0196 istable(j)=0;
0197 if isfield(dr_,'eigval')
0198 egg(:,j) = sort(dr_.eigval);
0199 if any(isnan(egg(1:nspred,j)))
0200 iwrong(j)=j;
0201 else
0202 if (nboth | nfwrd) & abs(egg(nspred+1,j))<=options_.qz_criterium,
0203 iindeterm(j)=j;
0204 end
0205 end
0206 else
0207 egg(:,j)=ones(size(egg,1),1).*1.1;
0208 iwrong(j)=j;
0209 end
0210 end
0211 ys_=real(dr_.ys);
0212 yys(:,j) = ys_;
0213 ys_=yys(:,1);
0214 waitbar(j/Nsam,h,['MC iteration ',int2str(j),'/',int2str(Nsam)])
0215 end
0216 close(h)
0217 if prepSA,
0218 T=T(:,:,1:jstab);
0219 end
0220 istable=istable(find(istable));
0221 iunstable=iunstable(find(iunstable));
0222 iindeterm=iindeterm(find(iindeterm));
0223 iwrong=iwrong(find(iwrong));
0224
0225
0226
0227
0228
0229
0230
0231
0232
0233
0234
0235
0236
0237
0238
0239
0240
0241
0242
0243
0244
0245
0246
0247
0248
0249
0250
0251
0252
0253
0254
0255
0256
0257
0258
0259 if pprior,
0260 if ~prepSA
0261 save([OutputDirectoryName '\' fname_ '_prior'],'lpmat','lpmat0','iunstable','istable','iindeterm','iwrong','egg','yys','nspred','nboth','nfwrd')
0262 else
0263 save([OutputDirectoryName '\' fname_ '_prior'],'lpmat','lpmat0','iunstable','istable','iindeterm','iwrong','egg','yys','T','nspred','nboth','nfwrd')
0264 end
0265
0266 else
0267 if ~prepSA
0268 save([OutputDirectoryName '\' fname_ '_mc'], ...
0269 'lpmat','lpmat0','iunstable','istable','iindeterm','iwrong','egg','yys','nspred','nboth','nfwrd')
0270 else
0271 save([OutputDirectoryName '\' fname_ '_mc'], ...
0272 'lpmat','lpmat0','iunstable','istable','iindeterm','iwrong','egg','yys','T','nspred','nboth','nfwrd')
0273 end
0274 end
0275 else
0276 if pprior,
0277 filetoload=[OutputDirectoryName '\' fname_ '_prior'];
0278 else
0279 filetoload=[OutputDirectoryName '\' fname_ '_mc'];
0280 end
0281 load(filetoload,'lpmat','lpmat0','iunstable','istable','iindeterm','iwrong','egg','yys','nspred','nboth','nfwrd')
0282 Nsam = size(lpmat,1);
0283
0284 if prepSA & isempty(strmatch('T',who('-file', filetoload),'exact')),
0285 h = waitbar(0,'Please wait...');
0286 options_.periods=0;
0287 options_.nomoments=1;
0288 options_.irf=0;
0289 options_.noprint=1;
0290 stoch_simul([]);
0291 T=zeros(size(dr_.ghx,1),size(dr_.ghx,2)+size(dr_.ghu,2),length(istable));
0292 ntrans=length(istable);
0293 for j=1:ntrans,
0294 M_.params(estim_params_.param_vals(:,1)) = lpmat(istable(j),:)';
0295 stoch_simul([]);
0296 dr_ = oo_.dr;
0297 T(:,:,j) = [dr_.ghx dr_.ghu];
0298 if ~exist('nspred')
0299 nspred = size(dr_.ghx,2);
0300 nboth = dr_.nboth;
0301 nfwrd = dr_.nfwrd;
0302 end
0303 ys_=real(dr_.ys);
0304 yys(:,j) = ys_;
0305 ys_=yys(:,1);
0306 waitbar(j/ntrans,h,['MC iteration ',int2str(j),'/',int2str(ntrans)])
0307 end
0308 close(h)
0309 save(filetoload,'T','-append')
0310 end
0311 end
0312
0313 if pprior
0314 aname='prior_stab';
0315 auname='prior_unacceptable';
0316 aunstname='prior_unstable';
0317 aindname='prior_indeterm';
0318 asname='prior_stable';
0319 else
0320 aname='mc_stab';
0321 auname='mc_unacceptable';
0322 aunstname='mc_unstable';
0323 aindname='mc_indeterm';
0324 asname='mc_stable';
0325 end
0326 delete([OutputDirectoryName,'\',fname_,'_',aname,'_*.*']);
0327
0328 delete([OutputDirectoryName,'\',fname_,'_',asname,'_corr_*.*']);
0329 delete([OutputDirectoryName,'\',fname_,'_',auname,'_corr_*.*']);
0330 delete([OutputDirectoryName,'\',fname_,'_',aunstname,'_corr_*.*']);
0331 delete([OutputDirectoryName,'\',fname_,'_',aindname,'_corr_*.*']);
0332
0333 if length(iunstable)>0 & length(iunstable)<Nsam,
0334 disp([num2str(length(istable)/Nsam*100),'\% of the prior support is stable.'])
0335 disp([num2str( (length(iunstable)-length(iwrong)-length(iindeterm) )/Nsam*100),'\% of the prior support is unstable.'])
0336 if ~isempty(iindeterm),
0337 disp([num2str(length(iindeterm)/Nsam*100),'\% of the prior support gives indeterminacy.'])
0338 end
0339 if ~isempty(iwrong),
0340 disp(' ');
0341 disp(['For ',num2str(length(iwrong)/Nsam*100),'\% of the prior support dynare could not find a solution.'])
0342 end
0343
0344 [proba, dproba] = stab_map_1(lpmat, istable, iunstable, aname,0);
0345 indstab=find(dproba>ksstat);
0346 disp('The following parameters mostly drive acceptable behaviour')
0347 disp(M_.param_names(estim_params_.param_vals(indstab,1),:))
0348 stab_map_1(lpmat, istable, iunstable, aname, 1, indstab, OutputDirectoryName);
0349 if ~isempty(iindeterm),
0350 ixun=iunstable(find(~ismember(iunstable,[iindeterm,iwrong])));
0351 [proba, dproba] = stab_map_1(lpmat, [1:Nsam], iindeterm, [aname, '_indet'],0);
0352 indindet=find(dproba>ksstat);
0353 disp('The following parameters mostly drive indeterminacy')
0354 disp(M_.param_names(estim_params_.param_vals(indindet,1),:))
0355 stab_map_1(lpmat, [1:Nsam], iindeterm, [aname, '_indet'], 1, indindet, OutputDirectoryName);
0356 if ~isempty(ixun),
0357 [proba, dproba] = stab_map_1(lpmat, [1:Nsam], ixun, [aname, '_unst'],0);
0358 indunst=find(dproba>ksstat);
0359 disp('The following parameters mostly drive instability')
0360 disp(M_.param_names(estim_params_.param_vals(indunst,1),:))
0361 stab_map_1(lpmat, [1:Nsam], ixun, [aname, '_unst'], 1, indunst, OutputDirectoryName);
0362 end
0363 end
0364
0365 disp(' ')
0366 disp('Starting bivariate analysis:')
0367
0368 c0=corrcoef(lpmat(istable,:));
0369 c00=tril(c0,-1);
0370
0371 stab_map_2(lpmat(istable,:),alpha2, asname, OutputDirectoryName);
0372 stab_map_2(lpmat(iunstable,:),alpha2, auname, OutputDirectoryName);
0373 if ~isempty(iindeterm),
0374 stab_map_2(lpmat(iindeterm,:),alpha2, aindname, OutputDirectoryName);
0375 if ~isempty(ixun),
0376 stab_map_2(lpmat(ixun,:),alpha2, aunstname, OutputDirectoryName);
0377 end
0378 end
0379
0380 x0=0.5.*(bayestopt_.ub(1:nshock)-bayestopt_.lb(1:nshock))+bayestopt_.lb(1:nshock);
0381 x0 = [x0; lpmat(istable(1),:)'];
0382 if istable(end)~=Nsam
0383 M_.params(estim_params_.param_vals(:,1)) = lpmat(istable(1),:)';
0384 stoch_simul([]);
0385 end
0386 else
0387 if length(iunstable)==0,
0388 disp('All parameter values in the specified ranges are stable!')
0389 x0=0.5.*(bayestopt_.ub(1:nshock)-bayestopt_.lb(1:nshock))+bayestopt_.lb(1:nshock);
0390 x0 = [x0; lpmat(istable(1),:)'];
0391 else
0392 disp('All parameter values in the specified ranges are not acceptable!')
0393 x0=[];
0394 end
0395
0396 end