Non-Negative Least Squares (NNLS) Fitting is a variant of the classical least squares regression where the coefficients being estimated are constrained to be non-negative.
📌 Problem Overview
In standard linear least squares fitting, we solve for a vector x that minimizes:
∥Ax−b∥2
Where:
A is an m×n matrix of predictor variables (or basis functions),
b is an m×1 vector of observed responses,
x is the n×1 vector of unknown coefficients,
∥⋅∥ denotes the Euclidean norm.
In non-negative least squares, we add a constraint:
xi≥0for all i=1,…,n
So, the full optimization problem becomes:
xmin∥Ax−b∥2subject to x≥0
🧠 Intuition
You might use NNLS when you know that the solution should not have negative values — for example:
Physical quantities like concentrations, intensities, or probabilities cannot be negative.
When decomposing data into additive parts (e.g., spectral unmixing).
In image reconstruction or signal processing.
🔧 How It's Solved
The original NNLS algorithm was proposed by Lawson and Hanson (1974). Their method uses an active set strategy, which works by:
Identifying a subset of variables that can be zero ("frozen" or "passive").
Solving the unconstrained least squares problem on the remaining ("active") variables.
Iteratively adjusting the active/passive sets until convergence.
This is different from simply truncating negative values after solving the unconstrained problem — instead, the non-negativity is enforced during the solution process.
✅ Example Use Case
Suppose you're analyzing a spectrum composed of multiple known components. You want to find how much each component contributes to the total signal. Since these contributions must be non-negative (you can't have negative amounts), you'd use NNLS.
📦 Implementation in Python
In Python, you can use scipy.optimize.nnls:
python
from scipy.optimize import nnls
import numpy as np
A = np.array([[1, 2], [3, 4], [5, 6]])
b = np.array([1, 2, 3])
x, rnorm = nnls(A, b)
print("Solution x:", x)
print("Residual norm:", rnorm)
⚖️ Comparison with Other Methods
Method
Constraints
Notes
Ordinary Least Squares (OLS)
None
Fast, closed-form solution
Ridge Regression
None (but penalizes large weights)
Adds L2 regularization
Lasso Regression
None (but promotes sparsity via L1)
Can give zero coefficients
Non-Negative Least Squares (NNLS)
xi≥0
Enforces positivity
🧮 Summary
Non-Negative Least Squares (NNLS) finds the best-fit linear combination under the constraint that all coefficients are ≥ 0.
Useful in applications where physical meaning requires non-negative parameters.
Solved using specialized algorithms like Lawson-Hanson.
Available in many numerical libraries (e.g., SciPy, MATLAB, R).
Let me know if you’d like a code example or derivation!