Debian Codegraph @

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

raw
1 function [fh,xh,gh,H,itct,fcount,retcodeh] = csminwel(fcn,x0,H0,grad,crit,nit,varargin)
2 %[fhat,xhat,ghat,Hhat,itct,fcount,retcodehat] = csminwel(fcn,x0,H0,grad,crit,nit,varargin)
3 % fcn: string naming the objective function to be minimized
4 % x0: initial value of the parameter vector
5 % H0: initial value for the inverse Hessian. Must be positive definite.
6 % grad: Either a string naming a function that calculates the gradient, or the null matrix.
7 % If it's null, the program calculates a numerical gradient. In this case fcn must
8 % be written so that it can take a matrix argument and produce a row vector of values.
9 % crit: Convergence criterion. Iteration will cease when it proves impossible to improve the
10 % function value by more than crit.
11 % nit: Maximum number of iterations.
12 % varargin: A list of optional length of additional parameters that get handed off to fcn each
13 % time it is called.
14 % Note that if the program ends abnormally, it is possible to retrieve the current x,
15 % f, and H from the files g1.mat and H.mat that are written at each iteration and at each
16 % hessian update, respectively. (When the routine hits certain kinds of difficulty, it
17 % write g2.mat and g3.mat as well. If all were written at about the same time, any of them
18 % may be a decent starting point. One can also start from the one with best function value.)
19 % NOTE: The display on screen can be turned off by seeting dispIndx=0 in this
20 % function. This option is used for the loop operation. T. Zha, 2 May 2000
21 % NOTE: You may want to change stps to 1.0e-02 or 1.0e-03 to get a better convergence. August, 2006
22 %
23 % Copyright (C) 1997-2012 Christopher A. Sims and Tao Zha
24 %
25 % This free software: you can redistribute it and/or modify
26 % it under the terms of the GNU General Public License as published by
27 % the Free Software Foundation, either version 3 of the License, or
28 % (at your option) any later version.
29 %
30 % It is distributed in the hope that it will be useful,
31 % but WITHOUT ANY WARRANTY; without even the implied warranty of
32 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 % GNU General Public License for more details.
34 %
35 % If you did not received a copy of the GNU General Public License
36 % with this software, see <http://www.gnu.org/licenses/>.
37 %
38
39 Verbose = 1; % 1: turn on all the diplays on the screen; 0: turn off (Added by T. Zha)
40 dispIndx = 1; % 1: turn on all the diplays on the screen; 0: turn off (Added by T. Zha)
41
42 [nx,no]=size(x0);
43 nx=max(nx,no);
44 NumGrad= ( ~isstr(grad) | length(grad)==0);
45 done=0;
46 itct=0;
47 fcount=0;
48 snit=100;
49 %tailstr = ')';
50 %stailstr = [];
51 % Lines below make the number of Pi's optional. This is inefficient, though, and precludes
52 % use of the matlab compiler. Without them, we use feval and the number of Pi's must be
53 % changed with the editor for each application. Places where this is required are marked
54 % with ARGLIST comments
55 %for i=nargin-6:-1:1
56 % tailstr=[ ',P' num2str(i) tailstr];
57 % stailstr=[' P' num2str(i) stailstr];
58 %end
59 if ischar(fcn)
60 f0 = eval([fcn '(x0,varargin{:})']);
61 else
62 f0 = fcn(x0,varargin{:});
63 end
64 %ARGLIST
65 %f0 = feval(fcn,x0,P1,P2,P3,P4,P5,P6,P7,P8,P9,P10,P11,P12,P13);
66 % disp('first fcn in csminwel.m ----------------') % Jinill on 9/5/95
67 if f0 > 1e50, disp('Bad initial parameter.'), return, end
68 if NumGrad
69 if length(grad)==0
70 [g badg] = numgradcd(fcn,x0, varargin{:});
71 %ARGLIST
72 %[g badg] = numgradcd(fcn,x0,P1,P2,P3,P4,P5,P6,P7,P8,P9,P10,P11,P12,P13);
73 else
74 badg=any(find(grad==0));
75 g=grad;
76 end
77 %numgradcd(fcn,x0,P1,P2,P3,P4);
78 else
79 [g badg] = eval([grad '(x0,varargin{:})']);
80 %ARGLIST
81 %[g badg] = feval(grad,x0,P1,P2,P3,P4,P5,P6,P7,P8,P9,P10,P11,P12,P13);
82 end
83 retcode3=101;
84 x=x0;
85 f=f0;
86 H=H0;
87 cliff=0;
88 while ~done
89 g1=[]; g2=[]; g3=[];
90 %addition fj. 7/6/94 for control
91 if dispIndx
92 disp('-----------------')
93 disp('-----------------')
94 %disp('f and x at the beginning of new iteration')
95 disp(sprintf('f at the beginning of new iteration, %20.10f',f))
96 %-----------Comment out this line if the x vector is long----------------
97 disp([sprintf('x = ') sprintf('%15.8g%15.8g%15.8g%15.8g%15.8g\n',x)]);
98 end
99 %-------------------------
100 itct=itct+1;
101 [f1 x1 fc retcode1] = csminit(fcn,x,f,g,badg,H,varargin{:});
102 %ARGLIST
103 %[f1 x1 fc retcode1] = csminit(fcn,x,f,g,badg,H,P1,P2,P3,P4,P5,P6,P7,...
104 % P8,P9,P10,P11,P12,P13);
105 % itct=itct+1;
106 fcount = fcount+fc;
107 % erased on 8/4/94
108 % if (retcode == 1) | (abs(f1-f) < crit)
109 % done=1;
110 % end
111 % if itct > nit
112 % done = 1;
113 % retcode = -retcode;
114 % end
115 if retcode1 ~= 1
116 if retcode1==2 | retcode1==4
117 wall1=1; badg1=1;
118 else
119 if NumGrad
120 [g1 badg1] = numgradcd(fcn, x1,varargin{:});
121 %ARGLIST
122 %[g1 badg1] = numgradcd(fcn, x1,P1,P2,P3,P4,P5,P6,P7,P8,P9,...
123 % P10,P11,P12,P13);
124 else
125 [g1 badg1] = eval([grad '(x1,varargin{:})']);
126 %ARGLIST
127 %[g1 badg1] = feval(grad, x1,P1,P2,P3,P4,P5,P6,P7,P8,P9,...
128 % P10,P11,P12,P13);
129 end
130 wall1=badg1;
131 % g1
132 save g1 g1 x1 f1 varargin;
133 %ARGLIST
134 %save g1 g1 x1 f1 P1 P2 P3 P4 P5 P6 P7 P8 P9 P10 P11 P12 P13;
135 end
136 if wall1 % & (~done) by Jinill
137 % Bad gradient or back and forth on step length. Possibly at
138 % cliff edge. Try perturbing search direction.
139 %
140 %fcliff=fh;xcliff=xh;
141 if dispIndx
142 disp(' ')
143 disp('************************* Random search. *****************************************')
144 disp('************************* Random search. *****************************************')
145 disp(' ')
146 pause(1.0)
147 end
148 Hcliff=H+diag(diag(H).*rand(nx,1));
149 if dispIndx, disp('Cliff. Perturbing search direction.'), end
150 [f2 x2 fc retcode2] = csminit(fcn,x,f,g,badg,Hcliff,varargin{:});
151 %ARGLIST
152 %[f2 x2 fc retcode2] = csminit(fcn,x,f,g,badg,Hcliff,P1,P2,P3,P4,...
153 % P5,P6,P7,P8,P9,P10,P11,P12,P13);
154 fcount = fcount+fc; % put by Jinill
155 if f2 < f
156 if retcode2==2 | retcode2==4
157 wall2=1; badg2=1;
158 else
159 if NumGrad
160 [g2 badg2] = numgradcd(fcn, x2,varargin{:});
161 %ARGLIST
162 %[g2 badg2] = numgradcd(fcn, x2,P1,P2,P3,P4,P5,P6,P7,P8,...
163 % P9,P10,P11,P12,P13);
164 else
165 [g2 badg2] = eval([grad '(x2,varargin{:})']);
166 %ARGLIST
167 %[g2 badg2] = feval(grad,x2,P1,P2,P3,P4,P5,P6,P7,P8,...
168 % P9,P10,P11,P12,P13);
169 end
170 wall2=badg2;
171 % g2
172 if dispIndx, badg2, end
173 save g2 g2 x2 f2 varargin
174 %ARGLIST
175 %save g2 g2 x2 f2 P1 P2 P3 P4 P5 P6 P7 P8 P9 P10 P11 P12 P13;
176 end
177 if wall2
178 if dispIndx, disp('Cliff again. Try traversing'), end
179 if norm(x2-x1) < 1e-13
180 f3=f; x3=x; badg3=1;retcode3=101;
181 else
182 gcliff=((f2-f1)/((norm(x2-x1))^2))*(x2-x1);
183 if(size(x0,2)>1), gcliff=gcliff', end
184 [f3 x3 fc retcode3] = csminit(fcn,x,f,gcliff,0,eye(nx),varargin{:});
185 %ARGLIST
186 %[f3 x3 fc retcode3] = csminit(fcn,x,f,gcliff,0,eye(nx),P1,P2,P3,...
187 % P4,P5,P6,P7,P8,...
188 % P9,P10,P11,P12,P13);
189 fcount = fcount+fc; % put by Jinill
190 if retcode3==2 | retcode3==4
191 wall3=1; badg3=1;
192 else
193 if NumGrad
194 [g3 badg3] = numgradcd(fcn, x3,varargin{:});
195 %ARGLIST
196 %[g3 badg3] = numgradcd(fcn, x3,P1,P2,P3,P4,P5,P6,P7,P8,...
197 % P9,P10,P11,P12,P13);
198 else
199 [g3 badg3] = eval([grad '(x3,varargin{:})']);
200 %ARGLIST
201 %[g3 badg3] = feval(grad,x3,P1,P2,P3,P4,P5,P6,P7,P8,...
202 % P9,P10,P11,P12,P13);
203 end
204 wall3=badg3;
205 % g3
206 if dispIndx, badg3, end
207 save g3 g3 x3 f3 varargin;
208 %ARGLIST
209 %save g3 g3 x3 f3 P1 P2 P3 P4 P5 P6 P7 P8 P9 P10 P11 P12 P13;
210 end
211 end
212 else
213 f3=f; x3=x; badg3=1; retcode3=101;
214 end
215 else
216 f3=f; x3=x; badg3=1;retcode3=101;
217 end
218 else
219 % normal iteration, no walls, or else we're finished here.
220 f2=f; f3=f; badg2=1; badg3=1; retcode2=101; retcode3=101;
221 end
222 else
223 f1=f; f2=f; f3=f; retcode2=retcode1; retcode3=retcode1;
224 end
225 %how to pick gh and xh
226 if f3<f & badg3==0
227 if dispIndx, ih=3, end
228 fh=f3;xh=x3;gh=g3;badgh=badg3;retcodeh=retcode3;
229 elseif f2<f & badg2==0
230 if dispIndx, ih=2, end
231 fh=f2;xh=x2;gh=g2;badgh=badg2;retcodeh=retcode2;
232 elseif f1<f & badg1==0
233 if dispIndx, ih=1, end
234 fh=f1;xh=x1;gh=g1;badgh=badg1;retcodeh=retcode1;
235 else
236 [fh,ih] = min([f1,f2,f3]);
237 if dispIndx, disp(sprintf('ih = %d',ih)), end
238 %eval(['xh=x' num2str(ih) ';'])
239 switch ih
240 case 1
241 xh=x1;
242 case 2
243 xh=x2;
244 case 3
245 xh=x3;
246 end %case
247 %eval(['gh=g' num2str(ih) ';'])
248 %eval(['retcodeh=retcode' num2str(ih) ';'])
249 retcodei=[retcode1,retcode2,retcode3];
250 retcodeh=retcodei(ih);
251 if exist('gh')
252 nogh=isempty(gh);
253 else
254 nogh=1;
255 end
256 if nogh
257 if NumGrad
258 [gh badgh] = numgradcd(fcn, xh,varargin{:}); %Pointed out by Junior Maih.
259 %[gh badgh] = feval('numgrad',fcn,xh,varargin{:});
260 else
261 [gh badgh] = numgradcd(fcn, xh,varargin{:}); %Pointed out by Junior Maih.
262 %[gh badgh] = feval('grad', xh,varargin{:});
263 end
264 end
265 badgh=1;
266 end
267 %end of picking
268 %ih
269 %fh
270 %xh
271 %gh
272 %badgh
273 stuck = (abs(fh-f) < crit);
274 if (~badg)&(~badgh)&(~stuck)
275 H = bfgsi(H,gh-g,xh-x);
276 end
277 if Verbose
278 if dispIndx
279 disp('----')
280 disp(sprintf('Improvement on iteration %d = %18.9f',itct,f-fh))
281 end
282 end
283
284 if itct > nit
285 if dispIndx, disp('iteration count termination'), end
286 done = 1;
287 elseif stuck
288 if dispIndx, disp('improvement < crit termination'), end
289 done = 1;
290 end
291 rc=retcodeh;
292 if rc == 1
293 if dispIndx, disp('zero gradient'), end
294 elseif rc == 6
295 if dispIndx, disp('smallest step still improving too slow, reversed gradient'), end
296 elseif rc == 5
297 if dispIndx, disp('largest step still improving too fast'), end
298 elseif (rc == 4) | (rc==2)
299 if dispIndx, disp('back and forth on step length never finished'), end
300 elseif rc == 3
301 if dispIndx, disp('smallest step still improving too slow'), end
302 elseif rc == 7
303 if dispIndx, disp('warning: possible inaccuracy in H matrix'), end
304 end
305
306 f=fh;
307 x=xh;
308 g=gh;
309 badg=badgh;
310 end
311 % what about making an m-file of 10 lines including numgrad.m
312 % since it appears three times in csminwel.m