Home > matlab > mode_check.m

mode_check

PURPOSE ^

Checks the estimated ML mode or Posterior mode.

SYNOPSIS ^

function mode_check(fun,x,hessian,DynareDataset,DynareOptions,Model,EstimatedParameters,BayesInfo,DynareResults)

DESCRIPTION ^

 Checks the estimated ML mode or Posterior mode.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function mode_check(fun,x,hessian,DynareDataset,DynareOptions,Model,EstimatedParameters,BayesInfo,DynareResults)
0002 % Checks the estimated ML mode or Posterior mode.
0003 
0004 %@info:
0005 %! @deftypefn {Function File} mode_check (@var{fun}, @var{x}, @var{hessian}, @var{DynareDataset}, @var{DynareOptions}, @var{Model}, @var{EstimatedParameters}, @var{BayesInfo}, @var{DynareResults})
0006 %! @anchor{mode_check}
0007 %! @sp 1
0008 %! Checks the estimated ML mode or Posterior mode by plotting sections of the likelihood/posterior kernel.
0009 %! Each plot shows the variation of the likelihood implied by the variations of a single parameter, ceteris paribus)
0010 %! @sp 2
0011 %! @strong{Inputs}
0012 %! @sp 1
0013 %! @table @ @var
0014 %! @item fun
0015 %! Objective function.
0016 %! @item x
0017 %! Estimated mode.
0018 %! @item start
0019 %! Hessian of the objective function at the estimated mode @var{x}.
0020 %! @item DynareDataset
0021 %! Structure specifying the dataset used for estimation (dataset_).
0022 %! @item DynareOptions
0023 %! Structure defining dynare's options (options_).
0024 %! @item Model
0025 %! Structure specifying the (estimated) model (M_).
0026 %! @item EstimatedParameters
0027 %! Structure specifying the estimated parameters (estimated_params_).
0028 %! @item BayesInfo
0029 %! Structure containing information about the priors used for estimation (bayestopt_).
0030 %! @item DynareResults
0031 %! Structure gathering the results (oo_).
0032 %! @end table
0033 %! @sp 2
0034 %! @strong{Outputs}
0035 %! @sp 2
0036 %! @strong{This function is called by:}
0037 %! @sp 2
0038 %! @strong{This function calls:}
0039 %! The objective function (@var{func}).
0040 %! @end deftypefn
0041 %@eod:
0042 
0043 % Copyright (C) 2003-2010, 2012 Dynare Team
0044 %
0045 % This file is part of Dynare.
0046 %
0047 % Dynare is free software: you can redistribute it and/or modify
0048 % it under the terms of the GNU General Public License as published by
0049 % the Free Software Foundation, either version 3 of the License, or
0050 % (at your option) any later version.
0051 %
0052 % Dynare is distributed in the hope that it will be useful,
0053 % but WITHOUT ANY WARRANTY; without even the implied warranty of
0054 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0055 % GNU General Public License for more details.
0056 %
0057 % You should have received a copy of the GNU General Public License
0058 % along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
0059 
0060 TeX = DynareOptions.TeX;
0061 if ~isempty(hessian);
0062     [ s_min, k ] = min(diag(hessian));
0063 end
0064 
0065 fval = feval(fun,x,DynareDataset,DynareOptions,Model,EstimatedParameters,BayesInfo,DynareResults);
0066 
0067 if ~isempty(hessian);
0068     disp(' ')
0069     disp('MODE CHECK')
0070     disp(' ')
0071     disp(sprintf('Fval obtained by the minimization routine: %f', fval))
0072     disp(' ')
0073     if s_min<eps
0074         disp(sprintf('Most negative variance %f for parameter %d (%s = %f)', s_min, k , BayesInfo.name{k}, x(k)))
0075     end
0076 end
0077 
0078 [nbplt,nr,nc,lr,lc,nstar] = pltorg(length(x));
0079 
0080 if TeX
0081     fidTeX = fopen([Model.fname '_CheckPlots.TeX'],'w');
0082     fprintf(fidTeX,'%% TeX eps-loader file generated by mode_check.m (Dynare).\n');
0083     fprintf(fidTeX,['%% ' datestr(now,0) '\n']);
0084     fprintf(fidTeX,' \n');
0085 end
0086 
0087 ll = DynareOptions.mode_check_neighbourhood_size;
0088 
0089 for plt = 1:nbplt,
0090     if TeX
0091         NAMES = [];
0092         TeXNAMES = [];
0093     end
0094     hh = dyn_figure(DynareOptions,'Name','Check plots');
0095     for k=1:min(nstar,length(x)-(plt-1)*nstar)
0096         subplot(nr,nc,k)
0097         kk = (plt-1)*nstar+k;
0098         [name,texname] = get_the_name(kk,TeX,Model,EstimatedParameters,DynareOptions);
0099         if TeX
0100             if isempty(NAMES)
0101                 NAMES = name;
0102                 TeXNAMES = texname;
0103             else
0104                 NAMES = char(NAMES,name);
0105                 TeXNAMES = char(TeXNAMES,texname);
0106             end
0107         end
0108         xx = x;
0109         l1 = max(BayesInfo.lb(kk),(1-ll)*x(kk)); m1 = 0;
0110         l2 = min(BayesInfo.ub(kk),(1+ll)*x(kk));
0111         if l2<(1+ll)*x(kk)
0112             l1 = x(kk) - (l2-x(kk));
0113             m1 = 1;
0114         end
0115         if ~m1 && (l1>(1-ll)*x(kk)) && (x(kk)+(x(kk)-l1)<BayesInfo.ub(kk))
0116             l2 = x(kk) + (x(kk)-l1);
0117         end
0118         z1 = l1:((x(kk)-l1)/10):x(kk);
0119         z2 = x(kk):((l2-x(kk))/10):l2;
0120         z  = union(z1,z2);
0121         if DynareOptions.mode_check_nolik==0,
0122             y = zeros(length(z),2);
0123             dy = priordens(xx,BayesInfo.pshape,BayesInfo.p6,BayesInfo.p7,BayesInfo.p3,BayesInfo.p4);
0124         end
0125         for i=1:length(z)
0126             xx(kk) = z(i);
0127             [fval, exit_flag] = feval(fun,xx,DynareDataset,DynareOptions,Model,EstimatedParameters,BayesInfo,DynareResults);
0128             if exit_flag
0129                 y(i,1) = fval;
0130             else
0131                 y(i,1) = NaN;
0132             end
0133             if DynareOptions.mode_check_nolik==0
0134                 lnprior = priordens(xx,BayesInfo.pshape,BayesInfo.p6,BayesInfo.p7,BayesInfo.p3,BayesInfo.p4);
0135                 y(i,2)  = (y(i,1)+lnprior-dy);
0136             end
0137         end
0138         plot(z,-y);
0139         hold on
0140         yl=get(gca,'ylim');
0141         plot( [x(kk) x(kk)], yl, 'c', 'LineWidth', 1)
0142         NaN_index = find(isnan(y(:,1)));
0143         zNaN = z(NaN_index);
0144         yNaN = yl(1)*ones(size(NaN_index));
0145         plot(zNaN,yNaN,'o','MarkerEdgeColor','r','MarkerFaceColor','r','MarkerSize',6);
0146         title(name,'interpreter','none')
0147         hold off
0148         axis tight
0149         drawnow
0150     end
0151     if DynareOptions.mode_check_nolik==0,
0152         if exist('OCTAVE_VERSION'),
0153             axes('outerposition',[0.3 0.93 0.42 0.07],'box','on'),
0154         else
0155             axes('position',[0.3 0.01 0.42 0.05],'box','on'),
0156         end
0157         plot([0.48 0.68],[0.5 0.5],'color',[0 0.5 0])
0158         hold on, plot([0.04 0.24],[0.5 0.5],'b')
0159         set(gca,'xlim',[0 1],'ylim',[0 1],'xtick',[],'ytick',[])
0160         text(0.25,0.5,'log-post')
0161         text(0.69,0.5,'log-lik kernel')
0162     end
0163     dyn_saveas(hh,[ Model.fname '_CheckPlots' int2str(plt) ],DynareOptions);
0164     if TeX
0165         % TeX eps loader file
0166         fprintf(fidTeX,'\\begin{figure}[H]\n');
0167         for jj = 1:min(nstar,length(x)-(plt-1)*nstar)
0168             fprintf(fidTeX,'\\psfrag{%s}[1][][0.5][0]{%s}\n',deblank(NAMES(jj,:)),deblank(TeXNAMES(jj,:)));
0169         end
0170         fprintf(fidTeX,'\\centering \n');
0171         fprintf(fidTeX,'\\includegraphics[scale=0.5]{%s_CheckPlots%s}\n',Model.fname,int2str(plt));
0172         fprintf(fidTeX,'\\caption{Check plots.}');
0173         fprintf(fidTeX,'\\label{Fig:CheckPlots:%s}\n',int2str(plt));
0174         fprintf(fidTeX,'\\end{figure}\n');
0175         fprintf(fidTeX,' \n');
0176     end
0177 end

Generated on Tue 22-May-2012 02:40:23 by m2html © 2005