Example #1
0
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)
Example #2
0
def test_fused_lasso(n=100,l1=2.,**control):

    D = (np.identity(n) - np.diag(np.ones(n-1),-1))[1:]
    M = np.linalg.eigvalsh(np.dot(D.T, D)).max() 

    Y = np.random.standard_normal(n)
    Y[int(0.1*n):int(0.3*n)] += 6.

    p1 = signal_approximator((D, Y),L=M)
    p1.assign_penalty(l1=l1)
    
    p2 = glasso.generalized_lasso((np.identity(n), D, Y),L=M)
    p2.assign_penalty(l1=l1)
    
    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.fit(tol=control['tol'], max_its=control['max_its'])
    t2 = time.time()
    ts2 = t2-t1

    beta1, _ = opt1.output
    beta2, _ = opt2.output
    X = np.arange(n)

    print (np.fabs(beta1-beta2).sum() / np.fabs(beta1).sum())
    if control['plot']:
        pylab.clf()
        pylab.step(X, beta1, linewidth=3, c='red')
        pylab.step(X, beta2, linewidth=3, c='blue')
        pylab.scatter(X, Y)
        pylab.show()
Example #3
0
def test_fused_lasso(n=100, l1=2., **control):

    D = (np.identity(n) - np.diag(np.ones(n - 1), -1))[1:]
    M = np.linalg.eigvalsh(np.dot(D.T, D)).max()

    Y = np.random.standard_normal(n)
    Y[int(0.1 * n):int(0.3 * n)] += 6.

    p1 = signal_approximator((D, Y), L=M)
    p1.assign_penalty(l1=l1)

    p2 = glasso.generalized_lasso((np.identity(n), D, Y), L=M)
    p2.assign_penalty(l1=l1)

    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.fit(tol=control['tol'], max_its=control['max_its'])
    t2 = time.time()
    ts2 = t2 - t1

    beta1, _ = opt1.output
    beta2, _ = opt2.output
    X = np.arange(n)

    print(np.fabs(beta1 - beta2).sum() / np.fabs(beta1).sum())
    if control['plot']:
        pylab.clf()
        pylab.step(X, beta1, linewidth=3, c='red')
        pylab.step(X, beta2, linewidth=3, c='blue')
        pylab.scatter(X, Y)
        pylab.show()