def test_ista(): """Test that objective goes down in ISTA for a simple problem.""" # || Ax - b ||_2^2 n, p = 100, 10 x = np.random.randn(p) x /= np.linalg.norm(x) A = np.random.randn(n, p) b = np.dot(A, x) def obj(x): res = A.dot(x) - b return 0.5 * np.dot(res.ravel(), res.ravel()) def grad(x): return A.T.dot(A.dot(x) - b) def prox(x, step_size=0): return x / max(np.linalg.norm(x), 1.) x0 = np.random.rand(p) L = power_iteration(A.dot(A.T)) step_size = 0.99 / L x_hat, pobj = fista(obj, grad, prox, step_size, x0, max_iter=600, momentum=False, eps=None, debug=True, verbose=0) np.testing.assert_array_almost_equal(x, x_hat) assert np.all(np.diff(pobj) <= 0)
def test_linear_operator(): """Test linear operator.""" n_times, n_atoms, n_times_atom = 64, 16, 32 n_times_valid = n_times - n_times_atom + 1 rng = check_random_state(42) ds = rng.randn(n_atoms, n_times_atom) some_sample_weights = np.abs(rng.randn(n_times)) for sample_weights in [None, some_sample_weights]: gbc = partial(gram_block_circulant, ds=ds, n_times_valid=n_times_valid, sample_weights=sample_weights) DTD_full = gbc(method='full') DTD_scipy = gbc(method='scipy') DTD_custom = gbc(method='custom') z = rng.rand(DTD_full.shape[1]) assert np.allclose(DTD_full.dot(z), DTD_scipy.dot(z)) assert np.allclose(DTD_full.dot(z), DTD_custom.dot(z)) # test power iterations with linear operator mu, _ = linalg.eigh(DTD_full) for DTD in [DTD_full, DTD_scipy, DTD_custom]: mu_hat = power_iteration(DTD) assert np.allclose(np.max(mu), mu_hat, rtol=1e-2)
def test_power_iterations(): """Test power iteration.""" A = np.diag((1, 2, 3)) mu, b = np.linalg.eig(A) mu_hat = power_iteration(A) assert np.isclose(mu_hat, mu.max())