2011-10-03 12:19:41 +02:00
|
|
|
function [xparam1, hh, gg, fval, igg] = newrat(func0, x, hh, gg, igg, ftol0, nit, flagg, DynareDataset,DynareOptions,Model,EstimatedParameters,BayesInfo,DynareResults)
|
2009-12-16 18:17:34 +01:00
|
|
|
% [xparam1, hh, gg, fval, igg] = newrat(func0, x, hh, gg, igg, ftol0, nit, flagg, varargin)
|
|
|
|
%
|
|
|
|
% Optimiser with outer product gradient and with sequences of univariate steps
|
|
|
|
% uses Chris Sims subroutine for line search
|
|
|
|
%
|
|
|
|
% func0 = name of the function
|
|
|
|
% there must be a version of the function called [func0,'_hh.m'], that also
|
|
|
|
% gives as second OUTPUT the single contributions at times t=1,...,T
|
|
|
|
% of the log-likelihood to compute outer product gradient
|
|
|
|
%
|
|
|
|
% x = starting guess
|
|
|
|
% hh = initial Hessian [OPTIONAL]
|
|
|
|
% gg = initial gradient [OPTIONAL]
|
|
|
|
% igg = initial inverse Hessian [OPTIONAL]
|
2011-10-03 12:19:41 +02:00
|
|
|
% ftol0 = ending criterion for function change
|
2009-12-16 18:17:34 +01:00
|
|
|
% nit = maximum number of iterations
|
|
|
|
%
|
|
|
|
% In each iteration, Hessian is computed with outer product gradient.
|
|
|
|
% for final Hessian (to start Metropolis):
|
|
|
|
% flagg = 0, final Hessian computed with outer product gradient
|
|
|
|
% flagg = 1, final 'mixed' Hessian: diagonal elements computed with numerical second order derivatives
|
2011-10-03 12:19:41 +02:00
|
|
|
% with correlation structure as from outer product gradient,
|
2009-12-16 18:17:34 +01:00
|
|
|
% flagg = 2, full numerical Hessian
|
|
|
|
%
|
2011-10-03 12:19:41 +02:00
|
|
|
% varargin = list of parameters for func0
|
2009-12-16 18:17:34 +01:00
|
|
|
|
2011-02-04 17:27:33 +01:00
|
|
|
% Copyright (C) 2004-2011 Dynare Team
|
2009-12-16 18:17:34 +01: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/>.
|
|
|
|
|
|
|
|
icount=0;
|
|
|
|
nx=length(x);
|
|
|
|
xparam1=x;
|
|
|
|
%ftol0=1.e-6;
|
2011-11-07 09:21:36 +01:00
|
|
|
htol_base = max(1.e-7, ftol0);
|
2009-12-16 18:17:34 +01:00
|
|
|
flagit=0; % mode of computation of hessian in each iteration
|
|
|
|
ftol=ftol0;
|
|
|
|
gtol=1.e-3;
|
|
|
|
htol=htol_base;
|
|
|
|
htol0=htol_base;
|
2011-10-03 12:19:41 +02:00
|
|
|
gibbstol=length(BayesInfo.pshape)/50; %25;
|
2009-12-16 18:17:34 +01:00
|
|
|
|
2011-10-03 12:19:41 +02:00
|
|
|
func_hh = str2func([func2str(func0),'_hh']);
|
2012-01-08 17:59:33 +01:00
|
|
|
% initialize persistent variable 'penalty' in func_hh()
|
|
|
|
func_hh();
|
2011-10-03 12:19:41 +02:00
|
|
|
fval0=feval(func0,x,DynareDataset,DynareOptions,Model,EstimatedParameters,BayesInfo,DynareResults);
|
2009-12-16 18:17:34 +01:00
|
|
|
fval=fval0;
|
2011-10-03 12:19:41 +02:00
|
|
|
|
2010-04-19 16:40:28 +02:00
|
|
|
% initialize mr_gstep and mr_hessian
|
2011-10-03 12:19:41 +02:00
|
|
|
mr_hessian(1,x,[],[],[],DynareDataset,DynareOptions,Model,EstimatedParameters,BayesInfo,DynareResults);
|
2010-04-19 16:40:28 +02:00
|
|
|
|
2009-12-16 18:17:34 +01:00
|
|
|
if isempty(hh)
|
2011-10-03 12:19:41 +02:00
|
|
|
[dum, gg, htol0, igg, hhg, h1]=mr_hessian(0,x,func_hh,flagit,htol,DynareDataset,DynareOptions,Model,EstimatedParameters,BayesInfo,DynareResults);
|
2009-12-16 18:17:34 +01:00
|
|
|
hh0 = reshape(dum,nx,nx);
|
|
|
|
hh=hhg;
|
2011-10-03 12:19:41 +02:00
|
|
|
if min(eig(hh0))<0
|
2009-12-16 18:17:34 +01:00
|
|
|
hh0=hhg; %generalized_cholesky(hh0);
|
2011-10-03 12:19:41 +02:00
|
|
|
elseif flagit==2
|
2009-12-16 18:17:34 +01:00
|
|
|
hh=hh0;
|
|
|
|
igg=inv(hh);
|
|
|
|
end
|
2011-10-03 12:19:41 +02:00
|
|
|
if htol0>htol
|
2009-12-16 18:17:34 +01:00
|
|
|
htol=htol0;
|
|
|
|
end
|
|
|
|
else
|
|
|
|
hh0=hh;
|
|
|
|
hhg=hh;
|
|
|
|
igg=inv(hh);
|
|
|
|
end
|
2011-02-14 11:51:15 +01:00
|
|
|
H = igg;
|
2009-12-16 18:17:34 +01:00
|
|
|
disp(['Gradient norm ',num2str(norm(gg))])
|
|
|
|
ee=eig(hh);
|
|
|
|
disp(['Minimum Hessian eigenvalue ',num2str(min(ee))])
|
|
|
|
disp(['Maximum Hessian eigenvalue ',num2str(max(ee))])
|
|
|
|
g=gg;
|
|
|
|
check=0;
|
|
|
|
if max(eig(hh))<0, disp('Negative definite Hessian! Local maximum!'), pause, end,
|
2011-01-21 11:28:28 +01:00
|
|
|
save m1.mat x hh g hhg igg fval0
|
2009-12-16 18:17:34 +01:00
|
|
|
|
|
|
|
igrad=1;
|
|
|
|
igibbs=1;
|
|
|
|
inx=eye(nx);
|
|
|
|
jit=0;
|
|
|
|
nig=[];
|
|
|
|
ig=ones(nx,1);
|
|
|
|
ggx=zeros(nx,1);
|
2011-10-03 12:19:41 +02:00
|
|
|
while norm(gg)>gtol && check==0 && jit<nit
|
2009-12-16 18:17:34 +01:00
|
|
|
jit=jit+1;
|
|
|
|
tic
|
|
|
|
icount=icount+1;
|
|
|
|
bayestopt_.penalty = fval0(icount);
|
|
|
|
disp([' '])
|
|
|
|
disp(['Iteration ',num2str(icount)])
|
2011-10-03 12:19:41 +02:00
|
|
|
[fval,x0,fc,retcode] = csminit1(func0,xparam1,fval0(icount),gg,0,H,DynareDataset,DynareOptions,Model,EstimatedParameters,BayesInfo,DynareResults);
|
|
|
|
if igrad
|
|
|
|
[fval1,x01,fc,retcode1] = csminit1(func0,x0,fval,gg,0,inx,DynareDataset,DynareOptions,Model,EstimatedParameters,BayesInfo,DynareResults);
|
|
|
|
if (fval-fval1)>1
|
2009-12-16 18:17:34 +01:00
|
|
|
disp('Gradient step!!')
|
|
|
|
else
|
|
|
|
igrad=0;
|
|
|
|
end
|
|
|
|
fval=fval1;
|
2011-10-03 12:19:41 +02:00
|
|
|
x0=x01;
|
2009-12-16 18:17:34 +01:00
|
|
|
end
|
2011-10-03 12:19:41 +02:00
|
|
|
if (fval0(icount)-fval)<1.e-2*(gg'*(H*gg))/2 && igibbs
|
|
|
|
if length(find(ig))<nx
|
2009-12-16 18:17:34 +01:00
|
|
|
ggx=ggx*0;
|
|
|
|
ggx(find(ig))=gg(find(ig));
|
|
|
|
hhx = reshape(dum,nx,nx);
|
|
|
|
iggx=eye(length(gg));
|
|
|
|
iggx(find(ig),find(ig)) = inv( hhx(find(ig),find(ig)) );
|
2011-10-03 12:19:41 +02:00
|
|
|
[fvala,x0,fc,retcode] = csminit1(func0,x0,fval,ggx,0,iggx,DynareDataset,DynareOptions,Model,EstimatedParameters,BayesInfo,DynareResults);
|
2009-12-16 18:17:34 +01:00
|
|
|
end
|
2011-10-03 12:19:41 +02:00
|
|
|
[fvala, x0, ig] = mr_gstep(h1,x0,func0,htol,DynareDataset,DynareOptions,Model,EstimatedParameters,BayesInfo,DynareResults);
|
2009-12-16 18:17:34 +01:00
|
|
|
nig=[nig ig];
|
2011-03-15 15:27:41 +01:00
|
|
|
disp('Sequence of univariate steps!!')
|
2009-12-16 18:17:34 +01:00
|
|
|
fval=fvala;
|
|
|
|
end
|
2011-10-03 12:19:41 +02:00
|
|
|
if (fval0(icount)-fval)<ftol && flagit==0
|
2009-12-16 18:17:34 +01:00
|
|
|
disp('Try diagonal Hessian')
|
2011-10-03 12:19:41 +02:00
|
|
|
ihh=diag(1./(diag(hhg)));
|
|
|
|
[fval2,x0,fc,retcode2] = csminit1(func0,x0,fval,gg,0,ihh,DynareDataset,DynareOptions,Model,EstimatedParameters,BayesInfo,DynareResults);
|
|
|
|
if (fval-fval2)>=ftol
|
|
|
|
disp('Diagonal Hessian successful')
|
2009-12-16 18:17:34 +01:00
|
|
|
end
|
|
|
|
fval=fval2;
|
2011-10-03 12:19:41 +02:00
|
|
|
end
|
|
|
|
if (fval0(icount)-fval)<ftol && flagit==0
|
2009-12-16 18:17:34 +01:00
|
|
|
disp('Try gradient direction')
|
2011-10-03 12:19:41 +02:00
|
|
|
ihh0=inx.*1.e-4;
|
|
|
|
[fval3,x0,fc,retcode3] = csminit1(func0,x0,fval,gg,0,ihh0,DynareDataset,DynareOptions,Model,EstimatedParameters,BayesInfo,DynareResults);
|
|
|
|
if (fval-fval3)>=ftol
|
|
|
|
disp('Gradient direction successful')
|
2009-12-16 18:17:34 +01:00
|
|
|
end
|
|
|
|
fval=fval3;
|
2011-10-03 12:19:41 +02:00
|
|
|
end
|
2009-12-16 18:17:34 +01:00
|
|
|
xparam1=x0;
|
|
|
|
x(:,icount+1)=xparam1;
|
|
|
|
fval0(icount+1)=fval;
|
2011-10-03 12:19:41 +02:00
|
|
|
if (fval0(icount)-fval)<ftol
|
2009-12-16 18:17:34 +01:00
|
|
|
disp('No further improvement is possible!')
|
|
|
|
check=1;
|
2011-10-03 12:19:41 +02:00
|
|
|
if flagit==2
|
2009-12-16 18:17:34 +01:00
|
|
|
hh=hh0;
|
2011-10-03 12:19:41 +02:00
|
|
|
elseif flagg>0
|
|
|
|
[dum, gg, htol0, igg, hhg,h1]=mr_hessian(0,xparam1,func_hh,flagg,ftol0,DynareDataset,DynareOptions,Model,EstimatedParameters,BayesInfo,DynareResults);
|
|
|
|
if flagg==2
|
2009-12-16 18:17:34 +01:00
|
|
|
hh = reshape(dum,nx,nx);
|
|
|
|
ee=eig(hh);
|
|
|
|
if min(ee)<0
|
|
|
|
hh=hhg;
|
|
|
|
end
|
|
|
|
else
|
|
|
|
hh=hhg;
|
|
|
|
end
|
|
|
|
end
|
|
|
|
disp(['Actual dxnorm ',num2str(norm(x(:,end)-x(:,end-1)))])
|
|
|
|
disp(['FVAL ',num2str(fval)])
|
|
|
|
disp(['Improvement ',num2str(fval0(icount)-fval)])
|
|
|
|
disp(['Ftol ',num2str(ftol)])
|
|
|
|
disp(['Htol ',num2str(htol0)])
|
|
|
|
disp(['Gradient norm ',num2str(norm(gg))])
|
|
|
|
ee=eig(hh);
|
|
|
|
disp(['Minimum Hessian eigenvalue ',num2str(min(ee))])
|
|
|
|
disp(['Maximum Hessian eigenvalue ',num2str(max(ee))])
|
|
|
|
g(:,icount+1)=gg;
|
|
|
|
else
|
|
|
|
df = fval0(icount)-fval;
|
|
|
|
disp(['Actual dxnorm ',num2str(norm(x(:,end)-x(:,end-1)))])
|
|
|
|
disp(['FVAL ',num2str(fval)])
|
|
|
|
disp(['Improvement ',num2str(df)])
|
|
|
|
disp(['Ftol ',num2str(ftol)])
|
|
|
|
disp(['Htol ',num2str(htol0)])
|
2011-03-15 15:27:41 +01:00
|
|
|
htol=htol_base;
|
2011-10-03 12:19:41 +02:00
|
|
|
if norm(x(:,icount)-xparam1)>1.e-12
|
|
|
|
try
|
2011-01-21 11:28:28 +01:00
|
|
|
save m1.mat x fval0 nig -append
|
2009-12-16 18:17:34 +01:00
|
|
|
catch
|
2011-10-03 12:19:41 +02:00
|
|
|
save m1.mat x fval0 nig
|
2009-12-16 18:17:34 +01:00
|
|
|
end
|
2011-10-03 12:19:41 +02:00
|
|
|
[dum, gg, htol0, igg, hhg, h1]=mr_hessian(0,xparam1,func_hh,flagit,htol,DynareDataset,DynareOptions,Model,EstimatedParameters,BayesInfo,DynareResults);
|
|
|
|
if htol0>htol
|
2009-12-16 18:17:34 +01:00
|
|
|
htol=htol0;
|
|
|
|
disp(' ')
|
|
|
|
disp('Numerical noise in the likelihood')
|
|
|
|
disp('Tolerance has to be relaxed')
|
|
|
|
disp(' ')
|
|
|
|
end
|
|
|
|
hh0 = reshape(dum,nx,nx);
|
|
|
|
hh=hhg;
|
2011-10-03 12:19:41 +02:00
|
|
|
if flagit==2
|
|
|
|
if min(eig(hh0))<=0
|
2009-12-16 18:17:34 +01:00
|
|
|
hh0=hhg; %generalized_cholesky(hh0);
|
2011-10-03 12:19:41 +02:00
|
|
|
else
|
2009-12-16 18:17:34 +01:00
|
|
|
hh=hh0;
|
|
|
|
igg=inv(hh);
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
disp(['Gradient norm ',num2str(norm(gg))])
|
|
|
|
ee=eig(hh);
|
|
|
|
disp(['Minimum Hessian eigenvalue ',num2str(min(ee))])
|
|
|
|
disp(['Maximum Hessian eigenvalue ',num2str(max(ee))])
|
|
|
|
if max(eig(hh))<0, disp('Negative definite Hessian! Local maximum!'), pause, end,
|
|
|
|
t=toc;
|
|
|
|
disp(['Elapsed time for iteration ',num2str(t),' s.'])
|
|
|
|
g(:,icount+1)=gg;
|
2011-02-14 11:51:15 +01:00
|
|
|
H = igg;
|
|
|
|
save m1.mat x hh g hhg igg fval0 nig H
|
2009-12-16 18:17:34 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-01-21 11:28:28 +01:00
|
|
|
save m1.mat x hh g hhg igg fval0 nig
|
2011-10-03 12:19:41 +02:00
|
|
|
if ftol>ftol0
|
2009-12-16 18:17:34 +01:00
|
|
|
disp(' ')
|
|
|
|
disp('Numerical noise in the likelihood')
|
|
|
|
disp('Tolerance had to be relaxed')
|
|
|
|
disp(' ')
|
|
|
|
end
|
|
|
|
|
2011-10-03 12:19:41 +02:00
|
|
|
if jit==nit
|
2009-12-16 18:17:34 +01:00
|
|
|
disp(' ')
|
|
|
|
disp('Maximum number of iterations reached')
|
|
|
|
disp(' ')
|
|
|
|
end
|
|
|
|
|
2011-10-03 12:19:41 +02:00
|
|
|
if norm(gg)<=gtol
|
2009-12-16 18:17:34 +01:00
|
|
|
disp(['Estimation ended:'])
|
|
|
|
disp(['Gradient norm < ', num2str(gtol)])
|
|
|
|
end
|
|
|
|
if check==1,
|
|
|
|
disp(['Estimation successful.'])
|
|
|
|
end
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
2011-10-03 12:19:41 +02:00
|
|
|
function f00 = lsearch(lam,func,x,dx,DynareDataset,DynareOptions,Model,EstimatedParameters,BayesInfo,DynareResults)
|
|
|
|
x0=x-dx*lam;
|
|
|
|
f00=feval(func,x0,DynareDataset,DynareOptions,Model,EstimatedParameters,BayesInfo,DynareResults);
|