-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
109 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# author: Nicolas Tessore <[email protected]> | ||
# license: MIT | ||
'''core module for algorithms''' | ||
|
||
from __future__ import annotations | ||
|
||
import numpy as np | ||
from numpy.typing import ArrayLike | ||
|
||
|
||
def nnls( | ||
a: ArrayLike, | ||
b: ArrayLike, | ||
*, | ||
tol: float = 1e-10, | ||
maxiter: int | None = None, | ||
) -> ArrayLike: | ||
"""Compute a non-negative least squares solution. | ||
Implementation of the algorithm due to [1]_ as described in [2]_. | ||
References | ||
---------- | ||
.. [1] Lawson, C. L. and Hanson, R. J. (1995), Solving Least Squares | ||
Problems. doi: 10.1137/1.9781611971217 | ||
.. [2] Bro, R. and De Jong, S. (1997), A fast | ||
non-negativity-constrained least squares algorithm. J. | ||
Chemometrics, 11, 393-401. | ||
""" | ||
|
||
a = np.asanyarray(a) | ||
b = np.asanyarray(b) | ||
|
||
if a.ndim != 2: | ||
raise ValueError("input `a` is not a matrix") | ||
if b.ndim != 1: | ||
raise ValueError("input `b` is not a vector") | ||
if a.shape[0] != b.shape[0]: | ||
raise ValueError("the shapes of `a` and `b` do not match") | ||
|
||
m, n = a.shape | ||
|
||
if maxiter is None: | ||
maxiter = 3 * n | ||
|
||
index = np.arange(n) | ||
p = np.full(n, False) | ||
x = np.zeros(n) | ||
for i in range(maxiter): | ||
if np.all(p): | ||
break | ||
w = np.dot(b - a @ x, a) | ||
m = index[~p][np.argmax(w[~p])] | ||
if w[m] <= tol: | ||
break | ||
p[m] = True | ||
while True: | ||
ap = a[:, p] | ||
xp = x[p] | ||
sp = np.linalg.solve(ap.T @ ap, b @ ap) | ||
t = (sp <= 0) | ||
if not np.any(t): | ||
break | ||
alpha = -np.min(xp[t]/(xp[t] - sp[t])) | ||
x[p] += alpha * (sp - xp) | ||
p[x <= 0] = False | ||
x[p] = sp | ||
x[~p] = 0 | ||
return x |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import pytest | ||
|
||
# check if scipy is available for testing | ||
try: | ||
import scipy | ||
except ImportError: | ||
HAVE_SCIPY = False | ||
else: | ||
del scipy | ||
HAVE_SCIPY = True | ||
|
||
|
||
@pytest.mark.skipif(not HAVE_SCIPY, reason="test requires SciPy") | ||
def test_nnls(): | ||
import numpy as np | ||
from scipy.optimize import nnls as nnls_scipy | ||
from glass.core.algorithm import nnls as nnls_glass | ||
|
||
a = np.random.randn(100, 20) | ||
b = np.random.randn(100) | ||
|
||
x_glass = nnls_glass(a, b) | ||
x_scipy, _ = nnls_scipy(a, b) | ||
|
||
np.testing.assert_allclose(x_glass, x_scipy) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters