321 lines
18 KiB
HTML
321 lines
18 KiB
HTML
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
|
||
|
"http://www.w3.org/TR/REC-html40/loose.dtd">
|
||
|
<html>
|
||
|
<head>
|
||
|
<title>Description of newrat</title>
|
||
|
<meta name="keywords" content="newrat">
|
||
|
<meta name="description" content="">
|
||
|
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
||
|
<meta name="generator" content="m2html © 2003 Guillaume Flandin">
|
||
|
<meta name="robots" content="index, follow">
|
||
|
<link type="text/css" rel="stylesheet" href="../m2html.css">
|
||
|
</head>
|
||
|
<body>
|
||
|
<a name="_top"></a>
|
||
|
<div><a href="../index.html">Home</a> > <a href="index.html">.</a> > newrat.m</div>
|
||
|
|
||
|
<!--<table width="100%"><tr><td align="left"><a href="../index.html"><img alt="<" border="0" src="../left.png"> Master index</a></td>
|
||
|
<td align="right"><a href="index.html">Index for . <img alt=">" border="0" src="../right.png"></a></td></tr></table>-->
|
||
|
|
||
|
<h1>newrat
|
||
|
</h1>
|
||
|
|
||
|
<h2><a name="_name"></a>PURPOSE <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
|
||
|
<div class="box"><strong></strong></div>
|
||
|
|
||
|
<h2><a name="_synopsis"></a>SYNOPSIS <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
|
||
|
<div class="box"><strong>function [xparam1, hh, gg, fval, igg] = newrat(func0, x, hh, gg, igg, ftol0, nit, flagg, varargin) </strong></div>
|
||
|
|
||
|
<h2><a name="_description"></a>DESCRIPTION <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
|
||
|
<div class="fragment"><pre class="comment">
|
||
|
Copyright (C) 2004 Marco Ratto
|
||
|
|
||
|
[xparam1, hh, gg, fval, igg] = newrat(func0, x, hh, gg, igg, ftol0, nit, flagg, varargin)
|
||
|
|
||
|
Optimiser with outer product gradient and 'Gibbs type' 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</pre></div>
|
||
|
|
||
|
<!-- crossreference -->
|
||
|
<h2><a name="_cross"></a>CROSS-REFERENCE INFORMATION <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
|
||
|
This function calls:
|
||
|
<ul style="list-style-image:url(../matlabicon.gif)">
|
||
|
<li><a href="check.html" class="code" title="function result = check">check</a> Copyright (C) 2001 Michel Juillard</li><li><a href="csminit.html" class="code" title="function [fhat,xhat,fcount,retcode] = csminit(fcn,x0,f0,g0,badg,H0,varargin)">csminit</a> [fhat,xhat,fcount,retcode] = csminit(fcn,x0,f0,g0,badg,H0,...</li><li><a href="mr_gstep.html" class="code" title="function [f0, x] = mr_gstep(func0,x,htol0,varargin)">mr_gstep</a> Copyright (C) 2005 Marco Ratto</li><li><a href="mr_hessian.html" class="code" title="function [hessian_mat, gg, htol1, ihh, hh_mat0] = mr_hessian(func,x,hflag,htol0,varargin)">mr_hessian</a> Copyright (C) 2004 Marco Ratto</li></ul>
|
||
|
This function is called by:
|
||
|
<ul style="list-style-image:url(../matlabicon.gif)">
|
||
|
<li><a href="dynare_estimation.html" class="code" title="function dynare_estimation(var_list_)">dynare_estimation</a> </li></ul>
|
||
|
<!-- crossreference -->
|
||
|
|
||
|
<h2><a name="_subfunctions"></a>SUBFUNCTIONS <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
|
||
|
<ul style="list-style-image:url(../matlabicon.gif)">
|
||
|
<li><a href="#_sub1" class="code">function f00 = lsearch(lam,func,x,dx,varargin)</a></li></ul>
|
||
|
<h2><a name="_source"></a>SOURCE CODE <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
|
||
|
<div class="fragment"><pre>0001 <a name="_sub0" href="#_subfunctions" class="code">function [xparam1, hh, gg, fval, igg] = newrat(func0, x, hh, gg, igg, ftol0, nit, flagg, varargin)</a>
|
||
|
0002 <span class="comment">%</span>
|
||
|
0003 <span class="comment">% Copyright (C) 2004 Marco Ratto</span>
|
||
|
0004 <span class="comment">%</span>
|
||
|
0005 <span class="comment">% [xparam1, hh, gg, fval, igg] = newrat(func0, x, hh, gg, igg, ftol0, nit, flagg, varargin)</span>
|
||
|
0006 <span class="comment">%</span>
|
||
|
0007 <span class="comment">% Optimiser with outer product gradient and 'Gibbs type' steps</span>
|
||
|
0008 <span class="comment">% uses Chris Sims subroutine for line search</span>
|
||
|
0009 <span class="comment">%</span>
|
||
|
0010 <span class="comment">% func0 = name of the function</span>
|
||
|
0011 <span class="comment">% there must be a version of the function called [func0,'_hh.m'], that also</span>
|
||
|
0012 <span class="comment">% gives as second OUTPUT the single contributions at times t=1,...,T</span>
|
||
|
0013 <span class="comment">% of the log-likelihood to compute outer product gradient</span>
|
||
|
0014 <span class="comment">%</span>
|
||
|
0015 <span class="comment">% x = starting guess</span>
|
||
|
0016 <span class="comment">% hh = initial Hessian [OPTIONAL]</span>
|
||
|
0017 <span class="comment">% gg = initial gradient [OPTIONAL]</span>
|
||
|
0018 <span class="comment">% igg = initial inverse Hessian [OPTIONAL]</span>
|
||
|
0019 <span class="comment">% ftol0 = ending criterion for function change</span>
|
||
|
0020 <span class="comment">% nit = maximum number of iterations</span>
|
||
|
0021 <span class="comment">%</span>
|
||
|
0022 <span class="comment">% In each iteration, Hessian is computed with outer product gradient.</span>
|
||
|
0023 <span class="comment">% for final Hessian (to start Metropolis):</span>
|
||
|
0024 <span class="comment">% flagg = 0, final Hessian computed with outer product gradient</span>
|
||
|
0025 <span class="comment">% flagg = 1, final 'mixed' Hessian: diagonal elements computed with numerical second order derivatives</span>
|
||
|
0026 <span class="comment">% with correlation structure as from outer product gradient,</span>
|
||
|
0027 <span class="comment">% flagg = 2, full numerical Hessian</span>
|
||
|
0028 <span class="comment">%</span>
|
||
|
0029 <span class="comment">% varargin = list of parameters for func0</span>
|
||
|
0030 <span class="comment">%</span>
|
||
|
0031
|
||
|
0032 <span class="keyword">global</span> bayestopt_
|
||
|
0033 icount=0;
|
||
|
0034 nx=length(x);
|
||
|
0035 xparam1=x;
|
||
|
0036 <span class="comment">%ftol0=1.e-6;</span>
|
||
|
0037 htol_base = max(1.e-5, ftol0);
|
||
|
0038 flagit=0; <span class="comment">% mode of computation of hessian in each iteration</span>
|
||
|
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,<span class="string">'_hh'</span>];
|
||
|
0046 func = str2func(func0);
|
||
|
0047 fval0=feval(func,x,varargin{:});
|
||
|
0048 fval=fval0;
|
||
|
0049 <span class="keyword">if</span> isempty(hh)
|
||
|
0050 [dum, gg, htol0, igg, hhg]=<a href="mr_hessian.html" class="code" title="function [hessian_mat, gg, htol1, ihh, hh_mat0] = mr_hessian(func,x,hflag,htol0,varargin)">mr_hessian</a>(func_hh,x,flagit,htol,varargin{:});
|
||
|
0051 hh0 = reshape(dum,nx,nx);
|
||
|
0052 hh=hhg;
|
||
|
0053 <span class="keyword">if</span> min(eig(hh0))<0,
|
||
|
0054 hh0=hhg; <span class="comment">%generalized_cholesky(hh0);</span>
|
||
|
0055 <span class="keyword">elseif</span> flagit==2,
|
||
|
0056 hh=hh0;
|
||
|
0057 igg=inv(hh);
|
||
|
0058 <span class="keyword">end</span>
|
||
|
0059 <span class="keyword">if</span> htol0>htol,
|
||
|
0060 htol=htol0;
|
||
|
0061 <span class="comment">%ftol=htol0;</span>
|
||
|
0062 <span class="keyword">end</span>
|
||
|
0063 <span class="keyword">else</span>
|
||
|
0064 hh0=hh;
|
||
|
0065 hhg=hh;
|
||
|
0066 igg=inv(hh);
|
||
|
0067 <span class="keyword">end</span>
|
||
|
0068 disp([<span class="string">'Gradient norm '</span>,num2str(norm(gg))])
|
||
|
0069 ee=eig(hh);
|
||
|
0070 disp([<span class="string">'Minimum Hessian eigenvalue '</span>,num2str(min(ee))])
|
||
|
0071 disp([<span class="string">'Maximum Hessian eigenvalue '</span>,num2str(max(ee))])
|
||
|
0072 g=gg;
|
||
|
0073 <a href="check.html" class="code" title="function result = check">check</a>=0;
|
||
|
0074 <span class="keyword">if</span> max(eig(hh))<0, disp(<span class="string">'Negative definite Hessian! Local maximum!'</span>), pause, <span class="keyword">end</span>,
|
||
|
0075 save m1 x hh g hhg igg fval0
|
||
|
0076
|
||
|
0077 igrad=1;
|
||
|
0078 igibbs=1;
|
||
|
0079 inx=eye(nx);
|
||
|
0080 jit=0;
|
||
|
0081 <span class="keyword">while</span> norm(gg)>gtol & <a href="check.html" class="code" title="function result = check">check</a>==0 & jit<nit,
|
||
|
0082 jit=jit+1;
|
||
|
0083 tic
|
||
|
0084 icount=icount+1;
|
||
|
0085 bayestopt_.penalty = fval0(icount);
|
||
|
0086 disp([<span class="string">' '</span>])
|
||
|
0087 disp([<span class="string">'Iteration '</span>,num2str(icount)])
|
||
|
0088 [fval x0 fc retcode] = <a href="csminit.html" class="code" title="function [fhat,xhat,fcount,retcode] = csminit(fcn,x0,f0,g0,badg,H0,varargin)">csminit</a>(func0,xparam1,fval0(icount),gg,0,igg,varargin{:});
|
||
|
0089 <span class="keyword">if</span> igrad,
|
||
|
0090 [fval1 x01 fc retcode1] = <a href="csminit.html" class="code" title="function [fhat,xhat,fcount,retcode] = csminit(fcn,x0,f0,g0,badg,H0,varargin)">csminit</a>(func0,x0,fval,gg,0,inx,varargin{:});
|
||
|
0091 <span class="keyword">if</span> (fval-fval1)>1, <span class="comment">%(fval0(icount)-fval),</span>
|
||
|
0092 disp(<span class="string">'Gradient step!!'</span>)
|
||
|
0093 <span class="keyword">else</span>
|
||
|
0094 igrad=0;
|
||
|
0095 <span class="keyword">end</span>
|
||
|
0096 fval=fval1;
|
||
|
0097 x0=x01;
|
||
|
0098 <span class="keyword">end</span>
|
||
|
0099 <span class="keyword">if</span> (fval0(icount)-fval)<1.e-2*(gg'*(igg*gg))/2 & igibbs,
|
||
|
0100 [fvala, x0] = <a href="mr_gstep.html" class="code" title="function [f0, x] = mr_gstep(func0,x,htol0,varargin)">mr_gstep</a>(func0,x0,htol,varargin{:});
|
||
|
0101 <span class="keyword">if</span> (fval-fvala)<gibbstol*(fval0(icount)-fval),
|
||
|
0102 igibbs=0;
|
||
|
0103 disp(<span class="string">'Last Gibbs step, gain too small!!'</span>)
|
||
|
0104 <span class="keyword">else</span>
|
||
|
0105 disp(<span class="string">'Gibbs step!!'</span>)
|
||
|
0106 <span class="keyword">end</span>
|
||
|
0107 fval=fvala;
|
||
|
0108 <span class="keyword">end</span>
|
||
|
0109 <span class="keyword">if</span> (fval0(icount)-fval)<ftol & flagit==0,
|
||
|
0110 disp(<span class="string">'Try diagonal Hessian'</span>)
|
||
|
0111 ihh=diag(1./(diag(hhg)));
|
||
|
0112 [fval2 x0 fc retcode2] = <a href="csminit.html" class="code" title="function [fhat,xhat,fcount,retcode] = csminit(fcn,x0,f0,g0,badg,H0,varargin)">csminit</a>(func2str(func),x0,fval,gg,0,ihh,varargin{:});
|
||
|
0113 <span class="keyword">if</span> (fval-fval2)>=ftol ,
|
||
|
0114 <span class="comment">%hh=diag(diag(hh));</span>
|
||
|
0115 disp(<span class="string">'Diagonal Hessian successful'</span>)
|
||
|
0116 <span class="keyword">end</span>
|
||
|
0117 fval=fval2;
|
||
|
0118 <span class="keyword">end</span>
|
||
|
0119 <span class="keyword">if</span> (fval0(icount)-fval)<ftol & flagit==0,
|
||
|
0120 disp(<span class="string">'Try gradient direction'</span>)
|
||
|
0121 ihh0=inx.*1.e-4;
|
||
|
0122 [fval3 x0 fc retcode3] = <a href="csminit.html" class="code" title="function [fhat,xhat,fcount,retcode] = csminit(fcn,x0,f0,g0,badg,H0,varargin)">csminit</a>(func2str(func),x0,fval,gg,0,ihh0,varargin{:});
|
||
|
0123 <span class="keyword">if</span> (fval-fval3)>=ftol ,
|
||
|
0124 <span class="comment">%hh=hh0;</span>
|
||
|
0125 <span class="comment">%ihh=ihh0;</span>
|
||
|
0126 disp(<span class="string">'Gradient direction successful'</span>)
|
||
|
0127 <span class="keyword">end</span>
|
||
|
0128 fval=fval3;
|
||
|
0129 <span class="keyword">end</span>
|
||
|
0130 xparam1=x0;
|
||
|
0131 x(:,icount+1)=xparam1;
|
||
|
0132 fval0(icount+1)=fval;
|
||
|
0133 <span class="comment">%if (fval0(icount)-fval)<ftol*ftol & flagg==1;,</span>
|
||
|
0134 <span class="keyword">if</span> (fval0(icount)-fval)<ftol,
|
||
|
0135 disp(<span class="string">'No further improvement is possible!'</span>)
|
||
|
0136 <a href="check.html" class="code" title="function result = check">check</a>=1;
|
||
|
0137 <span class="keyword">if</span> flagit==2,
|
||
|
0138 hh=hh0;
|
||
|
0139 <span class="keyword">elseif</span> flagg>0,
|
||
|
0140 [dum, gg, htol0, igg, hhg]=<a href="mr_hessian.html" class="code" title="function [hessian_mat, gg, htol1, ihh, hh_mat0] = mr_hessian(func,x,hflag,htol0,varargin)">mr_hessian</a>(func_hh,xparam1,flagg,ftol0,varargin{:});
|
||
|
0141 <span class="keyword">if</span> flagg==2,
|
||
|
0142 hh = reshape(dum,nx,nx);
|
||
|
0143 ee=eig(hh);
|
||
|
0144 <span class="keyword">if</span> min(ee)<0
|
||
|
0145 hh=hhg;
|
||
|
0146 <span class="keyword">end</span>
|
||
|
0147 <span class="keyword">else</span>
|
||
|
0148 hh=hhg;
|
||
|
0149 <span class="keyword">end</span>
|
||
|
0150 <span class="keyword">end</span>
|
||
|
0151 disp([<span class="string">'Actual dxnorm '</span>,num2str(norm(x(:,end)-x(:,end-1)))])
|
||
|
0152 disp([<span class="string">'FVAL '</span>,num2str(fval)])
|
||
|
0153 disp([<span class="string">'Improvement '</span>,num2str(fval0(icount)-fval)])
|
||
|
0154 disp([<span class="string">'Ftol '</span>,num2str(ftol)])
|
||
|
0155 disp([<span class="string">'Htol '</span>,num2str(htol0)])
|
||
|
0156 disp([<span class="string">'Gradient norm '</span>,num2str(norm(gg))])
|
||
|
0157 ee=eig(hh);
|
||
|
0158 disp([<span class="string">'Minimum Hessian eigenvalue '</span>,num2str(min(ee))])
|
||
|
0159 disp([<span class="string">'Maximum Hessian eigenvalue '</span>,num2str(max(ee))])
|
||
|
0160 g(:,icount+1)=gg;
|
||
|
0161 <span class="keyword">else</span>
|
||
|
0162
|
||
|
0163 df = fval0(icount)-fval;
|
||
|
0164 disp([<span class="string">'Actual dxnorm '</span>,num2str(norm(x(:,end)-x(:,end-1)))])
|
||
|
0165 disp([<span class="string">'FVAL '</span>,num2str(fval)])
|
||
|
0166 disp([<span class="string">'Improvement '</span>,num2str(df)])
|
||
|
0167 disp([<span class="string">'Ftol '</span>,num2str(ftol)])
|
||
|
0168 disp([<span class="string">'Htol '</span>,num2str(htol0)])
|
||
|
0169
|
||
|
0170 <span class="keyword">if</span> df<htol0,
|
||
|
0171 htol=max(htol_base,df/10);
|
||
|
0172 <span class="keyword">end</span>
|
||
|
0173
|
||
|
0174 <span class="keyword">if</span> norm(x(:,icount)-xparam1)>1.e-12,
|
||
|
0175 save m1 x fval0 -append
|
||
|
0176 [dum, gg, htol0, igg, hhg]=<a href="mr_hessian.html" class="code" title="function [hessian_mat, gg, htol1, ihh, hh_mat0] = mr_hessian(func,x,hflag,htol0,varargin)">mr_hessian</a>(func_hh,xparam1,flagit,htol,varargin{:});
|
||
|
0177 <span class="keyword">if</span> htol0>htol, <span class="comment">%ftol,</span>
|
||
|
0178 <span class="comment">%ftol=htol0;</span>
|
||
|
0179 htol=htol0;
|
||
|
0180 disp(<span class="string">' '</span>)
|
||
|
0181 disp(<span class="string">'Numerical noise in the likelihood'</span>)
|
||
|
0182 disp(<span class="string">'Tolerance has to be relaxed'</span>)
|
||
|
0183 disp(<span class="string">' '</span>)
|
||
|
0184 <span class="comment">% elseif htol0<ftol,</span>
|
||
|
0185 <span class="comment">% ftol=max(htol0, ftol0);</span>
|
||
|
0186 <span class="keyword">end</span>
|
||
|
0187 hh0 = reshape(dum,nx,nx);
|
||
|
0188 hh=hhg;
|
||
|
0189 <span class="keyword">if</span> flagit==2,
|
||
|
0190 <span class="keyword">if</span> min(eig(hh0))<=0,
|
||
|
0191 hh0=hhg; <span class="comment">%generalized_cholesky(hh0);</span>
|
||
|
0192 <span class="keyword">else</span>
|
||
|
0193 hh=hh0;
|
||
|
0194 igg=inv(hh);
|
||
|
0195 <span class="keyword">end</span>
|
||
|
0196 <span class="keyword">end</span>
|
||
|
0197 <span class="keyword">end</span>
|
||
|
0198 disp([<span class="string">'Gradient norm '</span>,num2str(norm(gg))])
|
||
|
0199 ee=eig(hh);
|
||
|
0200 disp([<span class="string">'Minimum Hessian eigenvalue '</span>,num2str(min(ee))])
|
||
|
0201 disp([<span class="string">'Maximum Hessian eigenvalue '</span>,num2str(max(ee))])
|
||
|
0202 <span class="keyword">if</span> max(eig(hh))<0, disp(<span class="string">'Negative definite Hessian! Local maximum!'</span>), pause, <span class="keyword">end</span>,
|
||
|
0203 t=toc;
|
||
|
0204 disp([<span class="string">'Elapsed time for iteration '</span>,num2str(t),<span class="string">' s.'</span>])
|
||
|
0205
|
||
|
0206 g(:,icount+1)=gg;
|
||
|
0207 save m1 x hh g hhg igg fval0
|
||
|
0208 <span class="keyword">end</span>
|
||
|
0209 <span class="keyword">end</span>
|
||
|
0210
|
||
|
0211 save m1 x hh g hhg igg fval0
|
||
|
0212 <span class="keyword">if</span> ftol>ftol0,
|
||
|
0213 disp(<span class="string">' '</span>)
|
||
|
0214 disp(<span class="string">'Numerical noise in the likelihood'</span>)
|
||
|
0215 disp(<span class="string">'Tolerance had to be relaxed'</span>)
|
||
|
0216 disp(<span class="string">' '</span>)
|
||
|
0217 <span class="keyword">end</span>
|
||
|
0218
|
||
|
0219 <span class="keyword">if</span> jit==nit,
|
||
|
0220 disp(<span class="string">' '</span>)
|
||
|
0221 disp(<span class="string">'Maximum number of iterations reached'</span>)
|
||
|
0222 disp(<span class="string">' '</span>)
|
||
|
0223 <span class="keyword">end</span>
|
||
|
0224
|
||
|
0225 <span class="keyword">if</span> norm(gg)<=gtol,
|
||
|
0226 disp([<span class="string">'Estimation ended:'</span>])
|
||
|
0227 disp([<span class="string">'Gradient norm < '</span>, num2str(gtol)])
|
||
|
0228 <span class="keyword">end</span>
|
||
|
0229 <span class="keyword">if</span> <a href="check.html" class="code" title="function result = check">check</a>==1,
|
||
|
0230 disp([<span class="string">'Estimation successful.'</span>])
|
||
|
0231 <span class="keyword">end</span>
|
||
|
0232
|
||
|
0233 <span class="keyword">return</span>
|
||
|
0234
|
||
|
0235 <span class="comment">%</span>
|
||
|
0236 <a name="_sub1" href="#_subfunctions" class="code">function f00 = lsearch(lam,func,x,dx,varargin)</a>
|
||
|
0237
|
||
|
0238
|
||
|
0239 x0=x-dx*lam;
|
||
|
0240 f00=feval(func,x0,varargin{:});
|
||
|
0241
|
||
|
0242
|
||
|
0243
|
||
|
0244
|
||
|
0245
|
||
|
0246</pre></div>
|
||
|
<hr><address>Generated on Fri 16-Jun-2006 09:09:06 by <strong><a href="http://www.artefact.tk/software/matlab/m2html/">m2html</a></strong> © 2003</address>
|
||
|
</body>
|
||
|
</html>
|