# shrinkage::Lasso

Shrinkage for Rust · 0.3.0

[Source](<https://github.com/jolars/shrinkage/blob/eeafe449edce19d3c552a03060f681ae3891af74/src/model/lasso.rs>)

```rust
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`](<15ac14ea52500c664ef11283ff8f8268bb931648138cfb39e6f72b84444fee90#method-fit-cfe5dcbd11d6e74acd0dd47480c46376db4c2dee46fb183f17bf0a6df47e334c>) is called. 

```rust
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)?;
```

<a id="impl-cfe5dcbd11d6e74acd0dd47480c46376db4c2dee46fb183f17bf0a6df47e334c"></a>

## Inherent implementation

[Source](<https://github.com/jolars/shrinkage/blob/eeafe449edce19d3c552a03060f681ae3891af74/src/model/lasso.rs>)

```rust
impl Lasso
```

<a id="method-cross_validate_path-cfe5dcbd11d6e74acd0dd47480c46376db4c2dee46fb183f17bf0a6df47e334c"></a>

### `cross_validate_path`

```rust
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
```

[Source](<https://github.com/jolars/shrinkage/blob/eeafe449edce19d3c552a03060f681ae3891af74/src/model/lasso.rs>)

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. 

<a id="method-cross_validate_path-cfe5dcbd11d6e74acd0dd47480c46376db4c2dee46fb183f17bf0a6df47e334c-documentation-errors"></a>

#### Errors

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

<a id="method-cross_validate_regularization_path-cfe5dcbd11d6e74acd0dd47480c46376db4c2dee46fb183f17bf0a6df47e334c"></a>

### `cross_validate_regularization_path`

```rust
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
```

[Source](<https://github.com/jolars/shrinkage/blob/eeafe449edce19d3c552a03060f681ae3891af74/src/model/lasso.rs>)

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. 

<a id="method-cross_validate_regularization_path-cfe5dcbd11d6e74acd0dd47480c46376db4c2dee46fb183f17bf0a6df47e334c-documentation-errors"></a>

#### Errors

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

<a id="method-fit-cfe5dcbd11d6e74acd0dd47480c46376db4c2dee46fb183f17bf0a6df47e334c"></a>

### `fit`

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

[Source](<https://github.com/jolars/shrinkage/blob/eeafe449edce19d3c552a03060f681ae3891af74/src/model/lasso.rs>)

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`. 

<a id="method-fit-cfe5dcbd11d6e74acd0dd47480c46376db4c2dee46fb183f17bf0a6df47e334c-documentation-errors"></a>

#### Errors

Returns [`LassoError::InvalidInput`](<1b3b3442a395454abc564cf4b94d07f97e67975dd92cef1ff14dba46f928b87f#variant-InvalidInput>) for invalid dimensions, empty observations, nonfinite inputs, or invalid options. A preprocessing failure retains its backend source in [`LassoError::Backend`](<1b3b3442a395454abc564cf4b94d07f97e67975dd92cef1ff14dba46f928b87f#variant-Backend>). Finite inputs whose arithmetic produces nonfinite values return [`LassoError::NumericalFailure`](<1b3b3442a395454abc564cf4b94d07f97e67975dd92cef1ff14dba46f928b87f#variant-NumericalFailure>). Iteration limits return a fit instead. 

<a id="method-fit_intercept-cfe5dcbd11d6e74acd0dd47480c46376db4c2dee46fb183f17bf0a6df47e334c"></a>

### `fit_intercept`

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

[Source](<https://github.com/jolars/shrinkage/blob/eeafe449edce19d3c552a03060f681ae3891af74/src/model/lasso.rs>)

Enable or disable the unpenalized intercept. 

With [`Normalization::Auto`](<bac4d61fb1e6b79eebb82905456614bfd93b40a6512bc366d2f5856e338b2546#variant-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. 

<a id="method-fit_path-cfe5dcbd11d6e74acd0dd47480c46376db4c2dee46fb183f17bf0a6df47e334c"></a>

### `fit_path`

```rust
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
```

[Source](<https://github.com/jolars/shrinkage/blob/eeafe449edce19d3c552a03060f681ae3891af74/src/model/lasso.rs>)

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. 

<a id="method-fit_path-cfe5dcbd11d6e74acd0dd47480c46376db4c2dee46fb183f17bf0a6df47e334c-documentation-errors"></a>

#### Errors

Returns validation or fitting errors without a partial path. 

<a id="method-fit_regularization_path-cfe5dcbd11d6e74acd0dd47480c46376db4c2dee46fb183f17bf0a6df47e334c"></a>

### `fit_regularization_path`

```rust
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
```

[Source](<https://github.com/jolars/shrinkage/blob/eeafe449edce19d3c552a03060f681ae3891af74/src/model/lasso.rs>)

Generate and fit a decreasing regularization path with one preparation. 

<a id="method-fit_regularization_path-cfe5dcbd11d6e74acd0dd47480c46376db4c2dee46fb183f17bf0a6df47e334c-documentation-errors"></a>

#### Errors

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

<a id="method-lambda_max-cfe5dcbd11d6e74acd0dd47480c46376db4c2dee46fb183f17bf0a6df47e334c"></a>

### `lambda_max`

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

[Source](<https://github.com/jolars/shrinkage/blob/eeafe449edce19d3c552a03060f681ae3891af74/src/model/lasso.rs>)

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

<a id="method-lambda_max-cfe5dcbd11d6e74acd0dd47480c46376db4c2dee46fb183f17bf0a6df47e334c-documentation-errors"></a>

#### Errors

Returns validation, preprocessing, or arithmetic failures. 

<a id="method-max_iterations-cfe5dcbd11d6e74acd0dd47480c46376db4c2dee46fb183f17bf0a6df47e334c"></a>

### `max_iterations`

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

[Source](<https://github.com/jolars/shrinkage/blob/eeafe449edce19d3c552a03060f681ae3891af74/src/model/lasso.rs>)

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`](<39cce7b2fd80fbdafc360be72ef10a69a6e356c62299ef55325f803188b89b46#variant-IterationLimit>), not convergence. 

<a id="method-new-cfe5dcbd11d6e74acd0dd47480c46376db4c2dee46fb183f17bf0a6df47e334c"></a>

### `new`

```rust
pub fn new(lambda: f64) -> Self
```

[Source](<https://github.com/jolars/shrinkage/blob/eeafe449edce19d3c552a03060f681ae3891af74/src/model/lasso.rs>)

Configure a fit with a finite, nonnegative penalty strength. 

Defaults to an intercept, [`Normalization::Auto`](<bac4d61fb1e6b79eebb82905456614bfd93b40a6512bc366d2f5856e338b2546#variant-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. 

<a id="method-normalize-cfe5dcbd11d6e74acd0dd47480c46376db4c2dee46fb183f17bf0a6df47e334c"></a>

### `normalize`

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

[Source](<https://github.com/jolars/shrinkage/blob/eeafe449edce19d3c552a03060f681ae3891af74/src/model/lasso.rs>)

Select a training-column normalization preset or custom specification. 

Defaults to [`Normalization::Auto`](<bac4d61fb1e6b79eebb82905456614bfd93b40a6512bc366d2f5856e338b2546#variant-Auto>). Use [`Normalization::None`](<bac4d61fb1e6b79eebb82905456614bfd93b40a6512bc366d2f5856e338b2546#variant-None>) for the raw design or [`Normalization::Custom`](<bac4d61fb1e6b79eebb82905456614bfd93b40a6512bc366d2f5856e338b2546#variant-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. 

<a id="method-terminate_on-cfe5dcbd11d6e74acd0dd47480c46376db4c2dee46fb183f17bf0a6df47e334c"></a>

### `terminate_on`

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

[Source](<https://github.com/jolars/shrinkage/blob/eeafe449edce19d3c552a03060f681ae3891af74/src/model/lasso.rs>)

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`](<15ac14ea52500c664ef11283ff8f8268bb931648138cfb39e6f72b84444fee90#method-tolerance-cfe5dcbd11d6e74acd0dd47480c46376db4c2dee46fb183f17bf0a6df47e334c>) setter selects absolute KKT violation. 

<a id="method-tolerance-cfe5dcbd11d6e74acd0dd47480c46376db4c2dee46fb183f17bf0a6df47e334c"></a>

### `tolerance`

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

[Source](<https://github.com/jolars/shrinkage/blob/eeafe449edce19d3c552a03060f681ae3891af74/src/model/lasso.rs>)

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. 

<a id="method-with_centers-cfe5dcbd11d6e74acd0dd47480c46376db4c2dee46fb183f17bf0a6df47e334c"></a>

### `with_centers`

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

[Source](<https://github.com/jolars/shrinkage/blob/eeafe449edce19d3c552a03060f681ae3891af74/src/model/lasso.rs>)

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

This overrides the centering rule selected by [`Self::normalize`](<15ac14ea52500c664ef11283ff8f8268bb931648138cfb39e6f72b84444fee90#method-normalize-cfe5dcbd11d6e74acd0dd47480c46376db4c2dee46fb183f17bf0a6df47e334c>). Each center must be finite, and the vector must have one entry per column. The values are checked by [`Self::fit`](<15ac14ea52500c664ef11283ff8f8268bb931648138cfb39e6f72b84444fee90#method-fit-cfe5dcbd11d6e74acd0dd47480c46376db4c2dee46fb183f17bf0a6df47e334c>). If scales are computed using L1, L2, or maximum-absolute scaling, they use these centers. 

<a id="method-with_scales-cfe5dcbd11d6e74acd0dd47480c46376db4c2dee46fb183f17bf0a6df47e334c"></a>

### `with_scales`

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

[Source](<https://github.com/jolars/shrinkage/blob/eeafe449edce19d3c552a03060f681ae3891af74/src/model/lasso.rs>)

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

This overrides the scaling rule selected by [`Self::normalize`](<15ac14ea52500c664ef11283ff8f8268bb931648138cfb39e6f72b84444fee90#method-normalize-cfe5dcbd11d6e74acd0dd47480c46376db4c2dee46fb183f17bf0a6df47e334c>). Each scale must be finite and strictly positive, and the vector must have one entry per column. The values are checked by [`Self::fit`](<15ac14ea52500c664ef11283ff8f8268bb931648138cfb39e6f72b84444fee90#method-fit-cfe5dcbd11d6e74acd0dd47480c46376db4c2dee46fb183f17bf0a6df47e334c>). 

<a id="method-working_set_policy-cfe5dcbd11d6e74acd0dd47480c46376db4c2dee46fb183f17bf0a6df47e334c"></a>

### `working_set_policy`

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

[Source](<https://github.com/jolars/shrinkage/blob/eeafe449edce19d3c552a03060f681ae3891af74/src/model/lasso.rs>)

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

<a id="method-working_sets-cfe5dcbd11d6e74acd0dd47480c46376db4c2dee46fb183f17bf0a6df47e334c"></a>

### `working_sets`

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

[Source](<https://github.com/jolars/shrinkage/blob/eeafe449edce19d3c552a03060f681ae3891af74/src/model/lasso.rs>)

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

<a id="impl-f0c1dd85270e09057e5273bce1991717502c19da03067d31e73722dfdec0f72b"></a>

## Implementation of Clone

[Source](<https://github.com/jolars/shrinkage/blob/eeafe449edce19d3c552a03060f681ae3891af74/src/model/lasso.rs>)

```rust
impl Clone for Lasso
```

<a id="method-clone-f0c1dd85270e09057e5273bce1991717502c19da03067d31e73722dfdec0f72b"></a>

### `clone`

```rust
fn clone(&self) -> Self
```

[Source](<https://github.com/jolars/shrinkage/blob/eeafe449edce19d3c552a03060f681ae3891af74/src/model/lasso.rs>)

<a id="impl-bfb4318ae4618ca32aa639c82fbce9fcea8c8ebd0bd4eded424159af02970ca0"></a>

## Implementation of Debug

[Source](<https://github.com/jolars/shrinkage/blob/eeafe449edce19d3c552a03060f681ae3891af74/src/model/lasso.rs>)

```rust
impl Debug for Lasso
```

<a id="method-fmt-bfb4318ae4618ca32aa639c82fbce9fcea8c8ebd0bd4eded424159af02970ca0"></a>

### `fmt`

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

[Source](<https://github.com/jolars/shrinkage/blob/eeafe449edce19d3c552a03060f681ae3891af74/src/model/lasso.rs>)

<a id="impl-5042bb1e39f0ad8c04b88a1c930287275cebefe81c09d49278f471123d1ed23a"></a>

## Implementation of Freeze

```rust
impl Freeze for Lasso
```

<a id="impl-23219e6c81b7356ce8f2010a3cccf1c989128ec94ea79d4e0d696b0b0b73fc58"></a>

## Implementation of RefUnwindSafe

```rust
impl RefUnwindSafe for Lasso
```

<a id="impl-339a2793b5499b47cd31c739ca4fafd30ece7768e3ee255778d6bb3eaf0faed4"></a>

## Implementation of Send

```rust
impl Send for Lasso
```

<a id="impl-7a73653f6099edcb815bfeb738a2fc6d6b35dfa3c2be75dd01c63e538953e207"></a>

## Implementation of Sync

```rust
impl Sync for Lasso
```

<a id="impl-96f907cf89dca988b1a032937e19922af4965432b6fa7ba67949bc2ca65fc6d5"></a>

## Implementation of Unpin

```rust
impl Unpin for Lasso
```

<a id="impl-ee3dba893c066e636156e234a83dca573ce6ded2ecc5f56ae802edd7527ff55d"></a>

## Implementation of UnsafeUnpin

```rust
impl UnsafeUnpin for Lasso
```

<a id="impl-bcb264159fd47b6bfdc87945e32f21721bbcce5fcc7a6a096277211da72ffd12"></a>

## Implementation of UnwindSafe

```rust
impl UnwindSafe for Lasso
```

<a id="impl-1e5682dc18e03c648aa9b125290c107c1e8d96ce8a179a8532e4057da4daf0cd"></a>

## Implementation of Any

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

<a id="impl-6c0b25c2c92cdb117802d9c0a4c70afc9a8bfb0da875bc6e66191e2349fcdf3f"></a>

## Implementation of Borrow

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

<a id="impl-796e80d926348273a22b04d8e84f510693b11c06ec57c3c3e2e8c1524094a85c"></a>

## Implementation of BorrowMut

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

<a id="impl-8360e4092e7ec4a05a26dd304cd739398f0cacc50c8d08dd5be0223d1a08d02e"></a>

## Implementation of ByRef

```rust
impl<T> ByRef<T> for T
```

<a id="impl-dbb1293b7b5dd80c4d9a98cfdd6843dc7470224c0b1387731cd418da07046d7b"></a>

## Implementation of CastableFrom

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

<a id="impl-97b65e7dfc44b5ecaa495a3a19fe737914a1e9f8532200cf8f46191314e7d777"></a>

## Implementation of CastableFrom

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

<a id="impl-86d7da2bfd1140cc1675dfe6a975e6a2762338ab32731152da0723858ef3aa81"></a>

## Implementation of CloneToUninit

```rust
impl<T> CloneToUninit for T where T: Clone
```

<a id="impl-c22bdd3b1b677b66eb32d1c182cc1410c7f0c46491dd74e6a659647c238d19ec"></a>

## Implementation of From

```rust
impl<T> From<T> for T
```

<a id="impl-5adf6ca1830f30fbbac655ab8ff394d254954df7ededc7cc833c72fdc87d6f00"></a>

## Implementation of Imply

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

<a id="impl-85eec431edd188129f88290bbcb748e4d2fdc97f7efef3c162b8c252286ffec0"></a>

## Implementation of Into

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

<a id="impl-c40268dd59dd3329202d3c0544c62372e98ff3baa8ec442ec00752e024ec1d06"></a>

## Implementation of IntoEither

```rust
impl<T> IntoEither for T
```

<a id="impl-5655b1ef858b34a584cb80104f040c8a4063ced0d281311a0833ed12aefde86e"></a>

## Implementation of Pointable

```rust
impl<T> Pointable for T
```

<a id="impl-8cc2aa01198fc9fa44f08b889f62988d70d2a7f4f0f11f4666d4c08bdaddb22a"></a>

## Implementation of Read

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

<a id="impl-5ea09131039c62dd92b119d480a35c7e2b33d2b81dccf0b849f6b1f19f4bda24"></a>

## Implementation of Same

```rust
impl<T> Same for T
```

<a id="impl-7ee6ff38d6674f5edfe4734359062c2a67a572cff5899a89704764822b931c7f"></a>

## Implementation of SupersetOf

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

<a id="impl-a8ef6e94191301c3685488478f4905086c47137bac2d606ea4d09631d5472b06"></a>

## Implementation of ToOwned

```rust
impl<T> ToOwned for T where T: Clone
```

<a id="impl-83960135d00d98080c95a08867729696e36811280c2dd9cf34c1b4fe21c81e18"></a>

## Implementation of TryFrom

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

<a id="impl-1cc495bcaa5112ffb304e7f0de674feca391fd97f47bec86033a9225b72ac05c"></a>

## Implementation of TryInto

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