Non negative least square on some coefficient Non negative least square solves
$$
y = \alpha^T x \\
s.t. \forall i,~ \alpha_i \geq 0
$$
However, I would like to apply the non negativity constraint only on some coefficient, say only the $\alpha_i,~ \forall i \in \mathcal{S}$.
This is not possible using scipy's nnls function. However, I think I can solve the problem by using nnls with the following trick:
$$
y = \sum_{i \in \mathcal{S}} \alpha_i x_i + \sum_{i \notin \mathcal{S}} \beta_i x_i - \sum_{i \notin \mathcal{S}} \gamma_i x_i \\
s.t.~\forall i~, \alpha_i \geq 0, \beta_i \geq 0, \gamma_i \geq 0
$$
but this is hacky. Do you know any over way to implement this in python?
 A: Your approach with the extra parameters will be problematic as multiple values for the coefficients will provide a same solution (the solution won't be unique).
Non-negative least squares regression is often solved by an active set method that updates in steps the active constraints (see for instance How do Lawson and Hanson solve the unconstrained least squares problem?). In those steps various regular least squares estimates are computed for different active sets. Those steps will fail with your approach because of the multiple solutions (you will get some errors during the computations like non-invertible matrices).
An alternative nnls for Python (aside from Galen's response) could be scipy.optimize.lsq_linear, which allows setting individual constraints and uses an active set method.
A: The brute force approach would be using constrained minimization, applying the non-negativity constraints only to certain variables - along the lines of the python implementation provided in an answer to How to include constraint to Scipy NNLS function solution so that it sums to 1.
Another trick is variable transformation that makes certain variables always non-negative, like $x=y^2$. One can then use any general purpose minimization routine.
The only problem with these approaches is that they will perform much slower than NNLS for a large number of variables - whether this is a constraint depends on the problem, the available computational capacities, and the programming language used.
