How this calculation works
Nonnegative least squares restricts fitted coefficients to be zero or positive. It is useful when negative component amounts would be meaningless, but the constraint changes the optimization problem. This calculator enumerates bounded active sets, solves reduced least-squares problems and checks nonnegativity and KKT conditions. It does not simply solve an unconstrained system and clip negative entries afterward. Residual norms and scaled gradient diagnostics make that distinction inspectable.
Minimize ½||Ax−b||² subject to x≥0; g=Aᵀ(Ax−b), g≥0 and xᵢgᵢ=0 at an optimum.A worked example
For A=[[1,1],[0,1]] and b=[0,1], unconstrained fitting gives x=[−1,1]. Clipping that to [0,1] has residual norm one. The actual nonnegative optimum is [0,1/2], with residual [1/2,−1/2] and norm √(1/2). The nonnegativity constraint forces a tradeoff between the two rows.
Good to know
Supports at most six rows and six columns with bounded finite entries. Numerical active-set enumeration can reject unstable candidates. Rank-deficient designs may have multiple minimizing coefficient vectors; one passing optimum is returned without a uniqueness claim. No upper bounds, equality constraints, weighted objective or regularization term are inferred. Gradient scaling and complementarity tolerance are numerical diagnostics, not exact certificates.
Reference: SciPy — nonnegative least squares and KKT conditions (opens in a new tab) ↗
Clipping can miss the constrained optimum
The default two-variable example shows why coefficients cannot be adjusted independently after an unconstrained solve. Both variables influence the first row. Once x1 is forced to zero, the best x2 must be refitted to balance both residuals. A clipped vector preserves an unconstrained value that was optimal only when the forbidden negative coefficient was available.
For each candidate active set, the solver treats selected variables as potentially positive and fixes the others at zero. It rejects reduced solutions with negative coefficients, then tests the gradient conditions. The empty active set is included, so an all-zero optimum can be returned when every positive move would worsen the objective.
The Moore–Penrose pseudoinverse calculator for rectangular matrices gives an unconstrained generalized inverse rather than enforcing nonnegative fitted coefficients.
The residual and gradient explain different parts of the answer
The Euclidean residual norm measures mismatch in observation space. The gradient Aᵀ(Ax−b) measures how the objective responds to coefficient changes. At a positive coefficient the gradient should be near zero. At a coefficient fixed to zero, a nonnegative gradient means increasing it cannot improve the objective locally. Complementarity x_i g_i≈0 links those cases.
To keep numeric magnitudes manageable, the implementation scales A and b by a shared positive factor. This leaves the optimum unchanged but scales objective gradients by the factor squared. The output explicitly reports that convention. An ordinary least-squares or pseudoinverse result does not enforce these conditions. Multiple feasible optima may exist for dependent columns, so a successful KKT check establishes optimality within numerical tolerance rather than a unique physical mixture.
A few questions, answered
Are zero coefficients allowed?
Yes. Nonnegative means x≥0, not x>0. Zero coefficients often identify inactive components at the optimum.
Can the residual norm be positive at a correct optimum?
Yes. The target may not lie in the nonnegative cone generated by the columns. The optimum is then the closest feasible fit rather than an exact solution.