annotate main/optim/inst/test_d2_min_1.m @ 9930:d30cfca46e8a octave-forge

optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
author carandraug
date Fri, 30 Mar 2012 15:14:48 +0000
parents 97f77d4bd426
children 28b882879c6e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9930
d30cfca46e8a optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents: 9389
diff changeset
1 ## Copyright (C) 2002 Etienne Grossmann <etienne@cs.uky.edu>
d30cfca46e8a optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents: 9389
diff changeset
2 ##
d30cfca46e8a optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents: 9389
diff changeset
3 ## This program is free software; you can redistribute it and/or modify it under
d30cfca46e8a optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents: 9389
diff changeset
4 ## the terms of the GNU General Public License as published by the Free Software
d30cfca46e8a optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents: 9389
diff changeset
5 ## Foundation; either version 3 of the License, or (at your option) any later
d30cfca46e8a optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents: 9389
diff changeset
6 ## version.
2370
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
7 ##
9930
d30cfca46e8a optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents: 9389
diff changeset
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
d30cfca46e8a optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents: 9389
diff changeset
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
d30cfca46e8a optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents: 9389
diff changeset
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
d30cfca46e8a optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents: 9389
diff changeset
11 ## details.
2370
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
12 ##
9930
d30cfca46e8a optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents: 9389
diff changeset
13 ## You should have received a copy of the GNU General Public License along with
d30cfca46e8a optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents: 9389
diff changeset
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
2370
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
15
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
16 ## Test whether d2_min() functions correctly
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
17 ##
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
18 ## Gives a simple quadratic programming problem (function ff below).
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
19 ##
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
20 ## Sets a ok variable to 1 in case of success, 0 in case of failure
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
21 ##
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
22 ## If a variables "verbose" is set, then some comments are output.
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
23
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
24 1 ;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
25
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
26 if ! exist ("verbose"), verbose = 0; end
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
27
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
28 if verbose
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
29 printf ("\n Testing d2_min () on a quadratic programming problem\n\n");
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
30 end
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
31
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
32 P = 10+floor(30*rand(1)) ; # Nparams
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
33 R = P+floor(30*rand(1)) ; # Nobses
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
34 noise = 0 ;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
35 global obsmat ;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
36 obsmat = randn(R,P) ;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
37 global truep ;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
38 truep = randn(P,1) ;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
39 xinit = randn(P,1) ;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
40
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
41 global obses ;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
42 obses = obsmat*truep ;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
43 if noise, obses = adnois(obses,noise); end
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
44
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
45
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
46
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
47 function v = ff(x)
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
48 global obsmat;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
49 global obses;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
50 v = msq (obses - obsmat*x ) ;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
51 endfunction
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
52
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
53 function [v,dv,d2v] = d2ff(x) # Return pseudo-inverse
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
54 global obsmat;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
55 global obses;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
56 er = -obses + obsmat*x ;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
57 dv = er'*obsmat ;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
58 v = msq(er ) ;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
59 d2v = pinv (obsmat'*obsmat ) ;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
60 endfunction
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
61
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
62 function [v,dv,d2v] = d2ff_2(x) # Return 2nd derivs, not pseudo-inv
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
63 global obsmat;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
64 global obses;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
65 er = -obses + obsmat*x ;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
66 dv = er'*obsmat ;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
67 v = msq(er ) ;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
68 d2v = obsmat'*obsmat ;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
69 endfunction
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
70
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
71 ## dt = mytic()
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
72 ##
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
73 ## Returns the cputime since last call to 'mytic'.
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
74
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
75 function dt = mytic()
9389
97f77d4bd426 replacing instances of `static' by `persistent' since it has been deprecated
carandraug
parents: 2370
diff changeset
76 persistent last_mytic = 0 ;
2370
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
77 [t,u,s] = cputime() ;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
78 dt = t - last_mytic ;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
79 last_mytic = t ;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
80 endfunction
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
81
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
82 ## s = msq(x) - Mean squared value, ignoring nans
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
83 ##
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
84 ## s == mean(x(:).^2) , but ignores NaN's
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
85
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
86
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
87 function s = msq(x)
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
88 try
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
89 s = mean(x(find(!isnan(x))).^2);
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
90 catch
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
91 s = nan;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
92 end
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
93 endfunction
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
94
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
95 cnt = 1;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
96 ok = 1;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
97
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
98 ctl = nan*zeros(1,5); ctl(5) = 1;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
99
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
100 if verbose
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
101 printf ("Going to call d2_min\n");
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
102 end
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
103 mytic() ;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
104 [xlev,vlev,nev] = d2_min ("ff","d2ff",xinit,ctl);
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
105 tlev = mytic() ;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
106
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
107 if verbose,
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
108 printf("d2_min should find in one iteration + one more to check\n");
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
109 printf(["d2_min : niter=%-4d nev=%-4d nobs=%-4d,nparams=%-4d\n",...
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
110 " time=%-8.3g errx=%-8.3g minv=%-8.3g\n"],...
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
111 nev([2,1]),R,P,tlev,max(abs(xlev-truep )),vlev);
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
112 end
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
113
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
114
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
115
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
116 if nev(2) != 2,
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
117 if verbose
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
118 printf ("Too many iterations for this function\n");
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
119 end
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
120 ok = 0;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
121 else
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
122 if verbose
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
123 printf ("Ok: single iteration (%i)\n",cnt);
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
124 end
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
125 end
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
126
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
127 if max (abs(xlev-truep )) > sqrt (eps),
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
128 if verbose
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
129 printf ("Error is too big : %-8.3g\n", max (abs (xlev-truep)));
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
130 end
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
131 ok = 0;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
132 else
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
133 if verbose
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
134 printf ("Ok: single error amplitude (%i)\n",cnt);
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
135 end
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
136 end
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
137
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
138 cnt++;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
139
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
140 if verbose
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
141 printf ("Going to call d2_min() \n");
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
142 end
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
143 mytic() ;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
144 [xlev,vlev,nev] = d2_min("ff","d2ff_2",xinit) ;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
145 tlev = mytic() ;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
146
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
147 if verbose,
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
148 printf("d2_min should find in one iteration + one more to check\n");
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
149 printf(["d2_min : niter=%-4d nev=%-4d nobs=%-4d,nparams=%-4d\n",...
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
150 " time=%-8.3g errx=%-8.3g minv=%-8.3g\n"],...
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
151 nev([2,1]),R,P,tlev,max(abs(xlev-truep )),vlev);
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
152 end
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
153
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
154
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
155 if nev(2) != 2,
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
156 if verbose
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
157 printf ("Too many iterations for this function\n");
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
158 end
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
159 ok = 0;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
160 else
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
161 if verbose
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
162 printf ("Ok: single iteration (%i)\n",cnt);
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
163 end
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
164 end
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
165
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
166 if max (abs(xlev-truep )) > sqrt (eps),
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
167 if verbose
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
168 printf ("Error is too big : %-8.3g\n", max (abs (xlev-truep)));
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
169 end
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
170 ok = 0;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
171 else
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
172 if verbose
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
173 printf ("Ok: single error amplitude (%i)\n",cnt);
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
174 end
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
175 end
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
176
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
177 if verbose
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
178 if ok
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
179 printf ("All tests ok\n");
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
180 else
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
181 printf ("Some tests failed\n");
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
182 end
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
183 end
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
184