Exemple #1
0
    def normalize(self,crossvalidate=False,train_set=False,test_set=False):
        #if not hasattr(self, 'X'):
        
        if not crossvalidate:
            self.design = np.array(self.design)
            self.X = rr.normalize(self.design,center=True,scale=True)
            self.Y_signs = np.array(self.Y)
            self.Y_binary = (self.Y_signs + 1) / 2.
            
        else:

            train_design = np.array([self.design[i] for i in train_set])
            test_design = np.array([self.design[i] for i in test_set])
            
            self.X = rr.normalize(train_design,center=True,scale=True)
            self.X_test = rr.normalize(test_design,center=True,scale=True)
            
            print len(train_set)
            #print train_set
            print len(self.Y)
            
            self.Y_train = [self.Y[i] for i in train_set]
            self.Y_test = [self.Y[i] for i in test_set]
            
            self.Y_signs = np.array(self.Y_train)
            self.Y_binary = (self.Y_signs + 1) / 2.
            
            self.Y_signs_test = np.array(self.Y_test)
            self.Y_binary_test = (self.Y_signs_test + 1) / 2
            
        self.p = self.X.primal_shape
Exemple #2
0
def test_scaling_and_centering():
    """
    This test verifies that the normalized transform
    of affine correctly implements the linear
    transform that multiplies first by X, then centers.
    """
    # N - number of data points
    # P - number of columns in design == number of betas
    N, P = 40, 30
    # an arbitrary positive offset for data and design
    offset = 50

    # design - with no colum of ones!
    X = np.random.normal(size=(N, P)) + offset

    L = rr.normalize(X, center=True, scale=True)  # the default
    # coef for loss

    scalings = np.std(X, 0)
    scaling_matrix = np.diag(1. / scalings)

    for _ in range(10):
        beta = np.random.normal(size=(P, ))
        v = L.linear_map(beta)
        v2 = np.dot(X, np.dot(scaling_matrix, beta))
        v2 -= v2.mean()
        np.testing.assert_almost_equal(v, v2)

        y = np.random.standard_normal(N)
        u1 = L.adjoint_map(y)
        y2 = y - y.mean()
        u2 = np.dot(scaling_matrix, np.dot(X.T, y2))
        np.testing.assert_almost_equal(u1, u2)
Exemple #3
0
def test_scaling():
    """
    This test verifies that the normalized transform
    of affine correctly implements the linear
    transform that multiplies first by X, then centers.
    """
    # N - number of data points
    # P - number of columns in design == number of betas
    N, P = 40, 30
    # an arbitrary positive offset for data and design
    offset = 50

    # design - with ones as last column
    X = np.ones((N, P))
    X[:, :-1] = np.random.normal(size=(N, P - 1)) + offset

    L = rr.normalize(X, center=False, scale=True)
    # coef for loss

    scalings = np.sqrt((X**2).sum(0) / N)
    scaling_matrix = np.diag(1. / scalings)

    for _ in range(10):

        beta = np.random.normal(size=(P, ))
        v = L.linear_map(beta)
        v2 = np.dot(X, np.dot(scaling_matrix, beta))
        v3 = L.affine_map(beta)
        np.testing.assert_almost_equal(v, v2)
        np.testing.assert_almost_equal(v, v3)

        y = np.random.standard_normal(N)
        u1 = L.adjoint_map(y)
        u2 = np.dot(scaling_matrix, np.dot(X.T, y))
        np.testing.assert_almost_equal(u1, u2)
Exemple #4
0
def test_centering():
    """
    This test verifies that the normalized transform
    of affine correctly implements the linear
    transform that multiplies first by X, then centers.
    """
    # N - number of data points
    # P - number of columns in design == number of betas
    N, P = 40, 30
    # an arbitrary positive offset for data and design
    offset = 50

    # design - with ones as last column
    X = np.ones((N, P))
    X[:, :-1] = np.random.normal(size=(N, P - 1)) + offset
    X2 = X - X.mean(axis=0)[np.newaxis, :]
    L = rr.normalize(X, center=True, scale=False)
    # coef for loss

    for _ in range(10):
        beta = np.random.normal(size=(P, ))
        v = L.linear_map(beta)
        v2 = np.dot(X, beta)
        v2 -= v2.mean()
        v3 = np.dot(X2, beta)
        v4 = L.affine_map(beta)
        np.testing.assert_almost_equal(v, v3)
        np.testing.assert_almost_equal(v, v2)
        np.testing.assert_almost_equal(v, v4)

        y = np.random.standard_normal(N)
        u1 = L.adjoint_map(y)
        y2 = y - y.mean()
        u2 = np.dot(X.T, y2)
        np.testing.assert_almost_equal(u1, u2)
def test_scaling_and_centering():
    """
    This test verifies that the normalized transform
    of affine correctly implements the linear
    transform that multiplies first by X, then centers.
    """
    # N - number of data points
    # P - number of columns in design == number of betas
    N, P = 40, 30
    # an arbitrary positive offset for data and design
    offset = 50

    # design - with no colum of ones!
    X = np.random.normal(size=(N,P)) + offset

    L = rr.normalize(X, center=True, scale=True) # the default
    # coef for loss

    scalings = np.std(X, 0)
    scaling_matrix = np.diag(1./scalings)

    for _ in range(10):
        beta = np.random.normal(size=(P,))
        v = L.linear_map(beta)
        v2 = np.dot(X, np.dot(scaling_matrix, beta))
        v2 -= v2.mean()
        np.testing.assert_almost_equal(v, v2)

        y = np.random.standard_normal(N)
        u1 = L.adjoint_map(y)
        y2 = y - y.mean()
        u2 = np.dot(scaling_matrix, np.dot(X.T, y2))
        np.testing.assert_almost_equal(u1, u2)
def test_centering():
    """
    This test verifies that the normalized transform
    of affine correctly implements the linear
    transform that multiplies first by X, then centers.
    """
    # N - number of data points
    # P - number of columns in design == number of betas
    N, P = 40, 30
    # an arbitrary positive offset for data and design
    offset = 50

    # design - with ones as last column
    X = np.ones((N,P))
    X[:,:-1] = np.random.normal(size=(N,P-1)) + offset
    X2 = X - X.mean(axis=0)[np.newaxis,:]
    L = rr.normalize(X, center=True, scale=False)
    # coef for loss

    for _ in range(10):
        beta = np.random.normal(size=(P,))
        v = L.linear_map(beta)
        v2 = np.dot(X, beta)
        v2 -= v2.mean()
        v3 = np.dot(X2, beta)
        v4 = L.affine_map(beta)
        np.testing.assert_almost_equal(v, v3)
        np.testing.assert_almost_equal(v, v2)
        np.testing.assert_almost_equal(v, v4)

        y = np.random.standard_normal(N)
        u1 = L.adjoint_map(y)
        y2 = y - y.mean()
        u2 = np.dot(X.T, y2)
        np.testing.assert_almost_equal(u1, u2)
def test_scaling():
    """
    This test verifies that the normalized transform
    of affine correctly implements the linear
    transform that multiplies first by X, then centers.
    """
    # N - number of data points
    # P - number of columns in design == number of betas
    N, P = 40, 30
    # an arbitrary positive offset for data and design
    offset = 50

    # design - with ones as last column
    X = np.ones((N,P))
    X[:,:-1] = np.random.normal(size=(N,P-1)) + offset

    L = rr.normalize(X, center=False, scale=True)
    # coef for loss

    scalings = np.sqrt((X**2).sum(0) / N)
    scaling_matrix = np.diag(1./scalings)
    
    for _ in range(10):

        beta = np.random.normal(size=(P,))
        v = L.linear_map(beta)
        v2 = np.dot(X, np.dot(scaling_matrix, beta))
        v3 = L.affine_map(beta)
        np.testing.assert_almost_equal(v, v2)
        np.testing.assert_almost_equal(v, v3)

        y = np.random.standard_normal(N)
        u1 = L.adjoint_map(y)
        u2 = np.dot(scaling_matrix, np.dot(X.T, y))
        np.testing.assert_almost_equal(u1, u2)
Exemple #8
0
def test_path_group_lasso():
    '''
    this test looks at the paths of three different parameterizations
    of the same problem

    '''
    n = 100
    X = np.random.standard_normal((n, 10))
    U = np.random.standard_normal((n, 2))
    Y = np.random.standard_normal(100)
    betaX = np.array([3, 4, 5, 0, 0] + [0] * 5)
    betaU = np.array([10, -5])
    Y += (np.dot(X, betaX) + np.dot(U, betaU)) * 5

    Xn = rr.normalize(np.hstack([np.ones((100, 1)), X]),
                      inplace=True,
                      center=True,
                      scale=True,
                      intercept_column=0).normalized_array()
    lasso = mixed_lasso.mixed_lasso_path.gaussian(Xn[:, 1:],
                                                  Y,
                                                  penalty_structure=[0] * 7 +
                                                  [1] * 3,
                                                  nstep=10)

    sol = lasso.main(inner_tol=1.e-12, verbose=True)
    beta = np.array(sol['beta'].todense())

    sols = []
    sols_sep = []
    for l in sol['lagrange']:
        loss = rr.glm.gaussian(Xn, Y)
        penalty = rr.mixed_lasso([mixed_lasso.UNPENALIZED] + [0] * 7 + [1] * 3,
                                 lagrange=l)  # matrix contains an intercept...
        problem = rr.simple_problem(loss, penalty)
        sols.append(problem.solve(tol=1.e-12).copy())

        sep = rr.separable((11, ), [
            rr.l2norm((7, ),
                      np.sqrt(7) * l),
            rr.l2norm((3, ),
                      np.sqrt(3) * l)
        ], [np.arange(1, 8), np.arange(8, 11)])
        sep_problem = rr.simple_problem(loss, sep)
        sols_sep.append(sep_problem.solve(tol=1.e-12).copy())

    sols = np.array(sols).T
    sols_sep = np.array(sols_sep).T

    nt.assert_true(
        np.linalg.norm(beta - sols) / (1 + np.linalg.norm(beta)) <= 1.e-4)
    nt.assert_true(
        np.linalg.norm(beta - sols_sep) / (1 + np.linalg.norm(beta)) <= 1.e-4)
Exemple #9
0
def test_path_group_lasso():
    """
    this test looks at the paths of three different parameterizations
    of the same problem

    """
    n = 100
    X = np.random.standard_normal((n, 10))
    U = np.random.standard_normal((n, 2))
    Y = np.random.standard_normal(100)
    betaX = np.array([3, 4, 5, 0, 0] + [0] * 5)
    betaU = np.array([10, -5])
    Y += (np.dot(X, betaX) + np.dot(U, betaU)) * 5

    Xn = rr.normalize(
        np.hstack([np.ones((100, 1)), X]), inplace=True, center=True, scale=True, intercept_column=0
    ).normalized_array()
    lasso = rr.lasso.squared_error(Xn[:, 1:], Y, penalty_structure=[0] * 7 + [1] * 3, nstep=10)

    sol = lasso.main(inner_tol=1.0e-12, verbose=True)
    beta = np.array(sol["beta"].todense())

    sols = []
    sols_sep = []
    for l in sol["lagrange"]:
        loss = rr.squared_error(Xn, Y, coef=1.0 / n)
        penalty = rr.mixed_lasso([rr.UNPENALIZED] + [0] * 7 + [1] * 3, lagrange=l)  # matrix contains an intercept...
        problem = rr.simple_problem(loss, penalty)
        sols.append(problem.solve(tol=1.0e-12).copy())

        sep = rr.separable(
            (11,),
            [rr.l2norm((7,), np.sqrt(7) * l), rr.l2norm((3,), np.sqrt(3) * l)],
            [np.arange(1, 8), np.arange(8, 11)],
        )
        sep_problem = rr.simple_problem(loss, sep)
        sols_sep.append(sep_problem.solve(tol=1.0e-12).copy())

    sols = np.array(sols).T
    sols_sep = np.array(sols_sep).T

    nt.assert_true(np.linalg.norm(beta - sols) / (1 + np.linalg.norm(beta)) <= 1.0e-4)
    nt.assert_true(np.linalg.norm(beta - sols_sep) / (1 + np.linalg.norm(beta)) <= 1.0e-4)
Exemple #10
0
def test_normalize_intercept():

    for issparse, value, inplace, intercept_column, scale, center in product(
        [False, True], [1, 3], [False, True], [None, 2], [True, False],
        [True, False]):

        print(issparse, value, inplace, intercept_column, scale, center)
        if not (issparse and inplace):

            X = np.random.standard_normal((20, 6))
            if intercept_column is not None:
                X[:, intercept_column] = 1
            Y = X.copy()

            if issparse:
                X = scipy.sparse.csr_matrix(X)

            Xn = rr.normalize(X,
                              value=value,
                              inplace=inplace,
                              intercept_column=intercept_column,
                              scale=scale,
                              center=center)

            if intercept_column is not None:
                v = np.zeros(Y.shape[1])
                v[intercept_column] = 4
                yield np.testing.assert_allclose, Xn.linear_map(
                    v), 4 * np.ones(Y.shape[0])

            if scale and center:

                Y -= Y.mean(0)[None, :]
                Y /= Y.std(0)[None, :]
                Y *= np.sqrt(value)
                if intercept_column is not None:
                    Y[:, intercept_column] = 1

            elif scale and not center:

                Y /= (np.sqrt((Y**2).sum(0))[None, :] / np.sqrt(Y.shape[0]))
                Y *= np.sqrt(value)
                if intercept_column is not None:
                    Y[:, intercept_column] = 1

            elif center and not scale:

                Y -= Y.mean(0)[None, :]
                if intercept_column is not None:
                    Y[:, intercept_column] = 1

            V = np.random.standard_normal((20, 3))
            U = np.random.standard_normal((6, 4))

            Xn.adjoint_map(V)
            yield np.testing.assert_allclose, np.dot(Y, U), Xn.linear_map(
                np.array(U))
            yield np.testing.assert_allclose, np.dot(Y, U), Xn.affine_map(
                np.array(U))
            yield np.testing.assert_allclose, np.dot(Y,
                                                     U[:, 0]), Xn.linear_map(
                                                         np.array(U[:, 0]))
            yield np.testing.assert_allclose, np.dot(Y,
                                                     U[:, 0]), Xn.affine_map(
                                                         np.array(U[:, 0]))
            yield np.testing.assert_allclose, np.dot(Y.T, V), Xn.adjoint_map(V)
            yield nt.assert_raises, ValueError, Xn.linear_map, np.zeros(
                (6, 4, 3))

            X2 = Xn.slice_columns(list(range(3)))
            Y2 = Y[:, :3]
            U2 = np.random.standard_normal((3, 4))
            V2 = np.random.standard_normal(20)

            yield np.testing.assert_allclose, np.dot(Y2, U2), X2.linear_map(
                np.array(U2))
            yield np.testing.assert_allclose, np.dot(Y2, U2), X2.affine_map(
                np.array(U2))
            yield np.testing.assert_allclose, np.dot(Y2,
                                                     U2[:, 0]), X2.linear_map(
                                                         np.array(U2[:, 0]))
            yield np.testing.assert_allclose, np.dot(Y2,
                                                     U2[:, 0]), X2.affine_map(
                                                         np.array(U2[:, 0]))
            yield np.testing.assert_allclose, np.dot(Y2.T,
                                                     V2), X2.adjoint_map(V2)

            X2 = Xn.slice_columns(list(range(3, 6)))
            Y2 = Y[:, 3:]
            U2 = np.random.standard_normal((3, 4))
            V2 = np.random.standard_normal(20)

            yield np.testing.assert_allclose, np.dot(Y2, U2), X2.linear_map(
                np.array(U2))
            yield np.testing.assert_allclose, np.dot(Y2, U2), X2.affine_map(
                np.array(U2))
            yield np.testing.assert_allclose, np.dot(Y2,
                                                     U2[:, 0]), X2.linear_map(
                                                         np.array(U2[:, 0]))
            yield np.testing.assert_allclose, np.dot(Y2,
                                                     U2[:, 0]), X2.affine_map(
                                                         np.array(U2[:, 0]))
            yield np.testing.assert_allclose, np.dot(Y2.T,
                                                     V2), X2.adjoint_map(V2)

            keep = np.zeros(6, np.bool)
            keep[:3] = 1
            X2 = Xn.slice_columns(keep)
            Y2 = Y[:, :3]
            U2 = np.random.standard_normal((3, 4))
            V2 = np.random.standard_normal(20)

            yield np.testing.assert_allclose, np.dot(Y2, U2), X2.linear_map(
                np.array(U2))
            yield np.testing.assert_allclose, np.dot(Y2, U2), X2.affine_map(
                np.array(U2))
            yield np.testing.assert_allclose, np.dot(Y2,
                                                     U2[:, 0]), X2.linear_map(
                                                         np.array(U2[:, 0]))
            yield np.testing.assert_allclose, np.dot(Y2,
                                                     U2[:, 0]), X2.affine_map(
                                                         np.array(U2[:, 0]))
            yield np.testing.assert_allclose, np.dot(Y2.T,
                                                     V2), X2.adjoint_map(V2)

    yield nt.assert_raises, ValueError, rr.normalize, scipy.sparse.csr_matrix(
        Y), True, True, 1, True
Exemple #11
0
def test_centering_fit_inplace(debug=False):

    # N - number of data points
    # P - number of columns in design == number of betas
    N, P = 40, 30
    # an arbitrary positive offset for data and design
    offset = 2

    # design - with ones as last column
    X = np.random.normal(size=(N, P)) + offset
    L = rr.normalize(X, center=True, scale=False, inplace=True)

    # X should have been normalized in place
    np.testing.assert_almost_equal(np.sum(X, 0), 0)

    # data
    Y = np.random.normal(size=(N, )) + offset

    # coef for loss
    coef = 0.5
    # lagrange for penalty
    lagrange = .1

    # Loss function (squared difference between fitted and actual data)
    loss = rr.quadratic_loss.affine(L, -Y, coef=coef)

    penalties = [
        rr.constrained_positive_part(25, lagrange=lagrange),
        rr.nonnegative(5)
    ]
    groups = [slice(0, 25), slice(25, 30)]
    penalty = rr.separable((P, ), penalties, groups)
    initial = np.random.standard_normal(P)

    composite_form = rr.separable_problem.fromatom(penalty, loss)

    solver = rr.FISTA(composite_form)
    solver.debug = debug
    solver.fit(tol=1.0e-12, min_its=200)
    coefs = solver.composite.coefs

    # Solve the problem with X, which has been normalized in place
    loss2 = rr.quadratic_loss.affine(X, -Y, coef=coef)

    initial2 = np.random.standard_normal(P)
    composite_form2 = rr.separable_problem.fromatom(penalty, loss2)

    solver2 = rr.FISTA(composite_form2)
    solver2.debug = debug
    solver2.fit(tol=1.0e-12, min_its=200)
    coefs2 = solver2.composite.coefs

    for _ in range(10):
        beta = np.random.standard_normal(P)
        g1 = loss.smooth_objective(beta, mode='grad')
        g2 = loss2.smooth_objective(beta, mode='grad')
        np.testing.assert_almost_equal(g1, g2)
        b1 = penalty.proximal(sq(1, beta, g1, 0))
        b2 = penalty.proximal(sq(1, beta, g2, 0))
        np.testing.assert_almost_equal(b1, b2)

        f1 = composite_form.objective(beta)
        f2 = composite_form2.objective(beta)
        np.testing.assert_almost_equal(f1, f2)

    np.testing.assert_almost_equal(composite_form.objective(coefs),
                                   composite_form.objective(coefs2))
    np.testing.assert_almost_equal(composite_form2.objective(coefs),
                                   composite_form2.objective(coefs2))

    nt.assert_true(
        np.linalg.norm(coefs - coefs2) /
        max(np.linalg.norm(coefs), 1) < 1.0e-04)
def test_centering_fit_inplace(debug=False):

    # N - number of data points
    # P - number of columns in design == number of betas
    N, P = 40, 30
    # an arbitrary positive offset for data and design
    offset = 2

    # design - with ones as last column
    X = np.random.normal(size=(N,P)) + offset
    L = rr.normalize(X, center=True, scale=False, inplace=True)

    # X should have been normalized in place
    np.testing.assert_almost_equal(np.sum(X, 0), 0)

    # data
    Y = np.random.normal(size=(N,)) + offset

    # coef for loss
    coef = 0.5
    # lagrange for penalty
    lagrange = .1

    # Loss function (squared difference between fitted and actual data)
    loss = rr.quadratic.affine(L, -Y, coef=coef)

    penalties = [rr.constrained_positive_part(25, lagrange=lagrange),
                 rr.nonnegative(5)]
    groups = [slice(0,25), slice(25,30)]
    penalty = rr.separable((P,), penalties,
                           groups)
    initial = np.random.standard_normal(P)

    composite_form = rr.separable_problem.fromatom(penalty, loss)

    solver = rr.FISTA(composite_form)
    solver.debug = debug
    solver.fit(tol=1.0e-12, min_its=200)
    coefs = solver.composite.coefs

    # Solve the problem with X, which has been normalized in place
    loss2 = rr.quadratic.affine(X, -Y, coef=coef)

    initial2 = np.random.standard_normal(P)
    composite_form2 = rr.separable_problem.fromatom(penalty, loss2)

    solver2 = rr.FISTA(composite_form2)
    solver2.debug = debug
    solver2.fit(tol=1.0e-12, min_its=200)
    coefs2 = solver2.composite.coefs

    for _ in range(10):
        beta = np.random.standard_normal(P)
        g1 = loss.smooth_objective(beta, mode='grad')
        g2 = loss2.smooth_objective(beta, mode='grad')
        np.testing.assert_almost_equal(g1, g2)
        b1 = penalty.proximal(sq(1, beta, g1,0))
        b2 = penalty.proximal(sq(1, beta, g2,0))
        np.testing.assert_almost_equal(b1, b2)

        f1 = composite_form.objective(beta)
        f2 = composite_form2.objective(beta)
        np.testing.assert_almost_equal(f1, f2)


    np.testing.assert_almost_equal(composite_form.objective(coefs), composite_form.objective(coefs2))
    np.testing.assert_almost_equal(composite_form2.objective(coefs), composite_form2.objective(coefs2))

    nt.assert_true(np.linalg.norm(coefs - coefs2) / max(np.linalg.norm(coefs),1) < 1.0e-04)
Exemple #13
0
def test_normalize_intercept():

    for issparse, value, inplace, intercept_column, scale, center in product([False, True], 
                                                       [1,3], 
                                                       [False, True], 
                                                       [None, 2],
                                                       [True, False],
                                                       [True, False]):
        
        print (issparse, value, inplace, intercept_column, scale, center)
        if not (issparse and inplace):

            X = np.random.standard_normal((20,6))
            if intercept_column is not None:
                X[:,intercept_column] = 1
            Y = X.copy()

            if issparse:
                X = scipy.sparse.csr_matrix(X)

            Xn = rr.normalize(X, 
                              value=value, 
                              inplace=inplace, 
                              intercept_column=intercept_column,
                              scale=scale, 
                              center=center)

            if intercept_column is not None:
                v = np.zeros(Y.shape[1])
                v[intercept_column] = 4
                yield np.testing.assert_allclose, Xn.linear_map(v), 4 * np.ones(Y.shape[0])

            if scale and center:

                Y -= Y.mean(0)[None,:]
                Y /= Y.std(0)[None,:]
                Y *= np.sqrt(value)
                if intercept_column is not None:
                    Y[:,intercept_column] = 1
            
            elif scale and not center:

                Y /= (np.sqrt((Y**2).sum(0))[None,:] / np.sqrt(Y.shape[0]))
                Y *= np.sqrt(value)
                if intercept_column is not None:
                    Y[:,intercept_column] = 1

            elif center and not scale:

                Y -= Y.mean(0)[None,:]
                if intercept_column is not None:
                    Y[:,intercept_column] = 1

            V = np.random.standard_normal((20, 3))
            U = np.random.standard_normal((6,4))

            Xn.adjoint_map(V)
            yield np.testing.assert_allclose, np.dot(Y, U), Xn.linear_map(np.array(U))
            yield np.testing.assert_allclose, np.dot(Y, U), Xn.affine_map(np.array(U))
            yield np.testing.assert_allclose, np.dot(Y, U[:,0]), Xn.linear_map(np.array(U[:,0]))
            yield np.testing.assert_allclose, np.dot(Y, U[:,0]), Xn.affine_map(np.array(U[:,0]))
            yield np.testing.assert_allclose, np.dot(Y.T, V), Xn.adjoint_map(V)
            yield nt.assert_raises, ValueError, Xn.linear_map, np.zeros((6,4,3))

            X2 = Xn.slice_columns(list(range(3)))
            Y2 = Y[:,:3]
            U2 = np.random.standard_normal((3,4))
            V2 = np.random.standard_normal(20)

            yield np.testing.assert_allclose, np.dot(Y2, U2), X2.linear_map(np.array(U2))
            yield np.testing.assert_allclose, np.dot(Y2, U2), X2.affine_map(np.array(U2))
            yield np.testing.assert_allclose, np.dot(Y2, U2[:,0]), X2.linear_map(np.array(U2[:,0]))
            yield np.testing.assert_allclose, np.dot(Y2, U2[:,0]), X2.affine_map(np.array(U2[:,0]))
            yield np.testing.assert_allclose, np.dot(Y2.T, V2), X2.adjoint_map(V2)

            X2 = Xn.slice_columns(list(range(3,6)))
            Y2 = Y[:,3:]
            U2 = np.random.standard_normal((3,4))
            V2 = np.random.standard_normal(20)

            yield np.testing.assert_allclose, np.dot(Y2, U2), X2.linear_map(np.array(U2))
            yield np.testing.assert_allclose, np.dot(Y2, U2), X2.affine_map(np.array(U2))
            yield np.testing.assert_allclose, np.dot(Y2, U2[:,0]), X2.linear_map(np.array(U2[:,0]))
            yield np.testing.assert_allclose, np.dot(Y2, U2[:,0]), X2.affine_map(np.array(U2[:,0]))
            yield np.testing.assert_allclose, np.dot(Y2.T, V2), X2.adjoint_map(V2)

            keep = np.zeros(6, np.bool)
            keep[:3] = 1
            X2 = Xn.slice_columns(keep)
            Y2 = Y[:,:3]
            U2 = np.random.standard_normal((3,4))
            V2 = np.random.standard_normal(20)

            yield np.testing.assert_allclose, np.dot(Y2, U2), X2.linear_map(np.array(U2))
            yield np.testing.assert_allclose, np.dot(Y2, U2), X2.affine_map(np.array(U2))
            yield np.testing.assert_allclose, np.dot(Y2, U2[:,0]), X2.linear_map(np.array(U2[:,0]))
            yield np.testing.assert_allclose, np.dot(Y2, U2[:,0]), X2.affine_map(np.array(U2[:,0]))
            yield np.testing.assert_allclose, np.dot(Y2.T, V2), X2.adjoint_map(V2)

    yield nt.assert_raises, ValueError, rr.normalize, scipy.sparse.csr_matrix(Y), True, True, 1, True
Exemple #14
0
def test_normalize_intercept():

    for value in [1,3]:

        # test of intercept column = 2
        X = np.random.standard_normal((10,3))
        X[:,2] = 1
        Y = X.copy()
        Xn = rr.normalize(X, intercept_column=2, value=value)

        Y[:,1] -= Y[:,1].mean()
        Y[:,0] -= Y[:,0].mean()
        Y[:,1] /= np.std(Y[:,1])
        Y[:,0] /= np.std(Y[:,0])
        Y *= np.sqrt(value)
        np.testing.assert_allclose(np.dot(Y, [2,4,6]), Xn.linear_map(np.array([2,4,6])))

        # test of intercept column = 2, no scaling
        X = np.random.standard_normal((10,3))
        X[:,2] = 1
        Y = X.copy()
        Xn = rr.normalize(X, intercept_column=2, scale=False)

        Y[:,1] -= Y[:,1].mean()
        Y[:,0] -= Y[:,0].mean()
        np.testing.assert_allclose(np.dot(Y, [2,4,6]), Xn.linear_map(np.array([2,4,6])))

        # test of intercept column = 2, no centering
        X = np.random.standard_normal((10,3))
        X[:,2] = 1
        Y = X.copy()
        Xn = rr.normalize(X, intercept_column=2, center=False, value=value)

        Y[:,1] /= (np.linalg.norm(Y[:,1]) / np.sqrt(Y.shape[0]))
        Y[:,0] /= (np.linalg.norm(Y[:,0]) / np.sqrt(Y.shape[0]))
        Y *= np.sqrt(value)
        np.testing.assert_allclose(np.dot(Y, [2,4,6]), Xn.linear_map(np.array([2,4,6])))

        # test of no intercept column, no scaling
        X = np.random.standard_normal((10,3))
        Y = X.copy()
        Xn = rr.normalize(X, intercept_column=None, scale=False)

        Y[:,2] -= Y[:,2].mean()
        Y[:,1] -= Y[:,1].mean()
        Y[:,0] -= Y[:,0].mean()
        np.testing.assert_allclose(np.dot(Y, [2,4,6]), Xn.linear_map(np.array([2,4,6])))

        # test of no intercept column, no centering
        X = np.random.standard_normal((10,3))
        Y = X.copy()
        Xn = rr.normalize(X, intercept_column=None, center=False, value=value)

        Y[:,2] /= (np.linalg.norm(Y[:,2]) / np.sqrt(Y.shape[0]))
        Y[:,1] /= (np.linalg.norm(Y[:,1]) / np.sqrt(Y.shape[0]))
        Y[:,0] /= (np.linalg.norm(Y[:,0]) / np.sqrt(Y.shape[0]))
        Y *= np.sqrt(value)
        np.testing.assert_allclose(np.dot(Y, [2,4,6]), Xn.linear_map(np.array([2,4,6])))

        # test of no intercept column, no centering
        X = np.random.standard_normal((10,3))
        Y = X.copy()
        Xn = rr.normalize(X, intercept_column=None, value=value)

        Y[:,2] -= Y[:,2].mean()
        Y[:,1] -= Y[:,1].mean()
        Y[:,0] -= Y[:,0].mean()

        Y[:,2] /= (np.linalg.norm(Y[:,2]) / np.sqrt(Y.shape[0]))
        Y[:,1] /= (np.linalg.norm(Y[:,1]) / np.sqrt(Y.shape[0]))
        Y[:,0] /= (np.linalg.norm(Y[:,0]) / np.sqrt(Y.shape[0]))
        Y *= np.sqrt(value)
        np.testing.assert_allclose(np.dot(Y, [2,4,6]), Xn.linear_map(np.array([2,4,6])))
Exemple #15
0
def test_scaling_and_centering_intercept_fit(debug=False):

    # N - number of data points
    # P - number of columns in design == number of betas
    N, P = 40, 30
    # an arbitrary positive offset for data and design
    offset = 2

    # design - with ones as last column
    X = np.random.normal(size=(N, P)) + 0 * offset
    X2 = X - X.mean(0)[None, :]
    X2 = X2 / np.std(X2, 0, ddof=1)[None, :]
    X2 = np.hstack([np.ones((X2.shape[0], 1)), X2])

    L = rr.normalize(X, center=True, scale=True, intercept=True)
    # data
    Y = np.random.normal(size=(N, )) + offset

    # lagrange for penalty
    lagrange = .1

    # Loss function (squared difference between fitted and actual data)
    loss = rr.squared_error(L, Y)

    penalties = [
        rr.constrained_positive_part(25, lagrange=lagrange),
        rr.nonnegative(5)
    ]
    groups = [slice(0, 25), slice(25, 30)]
    penalty = rr.separable((P + 1, ), penalties, groups)

    initial = np.random.standard_normal(P + 1)
    composite_form = rr.separable_problem.fromatom(penalty, loss)
    solver = rr.FISTA(composite_form)
    solver.debug = debug
    solver.fit(tol=1.0e-12, min_its=200)
    coefs = solver.composite.coefs

    # Solve the problem with X2
    loss2 = rr.squared_error(X2, Y)

    initial2 = np.random.standard_normal(P + 1)
    composite_form2 = rr.separable_problem.fromatom(penalty, loss2)

    solver2 = rr.FISTA(composite_form2)
    solver2.debug = debug
    solver2.fit(tol=1.0e-12, min_its=200)
    coefs2 = solver2.composite.coefs

    for _ in range(10):
        beta = np.random.standard_normal(P + 1)
        g1 = loss.smooth_objective(beta, mode='grad')
        g2 = loss2.smooth_objective(beta, mode='grad')
        np.testing.assert_almost_equal(g1, g2)
        b1 = penalty.proximal(sq(1, beta, g1, 0))
        b2 = penalty.proximal(sq(1, beta, g2, 0))
        np.testing.assert_almost_equal(b1, b2)

        f1 = composite_form.objective(beta)
        f2 = composite_form2.objective(beta)
        np.testing.assert_almost_equal(f1, f2)

    np.testing.assert_almost_equal(composite_form.objective(coefs),
                                   composite_form.objective(coefs2))
    np.testing.assert_almost_equal(composite_form2.objective(coefs),
                                   composite_form2.objective(coefs2))

    nt.assert_true(
        np.linalg.norm(coefs - coefs2) /
        max(np.linalg.norm(coefs), 1) < 1.0e-04)
Exemple #16
0
def test_normalize_intercept():

    for value in [1, 3]:

        # test of intercept column = 2
        X = np.random.standard_normal((10, 3))
        X[:, 2] = 1
        Y = X.copy()
        Xn = rr.normalize(X, intercept_column=2, value=value)

        Y[:, 1] -= Y[:, 1].mean()
        Y[:, 0] -= Y[:, 0].mean()
        Y[:, 1] /= np.std(Y[:, 1])
        Y[:, 0] /= np.std(Y[:, 0])
        Y *= np.sqrt(value)
        np.testing.assert_allclose(np.dot(Y, [2, 4, 6]),
                                   Xn.linear_map(np.array([2, 4, 6])))

        # test of intercept column = 2, no scaling
        X = np.random.standard_normal((10, 3))
        X[:, 2] = 1
        Y = X.copy()
        Xn = rr.normalize(X, intercept_column=2, scale=False)

        Y[:, 1] -= Y[:, 1].mean()
        Y[:, 0] -= Y[:, 0].mean()
        np.testing.assert_allclose(np.dot(Y, [2, 4, 6]),
                                   Xn.linear_map(np.array([2, 4, 6])))

        # test of intercept column = 2, no centering
        X = np.random.standard_normal((10, 3))
        X[:, 2] = 1
        Y = X.copy()
        Xn = rr.normalize(X, intercept_column=2, center=False, value=value)

        Y[:, 1] /= (np.linalg.norm(Y[:, 1]) / np.sqrt(Y.shape[0]))
        Y[:, 0] /= (np.linalg.norm(Y[:, 0]) / np.sqrt(Y.shape[0]))
        Y *= np.sqrt(value)
        np.testing.assert_allclose(np.dot(Y, [2, 4, 6]),
                                   Xn.linear_map(np.array([2, 4, 6])))

        # test of no intercept column, no scaling
        X = np.random.standard_normal((10, 3))
        Y = X.copy()
        Xn = rr.normalize(X, intercept_column=None, scale=False)

        Y[:, 2] -= Y[:, 2].mean()
        Y[:, 1] -= Y[:, 1].mean()
        Y[:, 0] -= Y[:, 0].mean()
        np.testing.assert_allclose(np.dot(Y, [2, 4, 6]),
                                   Xn.linear_map(np.array([2, 4, 6])))

        # test of no intercept column, no centering
        X = np.random.standard_normal((10, 3))
        Y = X.copy()
        Xn = rr.normalize(X, intercept_column=None, center=False, value=value)

        Y[:, 2] /= (np.linalg.norm(Y[:, 2]) / np.sqrt(Y.shape[0]))
        Y[:, 1] /= (np.linalg.norm(Y[:, 1]) / np.sqrt(Y.shape[0]))
        Y[:, 0] /= (np.linalg.norm(Y[:, 0]) / np.sqrt(Y.shape[0]))
        Y *= np.sqrt(value)
        np.testing.assert_allclose(np.dot(Y, [2, 4, 6]),
                                   Xn.linear_map(np.array([2, 4, 6])))

        # test of no intercept column, no centering
        X = np.random.standard_normal((10, 3))
        Y = X.copy()
        Xn = rr.normalize(X, intercept_column=None, value=value)

        Y[:, 2] -= Y[:, 2].mean()
        Y[:, 1] -= Y[:, 1].mean()
        Y[:, 0] -= Y[:, 0].mean()

        Y[:, 2] /= (np.linalg.norm(Y[:, 2]) / np.sqrt(Y.shape[0]))
        Y[:, 1] /= (np.linalg.norm(Y[:, 1]) / np.sqrt(Y.shape[0]))
        Y[:, 0] /= (np.linalg.norm(Y[:, 0]) / np.sqrt(Y.shape[0]))
        Y *= np.sqrt(value)
        np.testing.assert_allclose(np.dot(Y, [2, 4, 6]),
                                   Xn.linear_map(np.array([2, 4, 6])))