2015-05-09 15:34:37 +02:00
function mode_check ( fun,x,hessian_mat,DynareDataset,DatasetInfo,DynareOptions,Model,EstimatedParameters,BayesInfo,BoundsInfo,DynareResults)
2012-03-05 15:03:55 +01:00
% Checks the estimated ML mode or Posterior mode.
%@info:
2015-05-09 15:34:37 +02:00
%! @deftypefn {Function File} mode_check (@var{fun}, @var{x}, @var{hessian_mat}, @var{DynareDataset}, @var{DynareOptions}, @var{Model}, @var{EstimatedParameters}, @var{BayesInfo}, @var{DynareResults})
2012-03-05 15:47:49 +01:00
%! @anchor{mode_check}
2012-03-05 15:03:55 +01:00
%! @sp 1
%! Checks the estimated ML mode or Posterior mode by plotting sections of the likelihood/posterior kernel.
%! Each plot shows the variation of the likelihood implied by the variations of a single parameter, ceteris paribus)
%! @sp 2
%! @strong{Inputs}
%! @sp 1
%! @table @ @var
2012-03-05 15:47:49 +01:00
%! @item fun
%! Objective function.
%! @item x
%! Estimated mode.
2012-03-05 15:03:55 +01:00
%! @item start
2012-03-05 15:47:49 +01:00
%! Hessian of the objective function at the estimated mode @var{x}.
%! @item DynareDataset
%! Structure specifying the dataset used for estimation (dataset_).
2012-03-05 15:03:55 +01:00
%! @item DynareOptions
2012-03-05 15:47:49 +01:00
%! Structure defining dynare's options (options_).
%! @item Model
%! Structure specifying the (estimated) model (M_).
%! @item EstimatedParameters
%! Structure specifying the estimated parameters (estimated_params_).
%! @item BayesInfo
%! Structure containing information about the priors used for estimation (bayestopt_).
%! @item DynareResults
%! Structure gathering the results (oo_).
2012-03-05 15:03:55 +01:00
%! @end table
%! @sp 2
%! @strong{Outputs}
%! @sp 2
%! @strong{This function is called by:}
%! @sp 2
%! @strong{This function calls:}
2012-03-05 15:47:49 +01:00
%! The objective function (@var{func}).
2012-03-05 15:03:55 +01:00
%! @end deftypefn
%@eod:
2017-03-22 16:54:11 +01:00
% Copyright (C) 2003-2017 Dynare Team
2008-08-01 14:40:33 +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/>.
2008-01-21 12:51:09 +01:00
2011-09-22 11:17:31 +02:00
TeX = DynareOptions . TeX ;
2017-05-16 12:42:01 +02:00
if ~ isempty ( hessian_mat )
2015-05-09 15:34:37 +02:00
[ s_min , k ] = min ( diag ( hessian_mat ) ) ;
2011-05-13 12:46:11 +02:00
end
2011-09-22 11:17:31 +02:00
2014-10-20 16:18:54 +02:00
fval = feval ( fun , x , DynareDataset , DatasetInfo , DynareOptions , Model , EstimatedParameters , BayesInfo , BoundsInfo , DynareResults ) ;
2011-05-13 12:46:11 +02:00
2017-05-16 12:42:01 +02:00
if ~ isempty ( hessian_mat )
2013-07-10 17:12:34 +02:00
skipline ( )
2011-05-13 12:46:11 +02:00
disp ( ' MODE CHECK' )
2013-07-10 17:12:34 +02:00
skipline ( )
2015-05-11 17:39:26 +02:00
fprintf ( ' Fval obtained by the minimization routine (minus the posterior/likelihood)): %f' , fval ) ;
2013-07-10 17:12:34 +02:00
skipline ( )
2011-05-13 12:46:11 +02:00
if s_min < eps
2011-09-22 11:17:31 +02:00
disp ( sprintf ( ' Most negative variance %f for parameter %d (%s = %f)' , s_min , k , BayesInfo . name { k } , x ( k ) ) )
2011-05-13 12:46:11 +02:00
end
2009-04-06 16:48:30 +02:00
end
2005-02-18 20:54:39 +01:00
[ nbplt , nr , nc , lr , lc , nstar ] = pltorg ( length ( x ) ) ;
2015-05-12 08:53:57 +02:00
if TeX && any ( strcmp ( ' eps' , cellstr ( DynareOptions . graph_format ) ) )
2016-06-14 09:48:35 +02:00
fidTeX = fopen ( [ Model . fname ' _CheckPlots.tex' ] , ' w' ) ;
2005-02-18 20:54:39 +01:00
fprintf ( fidTeX , ' %% TeX eps-loader file generated by mode_check.m (Dynare).\n' ) ;
fprintf ( fidTeX , [ ' %% ' datestr ( now , 0 ) ' \n' ] ) ;
fprintf ( fidTeX , ' \n' ) ;
end
2013-06-20 17:19:44 +02:00
ll = DynareOptions . mode_check . neighbourhood_size ;
2017-05-16 12:42:01 +02:00
if isinf ( ll )
2013-06-20 17:19:44 +02:00
DynareOptions . mode_check . symmetric_plots = 0 ;
2013-06-18 22:30:49 +02:00
end
2012-03-05 15:03:55 +01:00
2015-07-08 16:05:38 +02:00
mcheck = struct ( ' cross' , struct ( ) , ' emode' , struct ( ) ) ;
2017-05-16 12:42:01 +02:00
for plt = 1 : nbplt
2005-02-18 20:54:39 +01:00
if TeX
NAMES = [ ] ;
TeXNAMES = [ ] ;
end
2017-03-22 16:54:11 +01:00
hh = dyn_figure ( DynareOptions . nodisplay , ' Name' , ' Mode check plots' ) ;
2009-06-19 10:54:40 +02:00
for k = 1 : min ( nstar , length ( x ) - ( plt - 1 ) * nstar )
subplot ( nr , nc , k )
kk = ( plt - 1 ) * nstar + k ;
2011-09-22 11:17:31 +02:00
[ name , texname ] = get_the_name ( kk , TeX , Model , EstimatedParameters , DynareOptions ) ;
2005-02-18 20:54:39 +01:00
if TeX
2010-09-26 13:42:41 +02:00
if isempty ( NAMES )
NAMES = name ;
TeXNAMES = texname ;
else
NAMES = char ( NAMES , name ) ;
TeXNAMES = char ( TeXNAMES , texname ) ;
end
2009-06-19 10:54:40 +02:00
end
2005-02-18 20:54:39 +01:00
xx = x ;
2017-11-22 11:05:12 +01:00
if x ( kk ) ~= 0 || ~ isinf ( BoundsInfo . lb ( kk ) ) || ~ isinf ( BoundsInfo . lb ( kk ) )
2017-09-15 18:41:41 +02:00
l1 = max ( BoundsInfo . lb ( kk ) , ( 1 - sign ( x ( kk ) ) * ll ) * x ( kk ) ) ; m1 = 0 ; %lower bound
l2 = min ( BoundsInfo . ub ( kk ) , ( 1 + sign ( x ( kk ) ) * ll ) * x ( kk ) ) ; %upper bound
else
%size info for 0 parameter is missing, use prior standard
%deviation
2017-11-22 11:05:12 +01:00
upper_bound = BoundsInfo . lb ( kk ) ;
if isinf ( upper_bound )
upper_bound = - 1e-6 * DynareOptions . huge_number ;
end
lower_bound = BoundsInfo . ub ( kk ) ;
if isinf ( lower_bound )
lower_bound = - 1e-6 * DynareOptions . huge_number ;
end
l1 = max ( lower_bound , - BayesInfo . p2 ( kk ) ) ; m1 = 0 ; %lower bound
l2 = min ( upper_bound , BayesInfo . p2 ( kk ) ) ; %upper bound
2017-09-15 18:41:41 +02:00
end
2013-07-05 23:46:05 +02:00
binding_lower_bound = 0 ;
binding_upper_bound = 0 ;
2014-10-20 16:18:54 +02:00
if isequal ( x ( kk ) , BoundsInfo . lb ( kk ) )
2013-07-05 23:46:05 +02:00
binding_lower_bound = 1 ;
2014-10-20 16:18:54 +02:00
bound_value = BoundsInfo . lb ( kk ) ;
elseif isequal ( x ( kk ) , BoundsInfo . ub ( kk ) )
2013-07-05 23:46:05 +02:00
binding_upper_bound = 1 ;
2014-10-20 16:18:54 +02:00
bound_value = BoundsInfo . ub ( kk ) ;
2017-05-16 15:10:20 +02:00
end
2013-07-05 23:46:05 +02:00
if DynareOptions . mode_check . symmetric_plots && ~ binding_lower_bound && ~ binding_upper_bound
if l2 < ( 1 + ll ) * x ( kk ) %test whether upper bound is too small due to prior binding
l1 = x ( kk ) - ( l2 - x ( kk ) ) ; %adjust lower bound to become closer
2013-06-18 22:30:49 +02:00
m1 = 1 ;
end
2014-10-20 16:18:54 +02:00
if ~ m1 && ( l1 > ( 1 - ll ) * x ( kk ) ) && ( x ( kk ) + ( x ( kk ) - l1 ) < BoundsInfo . ub ( kk ) ) % if lower bound was truncated and using difference from lower bound does not violate upper bound
2013-07-05 23:46:05 +02:00
l2 = x ( kk ) + ( x ( kk ) - l1 ) ; %set upper bound to same distance as lower bound
2013-06-18 22:30:49 +02:00
end
2012-03-05 15:03:55 +01:00
end
2013-06-20 17:19:44 +02:00
z1 = l1 : ( ( x ( kk ) - l1 ) / ( DynareOptions . mode_check . number_of_points / 2 ) ) : x ( kk ) ;
z2 = x ( kk ) : ( ( l2 - x ( kk ) ) / ( DynareOptions . mode_check . number_of_points / 2 ) ) : l2 ;
2012-03-05 15:03:55 +01:00
z = union ( z1 , z2 ) ;
2017-05-16 12:42:01 +02:00
if DynareOptions . mode_check . nolik == 0
2009-12-16 18:17:34 +01:00
y = zeros ( length ( z ) , 2 ) ;
2011-09-22 11:17:31 +02:00
dy = priordens ( xx , BayesInfo . pshape , BayesInfo . p6 , BayesInfo . p7 , BayesInfo . p3 , BayesInfo . p4 ) ;
2009-06-19 10:54:40 +02:00
end
2005-02-18 20:54:39 +01:00
for i = 1 : length ( z )
xx ( kk ) = z ( i ) ;
2016-06-01 15:51:13 +02:00
[ fval , info , exit_flag ] = feval ( fun , xx , DynareDataset , DatasetInfo , DynareOptions , Model , EstimatedParameters , BayesInfo , BoundsInfo , DynareResults ) ;
2011-09-22 11:17:31 +02:00
if exit_flag
y ( i , 1 ) = fval ;
2007-06-22 11:23:45 +02:00
else
2011-09-22 11:17:31 +02:00
y ( i , 1 ) = NaN ;
2016-10-09 22:16:26 +02:00
if DynareOptions . debug
fprintf ( ' mode_check:: could not solve model for parameter %s at value %4.3f, error code: %u\n' , name , z ( i ) , info ( 1 ) )
end
2009-06-19 10:54:40 +02:00
end
2013-06-20 17:19:44 +02:00
if DynareOptions . mode_check . nolik == 0
2011-09-22 11:17:31 +02:00
lnprior = priordens ( xx , BayesInfo . pshape , BayesInfo . p6 , BayesInfo . p7 , BayesInfo . p3 , BayesInfo . p4 ) ;
2009-07-08 12:11:38 +02:00
y ( i , 2 ) = ( y ( i , 1 ) + lnprior - dy ) ;
2009-06-19 10:54:40 +02:00
end
2005-02-18 20:54:39 +01:00
end
2015-07-08 16:05:38 +02:00
mcheck . cross = setfield ( mcheck . cross , name , [ transpose ( z ) , - y ] ) ;
mcheck . emode = setfield ( mcheck . emode , name , x ( kk ) ) ;
2015-04-02 20:52:49 +02:00
fighandle = plot ( z , - y ) ;
2005-02-18 20:54:39 +01:00
hold on
yl = get ( gca , ' ylim' ) ;
plot ( [ x ( kk ) x ( kk ) ] , yl , ' c' , ' LineWidth' , 1 )
2009-07-08 12:11:38 +02:00
NaN_index = find ( isnan ( y ( : , 1 ) ) ) ;
zNaN = z ( NaN_index ) ;
yNaN = yl ( 1 ) * ones ( size ( NaN_index ) ) ;
plot ( zNaN , yNaN , ' o' , ' MarkerEdgeColor' , ' r' , ' MarkerFaceColor' , ' r' , ' MarkerSize' , 6 ) ;
2005-02-18 20:54:39 +01:00
title ( name , ' interpreter' , ' none' )
2009-07-08 12:11:38 +02:00
axis tight
2013-07-05 23:46:05 +02:00
if binding_lower_bound || binding_upper_bound
xl = get ( gca , ' xlim' ) ;
plot ( [ bound_value bound_value ] , yl , ' r--' , ' LineWidth' , 1 )
xlim ( [ xl ( 1 ) - 0.5 * binding_lower_bound * ( xl ( 2 ) - xl ( 1 ) ) xl ( 2 ) + 0.5 * binding_upper_bound * ( xl ( 2 ) - xl ( 1 ) ) ] )
end
hold off
2005-02-18 20:54:39 +01:00
drawnow
end
2017-05-16 12:42:01 +02:00
if DynareOptions . mode_check . nolik == 0
2013-11-04 10:54:45 +01:00
if isoctave
2009-06-19 10:54:40 +02:00
axes ( ' outerposition' , [ 0.3 0.93 0.42 0.07 ] , ' box' , ' on' ) ,
else
axes ( ' position' , [ 0.3 0.01 0.42 0.05 ] , ' box' , ' on' ) ,
end
2015-04-02 20:52:49 +02:00
line_color = get ( fighandle , ' color' ) ;
plot ( [ 0.48 0.68 ] , [ 0.5 0.5 ] , ' color' , line_color { 2 } )
hold on , plot ( [ 0.04 0.24 ] , [ 0.5 0.5 ] , ' color' , line_color { 1 } )
2009-06-19 10:54:40 +02:00
set ( gca , ' xlim' , [ 0 1 ] , ' ylim' , [ 0 1 ] , ' xtick' , [ ] , ' ytick' , [ ] )
text ( 0.25 , 0.5 , ' log-post' )
text ( 0.69 , 0.5 , ' log-lik kernel' )
end
2017-03-23 17:59:05 +01:00
dyn_saveas ( hh , [ Model . fname ' _CheckPlots' int2str ( plt ) ] , DynareOptions . nodisplay , DynareOptions . graph_format ) ;
2015-05-12 08:53:57 +02:00
if TeX && any ( strcmp ( ' eps' , cellstr ( DynareOptions . graph_format ) ) )
2009-06-19 10:54:40 +02:00
% TeX eps loader file
2005-02-18 20:54:39 +01:00
fprintf ( fidTeX , ' \\begin{figure}[H]\n' ) ;
2010-02-04 16:30:19 +01:00
for jj = 1 : min ( nstar , length ( x ) - ( plt - 1 ) * nstar )
2005-02-18 20:54:39 +01:00
fprintf ( fidTeX , ' \\psfrag{%s}[1][][0.5][0]{%s}\n' , deblank ( NAMES ( jj , : ) ) , deblank ( TeXNAMES ( jj , : ) ) ) ;
2009-06-19 10:54:40 +02:00
end
2005-02-18 20:54:39 +01:00
fprintf ( fidTeX , ' \\centering \n' ) ;
2016-06-19 12:38:31 +02:00
fprintf ( fidTeX , ' \\includegraphics[width=%2.2f\\textwidth]{%s_CheckPlots%s}\n' , DynareOptions . figures . textwidth * min ( k / nc , 1 ) , Model . fname , int2str ( plt ) ) ;
2005-02-18 20:54:39 +01:00
fprintf ( fidTeX , ' \\caption{Check plots.}' ) ;
2009-06-19 10:54:40 +02:00
fprintf ( fidTeX , ' \\label{Fig:CheckPlots:%s}\n' , int2str ( plt ) ) ;
2005-02-18 20:54:39 +01:00
fprintf ( fidTeX , ' \\end{figure}\n' ) ;
fprintf ( fidTeX , ' \n' ) ;
end
2015-07-08 16:05:38 +02:00
end
2015-11-28 14:07:36 +01:00
if TeX && any ( strcmp ( ' eps' , cellstr ( DynareOptions . graph_format ) ) )
2017-05-16 15:10:20 +02:00
fclose ( fidTeX ) ;
2015-11-28 14:07:36 +01:00
end
2015-07-08 16:05:38 +02:00
2015-07-08 17:18:27 +02:00
OutputDirectoryName = CheckPath ( ' modecheck' , Model . dname ) ;
2016-09-26 11:00:41 +02:00
save ( [ OutputDirectoryName ' /check_plot_data.mat' ] , ' mcheck' ) ;