File: mr_hessian.m

package info (click to toggle)
dynare 4.5.7-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 49,408 kB
  • sloc: cpp: 84,998; ansic: 29,058; pascal: 13,843; sh: 4,833; objc: 4,236; yacc: 3,622; makefile: 2,278; lex: 1,541; python: 236; lisp: 69; xml: 8
file content (275 lines) | stat: -rw-r--r-- 10,793 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
function [hessian_mat, gg, htol1, ihh, hh_mat0, hh1, hess_info] = mr_hessian(x,func,penalty,hflag,htol0,hess_info,varargin)
% function [hessian_mat, gg, htol1, ihh, hh_mat0, hh1, hess_info] = mr_hessian(x,func,penalty,hflag,htol0,hess_info,varargin)
%  numerical gradient and Hessian, with 'automatic' check of numerical
%  error
%
% adapted from Michel Juillard original routine hessian.m
%
% Inputs:
%  - func               function handle. The function must give two outputs:
%                       the log-likelihood AND the single contributions at times t=1,...,T
%                       of the log-likelihood to compute outer product gradient
%  - x                  parameter values
%  - penalty            penalty due to error code
%  - hflag              0: Hessian computed with outer product gradient, one point
%                           increments for partial derivatives in gradients
%                       1: 'mixed' Hessian: diagonal elements computed with numerical second order derivatives
%                           with correlation structure as from outer product gradient;
%                           two point evaluation of derivatives for partial derivatives
%                           in gradients
%                       2: full numerical Hessian, computes second order partial derivatives
%                           uses Abramowitz and Stegun (1965) formulas 25.3.24 and 25.3.27
%                           p. 884.
%  - htol0              'precision' of increment of function values for numerical
%                       derivatives
%  - hess_info              structure storing the step sizes for
%                           computation of Hessian
%  - varargin               other inputs:
%                           varargin{1} --> DynareDataset
%                           varargin{2} --> DatasetInfo
%                           varargin{3} --> DynareOptions
%                           varargin{4} --> Model
%                           varargin{5} --> EstimatedParameters
%                           varargin{6} --> BayesInfo
%                           varargin{7} --> Bounds
%                           varargin{8} --> DynareResults
%
% Outputs
%  - hessian_mat        hessian
%  - gg                 Jacobian
%  - htol1              updated 'precision' of increment of function values for numerical
%                       derivatives
%  - ihh                inverse outer product with modified std's
%  - hh_mat0            outer product hessian with modified std's
%  - hh1                updated hess_info.h1
%  - hess_info          structure with updated step length

% Copyright (C) 2004-2017 Dynare Team
%
% 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/>.

n=size(x,1);

[f0,exit_flag, ff0]=penalty_objective_function(x,func,penalty,varargin{:});
h2=varargin{7}.ub-varargin{7}.lb;
hmax=varargin{7}.ub-x;
hmax=min(hmax,x-varargin{7}.lb);
if isempty(ff0)
    outer_product_gradient=0;
else
    outer_product_gradient=1;
end


hess_info.h1 = min(hess_info.h1,0.5.*hmax);

if htol0<hess_info.htol
    hess_info.htol=htol0;
end
xh1=x;
f1=zeros(size(f0,1),n);
f_1=f1;
if outer_product_gradient
    ff1=zeros(size(ff0));
    ff_1=ff1;
    ggh=zeros(size(ff0,1),n);
end

i=0;
hhtol=hess_info.htol*ones(n,1);
while i<n
    i=i+1;
    hess_info.htol=hhtol(i);
    h10=hess_info.h1(i);
    hcheck=0;
    xh1(i)=x(i)+hess_info.h1(i);
    try
        [fx,exit_flag,ffx]=penalty_objective_function(xh1,func,penalty,varargin{:});
    catch
        fx=1.e8;
    end
    it=1;
    dx=(fx-f0);
    ic=0;
    icount = 0;
    h0=hess_info.h1(i);
    while (abs(dx(it))<0.5*hess_info.htol || abs(dx(it))>(3*hess_info.htol)) && icount<10 && ic==0
        icount=icount+1;
        if abs(dx(it))<0.5*hess_info.htol
            if abs(dx(it)) ~= 0
                hess_info.h1(i)=min(max(1.e-10,0.3*abs(x(i))), 0.9*hess_info.htol/abs(dx(it))*hess_info.h1(i));
            else
                hess_info.h1(i)=2.1*hess_info.h1(i);
            end
            hess_info.h1(i) = min(hess_info.h1(i),0.5*hmax(i));
            hess_info.h1(i) = max(hess_info.h1(i),1.e-10);
            xh1(i)=x(i)+hess_info.h1(i);
            try
                [fx,exit_flag,ffx]=penalty_objective_function(xh1,func,penalty,varargin{:});
            catch
                fx=1.e8;
            end
        end
        if abs(dx(it))>(3*hess_info.htol)
            hess_info.h1(i)= hess_info.htol/abs(dx(it))*hess_info.h1(i);
            hess_info.h1(i) = max(hess_info.h1(i),1e-10);
            xh1(i)=x(i)+hess_info.h1(i);
            try
                [fx,exit_flag,ffx]=penalty_objective_function(xh1,func,penalty,varargin{:});
            catch
                fx=1.e8;
            end
            iter=0;
            while (fx-f0)==0 && iter<50
                hess_info.h1(i)= hess_info.h1(i)*2;
                xh1(i)=x(i)+hess_info.h1(i);
                [fx,exit_flag,ffx]=penalty_objective_function(xh1,func,penalty,varargin{:});
                ic=1;
                iter=iter+1;
            end
        end
        it=it+1;
        dx(it)=(fx-f0);
        h0(it)=hess_info.h1(i);
        if (hess_info.h1(i)<1.e-12*min(1,h2(i)) && hess_info.h1(i)<0.5*hmax(i))
            ic=1;
            hcheck=1;
        end
    end
    f1(:,i)=fx;
    if outer_product_gradient
        if any(isnan(ffx)) || isempty(ffx)
            ff1=ones(size(ff0)).*fx/length(ff0);
        else
            ff1=ffx;
        end
    end
    xh1(i)=x(i)-hess_info.h1(i);
    [fx,exit_flag,ffx]=penalty_objective_function(xh1,func,penalty,varargin{:});
    f_1(:,i)=fx;
    if outer_product_gradient
        if any(isnan(ffx)) || isempty(ffx)
            ff_1=ones(size(ff0)).*fx/length(ff0);
        else
            ff_1=ffx;
        end
        ggh(:,i)=(ff1-ff_1)./(2.*hess_info.h1(i));
    end
    xh1(i)=x(i);
    if hcheck && hess_info.htol<1
        hess_info.htol=min(1,max(min(abs(dx))*2,hess_info.htol*10));
        hess_info.h1(i)=h10;
        hhtol(i) = hess_info.htol;
        i=i-1;
    end
end

h_1=hess_info.h1;
xh1=x;
xh_1=xh1;

gg=(f1'-f_1')./(2.*hess_info.h1);

if outer_product_gradient
    if hflag==2
        gg=(f1'-f_1')./(2.*hess_info.h1);
        hessian_mat = zeros(size(f0,1),n*n);
        for i=1:n
            if i > 1
                k=[i:n:n*(i-1)];
                hessian_mat(:,(i-1)*n+1:(i-1)*n+i-1)=hessian_mat(:,k);
            end
            hessian_mat(:,(i-1)*n+i)=(f1(:,i)+f_1(:,i)-2*f0)./(hess_info.h1(i)*h_1(i));
            temp=f1+f_1-f0*ones(1,n);
            for j=i+1:n
                xh1(i)=x(i)+hess_info.h1(i);
                xh1(j)=x(j)+h_1(j);
                xh_1(i)=x(i)-hess_info.h1(i);
                xh_1(j)=x(j)-h_1(j);
                temp1 = penalty_objective_function(xh1,func,penalty,varargin{:});
                temp2 = penalty_objective_function(xh_1,func,penalty,varargin{:});
                hessian_mat(:,(i-1)*n+j)=-(-temp1 -temp2+temp(:,i)+temp(:,j))./(2*hess_info.h1(i)*h_1(j));
                xh1(i)=x(i);
                xh1(j)=x(j);
                xh_1(i)=x(i);
                xh_1(j)=x(j);
                j=j+1;
            end
            i=i+1;
        end
    elseif hflag==1
        hessian_mat = zeros(size(f0,1),n*n);
        for i=1:n
            dum = (f1(:,i)+f_1(:,i)-2*f0)./(hess_info.h1(i)*h_1(i));
            if dum>eps
                hessian_mat(:,(i-1)*n+i)=dum;
            else
                hessian_mat(:,(i-1)*n+i)=max(eps, gg(i)^2);
            end
        end
    end

    gga=ggh.*kron(ones(size(ff1)),2.*hess_info.h1');  % re-scaled gradient
    hh_mat=gga'*gga;  % rescaled outer product hessian
    hh_mat0=ggh'*ggh;  % outer product hessian
    A=diag(2.*hess_info.h1);  % rescaling matrix
                              % igg=inv(hh_mat);  % inverted rescaled outer product hessian
    ihh=A'*(hh_mat\A);  % inverted outer product hessian
    if hflag>0 && min(eig(reshape(hessian_mat,n,n)))>0
        hh0 = A*reshape(hessian_mat,n,n)*A';  %rescaled second order derivatives
        hh = reshape(hessian_mat,n,n);  %rescaled second order derivatives
        sd0=sqrt(diag(hh0));   %rescaled 'standard errors' using second order derivatives
        sd=sqrt(diag(hh_mat));  %rescaled 'standard errors' using outer product
        hh_mat=hh_mat./(sd*sd').*(sd0*sd0');  %rescaled inverse outer product with 'true' std's
        igg=inv(hh_mat);   % rescaled outer product hessian with 'true' std's
        ihh=A'*(hh_mat\A);  % inverted outer product hessian
        hh_mat0=inv(A)'*hh_mat*inv(A);  % outer product hessian with 'true' std's
        sd=sqrt(diag(ihh));   %standard errors
        sdh=sqrt(1./diag(hh));   %diagonal standard errors
        for j=1:length(sd)
            sd0(j,1)=min(varargin{6}.p2(j), sd(j));  %prior std
            sd0(j,1)=10^(0.5*(log10(sd0(j,1))+log10(sdh(j,1))));
        end
        ihh=ihh./(sd*sd').*(sd0*sd0');  %inverse outer product with modified std's
        igg=inv(A)'*ihh*inv(A);  % inverted rescaled outer product hessian with modified std's
        hh_mat=inv(igg);   % outer product rescaled hessian with modified std's
        hh_mat0=inv(A)'*hh_mat*inv(A);  % outer product hessian with modified std's
                                        %     sd0=sqrt(1./diag(hh0));   %rescaled 'standard errors' using second order derivatives
                                        %     sd=sqrt(diag(igg));  %rescaled 'standard errors' using outer product
                                        %     igg=igg./(sd*sd').*(sd0*sd0');  %rescaled inverse outer product with 'true' std's
                                        %     hh_mat=inv(igg);   % rescaled outer product hessian with 'true' std's
                                        %     ihh=A'*igg*A;  % inverted outer product hessian
                                        %     hh_mat0=inv(A)'*hh_mat*inv(A);  % outer product hessian with 'true' std's
    end
    if hflag<2
        hessian_mat=hh_mat0(:);
    end

    if any(isnan(hessian_mat))
        hh_mat0=eye(length(hh_mat0));
        ihh=hh_mat0;
        hessian_mat=hh_mat0(:);
    end
    hh1=hess_info.h1;
    save hess.mat hessian_mat
else
    hessian_mat=[];
    ihh=[];
    hh_mat0 = [];
    hh1 = [];
end

htol1=hhtol;