Home > matlab > newrat.m

newrat

PURPOSE ^

[xparam1, hh, gg, fval, igg] = newrat(func0, x, hh, gg, igg, ftol0, nit, flagg, varargin)

SYNOPSIS ^

function [xparam1, hh, gg, fval, igg] = newrat(func0, x, hh, gg, igg, ftol0, nit, flagg, DynareDataset,DynareOptions,Model,EstimatedParameters,BayesInfo,DynareResults)

DESCRIPTION ^

  [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]
  ftol0 = ending criterion for function change
  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
             with correlation structure as from outer product gradient,
  flagg = 2, full numerical Hessian

  varargin = list of parameters for func0

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [xparam1, hh, gg, fval, igg] = newrat(func0, x, hh, gg, igg, ftol0, nit, flagg, DynareDataset,DynareOptions,Model,EstimatedParameters,BayesInfo,DynareResults)
0002 %  [xparam1, hh, gg, fval, igg] = newrat(func0, x, hh, gg, igg, ftol0, nit, flagg, varargin)
0003 %
0004 %  Optimiser with outer product gradient and with sequences of univariate steps
0005 %  uses Chris Sims subroutine for line search
0006 %
0007 %  func0 = name of the function
0008 %  there must be a version of the function called [func0,'_hh.m'], that also
0009 %  gives as second OUTPUT the single contributions at times t=1,...,T
0010 %    of the log-likelihood to compute outer product gradient
0011 %
0012 %  x = starting guess
0013 %  hh = initial Hessian [OPTIONAL]
0014 %  gg = initial gradient [OPTIONAL]
0015 %  igg = initial inverse Hessian [OPTIONAL]
0016 %  ftol0 = ending criterion for function change
0017 %  nit = maximum number of iterations
0018 %
0019 %  In each iteration, Hessian is computed with outer product gradient.
0020 %  for final Hessian (to start Metropolis):
0021 %  flagg = 0, final Hessian computed with outer product gradient
0022 %  flagg = 1, final 'mixed' Hessian: diagonal elements computed with numerical second order derivatives
0023 %             with correlation structure as from outer product gradient,
0024 %  flagg = 2, full numerical Hessian
0025 %
0026 %  varargin = list of parameters for func0
0027 
0028 % Copyright (C) 2004-2011 Dynare Team
0029 %
0030 % This file is part of Dynare.
0031 %
0032 % Dynare is free software: you can redistribute it and/or modify
0033 % it under the terms of the GNU General Public License as published by
0034 % the Free Software Foundation, either version 3 of the License, or
0035 % (at your option) any later version.
0036 %
0037 % Dynare is distributed in the hope that it will be useful,
0038 % but WITHOUT ANY WARRANTY; without even the implied warranty of
0039 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0040 % GNU General Public License for more details.
0041 %
0042 % You should have received a copy of the GNU General Public License
0043 % along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
0044 
0045 icount=0;
0046 nx=length(x);
0047 xparam1=x;
0048 %ftol0=1.e-6;
0049 htol_base = max(1.e-7, ftol0);
0050 flagit=0;  % mode of computation of hessian in each iteration
0051 ftol=ftol0;
0052 gtol=1.e-3;
0053 htol=htol_base;
0054 htol0=htol_base;
0055 gibbstol=length(BayesInfo.pshape)/50; %25;
0056 
0057 % func0 = str2func([func2str(func0),'_hh']);
0058 % func0 = func0;
0059 fval0=feval(func0,x,DynareDataset,DynareOptions,Model,EstimatedParameters,BayesInfo,DynareResults);
0060 fval=fval0;
0061 
0062 % initialize mr_gstep and mr_hessian
0063 mr_hessian(1,x,[],[],[],DynareDataset,DynareOptions,Model,EstimatedParameters,BayesInfo,DynareResults);
0064 
0065 if isempty(hh)
0066     [dum, gg, htol0, igg, hhg, h1]=mr_hessian(0,x,func0,flagit,htol,DynareDataset,DynareOptions,Model,EstimatedParameters,BayesInfo,DynareResults);
0067     hh0 = reshape(dum,nx,nx);
0068     hh=hhg;
0069     if min(eig(hh0))<0
0070         hh0=hhg; %generalized_cholesky(hh0);
0071     elseif flagit==2
0072         hh=hh0;
0073         igg=inv(hh);
0074     end
0075     if htol0>htol
0076         htol=htol0;
0077     end
0078 else
0079     hh0=hh;
0080     hhg=hh;
0081     igg=inv(hh);
0082 end
0083 H = igg;
0084 disp(['Gradient norm ',num2str(norm(gg))])
0085 ee=eig(hh);
0086 disp(['Minimum Hessian eigenvalue ',num2str(min(ee))])
0087 disp(['Maximum Hessian eigenvalue ',num2str(max(ee))])
0088 g=gg;
0089 check=0;
0090 if max(eig(hh))<0, disp('Negative definite Hessian! Local maximum!'), pause, end,
0091 save m1.mat x hh g hhg igg fval0
0092 
0093 igrad=1;
0094 igibbs=1;
0095 inx=eye(nx);
0096 jit=0;
0097 nig=[];
0098 ig=ones(nx,1);
0099 ggx=zeros(nx,1);
0100 while norm(gg)>gtol && check==0 && jit<nit
0101     jit=jit+1;
0102     tic
0103     icount=icount+1;
0104     bayestopt_.penalty = fval0(icount);
0105     disp([' '])
0106     disp(['Iteration ',num2str(icount)])
0107     [fval,x0,fc,retcode] = csminit1(func0,xparam1,fval0(icount),gg,0,H,DynareDataset,DynareOptions,Model,EstimatedParameters,BayesInfo,DynareResults);
0108     if igrad
0109         [fval1,x01,fc,retcode1] = csminit1(func0,x0,fval,gg,0,inx,DynareDataset,DynareOptions,Model,EstimatedParameters,BayesInfo,DynareResults);
0110         if (fval-fval1)>1
0111             disp('Gradient step!!')
0112         else
0113             igrad=0;
0114         end
0115         fval=fval1;
0116         x0=x01;
0117     end
0118     if (fval0(icount)-fval)<1.e-2*(gg'*(H*gg))/2 && igibbs
0119         if length(find(ig))<nx
0120             ggx=ggx*0;
0121             ggx(find(ig))=gg(find(ig));
0122             hhx = reshape(dum,nx,nx);
0123             iggx=eye(length(gg));
0124             iggx(find(ig),find(ig)) = inv( hhx(find(ig),find(ig)) );
0125             [fvala,x0,fc,retcode] = csminit1(func0,x0,fval,ggx,0,iggx,DynareDataset,DynareOptions,Model,EstimatedParameters,BayesInfo,DynareResults);
0126         end
0127         [fvala, x0, ig] = mr_gstep(h1,x0,func0,htol,DynareDataset,DynareOptions,Model,EstimatedParameters,BayesInfo,DynareResults);
0128         nig=[nig ig];
0129         disp('Sequence of univariate steps!!')
0130         fval=fvala;
0131     end
0132     if (fval0(icount)-fval)<ftol && flagit==0
0133         disp('Try diagonal Hessian')
0134         ihh=diag(1./(diag(hhg)));
0135         [fval2,x0,fc,retcode2] = csminit1(func0,x0,fval,gg,0,ihh,DynareDataset,DynareOptions,Model,EstimatedParameters,BayesInfo,DynareResults);
0136         if (fval-fval2)>=ftol
0137             disp('Diagonal Hessian successful')
0138         end
0139         fval=fval2;
0140     end
0141     if (fval0(icount)-fval)<ftol && flagit==0
0142         disp('Try gradient direction')
0143         ihh0=inx.*1.e-4;
0144         [fval3,x0,fc,retcode3] = csminit1(func0,x0,fval,gg,0,ihh0,DynareDataset,DynareOptions,Model,EstimatedParameters,BayesInfo,DynareResults);
0145         if (fval-fval3)>=ftol
0146             disp('Gradient direction successful')
0147         end
0148         fval=fval3;
0149     end
0150     xparam1=x0;
0151     x(:,icount+1)=xparam1;
0152     fval0(icount+1)=fval;
0153     if (fval0(icount)-fval)<ftol
0154         disp('No further improvement is possible!')
0155         check=1;
0156         if flagit==2
0157             hh=hh0;
0158         elseif flagg>0
0159             [dum, gg, htol0, igg, hhg,h1]=mr_hessian(0,xparam1,func0,flagg,ftol0,DynareDataset,DynareOptions,Model,EstimatedParameters,BayesInfo,DynareResults);
0160             if flagg==2
0161                 hh = reshape(dum,nx,nx);
0162                 ee=eig(hh);
0163                 if min(ee)<0
0164                     hh=hhg;
0165                 end
0166             else
0167                 hh=hhg;
0168             end
0169         end
0170         disp(['Actual dxnorm ',num2str(norm(x(:,end)-x(:,end-1)))])
0171         disp(['FVAL          ',num2str(fval)])
0172         disp(['Improvement   ',num2str(fval0(icount)-fval)])
0173         disp(['Ftol          ',num2str(ftol)])
0174         disp(['Htol          ',num2str(htol0)])
0175         disp(['Gradient norm  ',num2str(norm(gg))])
0176         ee=eig(hh);
0177         disp(['Minimum Hessian eigenvalue ',num2str(min(ee))])
0178         disp(['Maximum Hessian eigenvalue ',num2str(max(ee))])
0179         g(:,icount+1)=gg;
0180     else
0181         df = fval0(icount)-fval;
0182         disp(['Actual dxnorm ',num2str(norm(x(:,end)-x(:,end-1)))])
0183         disp(['FVAL          ',num2str(fval)])
0184         disp(['Improvement   ',num2str(df)])
0185         disp(['Ftol          ',num2str(ftol)])
0186         disp(['Htol          ',num2str(htol0)])
0187         htol=htol_base;
0188         if norm(x(:,icount)-xparam1)>1.e-12
0189             try
0190                 save m1.mat x fval0 nig -append
0191             catch
0192                 save m1.mat x fval0 nig
0193             end
0194             [dum, gg, htol0, igg, hhg, h1]=mr_hessian(0,xparam1,func0,flagit,htol,DynareDataset,DynareOptions,Model,EstimatedParameters,BayesInfo,DynareResults);
0195             if htol0>htol
0196                 htol=htol0;
0197                 disp(' ')
0198                 disp('Numerical noise in the likelihood')
0199                 disp('Tolerance has to be relaxed')
0200                 disp(' ')
0201             end
0202             hh0 = reshape(dum,nx,nx);
0203             hh=hhg;
0204             if flagit==2
0205                 if min(eig(hh0))<=0
0206                     hh0=hhg; %generalized_cholesky(hh0);
0207                 else
0208                     hh=hh0;
0209                     igg=inv(hh);
0210                 end
0211             end
0212         end
0213         disp(['Gradient norm  ',num2str(norm(gg))])
0214         ee=eig(hh);
0215         disp(['Minimum Hessian eigenvalue ',num2str(min(ee))])
0216         disp(['Maximum Hessian eigenvalue ',num2str(max(ee))])
0217         if max(eig(hh))<0, disp('Negative definite Hessian! Local maximum!'), pause, end,
0218         t=toc;
0219         disp(['Elapsed time for iteration ',num2str(t),' s.'])
0220         g(:,icount+1)=gg;
0221         H = igg;
0222         save m1.mat x hh g hhg igg fval0 nig H
0223     end
0224 end
0225 
0226 save m1.mat x hh g hhg igg fval0 nig
0227 if ftol>ftol0
0228     disp(' ')
0229     disp('Numerical noise in the likelihood')
0230     disp('Tolerance had to be relaxed')
0231     disp(' ')
0232 end
0233 
0234 if jit==nit
0235     disp(' ')
0236     disp('Maximum number of iterations reached')
0237     disp(' ')
0238 end
0239 
0240 if norm(gg)<=gtol
0241     disp(['Estimation ended:'])
0242     disp(['Gradient norm < ', num2str(gtol)])
0243 end
0244 if check==1,
0245     disp(['Estimation successful.'])
0246 end
0247 
0248 return
0249 
0250

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