15

I'm trying to perform a constrained least-squares estimation using Scipy such that all of the coefficients are in the range (0,1) and sum to 1 (this functionality is implemented in Matlab's LSQLIN function).

Does anybody have tips for setting up this calculation using Python/Scipy. I believe I should be using scipy.optimize.fmin_slsqp(), but am not entirely sure what parameters I should be passing to it.[1]

Many thanks for the help, Nick

[1] The one example in the documentation for fmin_slsqp is a bit difficult for me to parse without the referenced text -- and I'm new to using Scipy.

Nick
  • 655
  • 1
  • 5
  • 16

4 Answers4

7

scipy-optimize-leastsq-with-bound-constraints on SO givesleastsq_bounds, which is leastsq with bound constraints such as 0 <= x_i <= 1. The constraint that they sum to 1 can be added in the same way.
(I've found leastsq_bounds / MINPACK to be good on synthetic test functions in 5d, 10d, 20d; how many variables do you have ?)

Community
  • 1
  • 1
denis
  • 21,378
  • 10
  • 65
  • 88
1

Have a look at this tutorial, it seems pretty clear.

ev-br
  • 24,968
  • 9
  • 65
  • 78
0

Since MATLAB's lsqlin is a bounded linear least squares solver, you would want to check out scipy.optimize.lsq_linear.

Milind R
  • 676
  • 1
  • 8
  • 20
  • Yes, but how do I add linear constraint expressions? The documentation only seems to allow the specification of bounds on the variables. – Realhermit Mar 08 '18 at 00:05
0

Non-negative least squares optimization using scipy.optimize.nnls is a robust way of doing it. Note that, if the coefficients are constrained to be positive and sum to unity, they are automatically limited to interval [0,1], that is one need not additionally constrain them from above.

scipy.optimize.nnls automatically makes variables positive using Lawson and Hanson algorithm, whereas the sum constraint can be taken care of as discussed in this thread and this one.

Scipy nnls uses an old fortran backend, which is apparently widely used in equivalent implementations of nnls by other software.

Roger Vadim
  • 373
  • 2
  • 12