def test_sparse_regression(): """ Test sparse regression """ A, y, x_true = generate_sparse_system() # least squares solution xls = np.linalg.lstsq(A, y)[0] # proximal algorithm for sparse regression opt = ADMM(xls) opt.add('linsys', A=A, b=y) opt.add('sparse', 1.) opt.display = None opt.storage = None opt.run(maxiter=100) xhat = opt.theta test_err = np.linalg.norm(xhat - x_true, 2) naive_err = np.linalg.norm(xls - x_true, 2) err_ratio = test_err / naive_err print("The error ratio is: %5.4f" % err_ratio) assert err_ratio <= 0.01
def test_lowrank_matrix_approx(): """ Test low rank matrix approximation """ Xobs, Xtrue = generate_lowrank_matrix() # proximal algorithm for low rank matrix approximation opt = ADMM(Xobs) opt.add('squared_error', Xobs) opt.add('nucnorm', 0.2) opt.display = None opt.storage = None opt.run(maxiter=100) Xhat = opt.theta test_err = np.linalg.norm(Xhat - Xtrue, 'fro') naive_err = np.linalg.norm(Xobs - Xtrue, 'fro') err_ratio = test_err / naive_err print("The error ratio is: %5.4f" % err_ratio) assert err_ratio <= 0.5