2014-11-19 19:37:51 +01:00
function map_calibration ( OutputDirectoryName, Model, DynareOptions, DynareResults, EstimatedParameters, BayesInfo)
2014-09-04 16:39:59 +02:00
2017-02-03 12:48:27 +01:00
% Written by Marco Ratto
% Joint Research Centre, The European Commission,
2017-05-16 15:10:20 +02:00
% marco.ratto@ec.europa.eu
2017-02-03 12:48:27 +01:00
% Copyright (C) 2014-2016 European Commission
2017-10-10 10:05:59 +02:00
% Copyright (C) 2014-2018 Dynare Team
2014-09-04 16:39:59 +02:00
%
% This file is part of Dynare.
%
% Dynare is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% Dynare is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
fname_ = Model . fname ;
2016-06-01 13:02:20 +02:00
np = EstimatedParameters . np ;
nshock = EstimatedParameters . nvx + EstimatedParameters . nvn + EstimatedParameters . ncx + EstimatedParameters . ncn ;
pnames = cell ( np , 1 ) ;
pnames_tex = cell ( np , 1 ) ;
2017-05-16 15:10:20 +02:00
for jj = 1 : np
2016-06-15 00:35:40 +02:00
if DynareOptions . TeX
[ param_name_temp , param_name_tex_temp ] = get_the_name ( nshock + jj , DynareOptions . TeX , Model , EstimatedParameters , DynareOptions ) ;
2016-06-01 13:02:20 +02:00
pnames_tex { jj , 1 } = strrep ( param_name_tex_temp , ' $' , ' ' ) ;
pnames { jj , 1 } = param_name_temp ;
else
2016-06-15 00:35:40 +02:00
param_name_temp = get_the_name ( nshock + jj , DynareOptions . TeX , Model , EstimatedParameters , DynareOptions ) ;
2016-06-01 13:02:20 +02:00
pnames { jj , 1 } = param_name_temp ;
end
end
2014-09-04 16:39:59 +02:00
pvalue_ks = DynareOptions . opt_gsa . pvalue_ks ;
indx_irf = [ ] ;
indx_moment = [ ] ;
2014-11-19 19:37:51 +01:00
init = ~ DynareOptions . opt_gsa . load_stab ;
options_mcf . pvalue_ks = DynareOptions . opt_gsa . pvalue_ks ;
options_mcf . pvalue_corr = DynareOptions . opt_gsa . pvalue_corr ;
options_mcf . alpha2 = DynareOptions . opt_gsa . alpha2_stab ;
2017-10-10 10:05:59 +02:00
options_mcf . param_names = pnames ;
2016-06-15 00:35:40 +02:00
if DynareOptions . TeX
2017-10-10 10:05:59 +02:00
options_mcf . param_names_tex = pnames_tex ;
2016-06-01 13:02:20 +02:00
end
2014-11-19 19:37:51 +01:00
options_mcf . fname_ = fname_ ;
options_mcf . OutputDirectoryName = OutputDirectoryName ;
2014-09-04 16:39:59 +02:00
skipline ( )
disp ( ' Sensitivity analysis for calibration criteria' )
2017-05-16 12:42:01 +02:00
if DynareOptions . opt_gsa . ppost
2014-09-12 14:49:25 +02:00
filetoload = dir ( [ Model . dname filesep ' metropolis' filesep fname_ ' _param_irf*.mat' ] ) ;
lpmat = [ ] ;
2017-05-16 12:42:01 +02:00
for j = 1 : length ( filetoload )
2014-09-12 14:49:25 +02:00
load ( [ Model . dname filesep ' metropolis' filesep fname_ ' _param_irf' , int2str ( j ) , ' .mat' ] )
lpmat = [ lpmat ; stock ] ;
clear stock
end
type = ' post' ;
2014-09-04 16:39:59 +02:00
else
2014-09-12 14:49:25 +02:00
if DynareOptions . opt_gsa . pprior
filetoload = [ OutputDirectoryName ' /' fname_ ' _prior' ] ;
load ( filetoload , ' lpmat' , ' lpmat0' , ' istable' , ' iunstable' , ' iindeterm' , ' iwrong' , ' infox' )
lpmat = [ lpmat0 lpmat ] ;
type = ' prior' ;
else
filetoload = [ OutputDirectoryName ' /' fname_ ' _mc' ] ;
load ( filetoload , ' lpmat' , ' lpmat0' , ' istable' , ' iunstable' , ' iindeterm' , ' iwrong' , ' infox' )
lpmat = [ lpmat0 lpmat ] ;
type = ' mc' ;
2015-04-14 12:24:49 +02:00
end
2014-09-04 16:39:59 +02:00
end
[ Nsam , np ] = size ( lpmat ) ;
2014-11-19 19:37:51 +01:00
npar = size ( pnames , 1 ) ;
nshock = np - npar ;
2014-09-04 16:39:59 +02:00
nbr_irf_restrictions = size ( DynareOptions . endogenous_prior_restrictions . irf , 1 ) ;
2014-11-19 19:37:51 +01:00
nbr_moment_restrictions = size ( DynareOptions . endogenous_prior_restrictions . moment , 1 ) ;
if init
2015-04-14 12:24:49 +02:00
mat_irf = cell ( nbr_irf_restrictions , 1 ) ;
2017-05-16 12:42:01 +02:00
for ij = 1 : nbr_irf_restrictions
2015-04-14 12:24:49 +02:00
mat_irf { ij } = NaN ( Nsam , length ( DynareOptions . endogenous_prior_restrictions . irf { ij , 3 } ) ) ;
end
2017-05-16 15:10:20 +02:00
2015-04-14 12:24:49 +02:00
mat_moment = cell ( nbr_moment_restrictions , 1 ) ;
2017-05-16 12:42:01 +02:00
for ij = 1 : nbr_moment_restrictions
2015-04-14 12:24:49 +02:00
mat_moment { ij } = NaN ( Nsam , length ( DynareOptions . endogenous_prior_restrictions . moment { ij , 3 } ) ) ;
end
2017-05-16 15:10:20 +02:00
2015-04-14 12:24:49 +02:00
irestrictions = [ 1 : Nsam ] ;
h = dyn_waitbar ( 0 , ' Please wait...' ) ;
2017-05-16 12:42:01 +02:00
for j = 1 : Nsam
2015-04-14 12:24:49 +02:00
Model = set_all_parameters ( lpmat ( j , : ) ' , EstimatedParameters , Model ) ;
2017-05-16 12:42:01 +02:00
if nbr_moment_restrictions
2015-04-14 12:24:49 +02:00
[ Tt , Rr , SteadyState , info , Model , DynareOptions , DynareResults ] = dynare_resolve ( Model , DynareOptions , DynareResults ) ;
else
[ Tt , Rr , SteadyState , info , Model , DynareOptions , DynareResults ] = dynare_resolve ( Model , DynareOptions , DynareResults , ' restrict' ) ;
2014-09-04 16:39:59 +02:00
end
2017-05-16 12:42:01 +02:00
if info ( 1 ) == 0
2015-04-14 12:24:49 +02:00
[ info , info_irf , info_moment , data_irf , data_moment ] = endogenous_prior_restrictions ( Tt , Rr , Model , DynareOptions , DynareResults ) ;
if ~ isempty ( info_irf )
2017-05-16 12:42:01 +02:00
for ij = 1 : nbr_irf_restrictions
2015-04-14 12:24:49 +02:00
mat_irf { ij } ( j , : ) = data_irf { ij } ( : , 2 ) ' ;
end
indx_irf ( j , : ) = info_irf ( : , 1 ) ;
2014-09-04 16:39:59 +02:00
end
2015-04-14 12:24:49 +02:00
if ~ isempty ( info_moment )
2017-05-16 12:42:01 +02:00
for ij = 1 : nbr_moment_restrictions
2015-04-14 12:24:49 +02:00
mat_moment { ij } ( j , : ) = data_moment { ij } ( : , 2 ) ' ;
end
indx_moment ( j , : ) = info_moment ( : , 1 ) ;
end
else
irestrictions ( j ) = 0 ;
2014-09-04 16:39:59 +02:00
end
2015-04-14 12:24:49 +02:00
dyn_waitbar ( j / Nsam , h , [ ' MC iteration ' , int2str ( j ) , ' /' , int2str ( Nsam ) ] )
2014-09-04 16:39:59 +02:00
end
2015-04-14 12:24:49 +02:00
dyn_waitbar_close ( h ) ;
2017-05-16 15:10:20 +02:00
2015-04-14 12:24:49 +02:00
irestrictions = irestrictions ( find ( irestrictions ) ) ;
xmat = lpmat ( irestrictions , : ) ;
skipline ( )
endo_prior_restrictions = DynareOptions . endogenous_prior_restrictions ;
save ( [ OutputDirectoryName , filesep , fname_ , ' _' , type , ' _restrictions' ] , ' xmat' , ' mat_irf' , ' mat_moment' , ' irestrictions' , ' indx_irf' , ' indx_moment' , ' endo_prior_restrictions' ) ;
2014-11-19 19:37:51 +01:00
else
2015-04-14 12:24:49 +02:00
load ( [ OutputDirectoryName , filesep , fname_ , ' _' , type , ' _restrictions' ] , ' xmat' , ' mat_irf' , ' mat_moment' , ' irestrictions' , ' indx_irf' , ' indx_moment' , ' endo_prior_restrictions' ) ;
end
2017-05-16 12:42:01 +02:00
if ~ isempty ( indx_irf )
2015-04-14 12:24:49 +02:00
skipline ( )
disp ( ' Deleting old IRF calibration plots ...' )
a = dir ( [ OutputDirectoryName , filesep , fname_ , ' _' , type , ' _irf_calib*.eps' ] ) ;
2017-05-16 12:42:01 +02:00
for j = 1 : length ( a )
2015-04-14 12:24:49 +02:00
delete ( [ OutputDirectoryName , filesep , a ( j ) . name ] ) ;
end
a = dir ( [ OutputDirectoryName , filesep , fname_ , ' _' , type , ' _irf_calib*.fig' ] ) ;
2017-05-16 12:42:01 +02:00
for j = 1 : length ( a )
2015-04-14 12:24:49 +02:00
delete ( [ OutputDirectoryName , filesep , a ( j ) . name ] ) ;
end
a = dir ( [ OutputDirectoryName , filesep , fname_ , ' _' , type , ' _irf_calib*.pdf' ] ) ;
2017-05-16 12:42:01 +02:00
for j = 1 : length ( a )
2015-04-14 12:24:49 +02:00
delete ( [ OutputDirectoryName , filesep , a ( j ) . name ] ) ;
end
a = dir ( [ OutputDirectoryName , filesep , fname_ , ' _' , type , ' _irf_restrictions.eps' ] ) ;
2017-05-16 12:42:01 +02:00
for j = 1 : length ( a )
2015-04-14 12:24:49 +02:00
delete ( [ OutputDirectoryName , filesep , a ( j ) . name ] ) ;
end
a = dir ( [ OutputDirectoryName , filesep , fname_ , ' _' , type , ' _irf_restrictions.fig' ] ) ;
2017-05-16 12:42:01 +02:00
for j = 1 : length ( a )
2015-04-14 12:24:49 +02:00
delete ( [ OutputDirectoryName , filesep , a ( j ) . name ] ) ;
end
a = dir ( [ OutputDirectoryName , filesep , fname_ , ' _' , type , ' _irf_restrictions.pdf' ] ) ;
2017-05-16 12:42:01 +02:00
for j = 1 : length ( a )
2015-04-14 12:24:49 +02:00
delete ( [ OutputDirectoryName , filesep , a ( j ) . name ] ) ;
end
disp ( ' done !' )
skipline ( )
2017-05-16 15:10:20 +02:00
2015-04-14 12:24:49 +02:00
% For single legend search which has maximum nbr of restrictions
2014-11-19 19:37:51 +01:00
all_irf_couples = cellstr ( [ char ( endo_prior_restrictions . irf ( : , 1 ) ) char ( endo_prior_restrictions . irf ( : , 2 ) ) ] ) ;
irf_couples = unique ( all_irf_couples ) ;
nbr_irf_couples = size ( irf_couples , 1 ) ;
plot_indx = NaN ( nbr_irf_couples , 1 ) ;
time_matrix = cell ( nbr_irf_couples , 1 ) ;
2015-04-14 12:24:49 +02:00
indx_irf_matrix = zeros ( length ( irestrictions ) , nbr_irf_couples ) ;
2014-11-19 19:37:51 +01:00
irf_matrix = cell ( nbr_irf_couples , 1 ) ;
irf_mean = cell ( nbr_irf_couples , 1 ) ;
irf_median = cell ( nbr_irf_couples , 1 ) ;
irf_var = cell ( nbr_irf_couples , 1 ) ;
irf_HPD = cell ( nbr_irf_couples , 1 ) ;
irf_distrib = cell ( nbr_irf_couples , 1 ) ;
2014-11-14 14:53:37 +01:00
maxijv = 0 ;
for ij = 1 : nbr_irf_restrictions
2014-11-19 19:37:51 +01:00
if length ( endo_prior_restrictions . irf { ij , 3 } ) > maxijv
maxij = ij ; maxijv = length ( endo_prior_restrictions . irf { ij , 3 } ) ;
2014-11-14 14:53:37 +01:00
end
2014-11-19 19:37:51 +01:00
plot_indx ( ij ) = find ( strcmp ( irf_couples , all_irf_couples ( ij , : ) ) ) ;
time_matrix { plot_indx ( ij ) } = [ time_matrix { plot_indx ( ij ) } endo_prior_restrictions . irf { ij , 3 } ] ;
end
2015-04-14 12:24:49 +02:00
iplot_indx = ones ( size ( plot_indx ) ) ;
2017-05-16 15:10:20 +02:00
2014-11-19 19:37:51 +01:00
indx_irf = indx_irf ( irestrictions , : ) ;
2017-05-16 12:42:01 +02:00
if ~ DynareOptions . nograph
2017-03-22 16:54:11 +01:00
h1 = dyn_figure ( DynareOptions . nodisplay , ' name' , [ type ' evaluation of irf restrictions' ] ) ;
2015-04-15 22:54:36 +02:00
nrow = ceil ( sqrt ( nbr_irf_couples ) ) ;
ncol = nrow ;
2017-05-16 12:42:01 +02:00
if nrow * ( nrow - 1 ) > nbr_irf_couples
2015-04-15 22:54:36 +02:00
ncol = nrow - 1 ;
end
2014-11-14 14:53:37 +01:00
end
2017-05-16 12:42:01 +02:00
for ij = 1 : nbr_irf_restrictions
2014-09-04 16:39:59 +02:00
mat_irf { ij } = mat_irf { ij } ( irestrictions , : ) ;
2014-11-19 19:37:51 +01:00
irf_matrix { plot_indx ( ij ) } = [ irf_matrix { plot_indx ( ij ) } mat_irf { ij } ] ;
2015-04-14 12:24:49 +02:00
indx_irf_matrix ( : , plot_indx ( ij ) ) = indx_irf_matrix ( : , plot_indx ( ij ) ) + indx_irf ( : , ij ) ;
2017-05-16 12:42:01 +02:00
for ik = 1 : size ( mat_irf { ij } , 2 )
2014-11-19 19:37:51 +01:00
[ Mean , Median , Var , HPD , Distrib ] = ...
posterior_moments ( mat_irf { ij } ( : , ik ) , 0 , DynareOptions . mh_conf_sig ) ;
irf_mean { plot_indx ( ij ) } = [ irf_mean { plot_indx ( ij ) } ; Mean ] ;
irf_median { plot_indx ( ij ) } = [ irf_median { plot_indx ( ij ) } ; Median ] ;
irf_var { plot_indx ( ij ) } = [ irf_var { plot_indx ( ij ) } ; Var ] ;
irf_HPD { plot_indx ( ij ) } = [ irf_HPD { plot_indx ( ij ) } ; HPD ] ;
irf_distrib { plot_indx ( ij ) } = [ irf_distrib { plot_indx ( ij ) } ; Distrib ' ] ;
2014-09-04 16:39:59 +02:00
end
2014-11-19 19:37:51 +01:00
leg = num2str ( endo_prior_restrictions . irf { ij , 3 } ( 1 ) ) ;
aleg = num2str ( endo_prior_restrictions . irf { ij , 3 } ( 1 ) ) ;
2017-05-16 12:42:01 +02:00
if size ( mat_irf { ij } , 2 ) > 1
2014-11-19 19:37:51 +01:00
leg = [ leg , ' :' , num2str ( endo_prior_restrictions . irf { ij , 3 } ( end ) ) ] ;
aleg = [ aleg , ' -' , num2str ( endo_prior_restrictions . irf { ij , 3 } ( end ) ) ] ;
2015-04-14 12:24:49 +02:00
iplot_indx ( ij ) = 0 ;
end
2017-05-16 12:42:01 +02:00
if ~ DynareOptions . nograph && length ( time_matrix { plot_indx ( ij ) } ) == 1
2015-04-15 22:54:36 +02:00
set ( 0 , ' currentfigure' , h1 ) ,
2014-11-19 19:37:51 +01:00
subplot ( nrow , ncol , plot_indx ( ij ) ) ,
hc = cumplot ( mat_irf { ij } ( : , ik ) ) ;
a = axis ;
2015-04-16 12:30:27 +02:00
delete ( hc ) ;
2014-11-19 19:37:51 +01:00
x1val = max ( endo_prior_restrictions . irf { ij , 4 } ( 1 ) , a ( 1 ) ) ;
x2val = min ( endo_prior_restrictions . irf { ij , 4 } ( 2 ) , a ( 2 ) ) ;
hp = patch ( [ x1val x2val x2val x1val ] , a ( [ 3 3 4 4 ] ) , ' b' ) ;
2015-04-16 12:30:27 +02:00
hold all ,
set ( hp , ' FaceColor' , [ 0.7 0.8 1 ] )
hc = cumplot ( mat_irf { ij } ( : , ik ) ) ;
set ( hc , ' color' , ' k' , ' linewidth' , 2 )
2014-11-19 19:37:51 +01:00
hold off ,
% hold off,
title ( [ endo_prior_restrictions . irf { ij , 1 } , ' vs ' , endo_prior_restrictions . irf { ij , 2 } , ' (' , leg , ' )' ] , ' interpreter' , ' none' ) ,
%set(legend_h,'Xlim',[0 1]);
% if ij==maxij
% leg1 = num2str(endo_prior_restrictions.irf{ij,3}(:));
% [legend_h,object_h,plot_h,text_strings]=legend(leg1);
% Position=get(legend_h,'Position');Position(1:2)=[-0.055 0.95-Position(4)];
% set(legend_h,'Position',Position);
% end
end
2014-11-14 14:53:37 +01:00
% hc = get(h,'Children');
%for i=2:2:length(hc)
%end
2014-09-04 16:39:59 +02:00
indx1 = find ( indx_irf ( : , ij ) == 0 ) ;
indx2 = find ( indx_irf ( : , ij ) ~= 0 ) ;
2014-11-19 19:37:51 +01:00
atitle0 = [ endo_prior_restrictions . irf { ij , 1 } , ' vs ' , endo_prior_restrictions . irf { ij , 2 } , ' (' , leg , ' )' ] ;
2015-04-14 12:24:49 +02:00
fprintf ( [ ' %4.1f%% of the ' , type , ' support matches IRF ' , atitle0 , ' inside [%4.1f, %4.1f]\n' ] , length ( indx1 ) / length ( irestrictions ) * 100 , endo_prior_restrictions . irf { ij , 4 } )
2014-11-14 14:53:37 +01:00
% aname=[type '_irf_calib_',int2str(ij)];
2014-11-19 19:37:51 +01:00
aname = [ type ' _irf_calib_' , endo_prior_restrictions . irf { ij , 1 } , ' _vs_' , endo_prior_restrictions . irf { ij , 2 } , ' _' , aleg ] ;
atitle = [ type ' IRF Calib: Parameter(s) driving ' , endo_prior_restrictions . irf { ij , 1 } , ' vs ' , endo_prior_restrictions . irf { ij , 2 } , ' (' , leg , ' )' ] ;
options_mcf . amcf_name = aname ;
options_mcf . amcf_title = atitle ;
2015-04-14 12:24:49 +02:00
options_mcf . beha_title = ' IRF restriction' ;
options_mcf . nobeha_title = ' NO IRF restriction' ;
2014-11-19 19:37:51 +01:00
options_mcf . title = atitle0 ;
if ~ isempty ( indx1 ) && ~ isempty ( indx2 )
mcf_analysis ( xmat ( : , nshock + 1 : end ) , indx1 , indx2 , options_mcf , DynareOptions ) ;
end
2017-05-16 15:10:20 +02:00
2015-04-14 12:24:49 +02:00
% [proba, dproba] = stab_map_1(xmat, indx1, indx2, aname, 0);
% indplot=find(proba<pvalue_ks);
% if ~isempty(indplot)
% stab_map_1(xmat, indx1, indx2, aname, 1, indplot, OutputDirectoryName,[],atitle);
% end
2014-11-19 19:37:51 +01:00
end
2017-05-16 12:42:01 +02:00
for ij = 1 : nbr_irf_couples
if length ( time_matrix { ij } ) > 1
if ~ DynareOptions . nograph
2015-04-16 08:52:42 +02:00
set ( 0 , ' currentfigure' , h1 ) ;
2015-04-15 22:54:36 +02:00
subplot ( nrow , ncol , ij )
itmp = ( find ( plot_indx == ij ) ) ;
2015-04-16 12:30:27 +02:00
htmp = plot ( time_matrix { ij } , [ max ( irf_matrix { ij } ) ' min ( irf_matrix { ij } ) ' ] , ' k--' , ' linewidth' , 2 ) ;
2015-04-15 22:54:36 +02:00
a = axis ;
2015-04-16 12:30:27 +02:00
delete ( htmp ) ;
2015-04-15 22:54:36 +02:00
tmp = [ ] ;
2017-05-16 12:42:01 +02:00
for ir = 1 : length ( itmp )
2015-04-15 22:54:36 +02:00
for it = 1 : length ( endo_prior_restrictions . irf { itmp ( ir ) , 3 } )
temp_index = find ( time_matrix { ij } == endo_prior_restrictions . irf { itmp ( ir ) , 3 } ( it ) ) ;
tmp ( temp_index , : ) = endo_prior_restrictions . irf { itmp ( ir ) , 4 } ;
end
2014-11-19 19:37:51 +01:00
end
2015-04-15 22:54:36 +02:00
% tmp = cell2mat(endo_prior_restrictions.irf(itmp,4));
tmp ( isinf ( tmp ( : , 1 ) ) , 1 ) = a ( 3 ) ;
tmp ( isinf ( tmp ( : , 2 ) ) , 2 ) = a ( 4 ) ;
2015-04-16 12:30:27 +02:00
hp = patch ( [ time_matrix { ij } time_matrix { ij } ( end : - 1 : 1 ) ] , [ tmp ( : , 1 ) ; tmp ( end : - 1 : 1 , 2 ) ] , ' c' ) ;
set ( hp , ' FaceColor' , [ 0.7 0.8 1 ] )
hold on ,
plot ( time_matrix { ij } , [ max ( irf_matrix { ij } ) ' min ( irf_matrix { ij } ) ' ] , ' k--' , ' linewidth' , 2 )
plot ( time_matrix { ij } , irf_median { ij } , ' k' , ' linewidth' , 2 )
plot ( time_matrix { ij } , [ irf_distrib { ij } ] , ' k-' )
2015-04-15 22:54:36 +02:00
plot ( a ( 1 : 2 ) , [ 0 0 ] , ' r' )
2017-05-16 12:42:01 +02:00
hold off
2015-04-15 22:54:36 +02:00
axis ( [ max ( 1 , a ( 1 ) ) a ( 2 : 4 ) ] )
2017-05-16 12:42:01 +02:00
box on
2016-06-19 15:46:16 +02:00
%set(gca,'xtick',sort(time_matrix{ij}))
2015-04-15 22:54:36 +02:00
itmp = min ( itmp ) ;
title ( [ endo_prior_restrictions . irf { itmp , 1 } , ' vs ' , endo_prior_restrictions . irf { itmp , 2 } ] , ' interpreter' , ' none' ) ,
2014-11-19 19:37:51 +01:00
end
2017-05-16 12:42:01 +02:00
if any ( iplot_indx .* plot_indx == ij )
2015-04-14 12:24:49 +02:00
% MCF of the couples with logical AND
2015-04-15 22:54:36 +02:00
itmp = min ( find ( plot_indx == ij ) ) ;
2015-04-14 12:24:49 +02:00
indx1 = find ( indx_irf_matrix ( : , ij ) == 0 ) ;
indx2 = find ( indx_irf_matrix ( : , ij ) ~= 0 ) ;
leg = num2str ( time_matrix { ij } ( 1 ) ) ;
leg = [ leg ' ...' num2str ( time_matrix { ij } ( end ) ) ] ;
aleg = ' ALL' ;
atitle0 = [ endo_prior_restrictions . irf { itmp , 1 } , ' vs ' , endo_prior_restrictions . irf { itmp , 2 } , ' (' , leg , ' )' ] ;
fprintf ( [ ' %4.1f%% of the ' , type , ' support matches IRF restrictions ' , atitle0 , ' \n' ] , length ( indx1 ) / length ( irestrictions ) * 100 )
% aname=[type '_irf_calib_',int2str(ij)];
aname = [ type ' _irf_calib_' , endo_prior_restrictions . irf { itmp , 1 } , ' _vs_' , endo_prior_restrictions . irf { itmp , 2 } , ' _' , aleg ] ;
atitle = [ type ' IRF Calib: Parameter(s) driving ' , endo_prior_restrictions . irf { itmp , 1 } , ' vs ' , endo_prior_restrictions . irf { itmp , 2 } , ' (' , leg , ' )' ] ;
options_mcf . amcf_name = aname ;
options_mcf . amcf_title = atitle ;
options_mcf . beha_title = ' IRF restriction' ;
options_mcf . nobeha_title = ' NO IRF restriction' ;
options_mcf . title = atitle0 ;
if ~ isempty ( indx1 ) && ~ isempty ( indx2 )
mcf_analysis ( xmat ( : , nshock + 1 : end ) , indx1 , indx2 , options_mcf , DynareOptions ) ;
end
end
2014-09-04 16:39:59 +02:00
end
end
2017-05-16 12:42:01 +02:00
if ~ DynareOptions . nograph
2017-03-23 17:59:05 +01:00
dyn_saveas ( h1 , [ OutputDirectoryName , filesep , fname_ , ' _' , type , ' _irf_restrictions' ] , DynareOptions . nodisplay , DynareOptions . graph_format ) ;
2016-06-19 14:32:33 +02:00
create_TeX_loader ( DynareOptions , [ OutputDirectoryName , filesep , fname_ , ' _' , type , ' _irf_restrictions' ] , [ type ' evaluation of irf restrictions' ] , ' irf_restrictions' , type , DynareOptions . figures . textwidth * min ( ij / ncol , 1 ) )
2015-04-15 22:54:36 +02:00
end
2014-09-04 16:39:59 +02:00
skipline ( )
end
if ~ isempty ( indx_moment )
2015-04-14 12:24:49 +02:00
skipline ( )
disp ( ' Deleting old MOMENT calibration plots ...' )
a = dir ( [ OutputDirectoryName , filesep , fname_ , ' _' , type , ' _moment_calib*.eps' ] ) ;
2017-05-16 12:42:01 +02:00
for j = 1 : length ( a )
2015-04-14 12:24:49 +02:00
delete ( [ OutputDirectoryName , filesep , a ( j ) . name ] ) ;
end
a = dir ( [ OutputDirectoryName , filesep , fname_ , ' _' , type , ' _moment_calib*.fig' ] ) ;
2017-05-16 12:42:01 +02:00
for j = 1 : length ( a )
2015-04-14 12:24:49 +02:00
delete ( [ OutputDirectoryName , filesep , a ( j ) . name ] ) ;
end
a = dir ( [ OutputDirectoryName , filesep , fname_ , ' _' , type , ' _moment_calib*.pdf' ] ) ;
2017-05-16 12:42:01 +02:00
for j = 1 : length ( a )
2015-04-14 12:24:49 +02:00
delete ( [ OutputDirectoryName , filesep , a ( j ) . name ] ) ;
end
a = dir ( [ OutputDirectoryName , filesep , fname_ , ' _' , type , ' _moment_restrictions.eps' ] ) ;
2017-05-16 12:42:01 +02:00
for j = 1 : length ( a )
2015-04-14 12:24:49 +02:00
delete ( [ OutputDirectoryName , filesep , a ( j ) . name ] ) ;
end
a = dir ( [ OutputDirectoryName , filesep , fname_ , ' _' , type , ' _moment_restrictions.fig' ] ) ;
2017-05-16 12:42:01 +02:00
for j = 1 : length ( a )
2015-04-14 12:24:49 +02:00
delete ( [ OutputDirectoryName , filesep , a ( j ) . name ] ) ;
end
a = dir ( [ OutputDirectoryName , filesep , fname_ , ' _' , type , ' _moment_restrictions.pdf' ] ) ;
2017-05-16 12:42:01 +02:00
for j = 1 : length ( a )
2015-04-14 12:24:49 +02:00
delete ( [ OutputDirectoryName , filesep , a ( j ) . name ] ) ;
end
disp ( ' done !' )
skipline ( )
2017-05-16 15:10:20 +02:00
2016-06-19 14:32:33 +02:00
%get parameter names including standard deviations
np = size ( BayesInfo . name , 1 ) ;
name = cell ( np , 1 ) ;
name_tex = cell ( np , 1 ) ;
for jj = 1 : np
if DynareOptions . TeX
[ param_name_temp , param_name_tex_temp ] = get_the_name ( jj , DynareOptions . TeX , Model , EstimatedParameters , DynareOptions ) ;
name_tex { jj , 1 } = strrep ( param_name_tex_temp , ' $' , ' ' ) ;
name { jj , 1 } = param_name_temp ;
else
param_name_temp = get_the_name ( jj , DynareOptions . TeX , Model , EstimatedParameters , DynareOptions ) ;
name { jj , 1 } = param_name_temp ;
end
end
2017-10-10 10:05:59 +02:00
options_mcf . param_names = name ;
2016-06-19 14:32:33 +02:00
if DynareOptions . TeX
2017-10-10 10:05:59 +02:00
options_mcf . param_names_tex = name_tex ;
2016-06-19 14:32:33 +02:00
end
2017-10-10 10:05:59 +02:00
options_mcf . param_names = BayesInfo . name ;
2014-11-19 19:37:51 +01:00
all_moment_couples = cellstr ( [ char ( endo_prior_restrictions . moment ( : , 1 ) ) char ( endo_prior_restrictions . moment ( : , 2 ) ) ] ) ;
moment_couples = unique ( all_moment_couples ) ;
nbr_moment_couples = size ( moment_couples , 1 ) ;
plot_indx = NaN ( nbr_moment_couples , 1 ) ;
time_matrix = cell ( nbr_moment_couples , 1 ) ;
2015-04-14 12:24:49 +02:00
indx_moment_matrix = zeros ( length ( irestrictions ) , nbr_moment_couples ) ;
2014-11-19 19:37:51 +01:00
moment_matrix = cell ( nbr_moment_couples , 1 ) ;
moment_mean = cell ( nbr_moment_couples , 1 ) ;
moment_median = cell ( nbr_moment_couples , 1 ) ;
moment_var = cell ( nbr_moment_couples , 1 ) ;
moment_HPD = cell ( nbr_moment_couples , 1 ) ;
moment_distrib = cell ( nbr_moment_couples , 1 ) ;
2015-04-14 12:24:49 +02:00
% For single legend search which has maximum nbr of restrictions
2014-11-19 19:37:51 +01:00
maxijv = 0 ;
for ij = 1 : nbr_moment_restrictions
2019-01-11 23:33:45 +01:00
endo_prior_restrictions . moment { ij , 3 } = sort ( endo_prior_restrictions . moment { ij , 3 } ) ;
2014-11-19 19:37:51 +01:00
if length ( endo_prior_restrictions . moment { ij , 3 } ) > maxijv
maxij = ij ; maxijv = length ( endo_prior_restrictions . moment { ij , 3 } ) ;
end
plot_indx ( ij ) = find ( strcmp ( moment_couples , all_moment_couples ( ij , : ) ) ) ;
time_matrix { plot_indx ( ij ) } = [ time_matrix { plot_indx ( ij ) } endo_prior_restrictions . moment { ij , 3 } ] ;
end
2015-04-14 12:24:49 +02:00
iplot_indx = ones ( size ( plot_indx ) ) ;
2017-05-16 15:10:20 +02:00
2014-09-04 16:39:59 +02:00
indx_moment = indx_moment ( irestrictions , : ) ;
2017-05-16 12:42:01 +02:00
if ~ DynareOptions . nograph
2017-03-22 16:54:11 +01:00
h2 = dyn_figure ( DynareOptions . nodisplay , ' name' , [ type ' evaluation of moment restrictions' ] ) ;
2015-04-15 22:54:36 +02:00
nrow = ceil ( sqrt ( nbr_moment_couples ) ) ;
ncol = nrow ;
2017-05-16 12:42:01 +02:00
if nrow * ( nrow - 1 ) > nbr_moment_couples
2015-04-15 22:54:36 +02:00
ncol = nrow - 1 ;
end
2014-09-04 16:39:59 +02:00
end
2017-05-16 15:10:20 +02:00
2017-05-16 12:42:01 +02:00
for ij = 1 : nbr_moment_restrictions
2014-09-04 16:39:59 +02:00
mat_moment { ij } = mat_moment { ij } ( irestrictions , : ) ;
2014-11-19 19:37:51 +01:00
moment_matrix { plot_indx ( ij ) } = [ moment_matrix { plot_indx ( ij ) } mat_moment { ij } ] ;
2015-04-14 12:24:49 +02:00
indx_moment_matrix ( : , plot_indx ( ij ) ) = indx_moment_matrix ( : , plot_indx ( ij ) ) + indx_moment ( : , ij ) ;
2017-05-16 12:42:01 +02:00
for ik = 1 : size ( mat_moment { ij } , 2 )
2014-11-19 19:37:51 +01:00
[ Mean , Median , Var , HPD , Distrib ] = ...
posterior_moments ( mat_moment { ij } ( : , ik ) , 0 , DynareOptions . mh_conf_sig ) ;
moment_mean { plot_indx ( ij ) } = [ moment_mean { plot_indx ( ij ) } ; Mean ] ;
moment_median { plot_indx ( ij ) } = [ moment_median { plot_indx ( ij ) } ; Median ] ;
moment_var { plot_indx ( ij ) } = [ moment_var { plot_indx ( ij ) } ; Var ] ;
moment_HPD { plot_indx ( ij ) } = [ moment_HPD { plot_indx ( ij ) } ; HPD ] ;
moment_distrib { plot_indx ( ij ) } = [ moment_distrib { plot_indx ( ij ) } ; Distrib ' ] ;
2014-09-04 16:39:59 +02:00
end
2014-11-19 19:37:51 +01:00
leg = num2str ( endo_prior_restrictions . moment { ij , 3 } ( 1 ) ) ;
aleg = num2str ( endo_prior_restrictions . moment { ij , 3 } ( 1 ) ) ;
2017-05-16 12:42:01 +02:00
if size ( mat_moment { ij } , 2 ) > 1
2014-11-19 19:37:51 +01:00
leg = [ leg , ' :' , num2str ( endo_prior_restrictions . moment { ij , 3 } ( end ) ) ] ;
aleg = [ aleg , ' _' , num2str ( endo_prior_restrictions . moment { ij , 3 } ( end ) ) ] ;
2015-04-14 12:24:49 +02:00
iplot_indx ( ij ) = 0 ;
2014-11-19 19:37:51 +01:00
end
2017-05-16 12:42:01 +02:00
if ~ DynareOptions . nograph && length ( time_matrix { plot_indx ( ij ) } ) == 1
2015-04-16 08:52:42 +02:00
set ( 0 , ' currentfigure' , h2 ) ;
2014-11-19 19:37:51 +01:00
subplot ( nrow , ncol , plot_indx ( ij ) ) ,
hc = cumplot ( mat_moment { ij } ( : , ik ) ) ;
2015-04-16 12:30:27 +02:00
a = axis ; delete ( hc ) ,
2014-11-19 19:37:51 +01:00
% hist(mat_moment{ij}),
x1val = max ( endo_prior_restrictions . moment { ij , 4 } ( 1 ) , a ( 1 ) ) ;
x2val = min ( endo_prior_restrictions . moment { ij , 4 } ( 2 ) , a ( 2 ) ) ;
hp = patch ( [ x1val x2val x2val x1val ] , a ( [ 3 3 4 4 ] ) , ' b' ) ;
2015-04-16 12:30:27 +02:00
set ( hp , ' FaceColor' , [ 0.7 0.8 1 ] )
2017-05-16 12:42:01 +02:00
hold all
2015-04-16 12:30:27 +02:00
hc = cumplot ( mat_moment { ij } ( : , ik ) ) ;
set ( hc , ' color' , ' k' , ' linewidth' , 2 )
2017-05-16 12:42:01 +02:00
hold off
2014-11-19 19:37:51 +01:00
title ( [ endo_prior_restrictions . moment { ij , 1 } , ' vs ' , endo_prior_restrictions . moment { ij , 2 } , ' (' , leg , ' )' ] , ' interpreter' , ' none' ) ,
2015-04-14 12:24:49 +02:00
% if ij==maxij
% leg1 = num2str(endo_prior_restrictions.moment{ij,3}(:));
% [legend_h,object_h,plot_h,text_strings]=legend(leg1);
% Position=get(legend_h,'Position');Position(1:2)=[-0.055 0.95-Position(4)];
% set(legend_h,'Position',Position);
% end
2014-09-04 16:39:59 +02:00
end
indx1 = find ( indx_moment ( : , ij ) == 0 ) ;
indx2 = find ( indx_moment ( : , ij ) ~= 0 ) ;
2014-11-19 19:37:51 +01:00
atitle0 = [ endo_prior_restrictions . moment { ij , 1 } , ' vs ' , endo_prior_restrictions . moment { ij , 2 } , ' (' , leg , ' )' ] ;
2015-04-14 12:24:49 +02:00
fprintf ( [ ' %4.1f%% of the ' , type , ' support matches MOMENT ' , atitle0 , ' inside [%4.1f, %4.1f]\n' ] , length ( indx1 ) / length ( irestrictions ) * 100 , endo_prior_restrictions . moment { ij , 4 } )
2014-11-14 14:53:37 +01:00
% aname=[type '_moment_calib_',int2str(ij)];
2014-11-19 19:37:51 +01:00
aname = [ type ' _moment_calib_' , endo_prior_restrictions . moment { ij , 1 } , ' _vs_' , endo_prior_restrictions . moment { ij , 2 } , ' _' , aleg ] ;
atitle = [ type ' MOMENT Calib: Parameter(s) driving ' , endo_prior_restrictions . moment { ij , 1 } , ' vs ' , endo_prior_restrictions . moment { ij , 2 } , ' (' , leg , ' )' ] ;
options_mcf . amcf_name = aname ;
options_mcf . amcf_title = atitle ;
2015-04-14 12:24:49 +02:00
options_mcf . beha_title = ' moment restriction' ;
options_mcf . nobeha_title = ' NO moment restriction' ;
2014-11-19 19:37:51 +01:00
options_mcf . title = atitle0 ;
if ~ isempty ( indx1 ) && ~ isempty ( indx2 )
mcf_analysis ( xmat , indx1 , indx2 , options_mcf , DynareOptions ) ;
end
2017-05-16 15:10:20 +02:00
2015-04-14 12:24:49 +02:00
% [proba, dproba] = stab_map_1(xmat, indx1, indx2, aname, 0);
% indplot=find(proba<pvalue_ks);
% if ~isempty(indplot)
% stab_map_1(xmat, indx1, indx2, aname, 1, indplot, OutputDirectoryName,[],atitle);
% end
2014-11-19 19:37:51 +01:00
end
2017-05-16 12:42:01 +02:00
for ij = 1 : nbr_moment_couples
2019-01-11 23:33:45 +01:00
time_matrix { ij } = sort ( time_matrix { ij } ) ;
2017-05-16 12:42:01 +02:00
if length ( time_matrix { ij } ) > 1
2015-04-15 22:54:36 +02:00
if ~ DynareOptions . nograph
itmp = ( find ( plot_indx == ij ) ) ;
set ( 0 , ' currentfigure' , h2 ) ;
subplot ( nrow , ncol , ij )
2015-04-16 12:30:27 +02:00
htmp = plot ( time_matrix { ij } , [ max ( moment_matrix { ij } ) ' min ( moment_matrix { ij } ) ' ] , ' k--' , ' linewidth' , 2 ) ;
2015-04-15 22:54:36 +02:00
a = axis ;
2015-04-16 12:30:27 +02:00
delete ( htmp ) ;
2015-04-15 22:54:36 +02:00
tmp = [ ] ;
2017-05-16 12:42:01 +02:00
for ir = 1 : length ( itmp )
2015-04-15 22:54:36 +02:00
for it = 1 : length ( endo_prior_restrictions . moment { itmp ( ir ) , 3 } )
temp_index = find ( time_matrix { ij } == endo_prior_restrictions . moment { itmp ( ir ) , 3 } ( it ) ) ;
tmp ( temp_index , : ) = endo_prior_restrictions . moment { itmp ( ir ) , 4 } ;
end
2014-11-19 19:37:51 +01:00
end
2015-04-15 22:54:36 +02:00
% tmp = cell2mat(endo_prior_restrictions.moment(itmp,4));
tmp ( isinf ( tmp ( : , 1 ) ) , 1 ) = a ( 3 ) ;
tmp ( isinf ( tmp ( : , 2 ) ) , 2 ) = a ( 4 ) ;
hp = patch ( [ time_matrix { ij } time_matrix { ij } ( end : - 1 : 1 ) ] , [ tmp ( : , 1 ) ; tmp ( end : - 1 : 1 , 2 ) ] , ' b' ) ;
2015-04-16 12:30:27 +02:00
set ( hp , ' FaceColor' , [ 0.7 0.8 1 ] )
2017-05-16 12:42:01 +02:00
hold on
2015-04-16 12:30:27 +02:00
plot ( time_matrix { ij } , [ max ( moment_matrix { ij } ) ' min ( moment_matrix { ij } ) ' ] , ' k--' , ' linewidth' , 2 )
plot ( time_matrix { ij } , moment_median { ij } , ' k' , ' linewidth' , 2 )
plot ( time_matrix { ij } , [ moment_distrib { ij } ] , ' k-' )
2015-04-15 22:54:36 +02:00
plot ( a ( 1 : 2 ) , [ 0 0 ] , ' r' )
2017-05-16 12:42:01 +02:00
hold off
2015-04-15 22:54:36 +02:00
axis ( a )
2017-05-16 12:42:01 +02:00
box on
2019-01-11 23:33:45 +01:00
% set(gca,'xtick',sort(time_matrix{ij}))
2015-04-15 22:54:36 +02:00
itmp = min ( itmp ) ;
title ( [ endo_prior_restrictions . moment { itmp , 1 } , ' vs ' , endo_prior_restrictions . moment { itmp , 2 } ] , ' interpreter' , ' none' ) ,
2014-11-19 19:37:51 +01:00
end
2017-05-16 12:42:01 +02:00
if any ( iplot_indx .* plot_indx == ij )
2015-04-14 12:24:49 +02:00
% MCF of the couples with logical AND
2015-04-15 22:54:36 +02:00
itmp = min ( find ( plot_indx == ij ) ) ;
2015-04-14 12:24:49 +02:00
indx1 = find ( indx_moment_matrix ( : , ij ) == 0 ) ;
indx2 = find ( indx_moment_matrix ( : , ij ) ~= 0 ) ;
leg = num2str ( time_matrix { ij } ( 1 ) ) ;
leg = [ leg ' ...' num2str ( time_matrix { ij } ( end ) ) ] ;
aleg = ' ALL' ;
atitle0 = [ endo_prior_restrictions . moment { itmp , 1 } , ' vs ' , endo_prior_restrictions . moment { itmp , 2 } , ' (' , leg , ' )' ] ;
fprintf ( [ ' %4.1f%% of the ' , type , ' support matches MOMENT restrictions ' , atitle0 , ' \n' ] , length ( indx1 ) / length ( irestrictions ) * 100 )
% aname=[type '_moment_calib_',int2str(ij)];
aname = [ type ' _moment_calib_' , endo_prior_restrictions . moment { itmp , 1 } , ' _vs_' , endo_prior_restrictions . moment { itmp , 2 } , ' _' , aleg ] ;
atitle = [ type ' MOMENT Calib: Parameter(s) driving ' , endo_prior_restrictions . moment { itmp , 1 } , ' vs ' , endo_prior_restrictions . moment { itmp , 2 } , ' (' , leg , ' )' ] ;
options_mcf . amcf_name = aname ;
options_mcf . amcf_title = atitle ;
options_mcf . beha_title = ' moment restriction' ;
options_mcf . nobeha_title = ' NO moment restriction' ;
options_mcf . title = atitle0 ;
if ~ isempty ( indx1 ) && ~ isempty ( indx2 )
mcf_analysis ( xmat , indx1 , indx2 , options_mcf , DynareOptions ) ;
end
end
2014-09-04 16:39:59 +02:00
end
end
2017-05-16 12:42:01 +02:00
if ~ DynareOptions . nograph
2017-03-23 17:59:05 +01:00
dyn_saveas ( h2 , [ OutputDirectoryName , filesep , fname_ , ' _' , type , ' _moment_restrictions' ] , DynareOptions . nodisplay , DynareOptions . graph_format ) ;
2016-06-19 14:32:33 +02:00
create_TeX_loader ( DynareOptions , [ OutputDirectoryName , filesep , fname_ , ' _' , type , ' _moment_restrictions' ] , [ type ' evaluation of moment restrictions' ] , ' moment_restrictions' , type , DynareOptions . figures . textwidth * min ( ij / ncol , 1 ) )
2015-04-15 22:54:36 +02:00
end
2017-05-16 15:10:20 +02:00
2014-09-04 16:39:59 +02:00
skipline ( )
end
return
2016-06-19 12:38:31 +02:00
function []= create_TeX_loader ( options,figpath,caption,label_name,label_type,scale_factor)
2016-03-05 21:28:16 +01:00
if options . TeX && any ( strcmp ( ' eps' , cellstr ( options . graph_format ) ) )
2016-06-14 09:48:35 +02:00
fidTeX = fopen ( [ figpath ' .tex' ] , ' w' ) ;
2015-12-14 14:09:37 +01:00
fprintf ( fidTeX , ' %% TeX eps-loader file generated by map_calibration.m (Dynare).\n' ) ;
fprintf ( fidTeX , [ ' %% ' datestr ( now , 0 ) ' \n\n' ] ) ;
fprintf ( fidTeX , ' \\begin{figure}[H]\n' ) ;
fprintf ( fidTeX , ' \\centering \n' ) ;
2016-06-19 12:38:31 +02:00
fprintf ( fidTeX , ' \\includegraphics[width=%2.2f\\textwidth]{%s}\n' , scale_factor , strrep ( figpath , ' \' , ' /' ) ) ;
2015-12-14 14:09:37 +01:00
fprintf ( fidTeX , ' \\caption{%s.}' , caption ) ;
fprintf ( fidTeX , ' \\label{Fig:%s:%s}\n' , label_name , label_type ) ;
fprintf ( fidTeX , ' \\end{figure}\n\n' ) ;
fprintf ( fidTeX , ' %% End Of TeX file. \n' ) ;
fclose ( fidTeX ) ;
end