Shrinkage

shrinkage::Lasso

Shrinkage for Rust · rust · 0.3.0

pub struct Lasso

Gaussian lasso configuration.

Fits ||y - X_tilde * beta - beta_0||² / (2n) + lambda * ||beta||₁. The intercept is unpenalized. By default, X_tilde uses training-column means and population standard deviations, and the penalty acts on the normalized coefficients. Reported coefficients and predictions use the original input scale.

Dense and CSC matrices use the same statically dispatched iteration routine. Solver storage is O(n + p); neither a normalized matrix nor a Gram matrix is materialized. Options are validated when Self::fit is called.

use faer::Mat;
use shrinkage::{Lasso, Termination};

let x = Mat::from_fn(3, 1, |i, _| i as f64);
let fit = Lasso::new(0.1).fit(&x, &[1.0, 3.0, 5.0])?;
assert_eq!(fit.termination(), Termination::Converged);
let predictions = fit.predict(&x)?;

Inherent implementation

impl Lasso

cross_validate_path

pub fn cross_validate_path<M>(
    &self,
    x: &M,
    y: &[f64],
    lambdas: &[f64],
    cv: &shrinkage::CrossValidation
) -> Result<shrinkage::CrossValidationFit<LassoFit, L1>, shrinkage::CrossValidationError<<M as >::Error>> where M: shrinkage::RowSubset + ?Sized

Cross-validate explicit lambda candidates in caller preference order.

Each fold reuses coordinate-descent storage across its entire path. The result retains fold paths and a fresh full-data path after selection.

Errors

Reject invalid folds, empty candidates, supplied preprocessing vectors, and failed or nonconverged fits, with fold context when available.

cross_validate_regularization_path

pub fn cross_validate_regularization_path<M>(
    &self,
    x: &M,
    y: &[f64],
    options: &shrinkage::PathOptions,
    cv: &shrinkage::CrossValidation
) -> Result<shrinkage::CrossValidationFit<LassoFit, L1>, shrinkage::CrossValidationError<<M as >::Error>> where M: shrinkage::RowSubset + ?Sized

Cross-validate automatic paths using training-fold anchors and preprocessing.

Paths share relative strengths, and every fold fits the entire sequence. The selected fraction is refitted with the full-data anchor and preprocessing.

Errors

Reject invalid options or folds, supplied preprocessing vectors, and any failed or nonconverged fit. Errors identify the fold when available.

fit

pub fn fit<M>(
    &self,
    x: &M,
    y: &[f64]
) -> Result<LassoFit, FitError<<M as >::Error>> where M: RawColumns<f64> + ColumnStats<f64> + ?Sized

Fit against raw training columns and a response slice.

Zero-feature designs are supported. Zero-norm normalized columns stay at zero. The fitted result owns its parameters and does not borrow x.

Errors

Returns LassoError::InvalidInput for invalid dimensions, empty observations, nonfinite inputs, or invalid options. A preprocessing failure retains its backend source in LassoError::Backend. Finite inputs whose arithmetic produces nonfinite values return LassoError::NumericalFailure. Iteration limits return a fit instead.

fit_intercept

pub fn fit_intercept(self, enabled: bool) -> Self

Enable or disable the unpenalized intercept.

With Normalization::Auto, disabling the intercept also disables centering while retaining population-standard-deviation scaling. Explicit normalization choices are honored independently. Explicit centering can induce a fixed original-scale intercept even when this option is disabled.

fit_path

pub fn fit_path<M>(
    &self,
    x: &M,
    y: &[f64],
    lambdas: &[f64]
) -> Result<shrinkage::PathFit<LassoFit, L1>, FitError<<M as >::Error>> where M: RawColumns<f64> + ColumnStats<f64> + ?Sized

Fit explicit strengths in caller order with warm starts and reused storage. The builder's single-fit lambda is replaced by these values. Defaults resolve the stopping criterion separately for each point.

Errors

Returns validation or fitting errors without a partial path.

fit_regularization_path

pub fn fit_regularization_path<M>(
    &self,
    x: &M,
    y: &[f64],
    options: &shrinkage::PathOptions
) -> Result<shrinkage::PathFit<LassoFit, L1>, FitError<<M as >::Error>> where M: RawColumns<f64> + ColumnStats<f64> + ?Sized

Generate and fit a decreasing regularization path with one preparation.

Errors

Returns invalid sequence options, input failures, or numerical errors.

lambda_max

pub fn lambda_max<M>(
    &self,
    x: &M,
    y: &[f64]
) -> Result<f64, FitError<<M as >::Error>> where M: RawColumns<f64> + ColumnStats<f64> + ?Sized

Calculate the smallest lambda with optimal zero optimization coefficients. Uses the builder's intercept and normalization settings.

Errors

Returns validation, preprocessing, or arithmetic failures.

max_iterations

pub fn max_iterations(self, max_iterations: usize) -> Self

Set a strictly positive limit on coordinate sweeps over the current working set.

An initially optimal fit takes zero sweeps. Reaching the limit returns a finite fit with Termination::IterationLimit, not convergence.

new

pub fn new(lambda: f64) -> Self

Configure a fit with a finite, nonnegative penalty strength.

Defaults to an intercept, Normalization::Auto, a relative duality gap tolerance of 1e-6, and at most 10_000 coordinate sweeps over the current working set. At zero penalty, the default is absolute KKT tolerance 1e-6: scaling a residual cannot generally construct a useful zero-penalty dual vector.

normalize

pub fn normalize(self, normalization: Normalization) -> Self

Select a training-column normalization preset or custom specification.

Defaults to Normalization::Auto. Use Normalization::None for the raw design or Normalization::Custom to choose centering and scaling independently. Computed zero scales are replaced with one. Penalties act on normalized coefficients; reported parameters and predictions use the original scale.

terminate_on

pub fn terminate_on(self, criterion: StoppingCriterion) -> Self

Select the stopping criterion and its tolerances.

The duality gap uses a fixed reference loss from the zero-coefficient model, with the intercept optimized when enabled. The legacy Self::tolerance setter selects absolute KKT violation.

tolerance

pub fn tolerance(self, tolerance: f64) -> Self

Set a finite, strictly positive absolute KKT tolerance.

With c_j = X_tilde_j' * residual / n, the violation is |c_j - lambda * sign(beta_j)| for nonzero coefficients and max(|c_j| - lambda, 0) otherwise. When fitting an intercept, include |mean(residual)|. Convergence requires the largest violation to be at most this tolerance, confirmed after reconstructing the residual.

with_centers

pub fn with_centers(self, centers: Vec<f64>) -> Self

Supply column centers instead of computing them from the training data.

This overrides the centering rule selected by Self::normalize. Each center must be finite, and the vector must have one entry per column. The values are checked by Self::fit. If scales are computed using L1, L2, or maximum-absolute scaling, they use these centers.

with_scales

pub fn with_scales(self, scales: Vec<f64>) -> Self

Supply column scales instead of computing them from the training data.

This overrides the scaling rule selected by Self::normalize. Each scale must be finite and strictly positive, and the vector must have one entry per column. The values are checked by Self::fit.

working_set_policy

pub fn working_set_policy(self, policy: WorkingSetPolicy) -> Self

Select the working-set policy for single fits and paths.

working_sets

pub fn working_sets(self, enabled: bool) -> Self

Enable automatic working sets, or disable heuristic restrictions. The last working-set setter wins.

Implementation of Clone

impl Clone for Lasso

clone

fn clone(&self) -> Self

Implementation of Debug

impl Debug for Lasso

fmt

fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result

Implementation of Freeze

impl Freeze for Lasso

Implementation of RefUnwindSafe

impl RefUnwindSafe for Lasso

Implementation of Send

impl Send for Lasso

Implementation of Sync

impl Sync for Lasso

Implementation of Unpin

impl Unpin for Lasso

Implementation of UnsafeUnpin

impl UnsafeUnpin for Lasso

Implementation of UnwindSafe

impl UnwindSafe for Lasso

Implementation of Any

impl<T> Any for T where T: 'static + ?Sized

Implementation of Borrow

impl<T> Borrow<T> for T where T: ?Sized

Implementation of BorrowMut

impl<T> BorrowMut<T> for T where T: ?Sized

Implementation of ByRef

impl<T> ByRef<T> for T

Implementation of CastableFrom

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT where ST: ?Sized, DT: ?Sized

Implementation of CastableFrom

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT where ST: ?Sized, DT: ?Sized

Implementation of CloneToUninit

impl<T> CloneToUninit for T where T: Clone

Implementation of From

impl<T> From<T> for T

Implementation of Imply

impl<T, U> Imply<T> for U where T: ?Sized, U: ?Sized

Implementation of Into

impl<T, U> Into<U> for T where U: From<T>

Implementation of IntoEither

impl<T> IntoEither for T

Implementation of Pointable

impl<T> Pointable for T

Implementation of Read

impl<T> Read<Exclusive, BecauseExclusive> for T where T: ?Sized

Implementation of Same

impl<T> Same for T

Implementation of SupersetOf

impl<SS, SP> SupersetOf<SS> for SP where SS: SubsetOf<SP>

Implementation of ToOwned

impl<T> ToOwned for T where T: Clone

Implementation of TryFrom

impl<T, U> TryFrom<U> for T where U: Into<T>

Implementation of TryInto

impl<T, U> TryInto<U> for T where U: TryFrom<T>