1 ## Copyright (C) 2008-2012 Bill Denney
2 ## Copyright (C) 2008 Jaroslav Hajek
3 ## Copyright (C) 2009 VZLU Prague
5 ## This file is part of Octave.
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
9 ## the Free Software Foundation; either version 3 of the License, or (at
10 ## your option) any later version.
12 ## Octave is distributed in the hope that it will be useful, but
13 ## WITHOUT ANY WARRANTY; without even the implied warranty of
14 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 ## General Public License for more details.
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/>.
22 ## @deftypefn {Function File} {@var{x} =} pqpnonneg (@var{c}, @var{d})
23 ## @deftypefnx {Function File} {@var{x} =} pqpnonneg (@var{c}, @var{d}, @var{x0})
24 ## @deftypefnx {Function File} {[@var{x}, @var{minval}] =} pqpnonneg (@dots{})
25 ## @deftypefnx {Function File} {[@var{x}, @var{minval}, @var{exitflag}] =} pqpnonneg (@dots{})
26 ## @deftypefnx {Function File} {[@var{x}, @var{minval}, @var{exitflag}, @var{output}] =} pqpnonneg (@dots{})
27 ## @deftypefnx {Function File} {[@var{x}, @var{minval}, @var{exitflag}, @var{output}, @var{lambda}] =} pqpnonneg (@dots{})
28 ## Minimize @code{1/2*x'*c*x + d'*x} subject to @code{@var{x} >= 0}. @var{c}
29 ## and @var{d} must be real, and @var{c} must be symmetric and positive
30 ## definite. @var{x0} is an optional initial guess for @var{x}.
36 ## The minimum attained model value, 1/2*xmin'*c*xmin + d'*xmin
40 ## An indicator of convergence. 0 indicates that the iteration count
41 ## was exceeded, and therefore convergence was not reached; >0 indicates
42 ## that the algorithm converged. (The algorithm is stable and will
43 ## converge given enough iterations.)
47 ## A structure with two fields:
49 ## @item "algorithm": The algorithm used ("nnls")
51 ## @item "iterations": The number of iterations taken.
58 ## @seealso{optimset, lsqnonneg, qp}
61 ## PKG_ADD: ## Discard result to avoid polluting workspace with ans at startup.
62 ## PKG_ADD: [~] = __all_opts__ ("pqpnonneg");
64 ## This is analogical to the lsqnonneg implementation, which is
65 ## implemented from Lawson and Hanson's 1973 algorithm on page
66 ## 161 of Solving Least Squares Problems.
67 ## It shares the convergence guarantees.
69 function [x, minval, exitflag, output, lambda] = pqpnonneg (c, d, x = [], options = struct ())
71 if (nargin == 1 && ischar (c) && strcmp (c, 'defaults'))
72 x = optimset ("MaxIter", 1e5);
76 if (! (nargin >= 2 && nargin <= 4 && ismatrix (c) && ismatrix (d) && isstruct (options)))
80 ## Lawson-Hanson Step 1 (LH1): initialize the variables.
84 error ("pqpnonneg: matrix must be square");
88 ## Initial guess is 0s.
91 ## ensure nonnegative guess.
95 max_iter = optimget (options, "MaxIter", 1e5);
97 ## Initialize P, according to zero pattern of x.
99 ## Initialize the Cholesky factorization.
105 ## LH3: test for completion.
106 while (iter < max_iter)
107 while (iter < max_iter)
110 ## LH6: compute the positive matrix and find the min norm solution
111 ## of the positive problem.
113 xtmp = -(r \ (r' \ d(p)));
115 xtmp = -(c(p,p) \ d(p));
117 idx = find (xtmp < 0);
120 ## LH7: tmp solution found, iterate.
125 ## LH8, LH9: find the scaling factor.
127 sf = x(pidx)./(x(pidx) - xtmp(idx));
133 ## LH11: move from P to Z all X == 0.
134 ## This corresponds to those indices where minimum of sf is attained.
135 idx = idx (sf == alpha);
138 ## update the Cholesky factorization.
139 r = choldelete (r, idx);
144 ## compute the gradient.
149 ## verify the solution achieved using qr updating.
150 ## in the best case, this should only take a single step.
159 ## find the maximum gradient.
160 idx = find (w == max (w));
162 warning ("pqpnonneg:nonunique",
163 "a non-unique solution may be returned due to equal gradients");
166 ## move the index from Z to P. Keep P sorted.
167 z = [1:n]; z(p) = [];
169 jdx = 1 + lookup (p, zidx);
170 p = [p(1:jdx-1), zidx, p(jdx:end)];
172 ## insert the column into the Cholesky factorization.
173 [r, bad] = cholinsert (r, jdx, c(p,zidx));
175 ## If the insertion failed, we switch off updates and go on.
183 ## Generate the additional output arguments.
185 minval = 1/2*(x'*c*x) + d'*x;
188 if (nargout > 2 && iter >= max_iter)
192 output = struct ("algorithm", "nnls-pqp", "iterations", iter);
195 lambda = zeros (size (x));
205 %! assert (pqpnonneg (C, d), [0;0.5], 100*eps)
207 ## Test equivalence of lsq and pqp
209 %! C = rand (20, 10);
211 %! assert (pqpnonneg (C'*C, -C'*d), lsqnonneg (C, d), 100*eps)