]> Creatis software - CreaPhase.git/blob - octave_packages/optim-1.2.0/test_d2_min_2.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / optim-1.2.0 / test_d2_min_2.m
1 ## Copyright (C) 2002 Etienne Grossmann <etienne@egdn.net>
2 ##
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
6 ## version.
7 ##
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
11 ## details.
12 ##
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
15
16 ## Test whether d2_min() functions correctly, with two args
17 ##
18 ## Gives a simple quadratic programming problem (function ff below).
19 ##
20 ## Sets a ok variable to 1 in case of success, 0 in case of failure
21 ##
22 ## If a variables "verbose" is set, then some comments are output.
23
24 1 ;
25
26 ok = 0;
27
28 if ! exist ("verbose"), verbose = 0; end
29
30 if verbose
31   printf ("\n   Testing d2_min () on a quadratic programming problem\n\n");
32 end
33
34 P = 10+floor(30*rand(1)) ;      # Nparams
35 R = P+floor(30*rand(1)) ;       # Nobses
36 noise = 0 ;
37 obsmat = randn(R,P) ;
38 truep = randn(P,1) ;
39 xinit = randn(P,1) ;
40
41 obses = obsmat*truep ;
42 if noise, obses = adnois(obses,noise); end
43
44 y.obses = obses;
45 y.obsmat = obsmat;
46
47 function v = ff (x, y)
48   v = msq( y.obses - y.obsmat*x ) ;
49 endfunction
50
51
52 function [v,dv,d2v] = d2ff (x, y)
53   er = -y.obses + y.obsmat*x ;
54   dv = er'*y.obsmat ;
55   v = msq( er ) ;
56   d2v = pinv( y.obsmat'*y.obsmat ) ;
57 endfunction
58
59 ##       dt = mytic()
60 ##
61 ## Returns the cputime since last call to 'mytic'.
62
63 function dt = mytic()
64    persistent last_mytic = 0 ;
65    [t,u,s] = cputime() ;
66    dt = t - last_mytic ;
67    last_mytic = t ;
68 endfunction
69
70 ## s = msq(x)                   - Mean squared value, ignoring nans
71 ##
72 ## s == mean(x(:).^2) , but ignores NaN's
73
74
75 function s = msq(x)
76 try
77   s = mean(x(find(!isnan(x))).^2);
78 catch
79   s = nan;
80 end
81 endfunction
82
83
84 ctl = nan*zeros(1,5); ctl(5) = 1;
85
86 if verbose, printf ( "Going to call d2_min()\n"); end
87 mytic() ;
88 [xlev,vlev,nev] = d2_min ("ff", "d2ff", {xinit,y}, ctl) ;
89 tlev = mytic ();
90
91 if verbose,
92   printf("d2_min should find in one iteration + one more to check\n");
93   printf(["d2_min :  niter=%-4d  nev=%-4d  nobs=%-4d  nparams=%-4d\n",\
94           "  time=%-8.3g errx=%-8.3g   minv=%-8.3g\n"],...
95          nev([2,1]), R, P, tlev, max (abs (xlev-truep)), vlev);
96 end
97
98 ok = 1;
99 if nev(2) != 2,
100   if verbose
101       printf ( "Too many iterations for this function\n");
102   end
103   ok = 0;
104 end
105
106 if max (abs(xlev-truep )) > sqrt (eps),
107   if verbose
108       printf ( "Error is too big : %-8.3g\n", max (abs (xlev-truep)));
109   end
110   ok = 0;
111 end
112
113 if verbose && ok
114     printf ( "All tests ok\n");
115 end