예제 #1
0
    def gaussian(klass,
                 X, 
                 Y, 
                 feature_weights, 
                 split_frac=0.9,
                 sigma=1.,
                 stage_one=None):
        
        n, p = X.shape
        if stage_one is None:
            splitn = int(n*split_frac)
            indices = np.arange(n)
            np.random.shuffle(indices)
            stage_one = indices[:splitn]
            stage_two = indices[splitn:]
        else:
            stage_two = [i for i in np.arange(n) if i not in stage_one]
        Y1, X1 = Y[stage_one], X[stage_one]
        Y2, X2 = Y[stage_two], X[stage_two]

        loglike = glm.gaussian(X, Y, coef=1. / sigma**2)
        loglike1 = glm.gaussian(X1, Y1, coef=1. / sigma**2)
        loglike2 = glm.gaussian(X2, Y2, coef=1. / sigma**2)

        return klass(loglike1, loglike2, loglike, np.sqrt(split_frac) * feature_weights / sigma**2)
예제 #2
0
 def __init__(self, X, Y, epsilon, penalty, randomization, solve_args={'min_its':50, 'tol':1.e-10}):
     loss = glm.gaussian(X, Y)
     M_estimator.__init__(self,
                          loss, 
                          epsilon, 
                          penalty, 
                          randomization, solve_args=solve_args)
예제 #3
0
 def gaussian(X, Y, feature_weights, sigma, quadratic=None):
     loglike = glm.gaussian(X, Y, coef=1. / sigma**2, quadratic=quadratic)
     return lasso(loglike, feature_weights)
예제 #4
0
    def gaussian(X, 
                 Y, 
                 feature_weights, 
                 sigma=1., 
                 covariance_estimator=None,
                 quadratic=None):
        r"""
        Squared-error LASSO with feature weights.

        Objective function is 
        $$
        \beta \mapsto \frac{1}{2} \|Y-X\beta\|^2_2 + \sum_{i=1}^p \lambda_i |\beta_i|
        $$

        where $\lambda$ is `feature_weights`.

        Parameters
        ----------

        X : ndarray
            Shape (n,p) -- the design matrix.

        Y : ndarray
            Shape (n,) -- the response.

        feature_weights: [float, sequence]
            Penalty weights. An intercept, or other unpenalized 
            features are handled by setting those entries of 
            `feature_weights` to 0. If `feature_weights` is 
            a float, then all parameters are penalized equally.

        sigma : float (optional)
            Noise variance. Set to 1 if `covariance_estimator` is not None.
            This scales the loglikelihood by `sigma**(-2)`.

        covariance_estimator : callable (optional)
            If None, use the parameteric
            covariance estimate of the selected model.

        quadratic : `regreg.identity_quadratic.identity_quadratic` (optional)
            An optional quadratic term to be added to the objective.
            Can also be a linear term by setting quadratic 
            coefficient to 0.

        Returns
        -------

        L : `selection.algorithms.lasso.lasso`
        
        Notes
        -----

        If not None, `covariance_estimator` should 
        take arguments (beta, active, inactive)
        and return an estimate of some of the
        rows and columns of the covariance of
        $(\bar{\beta}_E, \nabla \ell(\bar{\beta}_E)_{-E})$,
        the unpenalized estimator and the inactive
        coordinates of the gradient of the likelihood at
        the unpenalized estimator.

        """
        if covariance_estimator is not None:
            sigma = 1.
        # TODO should we scale the quadratic here when sigma is not 1?
        loglike = glm.gaussian(X, Y, coef=1. / sigma**2, quadratic=quadratic)
        return lasso(loglike, np.asarray(feature_weights) / sigma**2,
                     covariance_estimator=covariance_estimator)