0001 function [xparam1, hh, gg, fval, igg] = newrat(func0, x, hh, gg, igg, ftol0, nit, flagg, varargin)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032 global bayestopt_
0033 icount=0;
0034 nx=length(x);
0035 xparam1=x;
0036
0037 htol_base = max(1.e-5, ftol0);
0038 flagit=0;
0039 ftol=ftol0;
0040 gtol=1.e-3;
0041 htol=htol_base;
0042 htol0=htol_base;
0043 gibbstol=length(bayestopt_.pshape)/12;
0044
0045 func_hh = [func0,'_hh'];
0046 func = str2func(func0);
0047 fval0=feval(func,x,varargin{:});
0048 fval=fval0;
0049 if isempty(hh)
0050 [dum, gg, htol0, igg, hhg]=mr_hessian(func_hh,x,flagit,htol,varargin{:});
0051 hh0 = reshape(dum,nx,nx);
0052 hh=hhg;
0053 if min(eig(hh0))<0,
0054 hh0=hhg;
0055 elseif flagit==2,
0056 hh=hh0;
0057 igg=inv(hh);
0058 end
0059 if htol0>htol,
0060 htol=htol0;
0061
0062 end
0063 else
0064 hh0=hh;
0065 hhg=hh;
0066 igg=inv(hh);
0067 end
0068 disp(['Gradient norm ',num2str(norm(gg))])
0069 ee=eig(hh);
0070 disp(['Minimum Hessian eigenvalue ',num2str(min(ee))])
0071 disp(['Maximum Hessian eigenvalue ',num2str(max(ee))])
0072 g=gg;
0073 check=0;
0074 if max(eig(hh))<0, disp('Negative definite Hessian! Local maximum!'), pause, end,
0075 save m1 x hh g hhg igg fval0
0076
0077 igrad=1;
0078 igibbs=1;
0079 inx=eye(nx);
0080 jit=0;
0081 while norm(gg)>gtol & check==0 & jit<nit,
0082 jit=jit+1;
0083 tic
0084 icount=icount+1;
0085 bayestopt_.penalty = fval0(icount);
0086 disp([' '])
0087 disp(['Iteration ',num2str(icount)])
0088 [fval x0 fc retcode] = csminit(func0,xparam1,fval0(icount),gg,0,igg,varargin{:});
0089 if igrad,
0090 [fval1 x01 fc retcode1] = csminit(func0,x0,fval,gg,0,inx,varargin{:});
0091 if (fval-fval1)>1,
0092 disp('Gradient step!!')
0093 else
0094 igrad=0;
0095 end
0096 fval=fval1;
0097 x0=x01;
0098 end
0099 if (fval0(icount)-fval)<1.e-2*(gg'*(igg*gg))/2 & igibbs,
0100 [fvala, x0] = mr_gstep(func0,x0,htol,varargin{:});
0101 if (fval-fvala)<gibbstol*(fval0(icount)-fval),
0102 igibbs=0;
0103 disp('Last Gibbs step, gain too small!!')
0104 else
0105 disp('Gibbs step!!')
0106 end
0107 fval=fvala;
0108 end
0109 if (fval0(icount)-fval)<ftol & flagit==0,
0110 disp('Try diagonal Hessian')
0111 ihh=diag(1./(diag(hhg)));
0112 [fval2 x0 fc retcode2] = csminit(func2str(func),x0,fval,gg,0,ihh,varargin{:});
0113 if (fval-fval2)>=ftol ,
0114
0115 disp('Diagonal Hessian successful')
0116 end
0117 fval=fval2;
0118 end
0119 if (fval0(icount)-fval)<ftol & flagit==0,
0120 disp('Try gradient direction')
0121 ihh0=inx.*1.e-4;
0122 [fval3 x0 fc retcode3] = csminit(func2str(func),x0,fval,gg,0,ihh0,varargin{:});
0123 if (fval-fval3)>=ftol ,
0124
0125
0126 disp('Gradient direction successful')
0127 end
0128 fval=fval3;
0129 end
0130 xparam1=x0;
0131 x(:,icount+1)=xparam1;
0132 fval0(icount+1)=fval;
0133
0134 if (fval0(icount)-fval)<ftol,
0135 disp('No further improvement is possible!')
0136 check=1;
0137 if flagit==2,
0138 hh=hh0;
0139 elseif flagg>0,
0140 [dum, gg, htol0, igg, hhg]=mr_hessian(func_hh,xparam1,flagg,ftol0,varargin{:});
0141 if flagg==2,
0142 hh = reshape(dum,nx,nx);
0143 ee=eig(hh);
0144 if min(ee)<0
0145 hh=hhg;
0146 end
0147 else
0148 hh=hhg;
0149 end
0150 end
0151 disp(['Actual dxnorm ',num2str(norm(x(:,end)-x(:,end-1)))])
0152 disp(['FVAL ',num2str(fval)])
0153 disp(['Improvement ',num2str(fval0(icount)-fval)])
0154 disp(['Ftol ',num2str(ftol)])
0155 disp(['Htol ',num2str(htol0)])
0156 disp(['Gradient norm ',num2str(norm(gg))])
0157 ee=eig(hh);
0158 disp(['Minimum Hessian eigenvalue ',num2str(min(ee))])
0159 disp(['Maximum Hessian eigenvalue ',num2str(max(ee))])
0160 g(:,icount+1)=gg;
0161 else
0162
0163 df = fval0(icount)-fval;
0164 disp(['Actual dxnorm ',num2str(norm(x(:,end)-x(:,end-1)))])
0165 disp(['FVAL ',num2str(fval)])
0166 disp(['Improvement ',num2str(df)])
0167 disp(['Ftol ',num2str(ftol)])
0168 disp(['Htol ',num2str(htol0)])
0169
0170 if df<htol0,
0171 htol=max(htol_base,df/10);
0172 end
0173
0174 if norm(x(:,icount)-xparam1)>1.e-12,
0175 save m1 x fval0 -append
0176 [dum, gg, htol0, igg, hhg]=mr_hessian(func_hh,xparam1,flagit,htol,varargin{:});
0177 if htol0>htol,
0178
0179 htol=htol0;
0180 disp(' ')
0181 disp('Numerical noise in the likelihood')
0182 disp('Tolerance has to be relaxed')
0183 disp(' ')
0184
0185
0186 end
0187 hh0 = reshape(dum,nx,nx);
0188 hh=hhg;
0189 if flagit==2,
0190 if min(eig(hh0))<=0,
0191 hh0=hhg;
0192 else
0193 hh=hh0;
0194 igg=inv(hh);
0195 end
0196 end
0197 end
0198 disp(['Gradient norm ',num2str(norm(gg))])
0199 ee=eig(hh);
0200 disp(['Minimum Hessian eigenvalue ',num2str(min(ee))])
0201 disp(['Maximum Hessian eigenvalue ',num2str(max(ee))])
0202 if max(eig(hh))<0, disp('Negative definite Hessian! Local maximum!'), pause, end,
0203 t=toc;
0204 disp(['Elapsed time for iteration ',num2str(t),' s.'])
0205
0206 g(:,icount+1)=gg;
0207 save m1 x hh g hhg igg fval0
0208 end
0209 end
0210
0211 save m1 x hh g hhg igg fval0
0212 if ftol>ftol0,
0213 disp(' ')
0214 disp('Numerical noise in the likelihood')
0215 disp('Tolerance had to be relaxed')
0216 disp(' ')
0217 end
0218
0219 if jit==nit,
0220 disp(' ')
0221 disp('Maximum number of iterations reached')
0222 disp(' ')
0223 end
0224
0225 if norm(gg)<=gtol,
0226 disp(['Estimation ended:'])
0227 disp(['Gradient norm < ', num2str(gtol)])
0228 end
0229 if check==1,
0230 disp(['Estimation successful.'])
0231 end
0232
0233 return
0234
0235
0236 function f00 = lsearch(lam,func,x,dx,varargin)
0237
0238
0239 x0=x-dx*lam;
0240 f00=feval(func,x0,varargin{:});
0241
0242
0243
0244
0245
0246