Regularization paths

Contents

Regularization paths#

regreg.paths#

Generic machinery for computing regularization paths.

This module defines the grouped_path base class, which specific path classes (LASSO, group LASSO, etc.) subclass, as well as the path algorithms strong_rules and warm_start that operate on such objects.

regreg.paths.subsample_columns(X, columns)#

Extract columns of a (possibly implicit) design matrix.

Parameters:
  • X (ndarray or regreg.affine.affine_transform) – Design matrix with one-dimensional input shape.

  • columns (sequence of int) – Columns to extract.

Returns:

cols (ndarray) – Array of shape X.output_shape + (len(columns),) holding the selected columns.

class regreg.paths.grouped_path(saturated_loss, X, lasso_weights, elastic_net_param=None, alpha=1.0)#

Bases: object

Base class for regularization paths over groups of variables.

Subclasses implement the methods used by strong_rules and warm_start (e.g. check_KKT, strong_set, solve_subproblem).

full_loss()#

Saturated loss composed with the design matrix self.X.

subsample_columns(X, columns)#

Extract columns of X into ndarray or regreg transform.

Parameters:
  • X (object) – Representation of design matrix.

  • columns (sequence of int) – Columns to extract.

Returns:

cols (ndarray or regreg.affine.affine_transform) – The selected columns.

full_gradient(saturated_loss, linear_predictor)#

Gradient of saturated loss composed with self.X.

Parameters:
  • saturated_loss (regreg.smooth.smooth_atom) – Loss as a function of the linear predictor.

  • linear_predictor (ndarray) – Linear predictor at which to evaluate the gradient.

Returns:

gradient (ndarray) – Gradient with respect to the coefficients.

linpred(coef, X, test)#

Form linear predictor for given set of coefficients.

Typical use case will have coef of shape (l,p) for univariate response regressions and (l,q,p) for multiple response regressions (e.g. multinomial with q classes) where l is the number of Lagrange parameters.

Parameters:
  • coef (ndarray) – A set of coefficients.

  • X (object) – Representation of design matrix, usually self.X – subclasses may not assume this is actually an array.

  • test (index) – Indices of X.dot(coef.T) to return.

Returns:

linpred (ndarray) – Matrix of linear predictors.

subsample(case_idx)#

Create a new path, by subsampling cases of self.saturated_loss.

Case weights are computed with self.saturated_loss.subsample.

Parameters:

case_idx (index) – An index-like object used to specify which cases to include in the subsample.

Returns:

subsample_path (path object) – A path object with a modified smooth part reflecting the subsampling.

check_KKT(grad_solution, solution, lagrange, penalty=None, kkt_tol=None)#

Check KKT conditions over the groups in the path.

Determines which groups are failing the KKT conditions (these could be active groups or inactive groups).

Parameters:
  • grad_solution (ndarray) – Candidate for gradient of smooth loss at Lagrange value lagrange.

  • solution (ndarray) – Candidate for solution to problem at Lagrange value lagrange.

  • lagrange (float) – Lagrange value for penalty.

  • penalty (object, optional) – A penalty. If None, defaults to self.penalty.

  • kkt_tol (float, optional) – Relative tolerance for the groups at zero: such a group fails if its dual norm exceeds lagrange * (1 + kkt_tol). If None, defaults to self.kkt_tol.

Returns:

  • failing_A (ndarray) – Boolean indicator of active groups failing the KKT conditions.

  • failing_I_ranks (ndarray) – For inactive groups, the rank of each failing group (by the size of its violation), with -1 for groups that do not fail.

regreg.paths.default_lagrange_sequence(penalty, null_solution, lagrange_proportion=0.05, nstep=100, alpha=1)#

Default decreasing, log-spaced sequence of Lagrange parameters.

Parameters:
  • penalty (regreg.atoms.atom) – Penalty for the path.

  • null_solution (ndarray) – Gradient of the loss at the null solution. The largest Lagrange parameter is the dual seminorm of this value.

  • lagrange_proportion (float, optional) – Ratio of the smallest to the largest Lagrange parameter.

  • nstep (int, optional) – Number of Lagrange parameters.

  • alpha (float, optional) – Elastic net mixing parameter; the sequence is divided by alpha.

Returns:

lagrange_seq (ndarray) – Decreasing sequence of Lagrange parameters.

regreg.paths.strong_rules(path_obj, lagrange_seq, initial_data, inner_tol=1e-05, verbose=False, initial_step=None, check_active=False, max_tries=20, kkt_tol=None)#

Compute a regularization path using strong rules screening.

Parameters:
  • path_obj (grouped_path) – Path object describing the problem.

  • lagrange_seq (sequence of float) – Decreasing sequence of Lagrange parameters.

  • initial_data (tuple) – Pair (solution, grad_solution) at lagrange_seq[0].

  • inner_tol (float, optional) – Tolerance for each subproblem.

  • verbose (bool, optional) – Print progress information?

  • initial_step (float, optional) – Initial step size for the solver. If None, it is estimated from the Lipschitz constant of path_obj.X.

  • check_active (bool, optional) – Also check KKT conditions for active groups?

  • max_tries (int, optional) – Maximum number of attempts at each Lagrange parameter.

  • kkt_tol (float, optional) – Relative tolerance of the KKT check for groups at zero. If None, path_obj.kkt_tol is used.

Returns:

output (dict) – Dictionary with keys 'dev explained', 'lagrange' and 'beta' (the solutions along the path).

regreg.paths.warm_start(path_obj, lagrange_seq, initial_data, inner_tol=1e-05, verbose=False, initial_step=None, solve_args=None)#

Compute a regularization path using warm starts.

Each problem along the path is solved with all variables, starting from the solution at the previous Lagrange parameter.

Parameters:
  • path_obj (grouped_path) – Path object describing the problem.

  • lagrange_seq (sequence of float) – Sequence of Lagrange parameters.

  • initial_data (tuple) – Pair (solution, grad_solution); only solution is used, as the initial warm start.

  • inner_tol (float, optional) – Tolerance for each problem.

  • verbose (bool, optional) – Print progress information?

  • initial_step (float, optional) – Initial step size for the solver. If None, it is estimated from the Lipschitz constant of path_obj.X.

  • solve_args (dict, optional) – Extra arguments passed to simple_problem.solve.

Returns:

output (dict) – Dictionary with keys 'devratio', 'lagrange' and 'beta' (the solutions along the path).

regreg.paths.lasso#

Regularization paths for the (weighted) LASSO and elastic net.

class regreg.paths.lasso.lasso_path(saturated_loss, X, lasso_weights, elastic_net_param=None, alpha=1.0)#

Bases: grouped_path

Regularization path for the weighted LASSO / elastic net.

The problem at Lagrange parameter \(\lambda\) is

\[\ell(X\beta) + \lambda \alpha \sum_j w_j |\beta_j| + \frac{1 - \alpha}{2} \sum_j \lambda_j c_j \beta_j^2\]

where \(\ell\) is saturated_loss, \(w\) is lasso_weights, \(c\) is elastic_net_param, and \(\lambda_j=\lambda\) for penalized variables, \(\lambda_j=1\) for unpenalized ones.

Parameters:
  • saturated_loss (regreg.smooth.glm.glm) – Loss as a function of the linear predictor.

  • X (ndarray or regreg.affine.affine_transform) – Design matrix.

  • lasso_weights (ndarray) – Weights for the L1 penalty; variables with weight 0 are unpenalized.

  • elastic_net_param (ndarray, optional) – Ridge weights for each variable. Defaults to all ones.

  • alpha (float, optional) – Elastic net mixing parameter; 1 is the LASSO.

enet_loss(lagrange, candidate_set=None)#

Ridge (elastic net) part of the objective.

Parameters:
  • lagrange (float) – Lagrange parameter.

  • candidate_set (index, optional) – Restrict the ridge term to these variables.

Returns:

loss (regreg.smooth.quadratic.quadratic_loss) – Diagonal quadratic loss.

subsample(case_idx)#

Create a new path, by subsampling cases of self.saturated_loss.

Case weights are computed with self.saturated_loss.subsample.

Parameters:

case_idx (index) – An index-like object used to specify which cases to include in the subsample.

Returns:

subsample_path (path object) – A path object with a modified smooth part reflecting the subsampling.

check_KKT(grad_solution, solution, lagrange, penalty=None, kkt_tol=None)#

Check KKT conditions over the groups in the path.

Determines which groups are failing the KKT conditions (these could be active groups or inactive groups).

Parameters:
  • grad_solution (ndarray) – Candidate for gradient of smooth loss at Lagrange value lagrange.

  • solution (ndarray) – Candidate for solution to problem at Lagrange value lagrange.

  • lagrange (float) – Lagrange value for penalty.

  • penalty (object, optional) – A weighted L1 penalty. If None, defaults to self.penalty.

  • kkt_tol (float, optional) – Relative tolerance for the groups at zero: such a group fails if its dual norm exceeds lagrange * (1 + kkt_tol). If None, defaults to self.kkt_tol.

Returns:

  • failing_active (ndarray) – Boolean indicator of active (or unpenalized) groups failing the KKT conditions.

  • inactive_ranks (ndarray) – For inactive groups failing the KKT conditions, the rank of the size of the violation; -1 for all other groups.

strong_set(lagrange_cur, lagrange_new, grad_solution)#

Compute the strong set at lagrange_new.

Parameters:
  • lagrange_cur (float) – Current Lagrange parameter.

  • lagrange_new (float) – Next Lagrange parameter.

  • grad_solution (ndarray) – Gradient at the solution for lagrange_cur.

Returns:

  • strong (ndarray) – Indices of variables in the strong set.

  • strong_bool (ndarray) – Boolean indicator of the strong set.

  • strong_vars (ndarray) – Indices of variables in the strong set (same as strong).

active_set(solution)#

Boolean indicator of nonzero coefficients in solution.

restricted_penalty(subset)#

Weighted L1 penalty restricted to subset (all variables if None).

solve_subproblem(solution, candidate_set, lagrange_new, **solve_args)#

Solve the problem restricted to a candidate set of variables.

Parameters:
  • solution (ndarray) – Current solution, used as a warm start.

  • candidate_set (index) – Variables to include in the subproblem.

  • lagrange_new (float) – Lagrange parameter.

  • solve_args (dict) – Keyword arguments passed to simple_problem.solve.

Returns:

  • final_step (float) – Final step size of the solver.

  • sub_grad (ndarray) – Gradient of the restricted smooth loss at sub_soln.

  • sub_soln (ndarray) – Solution of the subproblem.

  • sub_linear_pred (ndarray) – Linear predictor at sub_soln.

  • candidate_bool (ndarray) – Boolean indicator of the candidate set.

enet_grad(solution, penalized, lagrange_new, subset=None)#

Gradient of the ridge (elastic net) part of the objective.

Parameters:
  • solution (ndarray) – Coefficients.

  • penalized (ndarray) – Boolean indicator of penalized variables; for these the ridge term is multiplied by lagrange_new.

  • lagrange_new (float) – Lagrange parameter.

  • subset (index, optional) – Restrict to these variables.

Returns:

G (ndarray) – Gradient of the ridge term.

updated_ever_active(index_obj, group_ids=False)#

Indices of variables that are ever active, with index_obj added.

Parameters:
  • index_obj (index) – Variables to add to the ever-active set.

  • group_ids (bool, optional) – Ignored.

Returns:

ever_active (list) – Sorted indices of ever-active variables.

property unpenalized#

Unpenalized variables.

Returns:

  • groups (ndarray) – Indices of variables with weights equal to 0.

  • variables (ndarray) – Boolean indicator that is True if no penalty on that variable.

regreg.paths.lasso.logistic(X, Y, *args, **keyword_args)#

Construct a lasso_path for the logistic regression loss with binary response Y.

Remaining arguments are passed to lasso_path.

regreg.paths.lasso.probit(X, Y, *args, **keyword_args)#

Construct a lasso_path for the probit regression loss with binary response Y.

Remaining arguments are passed to lasso_path.

regreg.paths.lasso.cloglog(X, Y, *args, **keyword_args)#

Construct a lasso_path for the complementary log-log regression loss with binary response Y.

Remaining arguments are passed to lasso_path.

regreg.paths.lasso.gaussian(X, Y, *args, **keyword_args)#

Construct a lasso_path for the Gaussian (squared error) loss with response Y.

Remaining arguments are passed to lasso_path.

regreg.paths.lasso.cox(X, T, S, *args, **keyword_args)#

Construct a lasso_path for the Cox partial likelihood with event times T and status S.

Remaining arguments are passed to lasso_path.

regreg.paths.lasso.poisson(X, Y, *args, **keyword_args)#

Construct a lasso_path for the Poisson regression loss with count response Y.

Remaining arguments are passed to lasso_path.

regreg.paths.lasso.huber(X, Y, smoothing_parameter, *args, **keyword_args)#

Construct a lasso_path for the Huber loss with response Y and smoothing parameter smoothing_parameter.

Remaining arguments are passed to lasso_path.

regreg.paths.lasso.huber_svm(X, Y, smoothing_parameter, *args, **keyword_args)#

Construct a lasso_path for the smoothed (Huberized) hinge loss with binary response Y and smoothing parameter smoothing_parameter.

Remaining arguments are passed to lasso_path.

regreg.paths.group_lasso#

Regularization paths for the group LASSO and group elastic net.

class regreg.paths.group_lasso.group_lasso_path(saturated_loss, X, groups, weights={}, elastic_net_param=None, alpha=1.0)#

Bases: grouped_path

Regularization path for the group LASSO / group elastic net.

The problem at Lagrange parameter \(\lambda\) is

\[\ell(X\beta) + \lambda \alpha \sum_g w_g \|\beta_g\|_2 + \frac{1 - \alpha}{2} \sum_j \lambda_j c_j \beta_j^2\]

where \(\ell\) is saturated_loss, \(w\) are the group weights, \(c\) is elastic_net_param, and \(\lambda_j=\lambda\) for penalized variables, \(\lambda_j=1\) for unpenalized ones.

Parameters:
  • saturated_loss (regreg.smooth.glm.glm) – Loss as a function of the linear predictor.

  • X (ndarray or regreg.affine.affine_transform) – Design matrix.

  • groups (ndarray) – Group label for each variable.

  • weights (dict, optional) – Weight for each group; see regreg.atoms.group_lasso.group_lasso. Groups with weight 0 are unpenalized.

  • elastic_net_param (ndarray, optional) – Ridge weights for each variable. Defaults to all ones.

  • alpha (float, optional) – Elastic net mixing parameter; 1 is the group LASSO.

enet_loss(lagrange, candidate_groups=None)#

Ridge (elastic net) part of the objective.

Parameters:
  • lagrange (float) – Lagrange parameter.

  • candidate_groups (sequence, optional) – Restrict the ridge term to variables in these groups.

Returns:

loss (regreg.smooth.quadratic.quadratic_loss) – Diagonal quadratic loss.

subsample(case_idx)#

Create a new path, by subsampling cases of self.saturated_loss.

Case weights are computed with self.saturated_loss.subsample.

Parameters:

case_idx (index) – An index-like object used to specify which cases to include in the subsample.

Returns:

subsample_path (path object) – A path object with a modified smooth part reflecting the subsampling.

check_KKT(grad_solution, solution, lagrange, penalty=None, kkt_tol=None)#

Check KKT conditions over the groups in the path.

Determines which groups are failing the KKT conditions (these could be active groups or inactive groups).

Parameters:
  • grad_solution (ndarray) – Candidate for gradient of smooth loss at Lagrange value lagrange.

  • solution (ndarray) – Candidate for solution to problem at Lagrange value lagrange.

  • lagrange (float) – Lagrange value for penalty.

  • penalty (object, optional) – A group LASSO penalty. If None, defaults to self.penalty.

  • kkt_tol (float, optional) – Relative tolerance for the groups at zero: such a group fails if its dual norm exceeds lagrange * (1 + kkt_tol). If None, defaults to self.kkt_tol.

Returns:

  • failing_active (ndarray) – Boolean indicator of active (or unpenalized) groups failing the KKT conditions.

  • inactive_ranks (ndarray) – For inactive groups failing the KKT conditions, the rank of the size of the violation; -1 for all other groups.

strong_set(lagrange_cur, lagrange_new, grad_solution)#

Compute the strong set of groups at lagrange_new.

Parameters:
  • lagrange_cur (float) – Current Lagrange parameter.

  • lagrange_new (float) – Next Lagrange parameter.

  • grad_solution (ndarray) – Gradient at the solution for lagrange_cur.

Returns:

  • strong (ndarray) – Group ids in the strong set.

  • strong_bool (ndarray) – Boolean indicator (over groups) of the strong set.

  • strong_vars (ndarray) – Indices of variables in groups in the strong set.

solve_subproblem(solution, candidate_groups, lagrange_new, **solve_args)#

Solve the problem restricted to a candidate set of groups.

Parameters:
  • solution (ndarray) – Current solution, used as a warm start.

  • candidate_groups (sequence) – Groups to include in the subproblem.

  • lagrange_new (float) – Lagrange parameter.

  • solve_args (dict) – Keyword arguments passed to simple_problem.solve.

Returns:

  • final_step (float) – Final step size of the solver.

  • sub_grad (ndarray) – Gradient of the restricted smooth loss at sub_soln.

  • sub_soln (ndarray) – Solution of the subproblem.

  • sub_linear_pred (ndarray) – Linear predictor at sub_soln.

  • candidate_bool (ndarray) – Boolean indicator of variables in the candidate groups.

enet_grad(solution, penalized, lagrange_new, subset=None)#

Gradient of the ridge (elastic net) part of the objective.

Parameters:
  • solution (ndarray) – Coefficients.

  • penalized (ndarray) – Boolean indicator of penalized variables; for these the ridge term is multiplied by lagrange_new.

  • lagrange_new (float) – Lagrange parameter.

  • subset (index, optional) – Restrict to these variables.

Returns:

G (ndarray) – Gradient of the ridge term.

updated_ever_active(index_obj, group_ids=True)#

Groups that are ever active, with index_obj added.

Parameters:
  • index_obj (index) – Index into the (sorted) groups to add to the ever-active set.

  • group_ids (bool, optional) – If True, return group ids, otherwise return indices into the sorted groups.

Returns:

ever_active (list) – Ever-active groups.

property unpenalized#

Unpenalized groups.

Returns:

  • groups (list) – Group ids with weights equal to 0.

  • group_idx (ndarray) – Boolean indicator (over sorted groups) that is True if no penalty on that group.

active_set(solution)#

Boolean indicator of groups with nonzero coefficients in solution.

restricted_penalty(var_subset)#

Group LASSO penalty restricted to var_subset (all variables if None).

regreg.paths.group_lasso.logistic(X, Y, *args, **keyword_args)#

Construct a group_lasso_path for the logistic regression loss with binary response Y.

Remaining arguments are passed to group_lasso_path.

regreg.paths.group_lasso.probit(X, Y, *args, **keyword_args)#

Construct a group_lasso_path for the probit regression loss with binary response Y.

Remaining arguments are passed to group_lasso_path.

regreg.paths.group_lasso.cloglog(X, Y, *args, **keyword_args)#

Construct a group_lasso_path for the complementary log-log regression loss with binary response Y.

Remaining arguments are passed to group_lasso_path.

regreg.paths.group_lasso.gaussian(X, Y, *args, **keyword_args)#

Construct a group_lasso_path for the Gaussian (squared error) loss with response Y.

Remaining arguments are passed to group_lasso_path.

regreg.paths.group_lasso.cox(X, T, S, *args, **keyword_args)#

Construct a group_lasso_path for the Cox partial likelihood with event times T and status S.

Remaining arguments are passed to group_lasso_path.

regreg.paths.group_lasso.poisson(X, Y, *args, **keyword_args)#

Construct a group_lasso_path for the Poisson regression loss with count response Y.

Remaining arguments are passed to group_lasso_path.

regreg.paths.group_lasso.huber(X, Y, smoothing_parameter, *args, **keyword_args)#

Construct a group_lasso_path for the Huber loss with response Y and smoothing parameter smoothing_parameter.

Remaining arguments are passed to group_lasso_path.

regreg.paths.group_lasso.huber_svm(X, Y, smoothing_parameter, *args, **keyword_args)#

Construct a group_lasso_path for the smoothed (Huberized) hinge loss with binary response Y and smoothing parameter smoothing_parameter.

Remaining arguments are passed to group_lasso_path.

regreg.paths.sparse_group_lasso#

Regularization paths for the sparse group LASSO.

class regreg.paths.sparse_group_lasso.sparse_group_lasso_path(saturated_loss, X, groups, lasso_weights, weights={}, elastic_net_param=None, alpha=1.0, l1_alpha=0.95)#

Bases: group_lasso_path

Regularization path for the sparse group LASSO.

The penalty at Lagrange parameter \(\lambda\) is

\[\lambda \alpha \sum_g \left(\alpha_1 \sum_{j \in g} v_j |\beta_j| + (1 - \alpha_1) w_g \|\beta_g\|_2 \right)\]

with \(v\) the lasso_weights, \(w\) the group weights and \(\alpha_1\) given by l1_alpha, plus an elastic net term as in group_lasso_path.

Parameters:
  • saturated_loss (regreg.smooth.glm.glm) – Loss as a function of the linear predictor.

  • X (ndarray or regreg.affine.affine_transform) – Design matrix.

  • groups (ndarray) – Group label for each variable.

  • lasso_weights (ndarray) – Weights for the L1 part of the penalty.

  • weights (dict, optional) – Weight for each group in the L2 part of the penalty.

  • elastic_net_param (ndarray, optional) – Ridge weights for each variable. Defaults to all ones.

  • alpha (float, optional) – Elastic net mixing parameter; 1 is no ridge term.

  • l1_alpha (float, optional) – Mixing between the L1 and L2 parts of the penalty.

property unpenalized#

Unpenalized groups.

Returns:

  • groups (list) – Ids of unpenalized groups.

  • group_idx (ndarray) – Boolean indicator (over sorted groups) that is True if no penalty on that group.

subsample(case_idx)#

Create a new path, by subsampling cases of self.saturated_loss.

Case weights are computed with self.saturated_loss.subsample.

Parameters:

case_idx (index) – An index-like object used to specify which cases to include in the subsample.

Returns:

subsample_path (path object) – A path object with a modified smooth part reflecting the subsampling.

check_KKT(grad_solution, solution, lagrange, penalty=None, kkt_tol=None)#

Check KKT conditions over the groups in the path.

Determines which groups are failing the KKT conditions (these could be active groups or inactive groups).

Parameters:
  • grad_solution (ndarray) – Candidate for gradient of smooth loss at Lagrange value lagrange.

  • solution (ndarray) – Candidate for solution to problem at Lagrange value lagrange.

  • lagrange (float) – Lagrange value for penalty.

  • penalty (object, optional) – A sparse group LASSO penalty. If None, defaults to self.penalty.

  • kkt_tol (float, optional) – Relative tolerance for the groups at zero: such a group fails if its dual norm exceeds lagrange * (1 + kkt_tol). If None, defaults to self.kkt_tol.

Returns:

  • failing_active (ndarray) – Boolean indicator of active (or unpenalized) groups failing the KKT conditions.

  • inactive_ranks (ndarray) – For inactive groups failing the KKT conditions, the rank of the size of the violation; -1 for all other groups.

strong_set(lagrange_cur, lagrange_new, grad_solution)#

Compute the strong set of groups at lagrange_new.

Parameters:
  • lagrange_cur (float) – Current Lagrange parameter.

  • lagrange_new (float) – Next Lagrange parameter.

  • grad_solution (ndarray) – Gradient at the solution for lagrange_cur.

Returns:

  • strong (ndarray) – Group ids in the strong set.

  • strong_bool (ndarray) – Boolean indicator (over groups) of the strong set.

  • strong_vars (ndarray) – Indices of variables in groups in the strong set.

solve_subproblem(solution, candidate_groups, lagrange_new, **solve_args)#

Solve the problem restricted to a candidate set of groups.

Parameters:
  • solution (ndarray) – Current solution, used as a warm start.

  • candidate_groups (sequence) – Groups to include in the subproblem.

  • lagrange_new (float) – Lagrange parameter.

  • solve_args (dict) – Keyword arguments passed to simple_problem.solve.

Returns:

  • final_step (float) – Final step size of the solver.

  • sub_grad (ndarray) – Gradient of the restricted smooth loss at sub_soln.

  • sub_soln (ndarray) – Solution of the subproblem.

  • sub_linear_pred (ndarray) – Linear predictor at sub_soln.

  • candidate_bool (ndarray) – Boolean indicator of variables in the candidate groups.

restricted_penalty(subset)#

Sparse group LASSO penalty restricted to subset (all variables if None).

regreg.paths.sparse_group_lasso.logistic(X, Y, *args, **keyword_args)#

Construct a sparse_group_lasso_path for the logistic regression loss with binary response Y.

Remaining arguments are passed to sparse_group_lasso_path.

regreg.paths.sparse_group_lasso.probit(X, Y, *args, **keyword_args)#

Construct a sparse_group_lasso_path for the probit regression loss with binary response Y.

Remaining arguments are passed to sparse_group_lasso_path.

regreg.paths.sparse_group_lasso.cloglog(X, Y, *args, **keyword_args)#

Construct a sparse_group_lasso_path for the complementary log-log regression loss with binary response Y.

Remaining arguments are passed to sparse_group_lasso_path.

regreg.paths.sparse_group_lasso.gaussian(X, Y, *args, **keyword_args)#

Construct a sparse_group_lasso_path for the Gaussian (squared error) loss with response Y.

Remaining arguments are passed to sparse_group_lasso_path.

regreg.paths.sparse_group_lasso.cox(X, T, S, *args, **keyword_args)#

Construct a sparse_group_lasso_path for the Cox partial likelihood with event times T and status S.

Remaining arguments are passed to sparse_group_lasso_path.

regreg.paths.sparse_group_lasso.poisson(X, Y, *args, **keyword_args)#

Construct a sparse_group_lasso_path for the Poisson regression loss with count response Y.

Remaining arguments are passed to sparse_group_lasso_path.

regreg.paths.sparse_group_lasso.huber(X, Y, smoothing_parameter, *args, **keyword_args)#

Construct a sparse_group_lasso_path for the Huber loss with response Y and smoothing parameter smoothing_parameter.

Remaining arguments are passed to sparse_group_lasso_path.

regreg.paths.sparse_group_lasso.huber_svm(X, Y, smoothing_parameter, *args, **keyword_args)#

Construct a sparse_group_lasso_path for the smoothed (Huberized) hinge loss with binary response Y and smoothing parameter smoothing_parameter.

Remaining arguments are passed to sparse_group_lasso_path.

regreg.paths.sparse_group_block#

Regularization paths for sparse group block penalties on multiresponse problems.

class regreg.paths.sparse_group_block.sparse_group_common_path(saturated_loss, X, l1_penalty, l2_penalty, elastic_net_param=None, alpha=1.0, l1_alpha=None)#

Bases: group_lasso_path

Sparse group block path for responses sharing a common design.

Responses here share a common X, e.g. multinomial regression. The coefficients form a matrix with one row per feature and one column per response; each row is a group penalized by regreg.atoms.sparse_group_block.sparse_group_block.

Parameters:
  • saturated_loss (regreg.smooth.smooth_atom) – Multiresponse loss of shape (n,q), as a function of the linear predictor.

  • X (ndarray) – Design matrix of shape (n,p).

  • l1_penalty (float) – Weight of the L1 part of the penalty.

  • l2_penalty (float) – Weight of the L2 (row norm) part of the penalty.

  • elastic_net_param (ndarray, optional) – Ridge weights for each coefficient. Defaults to all ones.

  • alpha (float, optional) – Elastic net mixing parameter; 1 is no ridge term.

  • l1_alpha (float, optional) – The \(\alpha\) of the sparse group LASSO paper. If not None, the L1 and L2 weights are multiplied by l1_alpha and 1 - l1_alpha respectively.

enet_loss(lagrange, candidate_groups=None)#

Ridge (elastic net) part of the objective.

Parameters:
  • lagrange (float) – Lagrange parameter.

  • candidate_groups (index, optional) – Restrict the ridge term to these rows (features).

Returns:

loss (regreg.smooth.quadratic.quadratic_loss) – Diagonal quadratic loss.

subsample_columns(X, columns)#

Extract columns of the common design into a regreg transform.

Argument X is ignored here; columns are taken from self._X.

Parameters:
  • X (object) – Ignored.

  • columns (sequence of int) – Columns (features) to extract.

Returns:

Xsub (regreg.affine.affine_transform) – Transform with input shape (len(columns), q).

subsample(case_idx)#

Create a new path, by subsampling cases of self.saturated_loss.

Case weights are computed with self.saturated_loss.subsample.

Parameters:

case_idx (index) – An index-like object used to specify which cases to include in the subsample.

Returns:

subsample_path (path object) – A path object with a modified smooth part reflecting the subsampling.

check_KKT(grad_solution, solution, lagrange, penalty=None, kkt_tol=None)#

Check KKT conditions over the groups in the path.

Determines which groups are failing the KKT conditions (these could be active groups or inactive groups).

Parameters:
  • grad_solution (ndarray) – Candidate for gradient of smooth loss at Lagrange value lagrange.

  • solution (ndarray) – Candidate for solution to problem at Lagrange value lagrange.

  • lagrange (float) – Lagrange value for penalty.

  • penalty (object, optional) – A sparse group block penalty. Currently ignored: the weights of self.penalty are always used.

  • kkt_tol (float, optional) – Relative tolerance for the groups at zero: such a group fails if its dual norm exceeds lagrange * (1 + kkt_tol). If None, defaults to self.kkt_tol.

Returns:

  • failing_active (ndarray) – Boolean indicator of active (or unpenalized) groups failing the KKT conditions.

  • inactive_ranks (ndarray) – For inactive groups failing the KKT conditions, the rank of the size of the violation; -1 for all other groups.

strong_set(lagrange_cur, lagrange_new, grad_solution)#

Compute the strong set of groups (rows) at lagrange_new.

Parameters:
  • lagrange_cur (float) – Current Lagrange parameter.

  • lagrange_new (float) – Next Lagrange parameter.

  • grad_solution (ndarray) – Gradient at the solution for lagrange_cur.

Returns:

  • strong (ndarray) – Indices of rows in the strong set.

  • strong_bool (ndarray) – Boolean indicator of the strong set.

  • strong_vars (ndarray) – Indices of rows in the strong set (same as strong).

solve_subproblem(solution, candidate_groups, lagrange_new, **solve_args)#

Solve the problem restricted to a candidate set of rows (features).

Parameters:
  • solution (ndarray) – Current solution, used as a warm start.

  • candidate_groups (index) – Rows to include in the subproblem.

  • lagrange_new (float) – Lagrange parameter.

  • solve_args (dict) – Keyword arguments passed to simple_problem.solve.

Returns:

  • final_step (float) – Final step size of the solver.

  • sub_grad (ndarray) – Gradient of the restricted smooth loss at sub_soln.

  • sub_soln (ndarray) – Solution of the subproblem.

  • sub_linear_pred (ndarray) – Linear predictor at sub_soln.

  • candidate_bool (ndarray) – Boolean indicator of the candidate rows.

updated_ever_active(index_obj, group_ids=False)#

Indices of rows that are ever active, with index_obj added.

Parameters:
  • index_obj (index) – Rows to add to the ever-active set.

  • group_ids (bool, optional) – Ignored.

Returns:

ever_active (list) – Sorted indices of ever-active rows.

property unpenalized#

Unpenalized groups and variables.

There are no unpenalized groups for this penalty.

Returns:

  • groups (list) – Empty list.

  • variables (list) – Empty list.

restricted_penalty(subset)#

Sparse group block penalty restricted to rows subset (all rows if None).

regreg.paths.sparse_group_block.multinomial(X, Y, *args, **keyword_args)#

Construct a sparse_group_common_path for the multinomial loss with response Y.

Remaining arguments are passed to sparse_group_common_path.

regreg.paths.sparse_group_block.multiresponse_gaussian(X, Y, *args, **keyword_args)#

Construct a sparse_group_common_path for a Gaussian loss on each column of Y.

Remaining arguments are passed to sparse_group_common_path.

class regreg.paths.sparse_group_block.sparse_group_block_path(saturated_loss, Xs, l1_penalty, l2_penalty, elastic_net_param=None, alpha=1.0, l1_alpha=None)#

Bases: sparse_group_common_path

Sparse group block path for responses with different designs.

Responses here have different Xs but a common set of columns. Design is transpose of “seemingly unrelated regressions”.

Parameters:
  • saturated_loss (regreg.smooth.smooth_atom) – Multiresponse loss of shape (n,q), as a function of the linear predictor.

  • Xs (sequence of ndarray) – One design matrix for each response, all with the same columns.

  • l1_penalty (float) – Weight of the L1 part of the penalty.

  • l2_penalty (float) – Weight of the L2 (row norm) part of the penalty.

  • elastic_net_param (ndarray, optional) – Ridge weights for each coefficient. Defaults to all ones.

  • alpha (float, optional) – Elastic net mixing parameter; 1 is no ridge term.

  • l1_alpha (float, optional) – The \(\alpha\) of the sparse group LASSO paper.

linpred(coef, X, test)#

Form linear predictor for given set of coefficients.

Typical use case will have coef of shape (l,p) for univariate response regressions and (l,q,p) for multiple response regressions (e.g. multinomial with q classes) where l is the number of Lagrange parameters.

Parameters:
  • coef (ndarray) – A set of coefficients.

  • X (object) – Representation of design matrix, usually self.X – subclasses may not assume this is actually an array.

  • test (index) – Indices of X.dot(coef.T) to return.

Returns:

linpred (ndarray) – Matrix of linear predictors.

subsample_columns(X, columns)#

Extract columns of each design in self._Xs into a block transform.

Argument X is ignored here.

Parameters:
  • X (object) – Ignored.

  • columns (sequence of int) – Columns (features) to extract.

Returns:

Xsub (regreg.affine.block_maps.block_columns) – Block transform of the extracted columns.

subsample(case_idx)#

Create a new path, by subsampling cases of self.saturated_loss.

Case weights are computed with self.saturated_loss.subsample.

Parameters:

case_idx (index) – An index-like object used to specify which cases to include in the subsample.

Returns:

subsample_path (path object) – A path object with a modified smooth part reflecting the subsampling.

regreg.paths.sparse_group_block.stacked_gaussian(Xs, Ys, *args, **keyword_args)#

Construct a sparse_group_block_path for a Gaussian loss on each response in Ys (one per design in Xs).

Remaining arguments are passed to sparse_group_block_path.

regreg.paths.sparse_group_block.stacked_cox(Xs, Ts, Cs, *args, **keyword_args)#

Construct a sparse_group_block_path for a Cox partial likelihood for each pair of event times in Ts and censoring indicators in Cs.

Remaining arguments are passed to sparse_group_block_path.

regreg.paths.sparse_group_block.stacked_logistic(Xs, Ys, *args, **keyword_args)#

Construct a sparse_group_block_path for a logistic loss on each response in Ys.

Remaining arguments are passed to sparse_group_block_path.

regreg.paths.sparse_group_block.stacked_probit(Xs, Ys, *args, **keyword_args)#

Construct a sparse_group_block_path for a probit loss on each response in Ys.

Remaining arguments are passed to sparse_group_block_path.

regreg.paths.sparse_group_block.stacked_cloglog(Xs, Ys, *args, **keyword_args)#

Construct a sparse_group_block_path for a complementary log-log loss on each response in Ys.

Remaining arguments are passed to sparse_group_block_path.

regreg.paths.sparse_group_block.stacked_poisson(Xs, Ys, *args, **keyword_args)#

Construct a sparse_group_block_path for a Poisson loss on each response in Ys.

Remaining arguments are passed to sparse_group_block_path.

regreg.paths.sparse_group_block.stacked_huber(Xs, Ys, smoothing_parameter, *args, **keyword_args)#

Construct a sparse_group_block_path for a Huber loss on each response in Ys, with smoothing parameter smoothing_parameter.

Remaining arguments are passed to sparse_group_block_path.

regreg.paths.sparse_group_block.stacked_huber_svm(Xs, Ys, smoothing_parameter, *args, **keyword_args)#

Construct a sparse_group_block_path for a smoothed (Huberized) hinge loss on each response in Ys, with smoothing parameter smoothing_parameter.

Remaining arguments are passed to sparse_group_block_path.

regreg.paths.mixed_lasso#

Regularization paths for the mixed LASSO penalty.

This module uses an older path API than the other path modules.

class regreg.paths.mixed_lasso.mixed_lasso_path(loss_factory, X, penalty_structure=None, group_weights={}, elastic_net=identity_quadratic(0.000000, 0.0, 0.0, 0.000000), alpha=0.0, intercept=True, positive_part=None, unpenalized=None, lagrange_proportion=0.05, nstep=100, scale=True, center=True)#

Bases: lasso_path

Regularization path for the mixed LASSO penalty.

The penalty, regreg.atoms.mixed_lasso.mixed_lasso, allows each feature to be unpenalized, L1 penalized, positive part penalized, constrained to be nonnegative, or to belong to a group penalized by its L2 norm.

Parameters:
  • loss_factory (callable) – Function that takes a (normalized) design and returns the smooth loss.

  • X (ndarray or sparse matrix) – Design matrix.

  • penalty_structure (ndarray, optional) – Penalty type for each feature: one of the module constants UNPENALIZED, L1_PENALTY, POSITIVE_PART, NONNEGATIVE, or a nonnegative group label. Defaults to L1_PENALTY for all features.

  • group_weights (dict, optional) – Weights for the groups implied by penalty_structure.

  • elastic_net (regreg.identity_quadratic.identity_quadratic, optional) – Elastic net quadratic.

  • alpha (float, optional) – Elastic net mixing parameter.

  • intercept (bool, optional) – Add an unpenalized intercept column?

  • positive_part (optional) – Unused.

  • unpenalized (optional) – Unused.

  • lagrange_proportion (float, optional) – Ratio of the smallest to the largest Lagrange parameter.

  • nstep (int, optional) – Number of Lagrange parameters.

  • scale (bool, optional) – Scale the columns of X?

  • center (bool, optional) – Center the columns of X?

property shape#

Shape (n, p) of the normalized design.

property nonzero#

Selector for the columns with nonzero standard deviation.

property elastic_net#

Elastic net quadratic scaled by the current Lagrange parameter.

property Xn#

The normalized design.

property loss#

Smooth loss for the normalized design.

property null_solution#

Solution with only the initially active (unpenalized or nonnegative) features.

property lagrange_max#

Smallest Lagrange parameter for which the solution is the null solution.

get_lagrange_sequence()#

Decreasing, log-spaced sequence of Lagrange parameters.

set_lagrange_sequence(lagrange_sequence)#

Set the sequence of Lagrange parameters.

property lagrange_sequence#

Decreasing, log-spaced sequence of Lagrange parameters.

property problem#

The full problem, with all features.

get_lagrange()#

Current Lagrange parameter of the full problem.

set_lagrange(lagrange)#

Set the Lagrange parameter of the full problem and the penalty.

property lagrange#

Current Lagrange parameter of the full problem.

property solution#

Current coefficients of the full problem.

property active#

Boolean indicator of nonzero coefficients.

property lipschitz#

Estimate of the largest squared singular value of the normalized design.

grad(loss=None)#

Gradient at current value.

This includes the gradient of the smooth loss as well as the gradient of the elastic net part. This is used for determining whether the KKT conditions are met and which coefficients are in the strong set.

Parameters:

loss (regreg.smooth.smooth_atom, optional) – Smooth loss. Defaults to self.loss.

Returns:

grad (ndarray) – Gradient at self.solution.

strong_set(lagrange_cur, lagrange_new, grad=None, slope_estimate=1)#

Compute the strong set at lagrange_new.

Parameters:
  • lagrange_cur (float) – Current Lagrange parameter.

  • lagrange_new (float) – Next Lagrange parameter.

  • grad (ndarray, optional) – Gradient at the current solution. Defaults to self.grad().

  • slope_estimate (float, optional) – Estimate of the slope used in the strong rule.

Returns:

  • strong (ndarray) – Boolean indicator of the strong set.

  • strong_selector (regreg.affine.selector) – Selector for the strong set.

slice_columns(columns)#

Columns of the normalized design.

construct_loss(candidate_set, lagrange)#

Construct the loss restricted to candidate_set.

Parameters:
  • candidate_set (index) – Columns to include.

  • lagrange (float) – Lagrange parameter (unused).

Returns:

  • Xslice (object) – Restricted design.

  • loss (regreg.smooth.smooth_atom) – Restricted loss.

restricted_problem(candidate_set, lagrange)#

Construct the problem restricted to candidate_set.

Assumes the candidate set includes intercept as first column.

Parameters:
  • candidate_set (index) – Columns to include.

  • lagrange (float) – Lagrange parameter.

Returns:

  • problem (regreg.problems.simple.simple_problem) – Restricted problem.

  • candidate_selector (regreg.affine.selector) – Selector for candidate_set.

  • penalty_structure (ndarray) – Penalty structure restricted to candidate_set.

solve_subproblem(candidate_set, lagrange_new, **solve_args)#

Solve the problem restricted to candidate_set.

The solution is stored in self.solution.

Parameters:
  • candidate_set (index) – Columns to include.

  • lagrange_new (float) – Lagrange parameter.

  • solve_args (dict) – Keyword arguments passed to simple_problem.solve.

Returns:

  • final_step (float) – Final step size of the solver.

  • grad (ndarray) – Gradient of the restricted smooth loss at the solution.

  • sub_soln (ndarray) – Solution of the subproblem.

  • penalty_structure (ndarray) – Penalty structure restricted to candidate_set.

main(inner_tol=1e-05, verbose=False, kkt_tol=None)#

Compute the path over self.lagrange_sequence.

Parameters:
  • inner_tol (float, optional) – Tolerance for each subproblem.

  • verbose (bool, optional) – Print progress information?

  • kkt_tol (float, optional) – Relative tolerance of the KKT check for coordinates and groups at zero. If None, self.kkt_tol is used.

Returns:

output (dict) – Dictionary with keys 'devratio', 'df', 'lagrange', 'scalings' and 'beta'.

classmethod logistic(X, Y, *args, **keyword_args)#

Construct a path for the logistic loss with binary response Y.

classmethod gaussian(X, Y, *args, **keyword_args)#

Construct a path for the Gaussian loss with response Y.

regreg.paths.basil#

The BASIL (batch screening iterative lasso) path algorithm.

regreg.paths.basil.basil_inner_loop(path_obj, lagrange_subseq, initial_data, inner_tol=1e-05, verbose=False, initial_step=None, check_active=False, step_nvar=50, candidate_set=None, solve_args=None, kkt_tol=None)#

Solve a path over a short sequence of Lagrange parameters.

The subproblem is restricted to a candidate set formed from the unpenalized groups, the step_nvar inactive groups with the largest KKT violations, and candidate_set. The loop stops at the first Lagrange parameter at which the KKT conditions fail.

Parameters:
  • path_obj (regreg.paths.grouped_path) – Path object describing the problem.

  • lagrange_subseq (sequence of float) – Decreasing sequence of Lagrange parameters.

  • initial_data (tuple) – Pair (solution, grad_solution) used as a warm start.

  • inner_tol (float, optional) – Tolerance for each subproblem.

  • verbose (bool, optional) – Print progress information?

  • initial_step (float, optional) – Initial step size for the solver. If None, it is estimated from the Lipschitz constant of path_obj.X.

  • check_active (bool, optional) – Also check KKT conditions for active groups?

  • step_nvar (int, optional) – Number of inactive groups to add to the candidate set.

  • candidate_set (list, optional) – Extra groups to include in the subproblem.

  • solve_args (dict, optional) – Extra arguments passed to path_obj.solve_subproblem.

  • kkt_tol (float, optional) – Relative tolerance of the KKT check for groups at zero. If None, path_obj.kkt_tol is used.

Returns:

  • lagrange_solved (list) – Lagrange parameters for which a solution was found.

  • solutions (list) – Corresponding solutions.

  • last_grad (ndarray) – Gradient at the last solution found.

  • ever_active (list) – Updated candidate set of groups.

regreg.paths.basil.basil(path_obj, lagrange_seq, initial_data, inner_tol=1e-05, verbose=False, initial_step=None, check_active=False, step_nvar=50, step_lagrange=5, solve_args=None, max_tries=20, kkt_tol=None)#

Compute a regularization path with the BASIL algorithm.

The path is computed in batches of step_lagrange Lagrange parameters using basil_inner_loop.

Parameters:
  • path_obj (regreg.paths.grouped_path) – Path object describing the problem.

  • lagrange_seq (ndarray) – Decreasing sequence of Lagrange parameters.

  • initial_data (tuple) – Pair (solution, grad_solution) at lagrange_seq[0].

  • inner_tol (float, optional) – Tolerance for each subproblem.

  • verbose (bool, optional) – Print progress information?

  • initial_step (float, optional) – Initial step size for the solver. If None, it is estimated once from the Lipschitz constant of path_obj.X.

  • check_active (bool, optional) – Also check KKT conditions for active groups?

  • step_nvar (int, optional) – Number of inactive groups added to the candidate set in each batch.

  • step_lagrange (int, optional) – Number of Lagrange parameters in each batch.

  • solve_args (dict, optional) – Extra arguments passed to path_obj.solve_subproblem.

  • max_tries (int, optional) – Number of attempts, with the inner tolerance halved each time, at a batch that solves no Lagrange parameter without enlarging the candidate set. After that the active-set KKT check (if check_active) is dropped for the batch, with a warning.

  • kkt_tol (float, optional) – Relative tolerance of the KKT check for groups at zero. If None, path_obj.kkt_tol is used.

Returns:

solutions (ndarray) – Solutions along the path.

regreg.paths.cross_validate#

Cross-validation of regularization paths.

regreg.paths.cross_validate.cross_validate(path_obj, lagrange_seq, path_fitter=<function strong_rules>, initial_data=None, cv=5, group_labels=None, n_jobs=-1, score=<function <lambda>>, verbose=True, inner_tol=1e-05, rng=None)#

Cross-validate a path fitter over a set of Lagrange parameters.

The coefficient of each subsampled (training) loss is multiplied by nsample / ntrain.

Parameters:
  • path_obj (regreg.paths.grouped_path) – Path object describing the problem on the full data.

  • lagrange_seq (sequence of float) – Decreasing sequence of Lagrange parameters.

  • path_fitter (callable, optional) – Function that computes the path, with signature like regreg.paths.strong_rules.

  • initial_data (tuple, optional) – Pair (solution, grad_solution) to start each path. Defaults to the null solution and gradient of each training fold’s path.

  • cv (int or cross-validation splitter, optional) – Number of folds, or anything accepted by sklearn.model_selection.check_cv (a splitter object or an iterable of (train, test) index pairs).

  • group_labels (ndarray, optional) – Group label for each case, passed as groups to the splitter. With an integer cv, sklearn.model_selection.GroupKFold is used, so that no group is split across folds.

  • n_jobs (int, optional) – Number of jobs passed to joblib.Parallel.

  • score (callable, optional) – Function of (linear_predictor, response) returning the score on a test fold. Defaults to mean squared error.

  • verbose (bool, optional) – Verbosity passed to joblib.Parallel.

  • inner_tol (float, optional) – Tolerance for each subproblem.

  • rng (int or np.random.Generator, optional) – Seed or generator for the random assignment of cases to folds when cv is an integer, passed to np.random.default_rng.

Returns:

  • mean_score (ndarray) – Mean score over folds, for each Lagrange parameter.

  • std_score (ndarray) – Standard deviation of score over folds, for each Lagrange parameter.

regreg.paths.cross_validate.cross_validate_alt(path_obj, fit_fold, fit_args, lagrange_seq, path_fitter=<function strong_rules>, initial_data=None, cv=5, group_labels=None, n_jobs=-1, score=<function <lambda>>, verbose=True, inner_tol=1e-05, rng=None)#

Cross-validate a path fitter over a set of Lagrange parameters.

Like cross_validate, but with a user-supplied function to fit each fold.

Parameters:
  • path_obj (regreg.paths.grouped_path) – Path object describing the problem on the full data.

  • fit_fold (callable) – Called as fit_fold(lagrange_seq, test, train, path_fitter, initial_data, *fit_args); must return a dict with key 'beta'.

  • fit_args (sequence) – Extra positional arguments for fit_fold.

  • lagrange_seq (sequence of float) – Decreasing sequence of Lagrange parameters.

  • path_fitter (callable, optional) – Function that computes the path, passed to fit_fold.

  • initial_data (tuple, optional) – Pair (solution, grad_solution) to start each path. Defaults to (path_obj.solution, path_obj.grad_solution).

  • cv (int or cross-validation splitter, optional) – Number of folds, or anything accepted by sklearn.model_selection.check_cv (a splitter object or an iterable of (train, test) index pairs).

  • group_labels (ndarray, optional) – Group label for each case, passed as groups to the splitter. With an integer cv, sklearn.model_selection.GroupKFold is used, so that no group is split across folds.

  • n_jobs (int, optional) – Number of jobs passed to joblib.Parallel.

  • score (callable, optional) – Function of (linear_predictor, response) returning the score on a test fold. Defaults to mean squared error.

  • verbose (bool, optional) – Verbosity passed to joblib.Parallel.

  • inner_tol (float, optional) – Currently unused.

  • rng (int or np.random.Generator, optional) – Seed or generator for the random assignment of cases to folds when cv is an integer, passed to np.random.default_rng.

Returns:

  • mean_score (ndarray) – Mean score over folds, for each Lagrange parameter.

  • std_score (ndarray) – Standard deviation of score over folds, for each Lagrange parameter.