def test_group_lasso2(l1=1, **control): """ fits a LASSO as a group lasso with all 2norms are one dimensional """ X = np.load("X.npy") Y = np.load('Y.npy') n, p = X.shape XtX = np.dot(X.T, X) M = np.linalg.eigvalsh(XtX).max() #/ (1*len(Y)) def e(i, n): z = np.zeros(n) z[i] = 1. return z p1 = lasso.gengrad((X, Y), L=M) p1.assign_penalty(l1=l1 * n) Dv = [(e(i, p), l1 * n) for i in range(p)] p2 = group.group_lasso((X, Dv, Y)) p2.dualcontrol['tol'] = 1.0e-10 t1 = time.time() opt1 = regreg.FISTA(p1) opt1.fit(tol=control['tol'], max_its=control['max_its']) t2 = time.time() ts1 = t2 - t1 t1 = time.time() opt2 = regreg.FISTA(p2) opt2.debug = True opt2.fit(tol=control['tol'], max_its=control['max_its']) t2 = time.time() ts3 = t2 - t1 p3 = glasso.generalized_lasso((X, np.identity(p), Y), L=M) p3.assign_penalty(l1=l1 * n) p3.dualcontrol['tol'] = 1.0e-10 t1 = time.time() opt3 = regreg.FISTA(p3) opt3.fit(tol=control['tol'], max_its=control['max_its']) t2 = time.time() ts3 = t2 - t1 beta1, _ = opt1.output beta2, _ = opt2.output beta3, _ = opt3.output X = np.arange(n) nose.tools.assert_true( (np.fabs(beta1 - beta2).sum() / np.fabs(beta1).sum()) < 5.0e-04) nose.tools.assert_true( (np.fabs(beta1 - beta3).sum() / np.fabs(beta1).sum()) < 5.0e-04)
def test_group_lasso_approximator1(l1=0.1, **control): Y = np.load('Y.npy') n = Y.shape[0] def e(i, n): z = np.zeros(n) z[i] = 1. return z Dv = [(e(i, n), l1 * n) for i in range(n)] p1 = group.group_approximator((Dv, Y)) p2 = lasso.gengrad((np.identity(n), Y)) p2.assign_penalty(l1=l1 * n) p3 = signal_approximator.signal_approximator((np.identity(n), Y)) p3.assign_penalty(l1=l1 * n) t1 = time.time() opt1 = regreg.FISTA(p1) opt1.debug = True opt1.fit(tol=control['tol'], max_its=control['max_its']) t2 = time.time() ts1 = t2 - t1 t1 = time.time() opt2 = regreg.FISTA(p2) opt2.fit(tol=control['tol'], max_its=control['max_its']) t2 = time.time() ts2 = t2 - t1 t1 = time.time() opt3 = regreg.FISTA(p3) opt3.fit(tol=control['tol'], max_its=control['max_its']) t2 = time.time() ts3 = t2 - t1 beta1, _ = opt1.output beta2, _ = opt2.output beta3, _ = opt3.output X = np.arange(n) nose.tools.assert_true( (np.fabs(beta1 - beta3).sum() / np.fabs(beta1).sum()) < 1.0e-04) nose.tools.assert_true( (np.fabs(beta1 - beta2).sum() / np.fabs(beta1).sum()) < 1.0e-04)
def test_FISTA(X=None, Y=None, l1=5., control=control): if X or Y is None: X = np.load('X.npy') Y = np.load('Y.npy') XtX = np.dot(X.T, X) M = np.linalg.eigvalsh(XtX).max() #/ (1*len(Y)) p3 = lasso.gengrad((X, Y), L=M) p3.assign_penalty(l1=l1 * X.shape[0]) t1 = time.time() opt3 = regreg.FISTA(p3) opt3.fit(tol=control['tol'], max_its=control['max_its']) beta3 = opt3.problem.coefs t2 = time.time() ts3 = t2 - t1
def test_FISTA(X=None,Y=None,l1=5., control=control): if X or Y is None: X = np.load('X.npy') Y = np.load('Y.npy') XtX = np.dot(X.T, X) M = np.linalg.eigvalsh(XtX).max() #/ (1*len(Y)) p3 = lasso.gengrad((X, Y),L=M) p3.assign_penalty(l1=l1*X.shape[0]) t1 = time.time() opt3 = regreg.FISTA(p3) opt3.fit(tol=control['tol'], max_its=control['max_its']) beta3 = opt3.problem.coefs t2 = time.time() ts3 = t2-t1
def test_lasso(X=None, Y=None, l1=5., control=control): if X or Y is None: X = np.load('X.npy') Y = np.load('Y.npy') XtX = np.dot(X.T, X) M = np.linalg.eigvalsh(XtX).max() #/ (1*len(Y)) print M Y += np.dot(X[:, :5], 10 * np.ones(5)) p1 = lasso.cwpath((X, Y)) p1.assign_penalty(l1=l1 * X.shape[0]) p2 = lasso.gengrad((X, Y), L=M) p2.assign_penalty(l1=l1 * X.shape[0]) p3 = lasso.gengrad((X, Y), L=M) p3.assign_penalty(l1=l1 * X.shape[0]) p4 = lasso.gengrad_smooth((X, Y), L=M) p4.assign_penalty(l1=l1 * X.shape[0]) t1 = time.time() opt1 = regreg.CWPath(p1) opt1.fit(tol=1e-8, max_its=control['max_its']) beta1 = opt1.problem.coefs t2 = time.time() ts1 = t2 - t1 t1 = time.time() opt2 = regreg.ISTA(p2) opt2.fit(tol=control['tol'], max_its=control['max_its'], backtrack=control['backtrack'], alpha=1.25) beta2 = opt2.problem.coefs t2 = time.time() ts2 = t2 - t1 time.sleep(0.5) t1 = time.time() opt3 = regreg.FISTA(p3) opt3.fit(tol=control['tol'], max_its=control['max_its'], backtrack=control['backtrack'], alpha=1.25) beta3 = opt3.problem.coefs t2 = time.time() ts3 = t2 - t1 epsvec = np.exp(np.arange(4, -16, -1)) t1 = time.time() opt4 = regreg.NesterovSmooth(p4) for eps in epsvec: f_s = opt4.fit(tol=control['tol'], max_its=50, epsilon=eps) # f_s = opt4.fit( tol=control['tol'], max_its=control['max_its'],epsilon=0.1) beta4 = opt4.problem.coefs t2 = time.time() ts4 = t2 - t1 print beta1 print beta2 print beta3 print p2.obj(beta1), p2.obj(beta2), p3.obj(beta3) nose.tools.assert_true( (np.fabs(beta1 - beta3).sum() / np.fabs(beta1).sum() <= 1.0e-04)) """ print p3.obj(beta1), p3.obj(beta2), p3.obj(beta3) """ print "Times", ts1, ts2, ts3, ts4
def test_lasso(X=None,Y=None,l1=5., control=control): if X or Y is None: X = np.load('X.npy') Y = np.load('Y.npy') XtX = np.dot(X.T, X) M = np.linalg.eigvalsh(XtX).max() #/ (1*len(Y)) print M Y += np.dot(X[:,:5], 10 * np.ones(5)) p1 = lasso.cwpath((X, Y)) p1.assign_penalty(l1=l1*X.shape[0]) p2 = lasso.gengrad((X, Y),L=M) p2.assign_penalty(l1=l1*X.shape[0]) p3 = lasso.gengrad((X, Y),L=M) p3.assign_penalty(l1=l1*X.shape[0]) p4 = lasso.gengrad_smooth((X, Y),L=M) p4.assign_penalty(l1=l1*X.shape[0]) t1 = time.time() opt1 = regreg.CWPath(p1) opt1.fit(tol=1e-8, max_its=control['max_its']) beta1 = opt1.problem.coefs t2 = time.time() ts1 = t2-t1 t1 = time.time() opt2 = regreg.ISTA(p2) opt2.fit(tol=control['tol'], max_its=control['max_its'],backtrack=control['backtrack'],alpha=1.25) beta2 = opt2.problem.coefs t2 = time.time() ts2 = t2-t1 time.sleep(0.5) t1 = time.time() opt3 = regreg.FISTA(p3) opt3.fit(tol=control['tol'], max_its=control['max_its'],backtrack=control['backtrack'],alpha=1.25) beta3 = opt3.problem.coefs t2 = time.time() ts3 = t2-t1 epsvec = np.exp(np.arange(4,-16,-1)) t1 = time.time() opt4 = regreg.NesterovSmooth(p4) for eps in epsvec: f_s = opt4.fit( tol=control['tol'], max_its=50,epsilon=eps) # f_s = opt4.fit( tol=control['tol'], max_its=control['max_its'],epsilon=0.1) beta4 = opt4.problem.coefs t2 = time.time() ts4 = t2-t1 print beta1 print beta2 print beta3 print p2.obj(beta1), p2.obj(beta2), p3.obj(beta3) nose.tools.assert_true((np.fabs(beta1-beta3).sum() / np.fabs(beta1).sum() <= 1.0e-04)) """ print p3.obj(beta1), p3.obj(beta2), p3.obj(beta3) """ print "Times", ts1, ts2, ts3, ts4