Exemple #1
0
def test_sparse_linear_trend(n=500, l1=2., ratio=0.1, control=control):

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

    Y = np.random.standard_normal(n) * 0.1
    X = np.linspace(0, 1, n)
    mu = 0 * Y
    mu[int(0.1 *
           n):int(0.3 *
                  n)] += (X[int(0.1 * n):int(0.3 * n)] - X[int(0.1 * n)]) * 6
    mu[int(0.3 * n):int(0.5 * n)] += (X[int(0.3 * n):int(0.5 * n)] -
                                      X[int(0.3 * n)]) * (-6) + 1.2
    Y += mu
    p1 = sapprox.signal_approximator((D, Y), L=M)
    p1.assign_penalty(l1=l1)

    t1 = time.time()
    opt1 = regreg.FISTA(p1)
    opt1.fit(tol=control['tol'], max_its=control['max_its'])
    beta1 = opt1.problem.coefs
    t2 = time.time()
    ts1 = t2 - t1

    beta, _ = opt1.output
    if control['plot']:
        pylab.clf()
        pylab.plot(X, beta, linewidth=3, c='red')
        pylab.scatter(X, Y)
        pylab.plot(X, mu)
        pylab.show()
Exemple #2
0
def test_sparse_fused_lasso(n=500, l1=2., ratio=1., control=control):

    D = (np.identity(n) - np.diag(np.ones(n - 1), -1))[1:]
    D = np.vstack([D, ratio * np.identity(n)])
    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)] += 3.
    p1 = sapprox.signal_approximator((D, Y), L=M)
    p1.assign_penalty(l1=l1)

    t1 = time.time()
    opt1 = regreg.FISTA(p1)
    opt1.fit(tol=control['tol'], max_its=control['max_its'])
    beta1 = opt1.problem.coefs
    t2 = time.time()
    ts1 = t2 - t1

    beta, _ = opt1.output
    X = np.arange(n)
    if control['plot']:
        pylab.clf()
        pylab.step(X, beta, linewidth=3, c='red')
        pylab.scatter(X, Y)
        pylab.show()
Exemple #3
0
def test_sparse_linear_trend(n=500,l1=2., ratio=0.1, control=control):

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

    Y = np.random.standard_normal(n) * 0.1
    X = np.linspace(0,1,n)
    mu = 0 * Y
    mu[int(0.1*n):int(0.3*n)] += (X[int(0.1*n):int(0.3*n)] - X[int(0.1*n)]) * 6
    mu[int(0.3*n):int(0.5*n)] += (X[int(0.3*n):int(0.5*n)] - X[int(0.3*n)]) * (-6) + 1.2
    Y += mu
    p1 = sapprox.signal_approximator((D, Y),L=M)
    p1.assign_penalty(l1=l1)
    
    t1 = time.time()
    opt1 = regreg.FISTA(p1)
    opt1.fit(tol=control['tol'], max_its=control['max_its'])
    beta1 = opt1.problem.coefs
    t2 = time.time()
    ts1 = t2-t1

    beta, _ = opt1.output
    if control['plot']:
        pylab.clf()
        pylab.plot(X, beta, linewidth=3, c='red')
        pylab.scatter(X, Y)
        pylab.plot(X, mu)
        pylab.show()
Exemple #4
0
def test_sparse_fused_lasso(n=500,l1=2.,ratio=1.,control=control):

    D = (np.identity(n) - np.diag(np.ones(n-1),-1))[1:]
    D = np.vstack([D, ratio*np.identity(n)])
    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)] += 3.
    p1 = sapprox.signal_approximator((D, Y),L=M)
    p1.assign_penalty(l1=l1)
    
    t1 = time.time()
    opt1 = regreg.FISTA(p1)
    opt1.fit(tol=control['tol'], max_its=control['max_its'])
    beta1 = opt1.problem.coefs
    t2 = time.time()
    ts1 = t2-t1

    beta, _ = opt1.output
    X = np.arange(n)
    if control['plot']:
        pylab.clf()
        pylab.step(X, beta, linewidth=3, c='red')
        pylab.scatter(X, Y)
        pylab.show()
Exemple #5
0
def test_group_lasso(l1=0.1, **control):
    """
    fits a fused lasso as a group lasso approximator, i.e.
    all 2norms are one dimensional
    """
    Y = np.load('Y.npy')
    n = Y.shape[0]

    def e(i, n):
        z = np.zeros(n)
        z[i] = 1.
        z[i + 1] = -1
        return z

    Dv = [(e(i, n), l1 * n) for i in range(n - 1)]
    D = (np.identity(n) - np.diag(np.ones(n - 1), -1))[1:]
    M = np.linalg.eigvalsh(np.dot(D.T, D)).max()

    p1 = group.group_approximator((Dv, Y), L=M)

    p2 = group.group_lasso((np.identity(n), Dv, Y), L=M)

    p3 = signal_approximator.signal_approximator((D, Y), L=M)
    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(p3)
    opt2.fit(tol=control['tol'], max_its=control['max_its'])
    t2 = time.time()
    ts3 = 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)
Exemple #6
0
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)
Exemple #7
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()
Exemple #8
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()