コード例 #1
0
ファイル: test_graphroot.py プロジェクト: sowuy/regreg
def test_graphroot(X=None, Y=None, l1=5., l2=10., control=control, mu=1.):

    if X is None or Y is None:
        X = np.load('X.npy')
        Y = np.load('Y.npy')

    p = X.shape[1]
    adj, L = gen_adj(p)
    Dsparse = mask.create_D(adj)
    D = Dsparse.toarray()
    Lsparse = scipy.sparse.lil_matrix(L)

    l1 *= X.shape[0]
    p1 = graphroot.gengrad((X, Y, D))
    p1.assign_penalty(l1=l1, l2=l2, mu=mu)
    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

    p2 = graphroot.gengrad_sparse((X, Y, Dsparse))
    p2.assign_penalty(l1=l1, l2=l2, mu=mu)
    t1 = time.time()
    opt2 = regreg.FISTA(p2)
    opt2.fit(tol=control['tol'], max_its=control['max_its'])
    beta2 = opt2.problem.coefs
    t2 = time.time()
    ts2 = t2 - t1

    def f(beta):
        return np.linalg.norm(Y - np.dot(X, beta))**2 / (2) + np.fabs(
            beta).sum() * l1 + l2 * np.sqrt(np.dot(beta, np.dot(L, beta)))

    v = scipy.optimize.fmin_powell(f,
                                   np.zeros(X.shape[1]),
                                   ftol=1.0e-10,
                                   xtol=1.0e-10,
                                   maxfun=100000)
    v = np.asarray(v)
    vs = scipy.optimize.fmin_powell(p1.obj,
                                    np.zeros(X.shape[1]),
                                    ftol=1.0e-10,
                                    xtol=1.0e-10,
                                    maxfun=100000)
    vs = np.asarray(vs)

    print np.round(1000 * beta1) / 1000
    print np.round(1000 * beta2) / 1000
    print np.round(1000 * vs) / 1000
    print np.round(1000 * v) / 1000
    print p1.obj(beta1), p1.obj(vs), f(beta1), f(v)

    print ts1, ts2
コード例 #2
0
ファイル: group_lasso.py プロジェクト: sowuy/regreg
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)
コード例 #3
0
ファイル: group_lasso.py プロジェクト: sowuy/regreg
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)
コード例 #4
0
ファイル: group_lasso.py プロジェクト: sowuy/regreg
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)
コード例 #5
0
ファイル: lasso_example.py プロジェクト: sowuy/regreg
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()
コード例 #6
0
ファイル: lasso_example.py プロジェクト: sowuy/regreg
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()
コード例 #7
0
ファイル: test_graphnet.py プロジェクト: sowuy/regreg
def test_graphnet(X=None, Y=None, l1=5., l2=0., l3=0., control=control):

    if X is None or Y is None:
        X = np.load('X.npy')
        Y = np.load('Y.npy')

    p = X.shape[1]
    _, L = gen_adj(p)
    Lsparse = scipy.sparse.lil_matrix(L)

    l1 *= X.shape[0]
    p1 = graphnet.gengrad((X, Y, L))
    p1.assign_penalty(l1=l1, l2=l2, l3=l3)
    t1 = time.time()
    opt1 = regreg.FISTA(p1)
    opt1.fit(tol=control['tol'], max_its=control['max_its'])
    beta1 = opt1.problem.coefs
    t2 = time.time()
    ts3 = t2 - t1

    p2 = graphnet.gengrad_sparse((X, Y, Lsparse))
    p2.assign_penalty(l1=l1, l2=l2, l3=l3)
    t1 = time.time()
    opt2 = regreg.FISTA(p2)
    opt2.fit(tol=control['tol'], max_its=control['max_its'])
    beta2 = opt2.problem.coefs
    t2 = time.time()
    ts3 = t2 - t1

    def f(beta):
        return np.linalg.norm(Y - np.dot(X, beta))**2 / (2) + np.fabs(
            beta).sum() * l1 + l2 * np.linalg.norm(beta)**2 + l3 * np.dot(
                beta, np.dot(L, beta))

    v = scipy.optimize.fmin_powell(f,
                                   np.zeros(X.shape[1]),
                                   ftol=1.0e-10,
                                   xtol=1.0e-10,
                                   maxfun=100000)
    v = np.asarray(v)

    print beta1
    print beta2
    print v
    print f(beta1), f(v), ts3
コード例 #8
0
def fused_lasso(n=100, l1=2., **control):

    D = (np.identity(n) - np.diag(np.ones(n - 1), -1))[1:]
    Dsp = scipy.sparse.lil_matrix(D)
    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 = sapprox.gengrad_sparse((Dsp, Y), L=M)
    p1.assign_penalty(l1=l1)

    p2 = sapprox.gengrad((D, Y), L=M)
    p2.assign_penalty(l1=l1)

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

    t1 = time.time()
    opt2 = regreg.FISTA(p2)
    opt2.fit(tol=control['tol'], max_its=control['max_its'])
    beta2 = opt2.problem.coefs
    t2 = time.time()
    ts2 = t2 - t1

    beta1, _ = opt1.output
    beta2, _ = opt2.output
    np.testing.assert_almost_equal(beta1, beta2)

    X = np.arange(n)
    if control['plot']:
        pylab.clf()
        pylab.step(X, beta1, linewidth=3, c='red')
        pylab.step(X, beta2, linewidth=3, c='green')
        pylab.scatter(X, Y)
        pylab.show()
コード例 #9
0
def test_fused_lasso(n=100, l1=15., ratio=0.1):


    Y = np.random.standard_normal(n)
    Y[int(0.1*n):int(0.3*n)] += 6.
    D1 = (np.identity(n) - np.diag(np.ones(n-1),-1))[1:]
    D2 = np.dot(D1[1:,1:], D1)
    D2 = np.vstack([D1, ratio*np.identity(n)])

    M = np.linalg.eigvalsh(np.dot(D2.T, D2)).max() 
    D1sp = scipy.sparse.csr_matrix(D1)
    Dsp = scipy.sparse.csr_matrix(D2)

    D3 = np.identity(n)
    D3sp =  scipy.sparse.csr_matrix(D3)



    semi = seminorm(l1norm(l1)) #+ seminorm(genl1norm(D1sp,l1))
    p2 = squaredloss((np.eye(n),Y),semi)
    opt2 = regreg.FISTA(p2)
    opt2.debug = True
    obj2 = opt2.fit(tol=1.0e-10, max_its=5000)
    beta2, _ = opt2.output
    print beta2


    p1 = sapprox.gengrad_sparse((D3sp, Y),L=M)
    p1.assign_penalty(l1=l1)


    opt1 = regreg.FISTA(p1)
    #opt1.debug = True
    obj1 = opt1.fit(tol=1.0e-10, max_its=5000, alpha=1.05)
    beta1, _ = opt1.output
    print beta1
    
    print np.allclose(beta1,beta2,rtol=1e-4)

    """
コード例 #10
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()
コード例 #11
0
ファイル: lasso_example.py プロジェクト: sowuy/regreg
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
コード例 #12
0
ファイル: lasso_example.py プロジェクト: sowuy/regreg
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
コード例 #13
0
ファイル: test_lin_graphnet.py プロジェクト: sowuy/regreg
def test_lin_graphnet(X=None,
                      Y=None,
                      l1=25.,
                      l2=0.,
                      l3=1.,
                      control=control,
                      nonneg=False):

    if X is None or Y is None:
        X = np.load('X.npy')
        Y = np.load('Y.npy')

    p = X.shape[1]
    _, L = gen_adj(p)
    Lsparse = scipy.sparse.lil_matrix(L)

    #np.random.shuffle(Y)
    Y = np.dot(Y, X)
    np.random.shuffle(Y)
    #Y = np.random.normal(0,1,X.shape[1])

    l1 *= X.shape[0]
    if nonneg:
        p1 = lin_graphnet.gengrad_nonneg((Y, L))
    else:
        p1 = lin_graphnet.gengrad((Y, L))

    p1.assign_penalty(l1=l1, l2=l2, l3=l3)
    t1 = time.time()
    opt1 = regreg.FISTA(p1)
    #opt1.debug = True
    opt1.fit(tol=control['tol'], max_its=control['max_its'])
    beta1 = opt1.problem.coefs
    t2 = time.time()
    ts3 = t2 - t1

    if nonneg:
        p2 = lin_graphnet.gengrad_nonneg_sparse((Y, Lsparse))
    else:
        p2 = lin_graphnet.gengrad_sparse((Y, Lsparse))
    p2.assign_penalty(l1=l1, l2=l2, l3=l3)
    t1 = time.time()
    opt2 = regreg.FISTA(p2)
    opt2.fit(tol=control['tol'], max_its=control['max_its'])
    beta2 = opt2.problem.coefs
    t2 = time.time()
    ts3 = t2 - t1

    def f(beta):
        if np.min(beta) < 0 and nonneg:
            return np.inf
        else:
            return -np.dot(Y, beta) + np.fabs(
                beta).sum() * l1 + l2 * np.linalg.norm(beta)**2 + l3 * np.dot(
                    beta, np.dot(L, beta))

    v = scipy.optimize.fmin_powell(f,
                                   np.zeros(len(Y)),
                                   ftol=1.0e-10,
                                   xtol=1.0e-10,
                                   maxfun=100000)
    v = np.asarray(v)

    N = 10000
    print np.round(N * beta1) / N
    print np.round(N * beta2) / N
    print np.round(N * v) / N
    print f(beta1), f(v)