cleands.Prediction.shrinkage module

Shrinkage and regularization models for regression.

Implements L1-regularized (Lasso) regression using quadratic programming via cvxopt, along with a cross-validated variant to pick the penalty and a bootstrap variant for uncertainty quantification.

Classes

l1_regularization_regressor

Lasso with a fixed L1 threshold.

l1_cross_validation_regressor

Selects the L1 threshold by k-fold cross-validation.

l1_bootstrap_regressor

Cross-validated Lasso with bootstrap-based variance estimation.

Factory Aliases

L1BootstrapRegressor

PredictionModel wrapper for l1_bootstrap_regressor.

class cleands.Prediction.shrinkage.l1_regularization_regressor(x, y, thresh, *args, **kwargs)[source]

Bases: linear_model

L1-regularized (Lasso) linear regression.

Solves a least-squares problem subject to an L1 constraint on the coefficients. If an intercept column of ones is detected as the first or last column of x, it is treated without penalization.

Variables:

threshold (float) – L1 budget (sum of absolute coefficients).

Parameters:
  • y (ndarray)

  • thresh (float)

static solve_lasso(x, y, thresh)[source]

Solve the Lasso via a QP with nonnegativity / L1 budget.

Uses the standard positive/negative split of coefficients b = b+ - b- with b+, b- ≥ 0 and sum(b+ + b-) ≤ thresh.

Parameters:
  • x (np.ndarray) – Feature matrix (n_obs, n_features).

  • y (np.ndarray) – Response vector (n_obs,) or (n_obs, 1).

  • thresh (float) – L1 constraint level.

Returns:

Coefficient vector (n_features,) for the original variables.

Return type:

np.ndarray

class cleands.Prediction.shrinkage.l1_cross_validation_regressor(x, y, max_thresh=None, folds=5, seed=None, *args, **kwargs)[source]

Bases: l1_regularization_regressor

Cross-validated L1-regularized regression.

Searches over a grid of λ values (scaled to max_thresh) and selects the one minimizing mean squared error via k-fold cross-validation.

Variables:
  • statistic (float) – Best cross-validated mean squared error.

  • lambda_value (float) – Selected λ in [0, 1] (scales max_thresh).

  • max_threshold (float) – Max L1 budget used to scale λ.

Parameters:
  • y (ndarray)

  • max_thresh (int | None)

  • folds (int)

class cleands.Prediction.shrinkage.l1_bootstrap_regressor(x, y, *args, bootstraps=1000, **kwargs)[source]

Bases: l1_cross_validation_regressor, variance_model

Bootstrap-augmented cross-validated Lasso.

Fits the cross-validated Lasso then estimates parameter variability via bootstrap resampling and constructs a .glance summary table.

Variables:
  • n_boot (int) – Number of bootstrap replications.

  • bootstraps (list[l1_regularization_regressor]) – Fitted bootstrap models.

  • bootstrap_params (np.ndarray) – Coefficients from bootstrap models (n_boot, p).

  • glance (pd.DataFrame) – Summary metrics assembled post-fit.

Parameters:
  • y (ndarray)

  • bootstraps (int)

property vcov_params

Bootstrap covariance of the parameter estimates.

Returns:

(p x p) covariance matrix computed from bootstrap coefficients.

Return type:

np.ndarray

class cleands.Prediction.shrinkage.L1BootstrapRegressor(formula, data, *args, **kwargs)[source]

Bases: PredictionModel

Convenience wrapper for L1-regularized regression with bootstrap inference.

This model applies L1 (lasso) regularization to linear regression and uses bootstrapping to estimate parameter variability and robust standard errors. Provides a formula/DataFrame interface for the l1_bootstrap_regressor.

Variables:

MODEL_TYPE (ClassVar[Type[cleands.base.supervised_model]]) – Underlying model type, fixed to l1_bootstrap_regressor.

Parameters:
  • formula (str)

  • data (DataFrame)

Example

>>> model = L1BootstrapRegressor.from_formula("y ~ x1 + x2", data=df, bootstraps=500)
>>> model.tidy  # parameter estimates
>>> model.glance  # model fit statistics