-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
150 lines (116 loc) · 4.26 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
"""
A Python implementation of NNLS algorithm
References:
[1] Lawson, C.L. and R.J. Hanson, Solving Least-Squares Problems, Prentice-Hall, Chapter 23, p. 161, 1974.
Contributed by Klaus Schuch ([email protected])
based on MATLAB's lsqnonneg function
https://gist.github.com/jdelafon/b7fdc7a0bae42af56366fc7786cc5d54
"""
import numpy as np
def lsqnonneg(C, d, x0=None, tol=None, itmax_factor=3):
'''Linear least squares with nonnegativity constraints.
(x, resnorm, residual) = lsqnonneg(C,d) returns the vector x that minimizes norm(d-C*x)
subject to x >= 0, C and d must be real
A Python implementation of NNLS algorithm
References:
[1] Lawson, C.L. and R.J. Hanson, Solving Least-Squares Problems, Prentice-Hall, Chapter 23, p. 161, 1974.
Contributed by Klaus Schuch ([email protected])
based on MATLAB's lsqnonneg function
https://gist.github.com/jdelafon/b7fdc7a0bae42af56366fc7786cc5d54
'''
eps = 2.22e-16 # from matlab
def norm1(x):
return abs(x).sum().max()
def msize(x, dim):
s = x.shape
if dim >= len(s):
return 1
else:
return s[dim]
if tol is None: tol = 10*eps*norm1(C)*(max(C.shape)+1)
C = np.asarray(C)
(m,n) = C.shape
P = np.zeros(n)
Z = np.arange(1, n+1)
if x0 is None: x=P
else:
if any(x0 < 0): x=P
else: x=x0
ZZ = Z
resid = (d - np.dot(C, x))
#resid = (d - np.dot(C, x)) / d
w = np.dot(C.T, resid)
outeriter=0; it=0
itmax=itmax_factor*n
exitflag=1
# outer loop to put variables into set to hold positive coefficients
while np.any(Z) and np.any(w[ZZ-1] > tol):
outeriter += 1
t = w[ZZ-1].argmax()
t = ZZ[t]
P[t-1]=t
Z[t-1]=0
PP = np.where(P != 0)[0]+1
ZZ = np.where(Z != 0)[0]+1
CP = np.zeros(C.shape)
CP[:, PP-1] = C[:, PP-1]
CP[:, ZZ-1] = np.zeros((m, msize(ZZ, 1)))
#z=np.dot(np.linalg.pinv(CP), d)
D = np.diag(1/d)
D_square = np.square(D)
z = np.linalg.pinv(CP.T @ D_square @ CP) @ CP.T @ D_square @ d
z[ZZ-1] = np.zeros((msize(ZZ,1), msize(ZZ,0)))
# inner loop to remove elements from the positve set which no longer belong
while np.any(z[PP-1] <= tol):
it += 1
if it > itmax:
max_error = z[PP-1].max()
#return (z, sum(resid*resid), resid)
raise Exception(f'Exiting: Iteration count {it} exceeded\n Try raising the tolerance tol. {max_error}')
QQ = np.where((z <= tol) & (P != 0))[0]
alpha = min(x[QQ]/(x[QQ] - z[QQ]))
x = x + alpha*(z-x)
ij = np.where((abs(x) < tol) & (P != 0))[0]+1
Z[ij-1] = ij
P[ij-1] = np.zeros(max(ij.shape))
PP = np.where(P != 0)[0]+1
ZZ = np.where(Z != 0)[0]+1
CP[:, PP-1] = C[:, PP-1]
CP[:, ZZ-1] = np.zeros((m, msize(ZZ, 1)))
z=np.dot(np.linalg.pinv(CP), d)
D = np.diag(1/d)
D_square = np.square(D)
#z = np.linalg.pinv(CP.T @ D_square @ CP) @ CP.T @ D_square @ d
z[ZZ-1] = np.zeros((msize(ZZ,1), msize(ZZ,0)))
x = z
#resid = (d - np.dot(C, x))
resid = (d - np.dot(C, x)) / d
w = np.dot(C.T, resid)
return (x, sum(resid * resid), resid)
# Unittest
if __name__ == '__main__':
from scipy.optimize import nnls
A = np.array([[1, 0], [1, 0], [0, 1]])
b = np.array([2, 1, 1])
x, res = nnls(A, b)
print(f'Scipy NNLS: {x, res**2}')
[x, resnorm, residual] = lsqnonneg(A, b)
print(f'Current: {x, residual}')
x, res, _, _ = np.linalg.lstsq(A, b, rcond=None)
print(f'Numpy LLS: {x, res}')
D = np.diag(1/b)
D_square = np.square(D)
x = np.linalg.pinv(A.T @ D_square @ A) @ A.T @ D_square @ b
print(f'% min: {x}')
print()
b = np.array([-1, -1, -1])
x, res = nnls(A, b)
print(f'Scipy NNLS: {x, res**2}')
[x, resnorm, residual] = lsqnonneg(A, b)
print(f'Current: {x, residual}')
x, res, _, _ = np.linalg.lstsq(A, b, rcond=None)
print(f'Numpy LLS: {x, res}')
D = np.diag(1/b)
D_square = np.square(D)
x = np.linalg.pinv(A.T @ D_square @ A) @ A.T @ D_square @ b
print(f'% min: {x}')