1 ## Copyright (C) 2002 Etienne Grossmann <etienne@egdn.net>
2 ## Copyright (C) 2004 Michael Creel <michael.creel@uab.es>
4 ## This program is free software; you can redistribute it and/or modify it under
5 ## the terms of the GNU General Public License as published by the Free Software
6 ## Foundation; either version 3 of the License, or (at your option) any later
9 ## This program is distributed in the hope that it will be useful, but WITHOUT
10 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14 ## You should have received a copy of the GNU General Public License along with
15 ## this program; if not, see <http://www.gnu.org/licenses/>.
17 ## ok - Test that bfgs works with extra
20 ## Defines some simple functions and verifies that calling
21 ## bfgs on them returns the correct minimum.
23 ## Sets 'ok' to 1 if success, 0 otherwise
25 if ! exist ("optim_func"), optim_func = "bfgsmin"; end
29 if ! exist ("verbose"), verbose = 0; end
34 ## Make tests reproducible
35 ## obsmat = randn(R,P) ;
37 obsmat(sub2ind([R,P],1:R,1+rem(0:R-1,P))) = 1:R;
39 ## Make test_min_2 repeatable by using fixed starting point
40 ## truep = randn(P,1) ;
41 ## xinit = randn(P,1) ;
42 truep = rem (1:P, P/4)';
43 xinit = truep + 2*(1:P)'/(P);
47 obses = obsmat*truep ;
49 extra = {obsmat, obses};
52 function v = ff(x, obsmat, obses)
53 v = mean ( (obses - obsmat*x)(:).^2 ) + 1 ;
57 function dv = dff(x, obsmat, obses)
58 er = -obses + obsmat*x ;
59 dv = 2*er'*obsmat / rows(obses) ;
60 ## dv = 2*er'*obsmat ;
66 printf (" Checking that extra arguments are accepted\n\n");
68 printf ([" Set 'optim_func' to the name of the optimization\n",\
69 " function you want to test (must have same synopsis\n",\
72 printf (" Tested function : %s\n",optim_func);
73 printf (" Nparams = P = %i, Nobses = R = %i\n",P,R);
77 persistent last_mytic = 0 ;
85 ## [xlev,vlev,nlev] = feval (optim_func, "ff", "dff", xinit, "extra", extra) ;
86 ## [xlev,vlev,nlev] = feval \
87 ## (optim_func, "ff", "dff", list (xinit, obsmat, obses));
88 if strcmp(optim_func,"bfgsmin")
91 [xlev,vlev,nlev] = feval \
92 (optim_func, "ff", {xinit, obsmat, obses}, ctl);
96 if max (abs(xlev-truep)) > 1e-4,
98 printf ("Error is too big : %8.3g\n", max (abs (xlev-truep)));
103 printf (" Costs : init=%8.3g, final=%8.3g, best=%8.3g\n",\
104 ff(xinit,obsmat,obses), vlev, ff(truep,obsmat,obses));
107 printf ( " time : %8.3g\n",tlev);
110 printf ( "All tests ok\n");