def test_admm_l1_seminorm(): """ Test ADMM using the l1norm in lagrange form """ p = 1000 Y = 10 * np.random.normal(0,1,p) loss = R.quadratic.shift(-Y, coef=0.5) sparsity = R.l1norm(p, lagrange=5.) prob = R.container(loss, sparsity) solver = R.admm_problem(prob) solver.fit(debug=False, tol=1e-12) solution = solver.beta npt.assert_array_almost_equal(solution, np.maximum(np.fabs(Y) - sparsity.lagrange,0.)*np.sign(Y), 3)
def test_admm_l1_constraint(): """ Test ADMM using the l1norm in bound form """ p = 1000 Y = 10 * np.random.normal(0, 1, p) loss = R.linear(Y, coef=0.5) sparsity = R.l1norm(p, bound=5.) sparsity.bound *= 1. prob = R.container(loss, sparsity) solver = R.admm_problem(prob) solver.fit(debug=False, tol=1e-12) solution = solver.beta npt.assert_almost_equal(np.fabs(solution).sum(), sparsity.bound, 3)
def test_admm_l1_seminorm(): """ Test ADMM using the l1norm in lagrange form """ p = 1000 Y = 10 * np.random.normal(0, 1, p) loss = R.quadratic.shift(-Y, coef=0.5) sparsity = R.l1norm(p, lagrange=5.) prob = R.container(loss, sparsity) solver = R.admm_problem(prob) solver.fit(debug=False, tol=1e-12) solution = solver.beta npt.assert_array_almost_equal( solution, np.maximum(np.fabs(Y) - sparsity.lagrange, 0.) * np.sign(Y), 3)
def test_admm_l1_constraint(): """ Test ADMM using the l1norm in bound form """ p = 1000 Y = 10 * np.random.normal(0,1,p) loss = R.linear(Y, coef=0.5) sparsity = R.l1norm(p, bound=5.) sparsity.bound *= 1. prob = R.container(loss, sparsity) solver = R.admm_problem(prob) solver.fit(debug=False, tol=1e-12) solution = solver.beta npt.assert_almost_equal(np.fabs(solution).sum(), sparsity.bound, 3)
import numpy as np import pylab from scipy import sparse import regreg.api as R Y = np.random.standard_normal(500) Y[100:150] += 7 Y[250:300] += 14 loss = R.signal_approximator(Y) sparsity = R.l1norm(len(Y), lagrange=0.8) D = (np.identity(500) + np.diag([-1] * 499, k=1))[:-1] D = sparse.csr_matrix(D) fused = R.l1norm.linear(D, lagrange=25.5) problem = R.container(loss, sparsity, fused) solver = R.admm_problem(problem) solver.fit(max_its=1000, tol=1e-8) solution = solver.beta pylab.plot(solution, c='g', linewidth=3) pylab.scatter(np.arange(Y.shape[0]), Y)
import numpy as np import pylab from scipy import sparse import regreg.api as R Y = np.random.standard_normal(500); Y[100:150] += 7; Y[250:300] += 14 loss = R.signal_approximator(Y) sparsity = R.l1norm(len(Y), lagrange=0.8) D = (np.identity(500) + np.diag([-1]*499,k=1))[:-1] D = sparse.csr_matrix(D) fused = R.l1norm.linear(D, lagrange=25.5) problem = R.container(loss, sparsity, fused) solver = R.admm_problem(problem) solver.fit(max_its=1000, tol=1e-8) solution = solver.beta pylab.plot(solution, c='g', linewidth=3) pylab.scatter(np.arange(Y.shape[0]), Y)