LLSP: bvls.f

File bvls.f, 19.9 KB (added by dmitrey.kroshko, 4 years ago)
Line 
1c=======================================================================
2      subroutine bvls(key, m, n, a, b, bl, bu, maxIter, x, w, act, zz,
3     + istate, istop, msg, loopA)
4c=======================================================================
5c
6c$$$$ calls qr
7c--------------------Bounded Variable Least Squares---------------------
8c
9c        Robert L. Parker and Philip B. Stark    Version 3/19/90
10c
11c  Robert L. Parker                           Philip B. Stark
12c  Scripps Institution of Oceanography        Department of Statistics
13c  University of California, San Diego        University of California
14c  La Jolla CA 92093                          Berkeley CA 94720-3860
15c  rlparker@ucsd.edu                          stark@stat.berkeley.edu
16c
17c  Copyright of this software is reserved by the authors; however, this
18c  algorithm and subroutine may be used freely for non-commercial
19c  purposes, and may be distributed freely for non-commercial purposes. 
20c
21c  The authors do not warrant this software in any way: use it at your
22c  own risk.
23c
24c
25c  See the article ``Bounded Variable Least Squares:  An Algorithm and
26c  Applications'' by P.B. Stark and R.L. Parker, in the journal
27c  Computational Statistics, in press (1995) for further description
28c  and applications to minimum l-1, l-2 and l-infinity fitting problems,
29c  as well as finding bounds on linear functionals subject to bounds on
30c  variables and fitting linear data within l-1, l-2 or l-infinity
31c  measures of misfit.
32c
33c  BVLS solves the problem:
34c
35c          min  || a.x - b ||     such that   bl <= x <= bu
36c                            2
37c    where 
38c               x  is an unknown n-vector
39c               a  is a given m by n matrix
40c               b  is a given  m-vector
41c               bl is a given n-vector of lower bounds on the
42c                                components of x.
43c               bu is a given n-vector of upper bounds on the
44c                                components of x.
45c
46c               
47c-----------------------------------------------------------------------
48c    Input parameters:
49c
50c  m, n, a, b, bl, bu   see above.   Let mm=min(m,n).
51c  maxIter - max number of iterations. Default value by routine authors
52c  was 3*n (changed by Dmitrey)
53c
54c  If key = 0, the subroutine solves the problem from scratch.
55c
56c  If key > 0 the routine initializes using the user's guess about
57c   which components of  x  are `active', i.e. are stricly within their
58c   bounds, which are at their lower bounds, and which are at their
59c   upper bounds.  This information is supplied through the array 
60c   istate.  istate(n+1) should contain the total number of components
61c   at their bounds (the `bound variables').  The absolute values of the
62c   first nbound=istate(n+1) entries of  istate  are the indices
63c   of these `bound' components of  x.  The sign of istate(j), j=1,...,
64c   nbound, indicates whether  x(|istate(j)|) is at its upper or lower
65c   bound.  istate(j) is positive if the component is at its upper
66c   bound, negative if the component is at its lower bound.
67c   istate(j), j=nbound+1,...,n  contain the indices of the components
68c   of  x  that are active (i.e. are expected to lie strictly within
69c   their bounds).  When key > 0, the routine initially sets the active
70c   components to the averages of their upper and lower bounds:
71c   x(j)=(bl(j)+bu(j))/2, for j in the active set. 
72c
73c-----------------------------------------------------------------------
74c    Output parameters:
75c
76c  x       the solution vector.
77c
78c  w(1)    the minimum 2-norm || a.x-b ||.
79c
80c  istate  vector indicating which components of  x  are active and
81c          which are at their bounds (see the previous paragraph). 
82c          istate can be supplied to the routine to give it a good
83c          starting guess for the solution.
84c
85c  loopA   number of iterations taken in the main loop, Loop A.
86c  istop   stop condition case, should be positive if converged
87c  msg     stop condition case text description
88c
89c-----------------------------------------------------------------------
90c    Working  arrays:
91c
92c  w      dimension n.               act      dimension m*(mm+2).
93c  zz     dimension m.               istate   dimension n+1.
94c
95c-----------------------------------------------------------------------
96c  Method: active variable method along the general plan of NNLS by
97c  Lawson & Hanson, "Solving Least Squares Problems," 1974.  See
98c  Algorithm 23.10.  Step numbers in comment statements refer to their
99c  scheme.
100c  For more details and further uses, see the article
101c  "Bounded Variable Least Squares:  An Algorithm and Applications"
102c  by Stark and Parker in 1995 Computational Statistics.
103c
104c-----------------------------------------------------------------------
105c  A number of measures are taken to enhance numerical reliability:
106c
107c 1. As noted by Lawson and Hanson, roundoff errors in the computation
108c   of the gradient of the misfit may cause a component on the bounds
109c   to appear to want to become active, yet when the component is added
110c   to the active set, it moves away from the feasible region.  In this
111c   case the component is not made active, the gradient of the misfit
112c   with respect to a change in that component is set to zero, and the
113c   program returns to the Kuhn-Tucker test.  Flag  ifrom5  is used in
114c   this test, which occurs at the end of Step 6.
115c
116c
117c 2. When the least-squares minimizer after Step 6 is infeasible, it
118c   is used in a convex interpolation with the previous solution to
119c   obtain a feasible vector.  The constant in this interpolation is
120c   supposed to put at least one component of  x   on a bound. There can
121c   be difficulties:
122c
123c 2a. Sometimes, due to roundoff, no interpolated component ends up on
124c   a bound.  The code in Step 11 uses the flag  jj, computed in Step 8,
125c   to ensure that at least the component that determined the
126c   interpolation constant  alpha  is moved to the appropriate bound. 
127c   This guarantees that what Lawson and Hanson call `Loop B' is finite.
128c
129c 2b. The code in Step 11 also incorporates Lawson and Hanson's feature
130c   that any components remaining infeasible at this stage (which must
131c   be due to roundoff) are moved to their nearer bound.
132c
133c
134c 3. If the columns of  a  passed to qr are linearly dependent, the new
135c   potentially active component is not introduced: the gradient of the
136c   misfit with respect to that component is set to zero, and control
137c   returns to the Kuhn-Tucker test.
138c
139c
140c 4. When some of the columns of  a  are approximately linearly
141c   dependent, we have observed cycling of active components: a
142c   component just moved to a bound desires immediately to become
143c   active again; qr allows it to become active and a different
144c   component is moved to its bound.   This component immediately wants
145c   to become active, which qr allows, and the original component is
146c   moved back to its bound.  We have taken two steps to avoid this
147c   problem:
148c
149c 4a. First, the column of the matrix  a  corresponding to the new
150c   potentially active component is passed to qr as the last column of
151c   its matrix.  This ordering tends to make a component recently moved
152c   to a bound fail the test mentioned in (1), above.
153c
154c 4b. Second, we have incorporated a test that prohibits short cycles.
155c   If the most recent successful change to the active set was to move
156c   the component x(jj) to a bound, x(jj) is not permitted to reenter
157c   the solution at this stage.  This test occurs just after checking
158c   the Kuhn-Tucker conditions, and uses the flag  jj, set in Step 8.
159c   The flag  jj  is reset after Step 6 if Step 6 was entered from
160c   Step 5 indicating that a new component has successfully entered the
161c   active set. The test for resetting  jj  uses the flag  ifrom5,
162c   which will not equal zero in case Step 6 was entered from Step 5.
163c
164c
165c-----------------------------------------------------------------------
166
167cf2py intent(out) x, w, istop, msg, loopa
168c-f-2-p-y intent(-in) key, a, b, bl, bu, maxIter, istate
169
170c  Initialize flags, etc.
171      implicit double precision (a-h, o-z)
172      character *(64) msg
173      dimension a(m,n), b(m), x(n), bl(n), bu(n)
174      dimension w(n), act(m,m+2), zz(m), istate(n+1)
175
176      data eps/1.0d-11/
177c
178c----------------------First Executable Statement-----------------------
179c
180c  Step 1.  Initialize everything--active and bound sets, initial
181c   values, etc.
182c
183
184      istop = 1000
185      msg = 'Converged'
186
187      mm=min(m,n)
188      mm1 = mm + 1
189      jj = 0
190      ifrom5 = 0
191c  Check consistency of given bounds  bl, bu.
192      bdiff = 0.0
193      do 1005 j=1, n
194        bdiff=max(bdiff, bu(j)-bl(j))
195        if (bl(j) .gt. bu(j)) then
196          istop = -101
197          msg = 'Inconsistent bounds in BVLS.'
198          return
199c          stop
200        endif
201 1005 continue
202      if (bdiff .eq. 0.0) then
203        istop = -102
204        msg = 'No free variables in BVLS--check input bounds'
205        return
206c        stop
207      endif
208c
209c  In a fresh initialization (key = 0) bind all variables at their lower
210c   bounds.  If (key != 0), use the supplied  istate  vector to
211c   initialize the variables.  istate(n+1) contains the number of
212c   bound variables.  The absolute values of the first
213c   nbound=istate(n+1) entries of  istate  are the indices of the bound
214c   variables.  The sign of each entry determines whether the indicated
215c   variable is at its upper (positive) or lower (negative) bound.
216      if (key .eq. 0) then
217        nbound=n
218        nact=0
219        do 1010 j=1, nbound
220          istate(j)=-j
221 1010   continue
222      else
223        nbound=istate(n+1)
224      endif
225      nact=n - nbound
226      if ( nact .gt. mm ) then
227        istop = -103
228        msg = 'Too many active variables in BVLS starting solution!'
229        return
230c        stop
231      endif
232      do 1100 k=1, nbound
233        j=abs(istate(k))
234        if (istate(k) .lt. 0) x(j)=bl(j)
235        if (istate(k) .gt. 0) x(j)=bu(j)
236 1100 continue
237c
238c  In a warm start (key != 0) initialize the active variables to
239c   (bl+bu)/2.  This is needed in case the initial qr results in
240c   active variables out-of-bounds and Steps 8-11 get executed the
241c   first time through.
242      do 1150 k=nbound+1,n
243        kk=istate(k)
244        x(kk)=(bu(kk)+bl(kk))/2
245 1150 continue
246c
247c  Compute bnorm, the norm of the data vector b, for reference.
248      bsq=0.0
249      do 1200 i=1, m
250        bsq=bsq + b(i)**2
251 1200 continue
252      bnorm=sqrt(bsq)
253c
254c-----------------------------Main Loop---------------------------------
255c
256c  Initialization complete.  Begin major loop (Loop A).
257      do 15000 loopA=1, maxIter
258c
259c  Step 2.
260c  Initialize the negative gradient vector w(*).
261 2000 obj=0.0
262      do 2050 j=1, n
263        w(j)=0.0
264 2050 continue
265c
266c  Compute the residual vector b-a.x , the negative gradient vector
267c   w(*), and the current objective value obj = || a.x - b ||.
268c   The residual vector is stored in the mm+1'st column of act(*,*).
269      do 2300 i=1, m
270        ri=b(i)
271        do 2100 j=1, n
272          ri=ri - a(i,j)*x(j)
273 2100   continue
274        obj=obj + ri**2
275        do 2200 j=1, n
276          w(j)=w(j) + a(i,j)*ri
277 2200   continue
278        act(i,mm1)=ri
279 2300 continue
280c
281c  Converged?  Stop if the misfit << || b ||, or if all components are
282c   active (unless this is the first iteration from a warm start).
283      if (sqrt(obj) .le. bnorm*eps .or.
284     +  (loopA .gt. 1 .and. nbound .eq. 0)) then
285         istate(n+1)=nbound
286         w(1)=sqrt(obj)
287         return
288      endif
289c
290c  Add the contribution of the active components back into the residual.
291      do 2500 k=nbound+1, n
292        j=istate(k)
293        do 2400 i=1, m
294          act(i,mm1)=act(i,mm1) + a(i,j)*x(j)
295 2400   continue
296 2500 continue
297c
298c  The first iteration in a warm start requires immediate qr.
299      if (loopA .eq. 1 .and. key .ne. 0) goto 6000
300c
301c  Steps 3, 4.
302c  Find the bound element that most wants to be active.
303 3000 worst=0.0
304      it=1
305      do 3100 j=1, nbound
306         ks=abs(istate(j))
307         bad=w(ks)*sign(1, istate(j))
308         if (bad .lt. worst) then
309            it=j
310            worst=bad
311            iact=ks
312         endif
313 3100 continue
314c
315c  Test whether the Kuhn-Tucker condition is met.
316      if (worst .ge. 0.0 ) then
317         istate(n+1)=nbound
318         w(1)=sqrt(obj)
319         return
320      endif
321c
322c  The component  x(iact)  is the one that most wants to become active.
323c   If the last successful change in the active set was to move x(iact)
324c   to a bound, don't let x(iact) in now: set the derivative of the
325c   misfit with respect to x(iact) to zero and return to the Kuhn-Tucker
326c   test.
327      if ( iact .eq. jj ) then
328        w(jj)=0.0
329        goto 3000
330      endif
331c
332c  Step 5.
333c  Undo the effect of the new (potentially) active variable on the
334c   residual vector.
335      if (istate(it) .gt. 0) bound=bu(iact)
336      if (istate(it) .lt. 0) bound=bl(iact)
337      do 5100 i=1, m
338        act(i,mm1)=act(i,mm1) + bound*a(i,iact)
339 5100 continue
340c
341c  Set flag ifrom5, indicating that Step 6 was entered from Step 5.
342c   This forms the basis of a test for instability: the gradient
343c   calculation shows that x(iact) wants to join the active set; if
344c   qr puts x(iact) beyond the bound from which it came, the gradient
345c   calculation was in error and the variable should not have been
346c   introduced.
347      ifrom5=istate(it)
348c
349c  Swap the indices (in istate) of the new active variable and the
350c   rightmost bound variable; `unbind' that location by decrementing
351c   nbound.
352      istate(it)=istate(nbound)
353      nbound=nbound - 1
354      nact=nact + 1
355      istate(nbound+1)=iact
356c
357      if (mm .lt. nact) then
358        istop = -104
359        msg = 'Too many free variables in BVLS!'
360        return
361c        stop
362      endif
363c
364c  Step 6.
365c  Load array  act  with the appropriate columns of  a  for qr.  For
366c   added stability, reverse the column ordering so that the most
367c   recent addition to the active set is in the last column.  Also
368c   copy the residual vector from act(., mm1) into act(., mm1+1).
369 6000 do 6200 i=1, m
370        act(i,mm1+1)=act(i,mm1)
371        do 6100 k=nbound+1, n
372          j=istate(k)
373          act(i,nact+1-k+nbound)=a(i,j)
374 6100   continue
375 6200 continue
376c
377      call qr(m, nact, act, act(1,mm1+1), zz, resq)
378c
379c  Test for linear dependence in qr, and for an instability that moves
380c   the variable just introduced away from the feasible region
381c   (rather than into the region or all the way through it).
382c   In either case, remove the latest vector introduced from the
383c   active set and adjust the residual vector accordingly. 
384c   Set the gradient component (w(iact)) to zero and return to
385c   the Kuhn-Tucker test.
386      if (resq .lt. 0.0
387     +   .or. (ifrom5 .gt. 0 .and. zz(nact) .gt. bu(iact))
388     +   .or. (ifrom5 .lt. 0 .and. zz(nact) .lt. bl(iact))) then
389         nbound=nbound + 1
390         istate(nbound)=istate(nbound)*sign(1.0d0, x(iact)-bu(iact))
391         nact=nact - 1
392         do 6500 i=1, m
393           act(i,mm1)=act(i,mm1) - x(iact)*a(i,iact)
394 6500    continue
395         ifrom5=0
396         w(iact)=0.0
397         goto 3000
398      endif
399c
400c  If Step 6 was entered from Step 5 and we are here, a new variable
401c   has been successfully introduced into the active set; the last
402c   variable that was fixed at a bound is again permitted to become
403c   active.
404      if ( ifrom5 .ne. 0 ) jj=0
405      ifrom5=0
406c
407c   Step 7.  Check for strict feasibility of the new qr solution.
408      do 7100 k=1, nact
409        k1=k
410        j=istate(k+nbound)
411        if (zz(nact+1-k).lt.bl(j) .or. zz(nact+1-k).gt.bu(j)) goto 8000
412 7100 continue
413      do 7200 k=1, nact
414        j=istate(k+nbound)
415        x(j)=zz(nact+1-k)
416 7200 continue
417c  New iterate is feasible; back to the top.
418      goto 15000
419c
420c  Steps 8, 9.
421 8000 alpha=2.0
422      alf=alpha
423      do 8200 k=k1, nact
424        j=istate(k+nbound)
425        if (zz(nact+1-k) .gt. bu(j))
426     +    alf=(bu(j)-x(j))/(zz(nact+1-k)-x(j))
427        if (zz(nact+1-k) .lt. bl(j))
428     +    alf=(bl(j)-x(j))/(zz(nact+1-k)-x(j))
429        if (alf .lt. alpha) then
430          alpha=alf
431          jj=j
432          sj=sign(1.0, zz(nact+1-k)-bl(j))
433        endif
434 8200 continue
435c
436c  Step 10
437      do 10000 k=1, nact
438        j=istate(k+nbound)
439        x(j)=x(j) + alpha*(zz(nact+1-k)-x(j))
44010000 continue
441c
442c  Step 11. 
443c  Move the variable that determined alpha to the appropriate bound. 
444c   (jj is its index; sj is + if zz(jj)> bu(jj), - if zz(jj)<bl(jj) ).
445c   If any other component of  x  is infeasible at this stage, it must
446c   be due to roundoff.  Bind every infeasible component and every
447c   component at a bound to the appropriate bound.  Correct the
448c   residual vector for any variables moved to bounds.  Since at least
449c   one variable is removed from the active set in this step, Loop B
450c   (Steps 6-11) terminates after at most  nact  steps.
451      noldb=nbound
452      do 11200 k=1, nact
453        j=istate(k+noldb)
454        if (((bu(j)-x(j)) .le. 0.0) .or.
455     +    (j .eq. jj .and. sj .gt. 0.0)) then
456c  Move x(j) to its upper bound.
457          x(j)=bu(j)
458          istate(k+noldb)=istate(nbound+1)
459          istate(nbound+1)=j
460          nbound=nbound+1
461          do 11100 i=1, m
462             act(i,mm1)=act(i,mm1) - bu(j)*a(i,j)
46311100     continue
464        else if (((x(j)-bl(j)) .le. 0.0) .or.
465     +    (j .eq. jj .and. sj .lt. 0.0)) then
466c  Move x(j) to its lower bound.
467          x(j)=bl(j)
468          istate(k+noldb)=istate(nbound+1)
469          istate(nbound+1)=-j
470          nbound=nbound+1
471          do 11150 i=1, m
472             act(i,mm1)=act(i,mm1) - bl(j)*a(i,j)
47311150     continue
474        endif
47511200 continue
476      nact=n - nbound
477c
478c  If there are still active variables left repeat the qr; if not,
479c    go back to the top.
480      if (nact .gt. 0 ) goto 6000
481     
482c
48315000 continue
484c
485      msg = 'BVLS fails to converge'
486      istop = -105
487      return
488c      stop
489      end
490c======================================================================
491      subroutine qr(m, n, a, b, x, resq)
492c======================================================================
493      implicit double precision (a-h, o-z)                              *doub
494c$$$$ calls no other routines
495c  Relies on FORTRAN77 do-loop conventions!
496c  Solves over-determined least-squares problem  ax ~ b
497c  where  a  is an  m by n  matrix,  b  is an m-vector .
498c  resq  is the sum of squared residuals of optimal solution.  Also used
499c  to signal error conditions - if -2 , system is underdetermined,  if
500c  -1,  system is singular.
501c  Method - successive Householder rotations.  See Lawson & Hanson -
502c  Solving Least Squares Problems (1974).
503c  Routine will also work when m=n.
504c*****   CAUTION -  a and b  are overwritten by this routine.
505      dimension a(m,n),b(m),x(n)
506      double precision sum, dot
507c
508      resq=-2.0
509      if (m .lt. n) return
510      resq=-1.0
511c   Loop ending on 1800 rotates  a  into upper triangular form.
512      do 1800 j=1, n
513c   Find constants for rotation and diagonal entry.
514        sq=0.0
515        do 1100 i=j, m
516          sq=a(i,j)**2 + sq
517 1100   continue
518        if (sq .eq. 0.0) return
519        qv1=-sign(sqrt(sq), a(j,j))
520        u1=a(j,j) - qv1
521        a(j,j)=qv1
522        j1=j + 1
523c  Rotate remaining columns of sub-matrix.
524        do 1400 jj=j1, n
525          dot=u1*a(j,jj)
526          do 1200 i=j1, m
527            dot=a(i,jj)*a(i,j) + dot
528 1200     continue
529          const=dot/abs(qv1*u1)
530          do 1300 i=j1, m
531            a(i,jj)=a(i,jj) - const*a(i,j)
532 1300     continue
533          a(j,jj)=a(j,jj) - const*u1
534 1400   continue
535c  Rotate  b  vector.
536        dot=u1*b(j)
537        do 1600 i=j1, m
538          dot=b(i)*a(i,j) + dot
539 1600   continue
540        const=dot/abs(qv1*u1)
541        b(j)=b(j) - const*u1
542        do 1700 i=j1, m
543          b(i)=b(i) - const*a(i,j)
544 1700   continue
545 1800 continue
546c  Solve triangular system by back-substitution.
547      do 2200 ii=1, n
548        i=n-ii+1
549        sum=b(i)
550        do 2100 j=i+1, n
551          sum=sum - a(i,j)*x(j)
552 2100   continue
553        if (a(i,i).eq. 0.0) return
554         x(i)=sum/a(i,i)
555 2200 continue
556c  Find residual in overdetermined case.
557      resq=0.0
558      do 2300 i=n+1, m
559        resq=b(i)**2 + resq
560 2300 continue
561      return
562      end                                                               qr
563c______________________________________________________________________
564