Debian Codegraph @

contrib/ms-sbvar/TZcode/MatlabFiles/csminitworksuntiil0205.m

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