admmsolver 0.7.7__tar.gz → 0.8.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {admmsolver-0.7.7 → admmsolver-0.8.0}/PKG-INFO +19 -2
- {admmsolver-0.7.7 → admmsolver-0.8.0}/README.md +5 -0
- {admmsolver-0.7.7 → admmsolver-0.8.0}/src/admmsolver/__init__.py +1 -1
- {admmsolver-0.7.7 → admmsolver-0.8.0}/src/admmsolver/objectivefunc.py +108 -27
- {admmsolver-0.7.7 → admmsolver-0.8.0}/src/admmsolver.egg-info/PKG-INFO +19 -2
- admmsolver-0.8.0/test/test_objectivefunc.py +379 -0
- admmsolver-0.7.7/test/test_objectivefunc.py +0 -186
- {admmsolver-0.7.7 → admmsolver-0.8.0}/LICENSE.txt +0 -0
- {admmsolver-0.7.7 → admmsolver-0.8.0}/setup.cfg +0 -0
- {admmsolver-0.7.7 → admmsolver-0.8.0}/setup.py +0 -0
- {admmsolver-0.7.7 → admmsolver-0.8.0}/src/admmsolver/matrix.py +0 -0
- {admmsolver-0.7.7 → admmsolver-0.8.0}/src/admmsolver/optimizer.py +0 -0
- {admmsolver-0.7.7 → admmsolver-0.8.0}/src/admmsolver/py.typed +0 -0
- {admmsolver-0.7.7 → admmsolver-0.8.0}/src/admmsolver/util.py +0 -0
- {admmsolver-0.7.7 → admmsolver-0.8.0}/src/admmsolver.egg-info/SOURCES.txt +0 -0
- {admmsolver-0.7.7 → admmsolver-0.8.0}/src/admmsolver.egg-info/dependency_links.txt +0 -0
- {admmsolver-0.7.7 → admmsolver-0.8.0}/src/admmsolver.egg-info/requires.txt +0 -0
- {admmsolver-0.7.7 → admmsolver-0.8.0}/src/admmsolver.egg-info/top_level.txt +0 -0
- {admmsolver-0.7.7 → admmsolver-0.8.0}/test/test_matrix.py +0 -0
- {admmsolver-0.7.7 → admmsolver-0.8.0}/test/test_optimizer.py +0 -0
- {admmsolver-0.7.7 → admmsolver-0.8.0}/test/test_util.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
2
|
Name: admmsolver
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.8.0
|
|
4
4
|
Summary: Fast and general ADMM solver
|
|
5
5
|
Home-page: https://github.com/SpM-lab/admmsolver
|
|
6
6
|
Author: Hiroshi Shinaoka
|
|
@@ -23,9 +23,26 @@ Requires-Dist: mypy; extra == "test"
|
|
|
23
23
|
Provides-Extra: dev
|
|
24
24
|
Requires-Dist: pytest; extra == "dev"
|
|
25
25
|
Requires-Dist: mypy; extra == "dev"
|
|
26
|
+
Dynamic: author
|
|
27
|
+
Dynamic: author-email
|
|
28
|
+
Dynamic: classifier
|
|
29
|
+
Dynamic: description
|
|
30
|
+
Dynamic: description-content-type
|
|
31
|
+
Dynamic: home-page
|
|
32
|
+
Dynamic: keywords
|
|
33
|
+
Dynamic: license-file
|
|
34
|
+
Dynamic: provides-extra
|
|
35
|
+
Dynamic: requires-dist
|
|
36
|
+
Dynamic: requires-python
|
|
37
|
+
Dynamic: summary
|
|
26
38
|
|
|
27
39
|
A fast and general ADMM solver
|
|
28
40
|
=======================================================================
|
|
41
|
+
|
|
42
|
+
> Part of the [SpM-lab](https://spm-lab.github.io/) tools for imaginary-time Green's functions and analytic continuation.
|
|
43
|
+
> Related: the [sparse-ir ecosystem](https://spm-lab.github.io/sparse-ir-doc/) (IR/DLR bases, sparse sampling) and the other
|
|
44
|
+
> [analytic continuation tools](https://spm-lab.github.io/#analytic-continuation).
|
|
45
|
+
|
|
29
46
|
# Installation
|
|
30
47
|
|
|
31
48
|
```
|
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
A fast and general ADMM solver
|
|
2
2
|
=======================================================================
|
|
3
|
+
|
|
4
|
+
> Part of the [SpM-lab](https://spm-lab.github.io/) tools for imaginary-time Green's functions and analytic continuation.
|
|
5
|
+
> Related: the [sparse-ir ecosystem](https://spm-lab.github.io/sparse-ir-doc/) (IR/DLR bases, sparse sampling) and the other
|
|
6
|
+
> [analytic continuation tools](https://spm-lab.github.io/#analytic-continuation).
|
|
7
|
+
|
|
3
8
|
# Installation
|
|
4
9
|
|
|
5
10
|
```
|
|
@@ -178,10 +178,10 @@ class L1Regularizer(ObjectiveFunctionBase):
|
|
|
178
178
|
"""
|
|
179
179
|
x <- argmin_x alpha * |x|_1 + h^+ x + x^+ h + mu x^+ x
|
|
180
180
|
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
181
|
+
``h`` (and hence ``x``) may be real or complex; the proximal operator is
|
|
182
|
+
the magnitude soft-threshold, which shrinks |x| by the threshold while
|
|
183
|
+
keeping the phase. ``mu`` must be a diagonal matrix. The result is
|
|
184
|
+
float64 for real input and complex128 for complex input.
|
|
185
185
|
"""
|
|
186
186
|
_assert_types(h, [np.ndarray])
|
|
187
187
|
assert isinstance(mu, DiagonalMatrix) or isinstance(mu, ScaledIdentityMatrix)
|
|
@@ -190,8 +190,9 @@ class L1Regularizer(ObjectiveFunctionBase):
|
|
|
190
190
|
raise ValueError("h must not be None!")
|
|
191
191
|
if mu is None:
|
|
192
192
|
raise ValueError("mu must not be None!")
|
|
193
|
-
|
|
194
|
-
|
|
193
|
+
# The L1 proximal operator (soft-thresholding) works for both real and
|
|
194
|
+
# complex-valued x; _softmax below handles the complex case by shrinking
|
|
195
|
+
# the magnitude while keeping the phase.
|
|
195
196
|
return _softmax(-(h/mu.diagonals), 0.5*self._alpha/mu.diagonals)
|
|
196
197
|
|
|
197
198
|
|
|
@@ -210,7 +211,7 @@ class L2Regularizer(ObjectiveFunctionBase):
|
|
|
210
211
|
self._AcA = matmul(A.conjugate().T, A)
|
|
211
212
|
|
|
212
213
|
# B = (A^+ A + mu)^{-1}
|
|
213
|
-
self._B_cache = (0, DenseMatrix(np.zeros((1,1))))
|
|
214
|
+
self._B_cache = (0, DenseMatrix(np.zeros((1,1)))) # type: Tuple[int, MatrixBase]
|
|
214
215
|
|
|
215
216
|
def __call__(self, x: np.ndarray):
|
|
216
217
|
return self._alpha * np.linalg.norm(matmul(self._A, x))**2
|
|
@@ -220,7 +221,7 @@ class L2Regularizer(ObjectiveFunctionBase):
|
|
|
220
221
|
if self._B_cache[0] != hash_:
|
|
221
222
|
self._B_cache = (
|
|
222
223
|
hash_,
|
|
223
|
-
inv(add(self._alpha * self._AcA, mu))
|
|
224
|
+
cast(MatrixBase, inv(add(self._alpha * self._AcA, mu)))
|
|
224
225
|
)
|
|
225
226
|
return self._B_cache[1]
|
|
226
227
|
|
|
@@ -277,6 +278,13 @@ class SemiPositiveDefinitePenalty(ObjectiveFunctionBase):
|
|
|
277
278
|
|
|
278
279
|
1) Reshape x into a three-way tensor
|
|
279
280
|
2) Along a given axis, we compute eigenvalues and remove negative ones (we assume hermition matrices).
|
|
281
|
+
|
|
282
|
+
Real-symmetric and complex-Hermitian blocks are both supported. ``solve``
|
|
283
|
+
returns the exact minimizer of ``h^+ x + x^+ h + x^+ mu x`` over the PSD cone
|
|
284
|
+
when the penalty ``mu`` is uniform within each block (a ScaledIdentityMatrix,
|
|
285
|
+
as produced by the ADMM optimizer). For a genuinely non-uniform diagonal
|
|
286
|
+
``mu`` the returned value is Hermitian, PSD and feasible but only an
|
|
287
|
+
approximation of the weighted argmin.
|
|
280
288
|
"""
|
|
281
289
|
def __init__(self, shape: Union[Sequence,np.ndarray], axis: int):
|
|
282
290
|
assert len(shape) == 3
|
|
@@ -312,18 +320,55 @@ class SemiPositiveDefinitePenalty(ObjectiveFunctionBase):
|
|
|
312
320
|
|
|
313
321
|
if h is None:
|
|
314
322
|
h = np.zeros(np.prod(self._shape))
|
|
315
|
-
elif np.iscomplexobj(h):
|
|
316
|
-
h = h.real
|
|
317
323
|
|
|
318
324
|
assert diagonals.size == h.size
|
|
319
325
|
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
326
|
+
# Minimize h^+ x + x^+ h + x^+ mu x over the (real-symmetric or
|
|
327
|
+
# complex-Hermitian) positive semi-definite cone, block by block.
|
|
328
|
+
#
|
|
329
|
+
# The Hermitian constraint couples the paired entries (i,j) and (j,i)
|
|
330
|
+
# (x_ji = conj(x_ij)). Under the diagonal metric mu with per-entry
|
|
331
|
+
# weights d, the metric projection onto the Hermitian subspace of the
|
|
332
|
+
# unconstrained centre x* = -h/d is the weighted average
|
|
333
|
+
# centre_ij = (d_ij x*_ij + d_ji conj(x*_ji)) / (d_ij + d_ji)
|
|
334
|
+
# = -(h_ij + conj(h_ji)) / (d_ij + d_ji).
|
|
335
|
+
# We divide numerator and denominator of each pair by a common per-pair
|
|
336
|
+
# scale before summing, so neither ``h_ij + conj(h_ji)`` nor
|
|
337
|
+
# ``d_ij + d_ji`` overflows for very large (or underflows for very small)
|
|
338
|
+
# finite operands. The scale cancels in the ratio.
|
|
339
|
+
#
|
|
340
|
+
# Eigenvalue clipping of this Hermitian centre is the *exact* PSD
|
|
341
|
+
# projection when the weights are uniform within each block -- which is
|
|
342
|
+
# the case for the scalar ADMM penalty (a ScaledIdentityMatrix mu). For
|
|
343
|
+
# a genuinely non-uniform diagonal metric the result is still Hermitian,
|
|
344
|
+
# PSD and feasible, but only an approximation of the weighted argmin.
|
|
345
|
+
hblk = np.asarray(h).reshape(self._shape)
|
|
346
|
+
dblk = np.asarray(diagonals, dtype=np.float64).reshape(self._shape)
|
|
347
|
+
hblk = np.moveaxis(hblk, self._axis, 0)
|
|
348
|
+
dblk = np.moveaxis(dblk, self._axis, 0)
|
|
349
|
+
hblkT = np.conjugate(np.swapaxes(hblk, 1, 2)) # conj(h_ji)
|
|
350
|
+
dblkT = np.swapaxes(dblk, 1, 2) # d_ji
|
|
351
|
+
# Per-pair scale factor (cancels in the ratio). Use max(|re|,|im|) for
|
|
352
|
+
# complex entries so it never overflows the way np.abs(complex) can when
|
|
353
|
+
# the components are large but finite. (A single shared scale assumes the
|
|
354
|
+
# paired operands are of comparable magnitude, which always holds for the
|
|
355
|
+
# O(1)-scaled ADMM penalties and data; it is not designed for adversarial
|
|
356
|
+
# blocks mixing sub-normal and near-DBL_MAX entries.)
|
|
357
|
+
def _comp_scale(a):
|
|
358
|
+
if np.iscomplexobj(a):
|
|
359
|
+
return np.maximum(np.abs(a.real), np.abs(a.imag))
|
|
360
|
+
return np.abs(a)
|
|
361
|
+
scale = np.maximum.reduce([_comp_scale(hblk), _comp_scale(hblkT), dblk, dblkT])
|
|
362
|
+
scale = np.where(scale > 0.0, scale, 1.0)
|
|
363
|
+
hbar = hblk / scale + hblkT / scale # (h_ij + conj(h_ji)) / scale
|
|
364
|
+
dbar = dblk / scale + dblkT / scale # (d_ij + d_ji) / scale
|
|
365
|
+
centre = -(hbar / dbar)
|
|
366
|
+
x_ = np.empty_like(centre)
|
|
367
|
+
for i in range(centre.shape[0]):
|
|
368
|
+
evals, evecs = np.linalg.eigh(centre[i, :, :])
|
|
324
369
|
idx = evals >= 0
|
|
325
370
|
U = evecs[:, idx]
|
|
326
|
-
x_[i
|
|
371
|
+
x_[i, :, :] = U @ (evals[idx, None] * U.conjugate().T)
|
|
327
372
|
return np.moveaxis(x_, 0, self._axis).ravel()
|
|
328
373
|
|
|
329
374
|
|
|
@@ -334,22 +379,58 @@ def _project_plus(x):
|
|
|
334
379
|
|
|
335
380
|
def _softmax(y: np.ndarray, lambda_: np.ndarray):
|
|
336
381
|
"""
|
|
337
|
-
|
|
382
|
+
Soft-thresholding (proximal operator of the L1 norm).
|
|
383
|
+
|
|
384
|
+
Shrinks the magnitude of each component by ``lambda_`` while keeping its
|
|
385
|
+
phase, and clips it at zero:
|
|
386
|
+
|
|
387
|
+
max(|y| - lambda_, 0) * y / |y| (y != 0)
|
|
388
|
+
0 (y == 0)
|
|
389
|
+
|
|
390
|
+
This is valid for both real and complex ``y``. For real ``y`` it reduces to
|
|
338
391
|
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
392
|
+
y - lambda_ (y > lambda_)
|
|
393
|
+
y + lambda_ (y < -lambda_)
|
|
394
|
+
0 (otherwise)
|
|
395
|
+
|
|
396
|
+
The result is returned as float64 for real input and complex128 for complex
|
|
397
|
+
input (integer/float32 input is promoted, matching the historical float64
|
|
398
|
+
real behavior and avoiding integer truncation of the threshold).
|
|
342
399
|
"""
|
|
343
400
|
assert isinstance(y, np.ndarray) and y.ndim == 1
|
|
344
401
|
assert isinstance(lambda_, np.ndarray) and lambda_.ndim == 1
|
|
345
402
|
assert (np.asarray(lambda_) > 0).all()
|
|
346
403
|
assert y.size == lambda_.size
|
|
347
|
-
res = np.zeros(y.size)
|
|
348
|
-
|
|
349
|
-
idx = y > lambda_
|
|
350
|
-
res[idx] = y[idx] - lambda_[idx]
|
|
351
404
|
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
405
|
+
if not np.iscomplexobj(y):
|
|
406
|
+
# Real path: literally the historical piecewise implementation, so the
|
|
407
|
+
# result is bit-for-bit identical for every real dtype (the arithmetic
|
|
408
|
+
# happens in the operands' dtype and is stored into a float64 array).
|
|
409
|
+
res = np.zeros(y.size)
|
|
410
|
+
idx = y > lambda_
|
|
411
|
+
res[idx] = y[idx] - lambda_[idx]
|
|
412
|
+
idx = y < -lambda_
|
|
413
|
+
res[idx] = y[idx] + lambda_[idx]
|
|
414
|
+
return res
|
|
415
|
+
|
|
416
|
+
# Complex path: shrink the magnitude by lambda_ while keeping the phase,
|
|
417
|
+
# i.e. multiply by scale = max(1 - lambda/|y|, 0).
|
|
418
|
+
y = y.astype(np.complex128, copy=False)
|
|
419
|
+
lambda_ = np.asarray(lambda_, dtype=np.float64)
|
|
420
|
+
# Evaluate lambda/|y| with per-element component scaling so it stays finite
|
|
421
|
+
# even when |y| = sqrt(re**2 + im**2) itself overflows float64 (both np.abs
|
|
422
|
+
# and np.hypot would overflow for e.g. y = 1.3e308 + 1.3e308j).
|
|
423
|
+
# m = max(|re|, |im|), |y| = m * hypot(re/m, im/m),
|
|
424
|
+
# lambda/|y| = (lambda/m) / hypot(re/m, im/m).
|
|
425
|
+
m = np.maximum(np.abs(y.real), np.abs(y.imag))
|
|
426
|
+
with np.errstate(divide="ignore", invalid="ignore"):
|
|
427
|
+
rm = np.where(m > 0.0, np.hypot(y.real / m, y.imag / m), 0.0) # |y|/m in [1, sqrt2]
|
|
428
|
+
ratio = np.where(m > 0.0, (lambda_ / m) / rm, 0.0) # lambda / |y|
|
|
429
|
+
scale = np.maximum(1.0 - ratio, 0.0)
|
|
430
|
+
res_c = y * scale
|
|
431
|
+
# Entries with an infinite component have infinite magnitude, so a finite
|
|
432
|
+
# threshold leaves them unchanged (and ``y * scale`` would give inf + nan*1j).
|
|
433
|
+
inf_mag = np.isinf(m)
|
|
434
|
+
if inf_mag.any():
|
|
435
|
+
res_c = np.where(inf_mag, y, res_c)
|
|
436
|
+
return cast(np.ndarray, res_c)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
2
|
Name: admmsolver
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.8.0
|
|
4
4
|
Summary: Fast and general ADMM solver
|
|
5
5
|
Home-page: https://github.com/SpM-lab/admmsolver
|
|
6
6
|
Author: Hiroshi Shinaoka
|
|
@@ -23,9 +23,26 @@ Requires-Dist: mypy; extra == "test"
|
|
|
23
23
|
Provides-Extra: dev
|
|
24
24
|
Requires-Dist: pytest; extra == "dev"
|
|
25
25
|
Requires-Dist: mypy; extra == "dev"
|
|
26
|
+
Dynamic: author
|
|
27
|
+
Dynamic: author-email
|
|
28
|
+
Dynamic: classifier
|
|
29
|
+
Dynamic: description
|
|
30
|
+
Dynamic: description-content-type
|
|
31
|
+
Dynamic: home-page
|
|
32
|
+
Dynamic: keywords
|
|
33
|
+
Dynamic: license-file
|
|
34
|
+
Dynamic: provides-extra
|
|
35
|
+
Dynamic: requires-dist
|
|
36
|
+
Dynamic: requires-python
|
|
37
|
+
Dynamic: summary
|
|
26
38
|
|
|
27
39
|
A fast and general ADMM solver
|
|
28
40
|
=======================================================================
|
|
41
|
+
|
|
42
|
+
> Part of the [SpM-lab](https://spm-lab.github.io/) tools for imaginary-time Green's functions and analytic continuation.
|
|
43
|
+
> Related: the [sparse-ir ecosystem](https://spm-lab.github.io/sparse-ir-doc/) (IR/DLR bases, sparse sampling) and the other
|
|
44
|
+
> [analytic continuation tools](https://spm-lab.github.io/#analytic-continuation).
|
|
45
|
+
|
|
29
46
|
# Installation
|
|
30
47
|
|
|
31
48
|
```
|
|
@@ -0,0 +1,379 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
from scipy.optimize import minimize
|
|
3
|
+
from typing import cast
|
|
4
|
+
|
|
5
|
+
from admmsolver.objectivefunc import *
|
|
6
|
+
from admmsolver.matrix import identity, asmatrixtype, PartialDiagonalMatrix
|
|
7
|
+
|
|
8
|
+
import pytest
|
|
9
|
+
|
|
10
|
+
def _randn_cmplx(*shape) -> np.ndarray:
|
|
11
|
+
return np.random.randn(*shape) + 1j* np.random.randn(*shape)
|
|
12
|
+
|
|
13
|
+
def _to_real_array(x):
|
|
14
|
+
if np.iscomplexobj(x):
|
|
15
|
+
return x.view(np.float64).ravel()
|
|
16
|
+
else:
|
|
17
|
+
return x
|
|
18
|
+
|
|
19
|
+
def _from_real_array(x):
|
|
20
|
+
x = x.reshape((-1, 2))
|
|
21
|
+
x = x.view(np.complex128)
|
|
22
|
+
return x.reshape(x.shape[0:-1])
|
|
23
|
+
|
|
24
|
+
def _minimize(f, x0, method="BFGS"):
|
|
25
|
+
x0 = _to_real_array(x0)
|
|
26
|
+
res = minimize(
|
|
27
|
+
lambda x: f(_from_real_array(x)),
|
|
28
|
+
x0,
|
|
29
|
+
method=method,
|
|
30
|
+
options={'maxiter': 100000}
|
|
31
|
+
)
|
|
32
|
+
return _from_real_array(res.x)
|
|
33
|
+
|
|
34
|
+
def test_least_squares():
|
|
35
|
+
"""
|
|
36
|
+
Least-squares fit with a dense coefficient matrix
|
|
37
|
+
"""
|
|
38
|
+
np.random.seed(100)
|
|
39
|
+
N1, N2 = 4, 2
|
|
40
|
+
alpha = 2.0
|
|
41
|
+
y = _randn_cmplx(N1)
|
|
42
|
+
A = _randn_cmplx(N1, N2)
|
|
43
|
+
h = _randn_cmplx(N2)
|
|
44
|
+
sqrt_mu = _randn_cmplx(N2, N2)
|
|
45
|
+
mu = asmatrixtype(sqrt_mu.T.conjugate() @ sqrt_mu)
|
|
46
|
+
lstsq = LeastSquares(alpha, A, y)
|
|
47
|
+
x = lstsq.solve(h, mu)
|
|
48
|
+
|
|
49
|
+
f_all = lambda x: np.real(alpha * np.linalg.norm(y - A @ x)**2 + \
|
|
50
|
+
h.T.conjugate() @ x + x.T.conjugate() @ h + x.conjugate().T @ (mu @ x))
|
|
51
|
+
x_ref = _minimize(f_all, x, "BFGS")
|
|
52
|
+
np.testing.assert_allclose(x, x_ref, rtol=1e-8)
|
|
53
|
+
np.testing.assert_allclose(f_all(x), f_all(x_ref), rtol=1e-8)
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def test_least_squares_partial():
|
|
57
|
+
"""
|
|
58
|
+
Least-squares fit with a coefficient matrix of type PartialDiagonalMatrix
|
|
59
|
+
"""
|
|
60
|
+
np.random.seed(100)
|
|
61
|
+
N1, N2 = 40, 20
|
|
62
|
+
alpha = 2.0
|
|
63
|
+
rest_dim_size = 20
|
|
64
|
+
y = _randn_cmplx(N1)
|
|
65
|
+
A = PartialDiagonalMatrix(_randn_cmplx(N1//rest_dim_size, N2//rest_dim_size), rest_dims=(rest_dim_size,))
|
|
66
|
+
assert A.shape == (N1, N2)
|
|
67
|
+
h = _randn_cmplx(N2)
|
|
68
|
+
sqrt_mu = _randn_cmplx(N2, N2)
|
|
69
|
+
mu = asmatrixtype(sqrt_mu.T.conjugate() @ sqrt_mu)
|
|
70
|
+
lstsq = LeastSquares(alpha, A, y)
|
|
71
|
+
x = lstsq.solve(h, mu)
|
|
72
|
+
|
|
73
|
+
f_all = lambda x: \
|
|
74
|
+
np.real(
|
|
75
|
+
alpha * np.linalg.norm(y - A @ x)**2 + h.T.conjugate() @ x + \
|
|
76
|
+
x.T.conjugate() @ h + x.conjugate().T @ (mu @ x))
|
|
77
|
+
x_ref = _minimize(f_all, x, "BFGS")
|
|
78
|
+
np.testing.assert_allclose(x, x_ref, rtol=1e-4)
|
|
79
|
+
np.testing.assert_allclose(f_all(x), f_all(x_ref), rtol=1e-8)
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def test_constrained_least_squares():
|
|
83
|
+
"""
|
|
84
|
+
Linearly constrained least squares
|
|
85
|
+
"""
|
|
86
|
+
np.random.seed(100)
|
|
87
|
+
|
|
88
|
+
N1, N2 = 8, 4
|
|
89
|
+
Nc = 2
|
|
90
|
+
alpha = 2.0
|
|
91
|
+
y = _randn_cmplx(N1)
|
|
92
|
+
A = _randn_cmplx(N1, N2)
|
|
93
|
+
h = _randn_cmplx(N2)
|
|
94
|
+
C = _randn_cmplx(Nc, N2)
|
|
95
|
+
D = _randn_cmplx(Nc)
|
|
96
|
+
sqrt_mu = _randn_cmplx(N2, N2)
|
|
97
|
+
mu = asmatrixtype(sqrt_mu.T.conjugate() @ sqrt_mu)
|
|
98
|
+
lstsq = ConstrainedLeastSquares(alpha, A, y, C, D)
|
|
99
|
+
|
|
100
|
+
x = lstsq.solve(h, mu)
|
|
101
|
+
assert np.abs(C@x - D).max() < 1e-10
|
|
102
|
+
|
|
103
|
+
#FIXME: how to check x?
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def test_L1():
|
|
107
|
+
"""
|
|
108
|
+
Minimize alpha * |x|_1 + h^+ x + x^+ h + mu x^+ x
|
|
109
|
+
"""
|
|
110
|
+
N = 20
|
|
111
|
+
assert N%2 == 0
|
|
112
|
+
h = 0.5*np.arange(-N//2, N//2)
|
|
113
|
+
mu = identity(N)
|
|
114
|
+
alpha = 1.0
|
|
115
|
+
|
|
116
|
+
l1 = L1Regularizer(alpha, N)
|
|
117
|
+
x = l1.solve(h, mu)
|
|
118
|
+
|
|
119
|
+
# Naive optimization
|
|
120
|
+
for i in range(N):
|
|
121
|
+
f = lambda x: alpha * np.abs(x) + 2*h[i]*x + mu.diagonals[i] * x**2
|
|
122
|
+
x0 = 0.0
|
|
123
|
+
res = minimize(f, x0, method="BFGS")
|
|
124
|
+
assert np.abs(x[i]-res.x[0]) < 1e-5
|
|
125
|
+
|
|
126
|
+
def test_non_negative():
|
|
127
|
+
"""
|
|
128
|
+
Minimize infty * Theta(-x) + h^+ x + x^+ h + mu x^+ x
|
|
129
|
+
"""
|
|
130
|
+
h = np.array([0, -10, 10])
|
|
131
|
+
N = h.size
|
|
132
|
+
mu = identity(N)
|
|
133
|
+
|
|
134
|
+
func = NonNegativePenalty(N)
|
|
135
|
+
x = func.solve(h, mu)
|
|
136
|
+
|
|
137
|
+
step_f = lambda x: x if x >= 0 else 0
|
|
138
|
+
|
|
139
|
+
# Naive optimization
|
|
140
|
+
for i in range(N):
|
|
141
|
+
f = lambda x: 1e+5 * step_f(-x) + 2*h[i]*x + mu.diagonals[i] * x**2
|
|
142
|
+
x0 = 0.0
|
|
143
|
+
res = minimize(f, x0, method="BFGS")
|
|
144
|
+
assert np.abs(x[i]-res.x[0]) < 1e-5
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
def test_L2():
|
|
148
|
+
"""
|
|
149
|
+
Minimize alpha * |A x|_2^2 + h^+ x + x^+ h + mu x^+ x
|
|
150
|
+
"""
|
|
151
|
+
N = 10
|
|
152
|
+
M = 5
|
|
153
|
+
sqrt_mu = _randn_cmplx(N, N)
|
|
154
|
+
mu = asmatrixtype(sqrt_mu.T.conjugate() @ sqrt_mu)
|
|
155
|
+
alpha = 2.0
|
|
156
|
+
A = _randn_cmplx(M, N)
|
|
157
|
+
h = _randn_cmplx(N)
|
|
158
|
+
|
|
159
|
+
l2 = L2Regularizer(alpha, A)
|
|
160
|
+
x = l2.solve(h, mu)
|
|
161
|
+
|
|
162
|
+
# Naive optimization
|
|
163
|
+
f = lambda x: alpha * np.linalg.norm(A @ x)**2 + \
|
|
164
|
+
2*np.real(h.conjugate().T @ x) + np.real(x.conjugate().T @ (mu @ x))
|
|
165
|
+
x_ref = _minimize(f, x, method="BFGS")
|
|
166
|
+
np.testing.assert_allclose(cast(np.ndarray, x), x_ref, atol=np.abs(x_ref).max()*1e-5, rtol=0)
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
def test_semi_positive_definite_penalty():
|
|
170
|
+
np.random.seed(100)
|
|
171
|
+
K = 20
|
|
172
|
+
N = 10
|
|
173
|
+
|
|
174
|
+
h = _randn_cmplx(N**2 * K)
|
|
175
|
+
|
|
176
|
+
for mu in [
|
|
177
|
+
asmatrixtype(identity(N**2 * K)),
|
|
178
|
+
PartialDiagonalMatrix(ScaledIdentityMatrix(N**2, 1.0), (K,))]:
|
|
179
|
+
p = SemiPositiveDefinitePenalty((N,N,K), axis=2)
|
|
180
|
+
res = p.solve(h, mu)
|
|
181
|
+
|
|
182
|
+
x = res.reshape((N,N,K))
|
|
183
|
+
for k in range(K):
|
|
184
|
+
evals, evecs = np.linalg.eigh(x[:,:,k])
|
|
185
|
+
assert all(evals > -1e-10)
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
def _old_softmax(y, lambda_):
|
|
190
|
+
"""Reference: original real-only piecewise soft-threshold."""
|
|
191
|
+
res = np.zeros(y.size)
|
|
192
|
+
idx = y > lambda_
|
|
193
|
+
res[idx] = y[idx] - lambda_[idx]
|
|
194
|
+
idx = y < -lambda_
|
|
195
|
+
res[idx] = y[idx] + lambda_[idx]
|
|
196
|
+
return res
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
def test_softmax_real_equivalence_and_dtype():
|
|
200
|
+
from admmsolver.objectivefunc import _softmax
|
|
201
|
+
rng = np.random.RandomState(0)
|
|
202
|
+
y = rng.randn(200)
|
|
203
|
+
lam = 0.3 * np.ones_like(y)
|
|
204
|
+
new = _softmax(y, lam)
|
|
205
|
+
old = _old_softmax(y, lam)
|
|
206
|
+
assert np.array_equal(new, old) # exact, not just close
|
|
207
|
+
assert new.dtype == np.float64 # real stays float64
|
|
208
|
+
# exact boundaries: 0, +-lambda, and nextafter on both sides
|
|
209
|
+
lm = 1.5
|
|
210
|
+
ys = np.array([0.0, lm, -lm,
|
|
211
|
+
np.nextafter(lm, np.inf), np.nextafter(lm, -np.inf),
|
|
212
|
+
np.nextafter(-lm, np.inf), np.nextafter(-lm, -np.inf),
|
|
213
|
+
1e12, -1e12], dtype=np.float64)
|
|
214
|
+
lams = np.full(ys.size, lm)
|
|
215
|
+
assert np.array_equal(_softmax(ys, lams), _old_softmax(ys, lams))
|
|
216
|
+
# float32 input is promoted to float64 (no threshold truncation for ints)
|
|
217
|
+
assert _softmax(np.array([5, -5, 0]), np.full(3, 1.5)).dtype == np.float64
|
|
218
|
+
assert np.array_equal(_softmax(np.array([5, -5, 0]), np.full(3, 1.5)), [3.5, -3.5, 0.0])
|
|
219
|
+
assert _softmax(np.array([1.0], np.float32), np.array([0.3])).dtype == np.float64
|
|
220
|
+
# real +-inf stays +-inf (not NaN)
|
|
221
|
+
r = _softmax(np.array([np.inf, -np.inf]), np.array([1.0, 1.0]))
|
|
222
|
+
assert r[0] == np.inf and r[1] == -np.inf
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
def test_softmax_complex_and_inf():
|
|
226
|
+
from admmsolver.objectivefunc import _softmax
|
|
227
|
+
# complex phase preserved, magnitude shrunk
|
|
228
|
+
y = np.array([3.0 + 4.0j, 0.6 - 0.8j, 0.0 + 0.0j]) # |y| = 5, 1, 0
|
|
229
|
+
lam = np.array([1.0, 2.0, 1.0])
|
|
230
|
+
out = _softmax(y, lam)
|
|
231
|
+
assert out.dtype == np.complex128
|
|
232
|
+
assert np.allclose(out[0], (5.0 - 1.0) * (y[0] / 5.0)) # shrink 5 -> 4, keep phase
|
|
233
|
+
assert out[1] == 0 # |y|=1 < lambda=2
|
|
234
|
+
assert out[2] == 0 # zero input
|
|
235
|
+
# complex entries with an infinite component must not become NaN
|
|
236
|
+
yi = np.array([complex(np.inf, 1.0), complex(1.0, np.inf), complex(np.inf, np.inf)])
|
|
237
|
+
ri = _softmax(yi, np.ones(3))
|
|
238
|
+
assert not np.isnan(ri).any()
|
|
239
|
+
assert ri[0] == complex(np.inf, 1.0) and ri[1] == complex(1.0, np.inf)
|
|
240
|
+
|
|
241
|
+
|
|
242
|
+
def test_L1_complex():
|
|
243
|
+
"""Complex L1 prox = magnitude soft-threshold keeping the phase."""
|
|
244
|
+
N = 30
|
|
245
|
+
rng = np.random.RandomState(1)
|
|
246
|
+
h = rng.randn(N) + 1j * rng.randn(N)
|
|
247
|
+
d = 0.5 + np.arange(N) * 0.1 # nonuniform positive weights
|
|
248
|
+
mu = DiagonalMatrix(d)
|
|
249
|
+
alpha = 0.7
|
|
250
|
+
x = L1Regularizer(alpha, N).solve(h, mu)
|
|
251
|
+
assert x.dtype == np.complex128
|
|
252
|
+
# closed form
|
|
253
|
+
center = -h / d
|
|
254
|
+
lam = 0.5 * alpha / d
|
|
255
|
+
mag = np.abs(center)
|
|
256
|
+
expect = np.where(mag > 0, np.maximum(mag - lam, 0.0) * center / np.where(mag > 0, mag, 1.0), 0.0)
|
|
257
|
+
assert np.allclose(x, expect, atol=1e-12)
|
|
258
|
+
# objective optimality (per component): f(x) <= f(x + perturbation)
|
|
259
|
+
for i in range(0, N, 7):
|
|
260
|
+
f = lambda z: alpha * np.abs(z) + 2 * np.real(np.conj(h[i]) * z) + d[i] * np.abs(z) ** 2
|
|
261
|
+
for pert in [0.05, -0.05, 0.05j, -0.05j, 0.03 + 0.03j]:
|
|
262
|
+
assert f(x[i]) <= f(x[i] + pert) + 1e-12
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
def test_spd_penalty_complex_correctness():
|
|
266
|
+
"""Complex-Hermitian SPD projection with a scalar (uniform) penalty must
|
|
267
|
+
equal the eigenvalue-clipped Hermitization of the unconstrained centre."""
|
|
268
|
+
rng = np.random.RandomState(2)
|
|
269
|
+
K, N = 6, 3
|
|
270
|
+
h = rng.randn(N * N * K) + 1j * rng.randn(N * N * K)
|
|
271
|
+
d = 2.0
|
|
272
|
+
mu = PartialDiagonalMatrix(ScaledIdentityMatrix(N * N, d), (K,))
|
|
273
|
+
res = SemiPositiveDefinitePenalty((N, N, K), axis=2).solve(h, mu).reshape((N, N, K))
|
|
274
|
+
hb = (-h / d).reshape((N, N, K))
|
|
275
|
+
for k in range(K):
|
|
276
|
+
H = 0.5 * (hb[:, :, k] + hb[:, :, k].conj().T) # Hermitize
|
|
277
|
+
ev, U = np.linalg.eigh(H)
|
|
278
|
+
keep = ev >= 0
|
|
279
|
+
ref = U[:, keep] @ (ev[keep][:, None] * U[:, keep].conj().T)
|
|
280
|
+
assert np.allclose(res[:, :, k], ref, atol=1e-10) # exact projection
|
|
281
|
+
assert np.allclose(res[:, :, k], res[:, :, k].conj().T, atol=1e-12) # Hermitian
|
|
282
|
+
assert np.linalg.eigvalsh(res[:, :, k]).min() > -1e-10 # PSD
|
|
283
|
+
# imaginary part is genuinely retained (not dropped)
|
|
284
|
+
assert np.max(np.abs(res.imag)) > 1e-3
|
|
285
|
+
|
|
286
|
+
|
|
287
|
+
def test_spd_penalty_nonuniform_center():
|
|
288
|
+
"""Verify the weighted paired-Hermitian center for a non-uniform diagonal
|
|
289
|
+
metric (independent of the known non-uniform PSD-projection limitation).
|
|
290
|
+
Uses a case whose center is already PSD, so eigenvalue clipping is a no-op
|
|
291
|
+
and the result must equal the center exactly."""
|
|
292
|
+
N, K = 2, 1
|
|
293
|
+
# per-entry weights d (Hermitian-pattern positive); build via DiagonalMatrix
|
|
294
|
+
d = np.array([1.0, 3.0, 0.5, 2.0]) # (0,0),(0,1),(1,0),(1,1)
|
|
295
|
+
# choose h so that centre = -(h_ij + conj(h_ji))/(d_ij + d_ji) is PSD
|
|
296
|
+
h = np.array([-2.0 + 0j, -(0.3 + 0.2j), -(0.3 - 0.2j), -3.0 + 0j])
|
|
297
|
+
mu = DiagonalMatrix(d)
|
|
298
|
+
res = SemiPositiveDefinitePenalty((N, N, K), axis=2).solve(h, mu).reshape((N, N))
|
|
299
|
+
hb = h.reshape((N, N)); db = d.reshape((N, N))
|
|
300
|
+
centre = -(hb + hb.conj().T) / (db + db.T)
|
|
301
|
+
assert np.linalg.eigvalsh(centre).min() >= 0 # center already PSD
|
|
302
|
+
assert np.allclose(res, centre, atol=1e-12) # clip is a no-op -> equals center
|
|
303
|
+
assert np.allclose(res, res.conj().T) # Hermitian
|
|
304
|
+
|
|
305
|
+
|
|
306
|
+
def test_spd_penalty_uniform_diagonal_matches_scaled_identity():
|
|
307
|
+
"""A uniform full DiagonalMatrix must give the same result as the scalar
|
|
308
|
+
ScaledIdentityMatrix path."""
|
|
309
|
+
rng = np.random.RandomState(3)
|
|
310
|
+
N, K = 3, 4
|
|
311
|
+
h = rng.randn(N * N * K) + 1j * rng.randn(N * N * K)
|
|
312
|
+
c = 1.7
|
|
313
|
+
p = SemiPositiveDefinitePenalty((N, N, K), axis=2)
|
|
314
|
+
r_scaled = p.solve(h, PartialDiagonalMatrix(ScaledIdentityMatrix(N * N, c), (K,)))
|
|
315
|
+
r_diag = p.solve(h, DiagonalMatrix(np.full(N * N * K, c)))
|
|
316
|
+
assert np.allclose(r_scaled, r_diag, atol=1e-10)
|
|
317
|
+
|
|
318
|
+
|
|
319
|
+
def test_softmax_low_precision_bit_exact():
|
|
320
|
+
"""Real path must be bit-for-bit identical to the historical implementation
|
|
321
|
+
for every real dtype (arithmetic in operand dtype, float64 result)."""
|
|
322
|
+
from admmsolver.objectivefunc import _softmax
|
|
323
|
+
rng = np.random.RandomState(4)
|
|
324
|
+
for dt in (np.float16, np.float32, np.float64):
|
|
325
|
+
y = (rng.randn(64) * 3).astype(dt)
|
|
326
|
+
lam = np.abs(rng.randn(64)).astype(dt) + dt(0.1)
|
|
327
|
+
assert np.array_equal(_softmax(y, lam), _old_softmax(y, lam))
|
|
328
|
+
# integer input: no threshold truncation, float64 output
|
|
329
|
+
yi = np.array([5, -5, 0, 2, -2])
|
|
330
|
+
li = np.full(yi.size, 1.5)
|
|
331
|
+
assert np.array_equal(_softmax(yi, li), _old_softmax(yi, li))
|
|
332
|
+
assert _softmax(yi, li).dtype == np.float64
|
|
333
|
+
|
|
334
|
+
|
|
335
|
+
def test_spd_center_no_overflow_large_values():
|
|
336
|
+
"""Paired centre must stay finite for near-float64-max operands."""
|
|
337
|
+
big = np.finfo(np.float64).max
|
|
338
|
+
N, K = 2, 1
|
|
339
|
+
# uniform h = d = big -> centre_ij = -(big+big)/(big+big) = -1 (must not be NaN)
|
|
340
|
+
h = np.array([big, big, big, big], dtype=np.float64)
|
|
341
|
+
d = np.array([big, big, big, big], dtype=np.float64)
|
|
342
|
+
p = SemiPositiveDefinitePenalty((N, N, K), axis=2)
|
|
343
|
+
res = p.solve(h, DiagonalMatrix(d)).reshape((N, N))
|
|
344
|
+
assert np.isfinite(res).all()
|
|
345
|
+
centre = -np.ones((N, N)) # all entries -1
|
|
346
|
+
ev, U = np.linalg.eigh(centre)
|
|
347
|
+
ref = U[:, ev >= 0] @ (ev[ev >= 0][:, None] * U[:, ev >= 0].conj().T)
|
|
348
|
+
assert np.allclose(res, ref, atol=1e-8)
|
|
349
|
+
|
|
350
|
+
|
|
351
|
+
def test_softmax_large_finite_complex_no_overflow():
|
|
352
|
+
"""Large-but-finite complex magnitude must be shrunk (not passed through),
|
|
353
|
+
even when |y| = sqrt(re^2+im^2) overflows float64."""
|
|
354
|
+
from admmsolver.objectivefunc import _softmax
|
|
355
|
+
y = np.array([1.3e308 + 1.3e308j])
|
|
356
|
+
lam = np.array([1.0e308])
|
|
357
|
+
out = _softmax(y, lam)
|
|
358
|
+
assert np.isfinite(out).all()
|
|
359
|
+
# exact prox = (1 - lambda/|y|) * y, with lambda/|y| via component scaling so
|
|
360
|
+
# the reference itself does not overflow ( |y| ~ 1.84e308 > DBL_MAX ).
|
|
361
|
+
m = max(abs(y[0].real), abs(y[0].imag))
|
|
362
|
+
ratio = (lam[0] / m) / np.hypot(y[0].real / m, y[0].imag / m)
|
|
363
|
+
expect = (1.0 - ratio) * y[0]
|
|
364
|
+
assert np.isclose(out[0].real, expect.real, rtol=1e-12)
|
|
365
|
+
assert np.isclose(out[0].imag, expect.imag, rtol=1e-12)
|
|
366
|
+
assert abs(out[0].real) < 6.0e307 # genuinely shrunk from 1.3e308
|
|
367
|
+
|
|
368
|
+
|
|
369
|
+
def test_spd_center_large_finite_complex():
|
|
370
|
+
"""SPD paired centre must stay finite for near-max complex operands."""
|
|
371
|
+
big = np.finfo(np.float64).max
|
|
372
|
+
N, K = 2, 1
|
|
373
|
+
h = np.array([complex(-big, big)] * (N * N))
|
|
374
|
+
d = np.array([big] * (N * N))
|
|
375
|
+
res = SemiPositiveDefinitePenalty((N, N, K), axis=2).solve(h, DiagonalMatrix(d)).reshape((N, N))
|
|
376
|
+
assert np.isfinite(res).all()
|
|
377
|
+
# centre_ij = -(h_ij + conj(h_ji))/(d_ij+d_ji) = -(-big+big j + (-big-big j))/(2 big) = 1
|
|
378
|
+
ones = np.ones((N, N))
|
|
379
|
+
assert np.allclose(res, ones, atol=1e-8) # ones is PSD -> projection is itself
|
|
@@ -1,186 +0,0 @@
|
|
|
1
|
-
import numpy as np
|
|
2
|
-
from scipy.optimize import minimize
|
|
3
|
-
from typing import cast
|
|
4
|
-
|
|
5
|
-
from admmsolver.objectivefunc import *
|
|
6
|
-
from admmsolver.matrix import identity, asmatrixtype, PartialDiagonalMatrix
|
|
7
|
-
|
|
8
|
-
import pytest
|
|
9
|
-
|
|
10
|
-
def _randn_cmplx(*shape) -> np.ndarray:
|
|
11
|
-
return np.random.randn(*shape) + 1j* np.random.randn(*shape)
|
|
12
|
-
|
|
13
|
-
def _to_real_array(x):
|
|
14
|
-
if np.iscomplexobj(x):
|
|
15
|
-
return x.view(np.float64).ravel()
|
|
16
|
-
else:
|
|
17
|
-
return x
|
|
18
|
-
|
|
19
|
-
def _from_real_array(x):
|
|
20
|
-
x = x.reshape((-1, 2))
|
|
21
|
-
x = x.view(np.complex128)
|
|
22
|
-
return x.reshape(x.shape[0:-1])
|
|
23
|
-
|
|
24
|
-
def _minimize(f, x0, method="BFGS"):
|
|
25
|
-
x0 = _to_real_array(x0)
|
|
26
|
-
res = minimize(
|
|
27
|
-
lambda x: f(_from_real_array(x)),
|
|
28
|
-
x0,
|
|
29
|
-
method=method,
|
|
30
|
-
options={'maxiter': 100000}
|
|
31
|
-
)
|
|
32
|
-
return _from_real_array(res.x)
|
|
33
|
-
|
|
34
|
-
def test_least_squares():
|
|
35
|
-
"""
|
|
36
|
-
Least-squares fit with a dense coefficient matrix
|
|
37
|
-
"""
|
|
38
|
-
np.random.seed(100)
|
|
39
|
-
N1, N2 = 4, 2
|
|
40
|
-
alpha = 2.0
|
|
41
|
-
y = _randn_cmplx(N1)
|
|
42
|
-
A = _randn_cmplx(N1, N2)
|
|
43
|
-
h = _randn_cmplx(N2)
|
|
44
|
-
sqrt_mu = _randn_cmplx(N2, N2)
|
|
45
|
-
mu = asmatrixtype(sqrt_mu.T.conjugate() @ sqrt_mu)
|
|
46
|
-
lstsq = LeastSquares(alpha, A, y)
|
|
47
|
-
x = lstsq.solve(h, mu)
|
|
48
|
-
|
|
49
|
-
f_all = lambda x: np.real(alpha * np.linalg.norm(y - A @ x)**2 + \
|
|
50
|
-
h.T.conjugate() @ x + x.T.conjugate() @ h + x.conjugate().T @ (mu @ x))
|
|
51
|
-
x_ref = _minimize(f_all, x, "BFGS")
|
|
52
|
-
np.testing.assert_allclose(x, x_ref, rtol=1e-8)
|
|
53
|
-
np.testing.assert_allclose(f_all(x), f_all(x_ref), rtol=1e-8)
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
def test_least_squares_partial():
|
|
57
|
-
"""
|
|
58
|
-
Least-squares fit with a coefficient matrix of type PartialDiagonalMatrix
|
|
59
|
-
"""
|
|
60
|
-
np.random.seed(100)
|
|
61
|
-
N1, N2 = 40, 20
|
|
62
|
-
alpha = 2.0
|
|
63
|
-
rest_dim_size = 20
|
|
64
|
-
y = _randn_cmplx(N1)
|
|
65
|
-
A = PartialDiagonalMatrix(_randn_cmplx(N1//rest_dim_size, N2//rest_dim_size), rest_dims=(rest_dim_size,))
|
|
66
|
-
assert A.shape == (N1, N2)
|
|
67
|
-
h = _randn_cmplx(N2)
|
|
68
|
-
sqrt_mu = _randn_cmplx(N2, N2)
|
|
69
|
-
mu = asmatrixtype(sqrt_mu.T.conjugate() @ sqrt_mu)
|
|
70
|
-
lstsq = LeastSquares(alpha, A, y)
|
|
71
|
-
x = lstsq.solve(h, mu)
|
|
72
|
-
|
|
73
|
-
f_all = lambda x: \
|
|
74
|
-
np.real(
|
|
75
|
-
alpha * np.linalg.norm(y - A @ x)**2 + h.T.conjugate() @ x + \
|
|
76
|
-
x.T.conjugate() @ h + x.conjugate().T @ (mu @ x))
|
|
77
|
-
x_ref = _minimize(f_all, x, "BFGS")
|
|
78
|
-
np.testing.assert_allclose(x, x_ref, rtol=1e-4)
|
|
79
|
-
np.testing.assert_allclose(f_all(x), f_all(x_ref), rtol=1e-8)
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
def test_constrained_least_squares():
|
|
83
|
-
"""
|
|
84
|
-
Linearly constrained least squares
|
|
85
|
-
"""
|
|
86
|
-
np.random.seed(100)
|
|
87
|
-
|
|
88
|
-
N1, N2 = 8, 4
|
|
89
|
-
Nc = 2
|
|
90
|
-
alpha = 2.0
|
|
91
|
-
y = _randn_cmplx(N1)
|
|
92
|
-
A = _randn_cmplx(N1, N2)
|
|
93
|
-
h = _randn_cmplx(N2)
|
|
94
|
-
C = _randn_cmplx(Nc, N2)
|
|
95
|
-
D = _randn_cmplx(Nc)
|
|
96
|
-
sqrt_mu = _randn_cmplx(N2, N2)
|
|
97
|
-
mu = asmatrixtype(sqrt_mu.T.conjugate() @ sqrt_mu)
|
|
98
|
-
lstsq = ConstrainedLeastSquares(alpha, A, y, C, D)
|
|
99
|
-
|
|
100
|
-
x = lstsq.solve(h, mu)
|
|
101
|
-
assert np.abs(C@x - D).max() < 1e-10
|
|
102
|
-
|
|
103
|
-
#FIXME: how to check x?
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
def test_L1():
|
|
107
|
-
"""
|
|
108
|
-
Minimize alpha * |x|_1 + h^+ x + x^+ h + mu x^+ x
|
|
109
|
-
"""
|
|
110
|
-
N = 20
|
|
111
|
-
assert N%2 == 0
|
|
112
|
-
h = 0.5*np.arange(-N//2, N//2)
|
|
113
|
-
mu = identity(N)
|
|
114
|
-
alpha = 1.0
|
|
115
|
-
|
|
116
|
-
l1 = L1Regularizer(alpha, N)
|
|
117
|
-
x = l1.solve(h, mu)
|
|
118
|
-
|
|
119
|
-
# Naive optimization
|
|
120
|
-
for i in range(N):
|
|
121
|
-
f = lambda x: alpha * np.abs(x) + 2*h[i]*x + mu.diagonals[i] * x**2
|
|
122
|
-
x0 = 0.0
|
|
123
|
-
res = minimize(f, x0, method="BFGS")
|
|
124
|
-
assert np.abs(x[i]-res.x[0]) < 1e-5
|
|
125
|
-
|
|
126
|
-
def test_non_negative():
|
|
127
|
-
"""
|
|
128
|
-
Minimize infty * Theta(-x) + h^+ x + x^+ h + mu x^+ x
|
|
129
|
-
"""
|
|
130
|
-
h = np.array([0, -10, 10])
|
|
131
|
-
N = h.size
|
|
132
|
-
mu = identity(N)
|
|
133
|
-
|
|
134
|
-
func = NonNegativePenalty(N)
|
|
135
|
-
x = func.solve(h, mu)
|
|
136
|
-
|
|
137
|
-
step_f = lambda x: x if x >= 0 else 0
|
|
138
|
-
|
|
139
|
-
# Naive optimization
|
|
140
|
-
for i in range(N):
|
|
141
|
-
f = lambda x: 1e+5 * step_f(-x) + 2*h[i]*x + mu.diagonals[i] * x**2
|
|
142
|
-
x0 = 0.0
|
|
143
|
-
res = minimize(f, x0, method="BFGS")
|
|
144
|
-
assert np.abs(x[i]-res.x[0]) < 1e-5
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
def test_L2():
|
|
148
|
-
"""
|
|
149
|
-
Minimize alpha * |A x|_2^2 + h^+ x + x^+ h + mu x^+ x
|
|
150
|
-
"""
|
|
151
|
-
N = 10
|
|
152
|
-
M = 5
|
|
153
|
-
sqrt_mu = _randn_cmplx(N, N)
|
|
154
|
-
mu = asmatrixtype(sqrt_mu.T.conjugate() @ sqrt_mu)
|
|
155
|
-
alpha = 2.0
|
|
156
|
-
A = _randn_cmplx(M, N)
|
|
157
|
-
h = _randn_cmplx(N)
|
|
158
|
-
|
|
159
|
-
l2 = L2Regularizer(alpha, A)
|
|
160
|
-
x = l2.solve(h, mu)
|
|
161
|
-
|
|
162
|
-
# Naive optimization
|
|
163
|
-
f = lambda x: alpha * np.linalg.norm(A @ x)**2 + \
|
|
164
|
-
2*np.real(h.conjugate().T @ x) + np.real(x.conjugate().T @ (mu @ x))
|
|
165
|
-
x_ref = _minimize(f, x, method="BFGS")
|
|
166
|
-
np.testing.assert_allclose(cast(np.ndarray, x), x_ref, atol=np.abs(x_ref).max()*1e-5, rtol=0)
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
def test_semi_positive_definite_penalty():
|
|
170
|
-
np.random.seed(100)
|
|
171
|
-
K = 20
|
|
172
|
-
N = 10
|
|
173
|
-
|
|
174
|
-
h = _randn_cmplx(N**2 * K)
|
|
175
|
-
|
|
176
|
-
for mu in [
|
|
177
|
-
asmatrixtype(identity(N**2 * K)),
|
|
178
|
-
PartialDiagonalMatrix(ScaledIdentityMatrix(N**2, 1.0), (K,))]:
|
|
179
|
-
p = SemiPositiveDefinitePenalty((N,N,K), axis=2)
|
|
180
|
-
res = p.solve(h, mu)
|
|
181
|
-
|
|
182
|
-
x = res.reshape((N,N,K))
|
|
183
|
-
for k in range(K):
|
|
184
|
-
evals, evecs = np.linalg.eigh(x[:,:,k])
|
|
185
|
-
assert all(evals > -1e-10)
|
|
186
|
-
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|