mode_check.m 6.49 KB
Newer Older
Marco Ratto's avatar
Marco Ratto committed
1
function mode_check(fun,x,hessian,DynareDataset,DynareOptions,Model,EstimatedParameters,BayesInfo,DynareResults)
2
3
4
% Checks the estimated ML mode or Posterior mode.

%@info:
Stéphane Adjemian's avatar
Stéphane Adjemian committed
5
6
%! @deftypefn {Function File} mode_check (@var{fun}, @var{x}, @var{hessian}, @var{DynareDataset}, @var{DynareOptions}, @var{Model}, @var{EstimatedParameters}, @var{BayesInfo}, @var{DynareResults})
%! @anchor{mode_check}
7
8
9
10
11
12
13
%! @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
Stéphane Adjemian's avatar
Stéphane Adjemian committed
14
15
16
17
%! @item fun
%! Objective function.
%! @item x
%! Estimated mode.
18
%! @item start
Stéphane Adjemian's avatar
Stéphane Adjemian committed
19
20
21
%! Hessian of the objective function at the estimated mode @var{x}.
%! @item DynareDataset
%! Structure specifying the dataset used for estimation (dataset_).
22
%! @item DynareOptions
Stéphane Adjemian's avatar
Stéphane Adjemian committed
23
24
25
26
27
28
29
30
31
%! 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_).
32
33
34
35
36
37
38
%! @end table
%! @sp 2
%! @strong{Outputs}
%! @sp 2
%! @strong{This function is called by:}
%! @sp 2
%! @strong{This function calls:}
Stéphane Adjemian's avatar
Stéphane Adjemian committed
39
%! The objective function (@var{func}).
40
41
42
%! @end deftypefn
%@eod:

43
% Copyright (C) 2003-2013 Dynare Team
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
%
% 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/>.
assia's avatar
assia committed
59

60
TeX = DynareOptions.TeX;
61
62
63
if ~isempty(hessian);
    [ s_min, k ] = min(diag(hessian));
end
64
65

fval = feval(fun,x,DynareDataset,DynareOptions,Model,EstimatedParameters,BayesInfo,DynareResults);
66
67
68
69
70
71
72
73

if ~isempty(hessian);
    disp(' ')
    disp('MODE CHECK')
    disp(' ')
    disp(sprintf('Fval obtained by the minimization routine: %f', fval))
    disp(' ')
    if s_min<eps
74
        disp(sprintf('Most negative variance %f for parameter %d (%s = %f)', s_min, k , BayesInfo.name{k}, x(k)))
75
    end
stepan's avatar
   
stepan committed
76
end
michel's avatar
michel committed
77
78
79
80

[nbplt,nr,nc,lr,lc,nstar] = pltorg(length(x));

if TeX
81
    fidTeX = fopen([Model.fname '_CheckPlots.TeX'],'w');
michel's avatar
michel committed
82
83
84
85
86
    fprintf(fidTeX,'%% TeX eps-loader file generated by mode_check.m (Dynare).\n');
    fprintf(fidTeX,['%% ' datestr(now,0) '\n']);
    fprintf(fidTeX,' \n');
end

87
ll = DynareOptions.mode_check_neighbourhood_size;
88
89
90
if isinf(ll),
    DynareOptions.mode_check_symmetric_plots = 0;
end
91
half_number_nodes = .5*DynareOptions.mode_check_node_number ;
92

93
for plt = 1:nbplt,
michel's avatar
michel committed
94
95
96
97
    if TeX
        NAMES = [];
        TeXNAMES = [];
    end
98
    hh = dyn_figure(DynareOptions,'Name','Mode check plots');
99
100
101
    for k=1:min(nstar,length(x)-(plt-1)*nstar)
        subplot(nr,nc,k)
        kk = (plt-1)*nstar+k;
102
        [name,texname] = get_the_name(kk,TeX,Model,EstimatedParameters,DynareOptions);
michel's avatar
michel committed
103
        if TeX
104
105
106
107
108
109
110
            if isempty(NAMES)
                NAMES = name;
                TeXNAMES = texname;
            else
                NAMES = char(NAMES,name);
                TeXNAMES = char(TeXNAMES,texname);
            end
111
        end
michel's avatar
michel committed
112
        xx = x;
113
114
115
116
117
118
119
120
121
122
        l1 = max(BayesInfo.lb(kk),(1-sign(x(kk))*ll)*x(kk)); m1 = 0;
        l2 = min(BayesInfo.ub(kk),(1+sign(x(kk))*ll)*x(kk));
        if DynareOptions.mode_check_symmetric_plots,
            if l2<(1+ll)*x(kk)
                l1 = x(kk) - (l2-x(kk));
                m1 = 1;
            end
            if ~m1 && (l1>(1-ll)*x(kk)) && (x(kk)+(x(kk)-l1)<BayesInfo.ub(kk))
                l2 = x(kk) + (x(kk)-l1);
            end
123
        end
124
125
        z1 = l1:((x(kk)-l1)/10):x(kk);
        z2 = x(kk):((l2-x(kk))/10):l2;
126
        z  = union(z1,z2);
127
        if DynareOptions.mode_check_nolik==0,
128
            y = zeros(length(z),2);
129
            dy = priordens(xx,BayesInfo.pshape,BayesInfo.p6,BayesInfo.p7,BayesInfo.p3,BayesInfo.p4);
130
        end
michel's avatar
michel committed
131
132
        for i=1:length(z)
            xx(kk) = z(i);
133
            [fval, junk1, junk2, exit_flag] = feval(fun,xx,DynareDataset,DynareOptions,Model,EstimatedParameters,BayesInfo,DynareResults);
134
135
            if exit_flag
                y(i,1) = fval;
adjemian's avatar
adjemian committed
136
            else
137
                y(i,1) = NaN;
138
            end
139
140
            if DynareOptions.mode_check_nolik==0
                lnprior = priordens(xx,BayesInfo.pshape,BayesInfo.p6,BayesInfo.p7,BayesInfo.p3,BayesInfo.p4);
stepan's avatar
   
stepan committed
141
                y(i,2)  = (y(i,1)+lnprior-dy);
142
            end
michel's avatar
michel committed
143
        end
144
        plot(z,-y);
michel's avatar
michel committed
145
146
147
        hold on
        yl=get(gca,'ylim');
        plot( [x(kk) x(kk)], yl, 'c', 'LineWidth', 1)
stepan's avatar
   
stepan committed
148
149
150
151
        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);
michel's avatar
michel committed
152
153
        title(name,'interpreter','none')
        hold off
stepan's avatar
   
stepan committed
154
        axis tight
michel's avatar
michel committed
155
156
        drawnow
    end
157
    if DynareOptions.mode_check_nolik==0,
158
159
160
161
162
163
164
165
166
167
168
        if exist('OCTAVE_VERSION'),
            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
        plot([0.48 0.68],[0.5 0.5],'color',[0 0.5 0])
        hold on, plot([0.04 0.24],[0.5 0.5],'b')
        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
169
    dyn_saveas(hh,[ Model.fname '_CheckPlots' int2str(plt) ],DynareOptions);
michel's avatar
michel committed
170
    if TeX
171
        % TeX eps loader file
michel's avatar
michel committed
172
        fprintf(fidTeX,'\\begin{figure}[H]\n');
173
        for jj = 1:min(nstar,length(x)-(plt-1)*nstar)
michel's avatar
michel committed
174
            fprintf(fidTeX,'\\psfrag{%s}[1][][0.5][0]{%s}\n',deblank(NAMES(jj,:)),deblank(TeXNAMES(jj,:)));
175
        end
michel's avatar
michel committed
176
        fprintf(fidTeX,'\\centering \n');
177
        fprintf(fidTeX,'\\includegraphics[scale=0.5]{%s_CheckPlots%s}\n',Model.fname,int2str(plt));
michel's avatar
michel committed
178
        fprintf(fidTeX,'\\caption{Check plots.}');
179
        fprintf(fidTeX,'\\label{Fig:CheckPlots:%s}\n',int2str(plt));
michel's avatar
michel committed
180
181
182
        fprintf(fidTeX,'\\end{figure}\n');
        fprintf(fidTeX,' \n');
    end
stepan's avatar
   
stepan committed
183
end