| 1 |
function [fhat,xhat,fcount,retcode] = csminit(fcn,x0,f0,g0,badg,H0,varargin) |
| 2 |
% [fhat,xhat,fcount,retcode] = csminit(fcn,x0,f0,g0,badg,H0,... |
| 3 |
% P1,P2,P3,P4,P5,P6,P7,P8) |
| 4 |
% retcodes: 0, normal step. 5, largest step still improves too fast. |
| 5 |
% 4,2 back and forth adjustment of stepsize didn't finish. 3, smallest |
| 6 |
% stepsize still improves too slow. 6, no improvement found. 1, zero |
| 7 |
% gradient. |
| 8 |
%--------------------- |
| 9 |
% Modified 7/22/96 to omit variable-length P list, for efficiency and compilation. |
| 10 |
% Places where the number of P's need to be altered or the code could be returned to |
| 11 |
% its old form are marked with ARGLIST comments. |
| 12 |
% |
| 13 |
% Fixed 7/17/93 to use inverse-hessian instead of hessian itself in bfgs |
| 14 |
% update. |
| 15 |
% |
| 16 |
% Fixed 7/19/93 to flip eigenvalues of H to get better performance when |
| 17 |
% it's not psd. |
| 18 |
% |
| 19 |
% Fixed 02/19/05 to correct for low angle problems. |
| 20 |
% |
| 21 |
%tailstr = ')'; |
| 22 |
%for i=nargin-6:-1:1 |
| 23 |
% tailstr=[ ',P' num2str(i) tailstr]; |
| 24 |
%end |
| 25 |
% |
| 26 |
% Copyright (C) 1997-2012 Christopher A. Sims |
| 27 |
% |
| 28 |
% This free software: you can redistribute it and/or modify |
| 29 |
% it under the terms of the GNU General Public License as published by |
| 30 |
% the Free Software Foundation, either version 3 of the License, or |
| 31 |
% (at your option) any later version. |
| 32 |
% |
| 33 |
% It is distributed in the hope that it will be useful, |
| 34 |
% but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 35 |
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 36 |
% GNU General Public License for more details. |
| 37 |
% |
| 38 |
% If you did not received a copy of the GNU General Public License |
| 39 |
% with this software, see <http://www.gnu.org/licenses/>. |
| 40 |
% |
| 41 |
|
| 42 |
dispIndx = 1; % 1: turn on all the diplays on the screen; 0: turn off (Added by T. Zha) |
| 43 |
|
| 44 |
|
| 45 |
%ANGLE = .03; % when output of this variable becomes negative, we have wrong analytical graident |
| 46 |
ANGLE = .005; % works for identified VARs and OLS |
| 47 |
%THETA = .03; |
| 48 |
THETA = .3; %(0<THETA<.5) THETA near .5 makes long line searches, possibly fewer iterations. |
| 49 |
FCHANGE = 1000; |
| 50 |
MINLAMB = 1e-9; |
| 51 |
% fixed 7/15/94 |
| 52 |
% MINDX = .0001; |
| 53 |
% MINDX = 1e-6; |
| 54 |
MINDFAC = .01; |
| 55 |
fcount=0; |
| 56 |
lambda=1; |
| 57 |
xhat=x0; |
| 58 |
f=f0; |
| 59 |
fhat=f0; |
| 60 |
g = g0; |
| 61 |
gnorm = norm(g); |
| 62 |
% |
| 63 |
if (gnorm < 1.e-12) & ~badg % put ~badg 8/4/94 |
| 64 |
retcode =1; |
| 65 |
dxnorm=0; |
| 66 |
% gradient convergence |
| 67 |
else |
| 68 |
% with badg true, we don't try to match rate of improvement to directional |
| 69 |
% derivative. We're satisfied just to get some improvement in f. |
| 70 |
% |
| 71 |
%if(badg) |
| 72 |
% dx = -g*FCHANGE/(gnorm*gnorm); |
| 73 |
% dxnorm = norm(dx); |
| 74 |
% if dxnorm > 1e12 |
| 75 |
% disp('Bad, small gradient problem.') |
| 76 |
% dx = dx*FCHANGE/dxnorm; |
| 77 |
% end |
| 78 |
%else |
| 79 |
% Gauss-Newton step; |
| 80 |
%---------- Start of 7/19/93 mod --------------- |
| 81 |
%[v d] = eig(H0); |
| 82 |
%toc |
| 83 |
%d=max(1e-10,abs(diag(d))); |
| 84 |
%d=abs(diag(d)); |
| 85 |
%dx = -(v.*(ones(size(v,1),1)*d'))*(v'*g); |
| 86 |
% toc |
| 87 |
dx = -H0*g; |
| 88 |
% toc |
| 89 |
dxnorm = norm(dx); |
| 90 |
if dxnorm > 1e12 |
| 91 |
if dispIndx, disp('Near-singular H problem.'), end |
| 92 |
dx = dx*FCHANGE/dxnorm; |
| 93 |
end |
| 94 |
dfhat = dx'*g0; |
| 95 |
%end |
| 96 |
% |
| 97 |
% |
| 98 |
if ~badg |
| 99 |
% test for alignment of dx with gradient and fix if necessary |
| 100 |
a = -dfhat/(gnorm*dxnorm); |
| 101 |
if a<ANGLE |
| 102 |
dx = dx - (ANGLE*dxnorm/gnorm+dfhat/(gnorm*gnorm))*g; |
| 103 |
% suggested alternate code: --------------------- |
| 104 |
dx = dx*dxnorm/norm(dx); % Added 02/19/05 by CAS. This keeps scale invariant to the angle correction |
| 105 |
% ------------------------------------------------ |
| 106 |
dfhat = dx'*g; |
| 107 |
% dxnorm = norm(dx); % Removed 02/19/05 by CAS. This line unnecessary with modification that keeps scale invariant |
| 108 |
if dispIndx, disp(sprintf('Correct for low angle: %g',a)), end |
| 109 |
end |
| 110 |
end |
| 111 |
if dispIndx, disp(sprintf('Predicted improvement: %18.9f',-dfhat/2)), end |
| 112 |
% |
| 113 |
% Have OK dx, now adjust length of step (lambda) until min and |
| 114 |
% max improvement rate criteria are met. |
| 115 |
done=0; |
| 116 |
factor=3; |
| 117 |
shrink=1; |
| 118 |
lambdaMin=0; |
| 119 |
lambdaMax=inf; |
| 120 |
lambdaPeak=0; |
| 121 |
fPeak=f0; |
| 122 |
lambdahat=0; |
| 123 |
while ~done |
| 124 |
if size(x0,2)>1 |
| 125 |
dxtest=x0+dx'*lambda; |
| 126 |
else |
| 127 |
dxtest=x0+dx*lambda; |
| 128 |
end |
| 129 |
% home |
| 130 |
f = feval(fcn,dxtest,varargin{:}); |
| 131 |
%ARGLIST |
| 132 |
%f = feval(fcn,dxtest,P1,P2,P3,P4,P5,P6,P7,P8,P9,P10,P11,P12,P13); |
| 133 |
% f = feval(fcn,x0+dx*lambda,P1,P2,P3,P4,P5,P6,P7,P8); |
| 134 |
if dispIndx, disp(sprintf('lambda = %10.5g; f = %20.7f',lambda,f )), end |
| 135 |
%debug |
| 136 |
%disp(sprintf('Improvement too great? f0-f: %g, criterion: %g',f0-f,-(1-THETA)*dfhat*lambda)) |
| 137 |
if f<fhat |
| 138 |
fhat=f; |
| 139 |
xhat=dxtest; |
| 140 |
lambdahat = lambda; |
| 141 |
end |
| 142 |
fcount=fcount+1; |
| 143 |
shrinkSignal = (~badg & (f0-f < max([-THETA*dfhat*lambda 0]))) | (badg & (f0-f) < 0) ; |
| 144 |
growSignal = ~badg & ( (lambda > 0) & (f0-f > -(1-THETA)*dfhat*lambda) ); |
| 145 |
if shrinkSignal & ( (lambda>lambdaPeak) | (lambda<0) ) |
| 146 |
if (lambda>0) & ((~shrink) | (lambda/factor <= lambdaPeak)) |
| 147 |
shrink=1; |
| 148 |
factor=factor^.6; |
| 149 |
while lambda/factor <= lambdaPeak |
| 150 |
factor=factor^.6; |
| 151 |
end |
| 152 |
%if (abs(lambda)*(factor-1)*dxnorm < MINDX) | (abs(lambda)*(factor-1) < MINLAMB) |
| 153 |
if abs(factor-1)<MINDFAC |
| 154 |
if abs(lambda)<4 |
| 155 |
retcode=2; |
| 156 |
else |
| 157 |
retcode=7; |
| 158 |
end |
| 159 |
done=1; |
| 160 |
end |
| 161 |
end |
| 162 |
if (lambda<lambdaMax) & (lambda>lambdaPeak) |
| 163 |
lambdaMax=lambda; |
| 164 |
end |
| 165 |
lambda=lambda/factor; |
| 166 |
if abs(lambda) < MINLAMB |
| 167 |
if (lambda > 0) & (f0 <= fhat) |
| 168 |
% try going against gradient, which may be inaccurate |
| 169 |
if dispIndx, lambda = -lambda*factor^6, end |
| 170 |
else |
| 171 |
if lambda < 0 |
| 172 |
retcode = 6; |
| 173 |
else |
| 174 |
retcode = 3; |
| 175 |
end |
| 176 |
done = 1; |
| 177 |
end |
| 178 |
end |
| 179 |
elseif (growSignal & lambda>0) | (shrinkSignal & ((lambda <= lambdaPeak) & (lambda>0))) |
| 180 |
if shrink |
| 181 |
shrink=0; |
| 182 |
factor = factor^.6; |
| 183 |
%if ( abs(lambda)*(factor-1)*dxnorm< MINDX ) | ( abs(lambda)*(factor-1)< MINLAMB) |
| 184 |
if abs(factor-1)<MINDFAC |
| 185 |
if abs(lambda)<4 |
| 186 |
retcode=4; |
| 187 |
else |
| 188 |
retcode=7; |
| 189 |
end |
| 190 |
done=1; |
| 191 |
end |
| 192 |
end |
| 193 |
if ( f<fPeak ) & (lambda>0) |
| 194 |
fPeak=f; |
| 195 |
lambdaPeak=lambda; |
| 196 |
if lambdaMax<=lambdaPeak |
| 197 |
lambdaMax=lambdaPeak*factor*factor; |
| 198 |
end |
| 199 |
end |
| 200 |
lambda=lambda*factor; |
| 201 |
if abs(lambda) > 1e20; |
| 202 |
retcode = 5; |
| 203 |
done =1; |
| 204 |
end |
| 205 |
else |
| 206 |
done=1; |
| 207 |
if factor < 1.2 |
| 208 |
retcode=7; |
| 209 |
else |
| 210 |
retcode=0; |
| 211 |
end |
| 212 |
end |
| 213 |
end |
| 214 |
end |
| 215 |
if dispIndx, disp(sprintf('Norm of dx %10.5g', dxnorm)), end |