Mercurial > octave
annotate scripts/optimization/lsqnonneg.m @ 23220:092078913d54
maint: Merge stable to default.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 22 Feb 2017 12:58:07 -0500 |
parents | ef4d915df748 3ac9f9ecfae5 |
children | 194eb4bd202b |
rev | line source |
---|---|
23219
3ac9f9ecfae5
maint: Update copyright dates.
John W. Eaton <jwe@octave.org>
parents:
23083
diff
changeset
|
1 ## Copyright (C) 2008-2017 Bill Denney |
8406
0b7566aea627
fix & optimize lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
8304
diff
changeset
|
2 ## Copyright (C) 2008 Jaroslav Hajek |
8600 | 3 ## Copyright (C) 2009 VZLU Prague |
7682 | 4 ## |
5 ## This file is part of Octave. | |
6 ## | |
7 ## Octave is free software; you can redistribute it and/or modify it | |
8 ## under the terms of the GNU General Public License as published by | |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
9 ## the Free Software Foundation; either version 3 of the License, or |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
10 ## (at your option) any later version. |
7682 | 11 ## |
12 ## Octave is distributed in the hope that it will be useful, but | |
13 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
14 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
15 ## GNU General Public License for more details. |
7682 | 16 ## |
17 ## You should have received a copy of the GNU General Public License | |
18 ## along with Octave; see the file COPYING. If not, see | |
19 ## <http://www.gnu.org/licenses/>. | |
20 | |
21 ## -*- texinfo -*- | |
20852
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20735
diff
changeset
|
22 ## @deftypefn {} {@var{x} =} lsqnonneg (@var{c}, @var{d}) |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20735
diff
changeset
|
23 ## @deftypefnx {} {@var{x} =} lsqnonneg (@var{c}, @var{d}, @var{x0}) |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20735
diff
changeset
|
24 ## @deftypefnx {} {@var{x} =} lsqnonneg (@var{c}, @var{d}, @var{x0}, @var{options}) |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20735
diff
changeset
|
25 ## @deftypefnx {} {[@var{x}, @var{resnorm}] =} lsqnonneg (@dots{}) |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20735
diff
changeset
|
26 ## @deftypefnx {} {[@var{x}, @var{resnorm}, @var{residual}] =} lsqnonneg (@dots{}) |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20735
diff
changeset
|
27 ## @deftypefnx {} {[@var{x}, @var{resnorm}, @var{residual}, @var{exitflag}] =} lsqnonneg (@dots{}) |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20735
diff
changeset
|
28 ## @deftypefnx {} {[@var{x}, @var{resnorm}, @var{residual}, @var{exitflag}, @var{output}] =} lsqnonneg (@dots{}) |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20735
diff
changeset
|
29 ## @deftypefnx {} {[@var{x}, @var{resnorm}, @var{residual}, @var{exitflag}, @var{output}, @var{lambda}] =} lsqnonneg (@dots{}) |
22769 | 30 ## |
31 ## Minimize @code{norm (@var{c}*@var{x} - @var{d})} subject to | |
20165
f1d0f506ee78
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
32 ## @code{@var{x} >= 0}. |
f1d0f506ee78
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
33 ## |
22769 | 34 ## @var{c} and @var{d} must be real matrices. |
20165
f1d0f506ee78
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
35 ## |
22769 | 36 ## @var{x0} is an optional initial guess for the solution @var{x}. |
20165
f1d0f506ee78
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
37 ## |
22769 | 38 ## @var{options} is an options structure to change the behavior of the |
39 ## algorithm (@pxref{XREFoptimset,,optimset}. @code{lsqnonneg} recognizes | |
40 ## these options: @qcode{"MaxIter"}, @qcode{"TolX"}. | |
7682 | 41 ## |
42 ## Outputs: | |
14366
b76f0740940e
doc: Periodic grammar check of documentation.
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
43 ## |
22770
31b86d30874d
lsqnonneg.m: Change @itemize to @table so docs compile.
Rik <rik@octave.org>
parents:
22769
diff
changeset
|
44 ## @table @var |
7682 | 45 ## @item resnorm |
22769 | 46 ## The squared 2-norm of the residual: @code{norm (@var{c}*@var{x}-@var{d})^2} |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
47 ## |
7682 | 48 ## @item residual |
22769 | 49 ## The residual: @code{@var{d}-@var{c}*@var{x}} |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
50 ## |
7682 | 51 ## @item exitflag |
20165
f1d0f506ee78
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
52 ## An indicator of convergence. 0 indicates that the iteration count was |
f1d0f506ee78
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
53 ## exceeded, and therefore convergence was not reached; >0 indicates that the |
f1d0f506ee78
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
54 ## algorithm converged. (The algorithm is stable and will converge given |
f1d0f506ee78
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
55 ## enough iterations.) |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
56 ## |
7682 | 57 ## @item output |
58 ## A structure with two fields: | |
14366
b76f0740940e
doc: Periodic grammar check of documentation.
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
59 ## |
7682 | 60 ## @itemize @bullet |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
61 ## @item @qcode{"algorithm"}: The algorithm used (@qcode{"nnls"}) |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
62 ## |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
63 ## @item @qcode{"iterations"}: The number of iterations taken. |
7682 | 64 ## @end itemize |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
65 ## |
7682 | 66 ## @item lambda |
22769 | 67 ## @c FIXME: Something is output from the function, but what is it? |
68 ## Undocumented output | |
69 ## @end table | |
70 ## @seealso{pqpnonneg, lscov, optimset} | |
7682 | 71 ## @end deftypefn |
72 | |
13027
b9a89ca0fb75
prevent optimization functions from setting ans in workspace at startup
John W. Eaton <jwe@octave.org>
parents:
11588
diff
changeset
|
73 ## PKG_ADD: ## Discard result to avoid polluting workspace with ans at startup. |
b9a89ca0fb75
prevent optimization functions from setting ans in workspace at startup
John W. Eaton <jwe@octave.org>
parents:
11588
diff
changeset
|
74 ## PKG_ADD: [~] = __all_opts__ ("lsqnonneg"); |
8648
ff61b53eb294
optimization: use PKG_ADD: comments instead of PKG_ADD file
John W. Eaton <jwe@octave.org>
parents:
8600
diff
changeset
|
75 |
22769 | 76 ## This is implemented from Lawson and Hanson's 1973 algorithm on page 161 of |
77 ## Solving Least Squares Problems. | |
7682 | 78 |
22769 | 79 function [x, resnorm, residual, exitflag, output, lambda] = lsqnonneg (c, d, |
80 x0 = [], | |
81 options = struct ()) | |
8600 | 82 |
22769 | 83 ## Special case: called to find default optimization options |
84 if (nargin == 1 && ischar (c) && strcmp (c, "defaults")) | |
8600 | 85 x = optimset ("MaxIter", 1e5); |
17312
088d014a7fe2
Use semicolon after "return" statement in core m-files.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
86 return; |
8600 | 87 endif |
88 | |
22769 | 89 if (nargin < 2 || nargin > 4) |
8600 | 90 print_usage (); |
91 endif | |
7682 | 92 |
22769 | 93 if (! (isnumeric (c) && ismatrix (c)) || ! (isnumeric (d) && ismatrix (d))) |
94 error ("lsqnonneg: C and D must be numeric matrices"); | |
95 endif | |
96 | |
97 if (! isstruct (options)) | |
98 error ("lsqnonneg: OPTIONS must be a struct"); | |
99 endif | |
100 | |
7697
0bdfff62cc49
lsqnonneg: use optimset, correctly index Z and P in main loop
bill@denney.ws
parents:
7682
diff
changeset
|
101 ## Lawson-Hanson Step 1 (LH1): initialize the variables. |
8600 | 102 m = rows (c); |
103 n = columns (c); | |
22769 | 104 if (isempty (x0)) |
105 ## Initial guess is all zeros. | |
8600 | 106 x = zeros (n, 1); |
107 else | |
108 ## ensure nonnegative guess. | |
22769 | 109 x = max (x0, 0); |
7682 | 110 endif |
111 | |
22769 | 112 useqr = (m >= n); |
8507 | 113 max_iter = optimget (options, "MaxIter", 1e5); |
7682 | 114 |
8600 | 115 ## Initialize P, according to zero pattern of x. |
116 p = find (x > 0).'; | |
117 if (useqr) | |
118 ## Initialize the QR factorization, economized form. | |
119 [q, r] = qr (c(:,p), 0); | |
8406
0b7566aea627
fix & optimize lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
8304
diff
changeset
|
120 endif |
0b7566aea627
fix & optimize lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
8304
diff
changeset
|
121 |
7682 | 122 iter = 0; |
8600 | 123 |
7697
0bdfff62cc49
lsqnonneg: use optimset, correctly index Z and P in main loop
bill@denney.ws
parents:
7682
diff
changeset
|
124 ## LH3: test for completion. |
8600 | 125 while (iter < max_iter) |
126 while (iter < max_iter) | |
20735
418ae0cb752f
Replace ++,-- with in-place operators for performance.
Rik <rik@octave.org>
parents:
20231
diff
changeset
|
127 iter += 1; |
8600 | 128 |
129 ## LH6: compute the positive matrix and find the min norm solution | |
130 ## of the positive problem. | |
131 if (useqr) | |
132 xtmp = r \ q'*d; | |
133 else | |
134 xtmp = c(:,p) \ d; | |
135 endif | |
136 idx = find (xtmp < 0); | |
137 | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
138 if (isempty (idx)) |
8600 | 139 ## LH7: tmp solution found, iterate. |
140 x(:) = 0; | |
141 x(p) = xtmp; | |
142 break; | |
143 else | |
144 ## LH8, LH9: find the scaling factor. | |
145 pidx = p(idx); | |
22769 | 146 sf = x(pidx) ./ (x(pidx) - xtmp(idx)); |
8600 | 147 alpha = min (sf); |
148 ## LH10: adjust X. | |
149 xx = zeros (n, 1); | |
150 xx(p) = xtmp; | |
151 x += alpha*(xx - x); | |
152 ## LH11: move from P to Z all X == 0. | |
153 ## This corresponds to those indices where minimum of sf is attained. | |
20231
83792dd9bcc1
Use in-place operators in m-files where possible.
Rik <rik@octave.org>
parents:
20165
diff
changeset
|
154 idx = idx(sf == alpha); |
8600 | 155 p(idx) = []; |
156 if (useqr) | |
157 ## update the QR factorization. | |
158 [q, r] = qrdelete (q, r, idx); | |
159 endif | |
160 endif | |
161 endwhile | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
162 |
8600 | 163 ## compute the gradient. |
164 w = c'*(d - c*x); | |
165 w(p) = []; | |
14787
acb09716fc94
lsqnonneg have tolerance option for convergence (bug #33347)
Axel Mathéi <axel.mathei@gmail.com>
parents:
14366
diff
changeset
|
166 tolx = optimget (options, "TolX", 10*eps*norm (c, 1)*length (c)); |
acb09716fc94
lsqnonneg have tolerance option for convergence (bug #33347)
Axel Mathéi <axel.mathei@gmail.com>
parents:
14366
diff
changeset
|
167 if (! any (w > tolx)) |
8600 | 168 if (useqr) |
169 ## verify the solution achieved using qr updating. | |
170 ## in the best case, this should only take a single step. | |
171 useqr = false; | |
172 continue; | |
173 else | |
174 ## we're finished. | |
175 break; | |
176 endif | |
177 endif | |
178 | |
179 ## find the maximum gradient. | |
7682 | 180 idx = find (w == max (w)); |
181 if (numel (idx) > 1) | |
182 warning ("lsqnonneg:nonunique", | |
11588
d5bd2766c640
style fixes for warning and error messages in script files
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
183 "a non-unique solution may be returned due to equal gradients"); |
7682 | 184 idx = idx(1); |
185 endif | |
21751
b571fc85953f
maint: Use two spaces after period to indicate sentence break.
Rik <rik@octave.org>
parents:
21178
diff
changeset
|
186 ## move the index from Z to P. Keep P sorted. |
8600 | 187 z = [1:n]; z(p) = []; |
188 zidx = z(idx); | |
189 jdx = 1 + lookup (p, zidx); | |
190 p = [p(1:jdx-1), zidx, p(jdx:end)]; | |
191 if (useqr) | |
192 ## insert the column into the QR factorization. | |
193 [q, r] = qrinsert (q, r, jdx, c(:,zidx)); | |
194 endif | |
7682 | 195 |
196 endwhile | |
7697
0bdfff62cc49
lsqnonneg: use optimset, correctly index Z and P in main loop
bill@denney.ws
parents:
7682
diff
changeset
|
197 ## LH12: complete. |
7682 | 198 |
7697
0bdfff62cc49
lsqnonneg: use optimset, correctly index Z and P in main loop
bill@denney.ws
parents:
7682
diff
changeset
|
199 ## Generate the additional output arguments. |
22769 | 200 if (isargout (2)) |
8406
0b7566aea627
fix & optimize lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
8304
diff
changeset
|
201 resnorm = norm (c*x - d) ^ 2; |
7682 | 202 endif |
22769 | 203 if (isargout (3)) |
8406
0b7566aea627
fix & optimize lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
8304
diff
changeset
|
204 residual = d - c*x; |
7682 | 205 endif |
22769 | 206 if (isargout (4)) |
207 if (iter >= max_iter) | |
208 exitflag = 0; | |
209 else | |
210 exitflag = iter; | |
211 endif | |
7682 | 212 endif |
22769 | 213 if (isargout (5)) |
7682 | 214 output = struct ("algorithm", "nnls", "iterations", iter); |
215 endif | |
22769 | 216 if (isargout (6)) |
8600 | 217 lambda = zeros (size (x)); |
218 lambda(p) = w; | |
7682 | 219 endif |
220 | |
221 endfunction | |
222 | |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
223 |
7682 | 224 %!test |
225 %! C = [1 0;0 1;2 1]; | |
226 %! d = [1;3;-2]; | |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
227 %! assert (lsqnonneg (C, d), [0;0.5], 100*eps); |
7682 | 228 |
229 %!test | |
230 %! C = [0.0372 0.2869;0.6861 0.7071;0.6233 0.6245;0.6344 0.6170]; | |
231 %! d = [0.8587;0.1781;0.0747;0.8405]; | |
232 %! xnew = [0;0.6929]; | |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
233 %! assert (lsqnonneg (C, d), xnew, 0.0001); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
234 |
22769 | 235 # Test input validation |
236 %!error lsqnonneg () | |
237 %!error lsqnonneg (1) | |
238 %!error lsqnonneg (1,2,3,4,5) | |
239 %!error <C .* must be numeric matrices> lsqnonneg ({1},2) | |
240 %!error <C .* must be numeric matrices> lsqnonneg (ones (2,2,2),2) | |
241 %!error <D must be numeric matrices> lsqnonneg (1,{2}) | |
242 %!error <D must be numeric matrices> lsqnonneg (1, ones (2,2,2)) | |
243 %!error <OPTIONS must be a struct> lsqnonneg (1, 2, [], 3) |