Esempio n. 1
0
def test_dot():

    A = np.random.standard_normal((20, 12))
    B = np.random.standard_normal((10, 20))

    C = rr.astransform(B).dot(rr.astransform(A))

    assert_equal(C.shape, (10, 12))
    Z = np.random.standard_normal(12)
    assert_array_almost_equal(C.dot(Z), B.dot(A.dot(Z)))

    assert_equal(C.ndim, 2)
Esempio n. 2
0
def test_dot():

    A = np.random.standard_normal((20, 12))
    B = np.random.standard_normal((10, 20))

    C = rr.astransform(B).dot(rr.astransform(A))

    assert_equal(C.shape, (10, 12))
    Z = np.random.standard_normal(12)
    assert_array_almost_equal(C.dot(Z), B.dot(A.dot(Z)))

    assert_equal(C.ndim, 2)
Esempio n. 3
0
def choose_lambda(X, quantile=0.95, ndraw=10000):
    """
    Choose a value of `lam` for the square-root LASSO
    based on an empirical quantile of the distribution of

    $$
    \frac{\|X^T\epsilon\|_{\infty}}{\|\epsilon\|_2}.
    $$

    Parameters
    ----------

    X : np.float((n, p))
        Design matrix.

    quantile : float
        What quantile should we use?

    ndraw : int
        How many draws?

    """

    X = rr.astransform(X)
    n, p = X.output_shape[0], X.input_shape[0]
    E = np.random.standard_normal((n, ndraw))
    E /= np.sqrt(np.sum(E**2, 0))[None,:]
    return np.percentile(np.fabs(X.adjoint_map(E)).max(0), 100*quantile)
Esempio n. 4
0
def test_misc():
    X = np.random.standard_normal((40, 5))
    power_L(X)
    Xa = rr.astransform(X)
    np.testing.assert_allclose(todense(Xa), X)

    reshapeA = adjoint(reshape((30, ), (6, 5)))
    assert_raises(NotImplementedError, todense, reshapeA)
Esempio n. 5
0
def test_misc():
    X = np.random.standard_normal((40, 5))
    power_L(X)
    Xa = rr.astransform(X)
    np.testing.assert_allclose(todense(Xa), X)

    reshapeA = adjoint(reshape((30,), (6,5)))
    assert_raises(NotImplementedError, todense, reshapeA)
Esempio n. 6
0
def choose_lambda_with_randomization(X, randomization, quantile=0.90, ndraw=10000):
    X = rr.astransform(X)
    n, p = X.output_shape[0], X.input_shape[0]
    E = np.random.standard_normal((n, ndraw))
    E /= np.sqrt(np.sum(E**2, 0))[None,:]
    dist1 = np.fabs(X.adjoint_map(E)).max(0)
    dist2 = np.fabs(randomization.sample((ndraw,))).max(0)
    return np.percentile(dist1+dist2, 100*quantile)
Esempio n. 7
0
def solve_sqrt_lasso_skinny(X,
                            Y,
                            weights=None,
                            initial=None,
                            quadratic=None,
                            solve_args={}):
    """

    Solve the square-root LASSO optimization problem:

    $$
    \text{minimize}_{\beta} \|y-X\beta\|_2 + D |\beta|,
    $$
    where $D$ is the diagonal matrix with weights on its diagonal.

    Parameters
    ----------

    y : np.float((n,))
        The target, in the model $y = X\beta$

    X : np.float((n, p))
        The data, in the model $y = X\beta$

    weights : np.float
        Coefficients of the L-1 penalty in
        optimization problem, note that different
        coordinates can have different coefficients.

    initial : np.float(p)
        Initial point for optimization.

    solve_args : dict
        Arguments passed to regreg solver.

    quadratic : `regreg.identity_quadratic`
        A quadratic term added to objective function.

    """
    X = rr.astransform(X)
    n, p = X.output_shape[0], X.input_shape[0]
    if weights is None:
        lam = choose_lambda(X)
        weights = lam * np.ones((p, ))
    weight_dict = dict(zip(np.arange(p), 2 * weights))
    penalty = rr.mixed_lasso(range(p) + [rr.NONNEGATIVE],
                             lagrange=1.,
                             weights=weight_dict)

    loss = sqlasso_objective_skinny(X, Y)
    problem = rr.simple_problem(loss, penalty)
    problem.coefs[-1] = np.linalg.norm(Y)
    if initial is not None:
        problem.coefs[:-1] = initial
    soln = problem.solve(quadratic, **solve_args)
    _loss = sqlasso_objective(X, Y)
    return soln[:-1], _loss
Esempio n. 8
0
    def fit(self, solve_args={'min_its': 200}):

        Y, M, p, weights = self.Y, self.M, self.Y.shape[
            0], self.feature_weights

        Y0 = Y - Y.mean()
        coef = solve_sqrt_lasso(M.T,
                                Y0,
                                weights=weights,
                                solve_args=solve_args)[0]

        self.active = active = coef != 0
        self.inactive = inactive = ~active

        if active.sum():
            jumps = self.merge_intervals(M.slices[active])
            M_inactive = copy(M)
            M_inactive.update_slices(M.slices[inactive])
            X = M.form_matrix(M.slices[active])[0]
            R = np.identity(p) - X.dot(np.linalg.pinv(X))
            L_inactive = M_inactive.dot(rr.astransform(R))
            L = lasso.sqrt_lasso(X, Y0, weights[active])
            L.fit(solve_args={'min_its': 200}, lasso_solution=coef[active])
            C = L.constraints.linear_part.dot(X.T)
            L_inactive_neg = ra.scalar_multiply(L_inactive, -1.)

            irrep = L_inactive.dot(
                X.dot(-np.diag(L.active_signs).dot(L.constraints.offset)))
            full_lin = ra.vstack([L_inactive, L_inactive_neg, C])
            full_offset = np.hstack([
                irrep + L._multiplier * weights[inactive],
                -irrep + L._multiplier * weights[inactive],
                L.constraints.offset
            ])

            full_con = constraints(full_lin,
                                   full_offset,
                                   covariance=L._sigma_hat**2 * np.identity(p))

            print full_con(Y - Y.mean())
            fit = X.dot(coef[active]) + Y.mean()

            segments = np.array([jumps[:-1], jumps[1:]]).T
            Xr = M.form_matrix(segments)[0]
            relaxed_fit = Xr.dot(np.linalg.pinv(Xr).dot(Y)) + Y.mean()
            #             S = L.summary('onesided')
            #             pS = pd.DataFrame(S)
            #             pS['interval'] = [M.slices[i] for i in np.nonzero(active)[0]]
            #             pS = pS.reindex(columns=['interval', 'pval', 'lasso', 'onestep'])
            pS = None
        else:
            fit = relaxed_fit = Y.mean() * np.ones(self.p)
            pS = None
            segments = np.array([[0, self.p]])
        return fit, relaxed_fit, pS, segments
Esempio n. 9
0
    def __init__(self, X, Y):

        self.X = rr.astransform(X)
        n, p = self.X.output_shape[0], self.X.input_shape[0]

        self.Y = Y
        if n > p:
            self._quadratic_term = np.dot(X.T, X)
            self._linear_term = -2 * np.dot(X.T, Y)
            self._constant_term = (Y**2).sum()
        self._sqerror = rr.squared_error(X, Y)
Esempio n. 10
0
    def __init__(self, X, Y, quadratic=None, initial=None, offset=None):

        rr.smooth_atom.__init__(self,
                                rr.astransform(X).input_shape,
                                coef=1.,
                                offset=offset,
                                quadratic=quadratic,
                                initial=initial)

        self.X = X
        self.Y = Y
        self.data = (X, Y)
        self._sqerror = rr.squared_error(X, Y)
Esempio n. 11
0
    def __init__(self, X, Y, 
                 quadratic=None, 
                 initial=None,
                 offset=None):

        X = rr.astransform(X)
        rr.smooth_atom.__init__(self,
                                X.input_shape,
                                coef=1.,
                                offset=offset,
                                quadratic=quadratic,
                                initial=initial)

        self.X = X
        self.Y = Y
        self._sqerror = rr.squared_error(X, Y)
Esempio n. 12
0
def solve_sqrt_lasso_fat(X, Y, weights=None, initial=None, quadratic=None, solve_args={}):
    """

    Solve the square-root LASSO optimization problem:

    $$
    \text{minimize}_{\beta} \|y-X\beta\|_2 + D |\beta|,
    $$
    where $D$ is the diagonal matrix with weights on its diagonal.

    Parameters
    ----------

    y : np.float((n,))
        The target, in the model $y = X\beta$

    X : np.float((n, p))
        The data, in the model $y = X\beta$

    weights : np.float
        Coefficients of the L-1 penalty in
        optimization problem, note that different
        coordinates can have different coefficients.

    initial : np.float(p)
        Initial point for optimization.

    solve_args : dict
        Arguments passed to regreg solver.

    quadratic : `regreg.identity_quadratic`
        A quadratic term added to objective function.

    """
    X = rr.astransform(X)
    n, p = X.output_shape[0], X.input_shape[0]
    if weights is None:
        lam = choose_lambda(X)
        weights = lam * np.ones((p,))

    loss = sqlasso_objective(X, Y)
    penalty = rr.weighted_l1norm(weights, lagrange=1.)
    problem = rr.simple_problem(loss, penalty)
    if initial is not None:
        problem.coefs[:] = initial
    soln = problem.solve(quadratic, **solve_args)
    return soln, loss
Esempio n. 13
0
def test_regreg_transform():

    A, b = np.random.standard_normal((4, 30)), np.random.standard_normal(4)
    A = rr.astransform(A)
    con = AC.constraints(A, b, covariance=np.identity(30))

    while True:
        Z = np.random.standard_normal(30)  # conditional distribution
        if con.value(Z) < 0:
            break

    C = np.random.standard_normal((2, 30))
    conditional = con.conditional(C, C.dot(Z))
    W = np.random.standard_normal(30)

    print(conditional.pivot(W, Z))
    print(con.pivot(W, Z))
Esempio n. 14
0
def solve_sqrt_lasso(X, Y, weights=None, initial=None, **solve_kwargs):
    """

    Solve the square-root LASSO optimization problem:

    $$
    \text{minimize}_{\beta} \|y-X\beta\|_2 + D |\beta|,
    $$
    where $D$ is the diagonal matrix with weights on its diagonal.

    Parameters
    ----------

    y : np.float((n,))
        The target, in the model $y = X\beta$

    X : np.float((n, p))
        The data, in the model $y = X\beta$

    weights : np.float
        Coefficients of the L-1 penalty in
        optimization problem, note that different
        coordinates can have different coefficients.

    initial : np.float(p)
        Initial point for optimization.

    solve_kwargs : dict
        Arguments passed to regreg solver.

    """
    X = rr.astransform(X)
    n, p = X.output_shape[0], X.input_shape[0]
    if weights is None:
        lam = choose_lambda(X)
        weights = lam * np.ones((p, ))
    loss = sqlasso_objective(X, Y)
    penalty = rr.weighted_l1norm(weights, lagrange=1.)
    problem = rr.simple_problem(loss, penalty)
    if initial is not None:
        problem.coefs[:] = initial
    soln = problem.solve(**solve_kwargs)
    return soln
Esempio n. 15
0
def solve_sqrt_lasso(X, Y, weights=None, initial=None, **solve_kwargs):
    """

    Solve the square-root LASSO optimization problem:

    $$
    \text{minimize}_{\beta} \|y-X\beta\|_2 + D |\beta|,
    $$
    where $D$ is the diagonal matrix with weights on its diagonal.

    Parameters
    ----------

    y : np.float((n,))
        The target, in the model $y = X\beta$

    X : np.float((n, p))
        The data, in the model $y = X\beta$

    weights : np.float
        Coefficients of the L-1 penalty in
        optimization problem, note that different
        coordinates can have different coefficients.

    initial : np.float(p)
        Initial point for optimization.

    solve_kwargs : dict
        Arguments passed to regreg solver.

    """
    X = rr.astransform(X)
    n, p = X.output_shape[0], X.input_shape[0]
    if n < p:
        return solve_sqrt_lasso_alt(X, Y, weights=weights, initial=initial, **solve_kwargs)
    else:
        return solve_sqrt_lasso_fat(X, Y, weights=weights, initial=initial, **solve_kwargs)