Beispiel #1
0
def test_nesta_lasso():

    n, p = 1000, 20
    X = np.random.standard_normal((n, p))
    beta = np.zeros(p)
    beta[:4] = 30
    Y = np.random.standard_normal(n) + np.dot(X, beta)

    loss = rr.squared_error(X,Y)
    penalty = rr.l1norm(p, lagrange=2.)

    # using nesta
    z = rr.zero(p)
    primal, dual = rr.nesta(loss, z, penalty, tol=1.e-10,
                            epsilon=2.**(-np.arange(30)),
                            initial_dual=np.zeros(p))

    # using simple problem

    problem = rr.simple_problem(loss, penalty)
    problem.solve()
    nt.assert_true(np.linalg.norm(primal - problem.coefs) / np.linalg.norm(problem.coefs) < 1.e-3)

    # test None as smooth_atom

    rr.nesta(None, z, penalty, tol=1.e-10,
             epsilon=2.**(-np.arange(30)),
             initial_dual=np.zeros(p))

    # using coefficients to stop

    rr.nesta(loss, z, penalty, tol=1.e-10,
             epsilon=2.**(-np.arange(30)),
             initial_dual=np.zeros(p),
             coef_stop=True)
Beispiel #2
0
def test_nesta_lasso():

    n, p = 1000, 20
    X = np.random.standard_normal((n, p))
    beta = np.zeros(p)
    beta[:4] = 30
    Y = np.random.standard_normal(n) + np.dot(X, beta)

    loss = rr.squared_error(X, Y)
    penalty = rr.l1norm(p, lagrange=4.)

    # using nesta
    z = rr.zero(p)
    primal, dual = rr.nesta(loss,
                            z,
                            penalty,
                            tol=1.e-10,
                            epsilon=2.**(-np.arange(30)))

    # using simple problem

    problem = rr.simple_problem(loss, penalty)
    problem.solve()
    nt.assert_true(
        np.linalg.norm(primal - problem.coefs) /
        np.linalg.norm(problem.coefs) < 1.e-3)
Beispiel #3
0
def test_quadratic():

    l = rr.quadratic(5, coef=3., offset=np.arange(5))
    l.quadratic = rr.identity_quadratic(1, np.ones(5), 2 * np.ones(5), 3.)
    c1 = l.get_conjugate(as_quadratic=True)

    q1 = rr.identity_quadratic(3, -np.arange(5), 0, 0)
    q2 = q1 + l.quadratic
    c2 = rr.zero(5, quadratic=q2.collapsed()).conjugate

    ww = np.random.standard_normal(5)
    np.testing.assert_almost_equal(c2.smooth_objective(ww, 'grad'),
                                   c1.smooth_objective(ww, 'grad'))

    np.testing.assert_almost_equal(c2.objective(ww), c1.objective(ww))

    np.testing.assert_almost_equal(
        c2.smooth_objective(ww, 'func') + c2.nonsmooth_objective(ww),
        c1.smooth_objective(ww, 'func') + c1.nonsmooth_objective(ww))
def test_quadratic():

    l = rr.quadratic(5, coef=3., offset=np.arange(5))
    l.quadratic = rr.identity_quadratic(1,np.ones(5), 2*np.ones(5), 3.)
    c1 = l.get_conjugate(as_quadratic=True)

    q1 = rr.identity_quadratic(3, -np.arange(5), 0, 0)
    q2 = q1 + l.quadratic
    c2 = rr.zero(5, quadratic=q2.collapsed()).conjugate

    ww = np.random.standard_normal(5)
    np.testing.assert_almost_equal(c2.smooth_objective(ww, 'grad'),
                                   c1.smooth_objective(ww, 'grad'))

    np.testing.assert_almost_equal(c2.objective(ww),
                                   c1.objective(ww))

    np.testing.assert_almost_equal(c2.smooth_objective(ww, 'func') + 
                                   c2.nonsmooth_objective(ww),
                                   c1.smooth_objective(ww, 'func') + 
                                   c1.nonsmooth_objective(ww))