Skip to content

Lasso

from SymbolicDSGE.regression.lasso import LassoResult, lasso, lasso_gs
lasso(
    X: ndarray,
    y: ndarray,
    alpha: float,
    variables: list[str] | None = None,
    intercept: bool = True,
    max_iter: int = 1000,
    tol: float = 1e-10,
) -> LassoResult

Run Lasso regression with an L1 penalty using coordinate descent on the normalized Gram system.

Inputs:

Name Description
X Design matrix. Shape (n, k).
y Response vector. Shape (n,).
alpha Non-negative L1 penalty weight.
variables Optional names for the columns of X. Defaults to x0, x1, ...
intercept If True, fit an unpenalized intercept by centering and restore it in the returned design.
max_iter Maximum coordinate-descent iterations.
tol Coordinate-descent convergence tolerance.

Returns:

Type Description
LassoResult Regression result with L1 sparsity diagnostics.

 

lasso_gs(
    X: ndarray,
    y: ndarray,
    start: float,
    stop: float,
    num: int,
    variables: list[str] | None = None,
    intercept: bool = True,
    max_iter: int = 1000,
    tol: float = 1e-10,
) -> LassoResult

Evaluate a logarithmic alpha grid using the LARS-Lasso path and select the coefficient vector with the lowest residual loss.

Inputs:

Name Description
start Positive lower endpoint for the alpha grid.
stop Positive upper endpoint for the alpha grid.
num Number of grid points.
X, y, variables, intercept, max_iter, tol Same contract as lasso(...).

 

@dataclass(frozen=True)
class LassoResult(RegressionResult)

Additional Fields and Properties:

Name Type Description
alpha float64 Selected L1 penalty weight.
effective_dof float64 Active penalized coefficient count plus the intercept, when present.
intercept bool Whether the returned design includes an intercept column.
alpha_grid ndarray | None Grid of alpha values evaluated by lasso_gs(...).
coefficient_path ndarray | None Coefficients evaluated on alpha_grid.
objective_trace ndarray | None Residual-loss trace over alpha_grid.
knot_lambdas ndarray | None LARS path knot locations from grid-search construction.
knot_coefficients ndarray | None LARS path coefficients aligned to knot_lambdas.
penalized_coefficients ndarray Coefficients subject to the L1 penalty.
active_mask ndarray Boolean mask over penalized coefficients.
n_active int Number of active penalized coefficients.
selected_variables list[str] Variable names with active penalized coefficients.
l1_norm float64 L1 norm of penalized coefficients.
l1_penalty float64 Realized L1 penalty value.
Path Output

Path fields are populated by lasso_gs(...). Direct lasso(...) calls return only the selected coefficient vector and scalar L1 diagnostics.