0001 function x0 = stab_map_(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
0053
0054
0055
0056
0057
0058
0059 global bayestopt_ estim_params_ options_ oo_ M_
0060
0061 opt_gsa=options_.opt_gsa;
0062
0063 Nsam = opt_gsa.Nsam;
0064 fload = opt_gsa.load_stab;
0065 ksstat = opt_gsa.ksstat;
0066 alpha2 = opt_gsa.alpha2_stab;
0067 pvalue_ks = opt_gsa.pvalue_ks;
0068 pvalue_corr = opt_gsa.pvalue_corr;
0069 prepSA = (opt_gsa.redform | opt_gsa.identification);
0070 pprior = opt_gsa.pprior;
0071 neighborhood_width = opt_gsa.neighborhood_width;
0072 ilptau = opt_gsa.ilptau;
0073 nliv = opt_gsa.morris_nliv;
0074 ntra = opt_gsa.morris_ntra;
0075
0076 dr_ = oo_.dr;
0077
0078 ys_ = oo_.dr.ys;
0079 nspred = dr_.nspred;
0080 nboth = dr_.nboth;
0081 nfwrd = dr_.nfwrd;
0082
0083 fname_ = M_.fname;
0084
0085 np = estim_params_.np;
0086 nshock = estim_params_.nvx;
0087 nshock = nshock + estim_params_.nvn;
0088 nshock = nshock + estim_params_.ncx;
0089 nshock = nshock + estim_params_.ncn;
0090 lpmat0=[];
0091 xparam1=[];
0092
0093 pshape = bayestopt_.pshape(nshock+1:end);
0094 p1 = bayestopt_.p1(nshock+1:end);
0095 p2 = bayestopt_.p2(nshock+1:end);
0096 p3 = bayestopt_.p3(nshock+1:end);
0097 p4 = bayestopt_.p4(nshock+1:end);
0098
0099 if nargin==0,
0100 OutputDirectoryName='';
0101 end
0102
0103 opt=options_;
0104 options_.periods=0;
0105 options_.nomoments=1;
0106 options_.irf=0;
0107 options_.noprint=1;
0108 options_.simul=0;
0109 if fload==0,
0110
0111
0112
0113
0114 if isfield(dr_,'ghx'),
0115 egg=zeros(length(dr_.eigval),Nsam);
0116 end
0117 yys=zeros(length(dr_.ys),Nsam);
0118
0119 if opt_gsa.morris == 1
0120 [lpmat, OutFact] = Sampling_Function_2(nliv, np+nshock, ntra, ones(np+nshock, 1), zeros(np+nshock,1), []);
0121 lpmat = lpmat.*(nliv-1)/nliv+1/nliv/2;
0122 Nsam=size(lpmat,1);
0123 lpmat0 = lpmat(:,1:nshock);
0124 lpmat = lpmat(:,nshock+1:end);
0125 elseif opt_gsa.morris==3,
0126 lpmat = prep_ide(Nsam,np,5);
0127 Nsam=size(lpmat,1);
0128 else
0129 if np<52 & ilptau>0,
0130 [lpmat] = qmc_sequence(np, int64(0), 0, Nsam)';
0131 if np>30 | ilptau==2,
0132 for j=1:np,
0133 lpmat(:,j)=lpmat(randperm(Nsam),j);
0134 end
0135 end
0136 else
0137
0138 for j=1:np,
0139 lpmat(:,j) = randperm(Nsam)'./(Nsam+1);
0140 end
0141
0142 end
0143 end
0144
0145 dummy=prior_draw_gsa(1);
0146
0147
0148
0149
0150
0151
0152
0153
0154 if pprior,
0155 for j=1:nshock,
0156 if opt_gsa.morris~=1,
0157 lpmat0(:,j) = randperm(Nsam)'./(Nsam+1);
0158 end
0159 if opt_gsa.prior_range
0160 lpmat0(:,j)=lpmat0(:,j).*(bayestopt_.ub(j)-bayestopt_.lb(j))+bayestopt_.lb(j);
0161 end
0162 end
0163 if opt_gsa.prior_range
0164
0165
0166
0167
0168
0169
0170 for j=1:np,
0171 lpmat(:,j)=lpmat(:,j).*(bayestopt_.ub(j+nshock)-bayestopt_.lb(j+nshock))+bayestopt_.lb(j+nshock);
0172 end
0173 else
0174 xx=prior_draw_gsa(0,[lpmat0 lpmat]);
0175
0176
0177
0178
0179
0180
0181
0182
0183
0184
0185 lpmat0=xx(:,1:nshock);
0186 lpmat=xx(:,nshock+1:end);
0187 clear xx;
0188 end
0189 else
0190
0191
0192
0193
0194
0195
0196
0197
0198
0199
0200
0201
0202
0203
0204
0205
0206
0207
0208
0209
0210
0211
0212
0213
0214
0215
0216
0217
0218
0219
0220 eval(['load ' options_.mode_file '.mat;']);
0221 if neighborhood_width>0,
0222 for j=1:nshock,
0223 lpmat0(:,j) = randperm(Nsam)'./(Nsam+1);
0224 ub=min([bayestopt_.ub(j) xparam1(j)*(1+neighborhood_width)]);
0225 lb=max([bayestopt_.lb(j) xparam1(j)*(1-neighborhood_width)]);
0226 lpmat0(:,j)=lpmat0(:,j).*(ub-lb)+lb;
0227 end
0228 for j=1:np,
0229 ub=min([bayestopt_.ub(j+nshock) xparam1(j+nshock)*(1+neighborhood_width)]);
0230 lb=max([bayestopt_.lb(j+nshock) xparam1(j+nshock)*(1-neighborhood_width)]);
0231 lpmat(:,j)=lpmat(:,j).*(ub-lb)+lb;
0232 end
0233 else
0234 d = chol(inv(hh));
0235 lp=randn(Nsam*2,nshock+np)*d+kron(ones(Nsam*2,1),xparam1');
0236 for j=1:Nsam*2,
0237 lnprior(j) = any(lp(j,:)'<=bayestopt_.lb | lp(j,:)'>=bayestopt_.ub);
0238 end
0239 ireal=[1:2*Nsam];
0240 ireal=ireal(find(lnprior==0));
0241 lp=lp(ireal,:);
0242 Nsam=min(Nsam, length(ireal));
0243 lpmat0=lp(1:Nsam,1:nshock);
0244 lpmat=lp(1:Nsam,nshock+1:end);
0245 clear lp lnprior ireal;
0246 end
0247 end
0248
0249 h = dyn_waitbar(0,'Please wait...');
0250 istable=[1:Nsam];
0251 jstab=0;
0252 iunstable=[1:Nsam];
0253 iindeterm=zeros(1,Nsam);
0254 iwrong=zeros(1,Nsam);
0255 for j=1:Nsam,
0256 M_.params(estim_params_.param_vals(:,1)) = lpmat(j,:)';
0257
0258 try
0259 [Tt,Rr,SteadyState,infox{j},M_,options_,oo_] = dynare_resolve(M_,options_,oo_,'restrict');
0260 if infox{j}(1)==0 && ~exist('T'),
0261 dr_=oo_.dr;
0262 T=zeros(size(dr_.ghx,1),size(dr_.ghx,2)+size(dr_.ghu,2),Nsam);
0263 egg=zeros(length(dr_.eigval),Nsam);
0264 end
0265 if infox{j},
0266
0267 if isfield(oo_.dr,'ghx'),
0268 oo_.dr=rmfield(oo_.dr,'ghx');
0269 end
0270 end
0271 catch
0272 if isfield(oo_.dr,'eigval'),
0273 oo_.dr=rmfield(oo_.dr,'eigval');
0274 end
0275 if isfield(oo_.dr,'ghx'),
0276 oo_.dr=rmfield(oo_.dr,'ghx');
0277 end
0278 disp('No solution could be found'),
0279 end
0280 dr_ = oo_.dr;
0281 if isfield(dr_,'ghx'),
0282 egg(:,j) = sort(dr_.eigval);
0283 iunstable(j)=0;
0284 if prepSA
0285 jstab=jstab+1;
0286 T(:,:,jstab) = [dr_.ghx dr_.ghu];
0287
0288
0289
0290
0291 end
0292 if ~exist('nspred'),
0293 nspred = dr_.nspred;
0294 nboth = dr_.nboth;
0295 nfwrd = dr_.nfwrd;
0296 end
0297 else
0298 istable(j)=0;
0299 if isfield(dr_,'eigval')
0300 egg(:,j) = sort(dr_.eigval);
0301 if exist('nspred')
0302 if any(isnan(egg(1:nspred,j)))
0303 iwrong(j)=j;
0304 else
0305 if (nboth | nfwrd) & abs(egg(nspred+1,j))<=options_.qz_criterium,
0306 iindeterm(j)=j;
0307 end
0308 end
0309 end
0310 else
0311 if exist('egg'),
0312 egg(:,j)=ones(size(egg,1),1).*NaN;
0313 end
0314 iwrong(j)=j;
0315 end
0316 end
0317 ys_=real(dr_.ys);
0318 yys(:,j) = ys_;
0319 ys_=yys(:,1);
0320 dyn_waitbar(j/Nsam,h,['MC iteration ',int2str(j),'/',int2str(Nsam)])
0321 end
0322 dyn_waitbar_close(h);
0323 if prepSA,
0324 T=T(:,:,1:jstab);
0325 end
0326 istable=istable(find(istable));
0327 iunstable=iunstable(find(iunstable));
0328 iindeterm=iindeterm(find(iindeterm));
0329 iwrong=iwrong(find(iwrong));
0330
0331
0332
0333
0334
0335
0336
0337
0338
0339
0340
0341
0342
0343
0344
0345
0346
0347
0348
0349
0350
0351
0352
0353
0354
0355
0356
0357
0358
0359
0360
0361
0362
0363
0364
0365 bkpprior.pshape=bayestopt_.pshape;
0366 bkpprior.p1=bayestopt_.p1;
0367 bkpprior.p2=bayestopt_.p2;
0368 bkpprior.p3=bayestopt_.p3;
0369 bkpprior.p4=bayestopt_.p4;
0370 if pprior,
0371 if ~prepSA
0372 save([OutputDirectoryName '/' fname_ '_prior.mat'], ...
0373 'bkpprior','lpmat','lpmat0','iunstable','istable','iindeterm','iwrong', ...
0374 'egg','yys','nspred','nboth','nfwrd')
0375 else
0376 save([OutputDirectoryName '/' fname_ '_prior.mat'], ...
0377 'bkpprior','lpmat','lpmat0','iunstable','istable','iindeterm','iwrong', ...
0378 'egg','yys','T','nspred','nboth','nfwrd')
0379 end
0380
0381 else
0382 if ~prepSA
0383 save([OutputDirectoryName '/' fname_ '_mc.mat'], ...
0384 'lpmat','lpmat0','iunstable','istable','iindeterm','iwrong', ...
0385 'egg','yys','nspred','nboth','nfwrd')
0386 else
0387 save([OutputDirectoryName '/' fname_ '_mc.mat'], ...
0388 'lpmat','lpmat0','iunstable','istable','iindeterm','iwrong', ...
0389 'egg','yys','T','nspred','nboth','nfwrd')
0390 end
0391 end
0392 else
0393 if pprior,
0394 filetoload=[OutputDirectoryName '/' fname_ '_prior.mat'];
0395 else
0396 filetoload=[OutputDirectoryName '/' fname_ '_mc.mat'];
0397 end
0398 load(filetoload,'lpmat','lpmat0','iunstable','istable','iindeterm','iwrong','egg','yys','nspred','nboth','nfwrd')
0399 Nsam = size(lpmat,1);
0400 if pprior==0,
0401 eval(['load ' options_.mode_file '.mat;']);
0402 end
0403
0404
0405 if prepSA & isempty(strmatch('T',who('-file', filetoload),'exact')),
0406 h = dyn_waitbar(0,'Please wait...');
0407 options_.periods=0;
0408 options_.nomoments=1;
0409 options_.irf=0;
0410 options_.noprint=1;
0411 stoch_simul([]);
0412
0413 ntrans=length(istable);
0414 for j=1:ntrans,
0415 M_.params(estim_params_.param_vals(:,1)) = lpmat(istable(j),:)';
0416
0417 [Tt,Rr,SteadyState,info,M_,options_,oo_] = dynare_resolve(M_,options_,oo_,'restrict');
0418
0419
0420
0421
0422 if ~exist('T')
0423 T=zeros(size(dr_.ghx,1),size(dr_.ghx,2)+size(dr_.ghu,2),ntrans);
0424 end
0425 dr_ = oo_.dr;
0426 T(:,:,j) = [dr_.ghx dr_.ghu];
0427 if ~exist('nspred')
0428 nspred = dr_.nspred;
0429 nboth = dr_.nboth;
0430 nfwrd = dr_.nfwrd;
0431 end
0432 ys_=real(dr_.ys);
0433 yys(:,j) = ys_;
0434 ys_=yys(:,1);
0435 dyn_waitbar(j/ntrans,h,['MC iteration ',int2str(j),'/',int2str(ntrans)])
0436 end
0437 dyn_waitbar_close(h);
0438 save(filetoload,'T','-append')
0439 elseif prepSA
0440 load(filetoload,'T')
0441 end
0442 end
0443
0444 if pprior
0445 aname='prior_stab';
0446 auname='prior_unacceptable';
0447 aunstname='prior_unstable';
0448 aindname='prior_indeterm';
0449 awrongname='prior_wrong';
0450 asname='prior_stable';
0451 else
0452 aname='mc_stab';
0453 auname='mc_unacceptable';
0454 aunstname='mc_unstable';
0455 aindname='mc_indeterm';
0456 awrongname='mc_wrong';
0457 asname='mc_stable';
0458 end
0459 delete([OutputDirectoryName,'/',fname_,'_',aname,'_*.*']);
0460
0461 delete([OutputDirectoryName,'/',fname_,'_',asname,'_corr_*.*']);
0462 delete([OutputDirectoryName,'/',fname_,'_',auname,'_corr_*.*']);
0463 delete([OutputDirectoryName,'/',fname_,'_',aunstname,'_corr_*.*']);
0464 delete([OutputDirectoryName,'/',fname_,'_',aindname,'_corr_*.*']);
0465
0466 if length(iunstable)>0 & length(iunstable)<Nsam,
0467 fprintf(['%4.1f%% of the prior support is stable.\n'],length(istable)/Nsam*100)
0468 fprintf(['%4.1f%% of the prior support is unstable.\n'],(length(iunstable)-length(iwrong)-length(iindeterm) )/Nsam*100)
0469 if ~isempty(iindeterm),
0470 fprintf(['%4.1f%% of the prior support gives indeterminacy.'],length(iindeterm)/Nsam*100)
0471 end
0472 if ~isempty(iwrong),
0473 disp(' ');
0474 disp(['For ',num2str(length(iwrong)/Nsam*100,'%1.3f'),'\% of the prior support dynare could not find a solution.'])
0475 end
0476 disp(' ');
0477
0478 [proba, dproba] = stab_map_1(lpmat, istable, iunstable, aname,0);
0479
0480 indstab=find(proba<pvalue_ks);
0481 disp('Smirnov statistics in driving acceptable behaviour')
0482 for j=1:length(indstab),
0483 disp([M_.param_names(estim_params_.param_vals(indstab(j),1),:),' d-stat = ', num2str(dproba(indstab(j)),'%1.3f'),' p-value = ', num2str(proba(indstab(j)),'%1.3f')])
0484 end
0485 disp(' ');
0486 if ~isempty(indstab)
0487 stab_map_1(lpmat, istable, iunstable, aname, 1, indstab, OutputDirectoryName);
0488 end
0489 ixun=iunstable(find(~ismember(iunstable,[iindeterm,iwrong])));
0490 if ~isempty(iindeterm),
0491 [proba, dproba] = stab_map_1(lpmat, [1:Nsam], iindeterm, [aname, '_indet'],0);
0492
0493 indindet=find(proba<pvalue_ks);
0494 disp('Smirnov statistics in driving indeterminacy')
0495 for j=1:length(indindet),
0496 disp([M_.param_names(estim_params_.param_vals(indindet(j),1),:),' d-stat = ', num2str(dproba(indindet(j)),'%1.3f'),' p-value = ', num2str(proba(indindet(j)),'%1.3f')])
0497 end
0498 disp(' ');
0499 if ~isempty(indindet)
0500 stab_map_1(lpmat, [1:Nsam], iindeterm, [aname, '_indet'], 1, indindet, OutputDirectoryName);
0501 end
0502 end
0503
0504 if ~isempty(ixun),
0505 [proba, dproba] = stab_map_1(lpmat, [1:Nsam], ixun, [aname, '_unst'],0);
0506
0507 indunst=find(proba<pvalue_ks);
0508 disp('Smirnov statistics in driving instability')
0509 for j=1:length(indunst),
0510 disp([M_.param_names(estim_params_.param_vals(indunst(j),1),:),' d-stat = ', num2str(dproba(indunst(j)),'%1.3f'),' p-value = ', num2str(proba(indunst(j)),'%1.3f')])
0511 end
0512 disp(' ');
0513 if ~isempty(indunst)
0514 stab_map_1(lpmat, [1:Nsam], ixun, [aname, '_unst'], 1, indunst, OutputDirectoryName);
0515 end
0516 end
0517
0518 if ~isempty(iwrong),
0519 [proba, dproba] = stab_map_1(lpmat, [1:Nsam], iwrong, [aname, '_wrong'],0);
0520
0521 indwrong=find(proba<pvalue_ks);
0522 disp('Smirnov statistics in driving no solution')
0523 for j=1:length(indwrong),
0524 disp([M_.param_names(estim_params_.param_vals(indwrong(j),1),:),' d-stat = ', num2str(dproba(indwrong(j)),'%1.3f'),' p-value = ', num2str(proba(indwrong(j)),'%1.3f')])
0525 end
0526 disp(' ');
0527 if ~isempty(indwrong)
0528 stab_map_1(lpmat, [1:Nsam], iwrong, [aname, '_wrong'], 1, indwrong, OutputDirectoryName);
0529 end
0530 end
0531
0532 disp(' ')
0533 disp('Starting bivariate analysis:')
0534
0535 c0=corrcoef(lpmat(istable,:));
0536 c00=tril(c0,-1);
0537
0538 stab_map_2(lpmat(istable,:),alpha2, pvalue_corr, asname, OutputDirectoryName,xparam1);
0539 if length(iunstable)>10,
0540 stab_map_2(lpmat(iunstable,:),alpha2, pvalue_corr, auname, OutputDirectoryName,xparam1);
0541 end
0542 if length(iindeterm)>10,
0543 stab_map_2(lpmat(iindeterm,:),alpha2, pvalue_corr, aindname, OutputDirectoryName,xparam1);
0544 end
0545 if length(ixun)>10,
0546 stab_map_2(lpmat(ixun,:),alpha2, pvalue_corr, aunstname, OutputDirectoryName,xparam1);
0547 end
0548 if length(iwrong)>10,
0549 stab_map_2(lpmat(iwrong,:),alpha2, pvalue_corr, awrongname, OutputDirectoryName,xparam1);
0550 end
0551
0552 x0=0.5.*(bayestopt_.ub(1:nshock)-bayestopt_.lb(1:nshock))+bayestopt_.lb(1:nshock);
0553 x0 = [x0; lpmat(istable(1),:)'];
0554 if istable(end)~=Nsam
0555 M_.params(estim_params_.param_vals(:,1)) = lpmat(istable(1),:)';
0556 [oo_.dr,info,M_,options_,oo_] = resol(0,M_,options_,oo_);
0557
0558 end
0559 else
0560 if length(iunstable)==0,
0561 disp('All parameter values in the specified ranges are stable!')
0562 x0=0.5.*(bayestopt_.ub(1:nshock)-bayestopt_.lb(1:nshock))+bayestopt_.lb(1:nshock);
0563 x0 = [x0; lpmat(istable(1),:)'];
0564 else
0565 disp('All parameter values in the specified ranges are not acceptable!')
0566 x0=[];
0567 end
0568
0569 end
0570
0571
0572 options_.periods=opt.periods;
0573 if isfield(opt,'nomoments'),
0574 options_.nomoments=opt.nomoments;
0575 end
0576 options_.irf=opt.irf;
0577 options_.noprint=opt.noprint;
0578 if isfield(opt,'simul'),
0579 options_.simul=opt.simul;
0580 end
0581
0582
0583