# shrinkage

Shrinkage for Rust · 0.3.0

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

```rust
pub mod shrinkage
```

Regularized statistical models with lazy matrix normalization. 

[`Lasso`](<15ac14ea52500c664ef11283ff8f8268bb931648138cfb39e6f72b84444fee90>) fits Gaussian lasso models using dense or sparse CSC columns. Fits return original-scale parameters, reusable training preprocessing, and explicit convergence diagnostics. See [`Lasso::terminate_on`](<15ac14ea52500c664ef11283ff8f8268bb931648138cfb39e6f72b84444fee90#method-terminate_on-cfe5dcbd11d6e74acd0dd47480c46376db4c2dee46fb183f17bf0a6df47e334c>) for the stopping criterion and [`LassoFit::termination`](<d7f3cc1c178006da270ca45d4069f972c99a6c736656b6241882f09fbaf163bc#method-termination-b7275ba661ef0fe0bb23beba12f3e5dc0f391154ba6bef5b22ed64b8923407bd>) before using a fit. 

The primal objective is `P(beta_0, beta) = F(beta_0, beta) + R(beta)`, with datafit `F`, complete penalty `R`, coefficients `beta`, and intercept `beta_0`. For a feasible dual variable `theta`, the dual objective is `D(theta)` and the duality gap is `G(beta_0, beta, theta) = P(beta_0, beta) - D(theta)`. 

No matrix backend is enabled by default. Versioned features select LazyMatrix's integrations with faer 0.24 (`faer_v0_24`), nalgebra 0.34 and nalgebra-sparse 0.11 (`nalgebra_v0_34`), ndarray 0.17 (`ndarray_v0_17`), or sprs 0.11 (`sprs_v0_11`). The short names `faer`, `nalgebra`, `ndarray`, and `sprs` are aliases for these features. Match your direct matrix dependency to the selected release line. 

LazyMatrix implements each enabled backend release independently, so feature unification retains support for the selected matrix types. faer CSC inputs also accept borrowed `SparseColMatRef` views, including cross-validation, without first cloning the source matrix. 

With `ndarray_v0_17` or its `ndarray` alias, fit directly against an array or borrowed view: 

```rust
use ndarray::array;
use shrinkage::Lasso;

let x = array![[0.0], [1.0], [2.0]];
let fit = Lasso::new(0.1).fit(&x.view(), &[1.0, 3.0, 5.0])?;
let predictions = fit.predict(&x)?;
```

With `sprs_v0_11` or its `sprs` alias, wrap CSC storage in `lazymatrix::SprsCsc`. Borrowing a view checks its orientation without copying its entries: 

```rust
use shrinkage::{Lasso, lazymatrix::SprsCsc};
use sprs::CsMat;

let x = CsMat::new_csc((3, 1), vec![0, 2], vec![1, 2], vec![1.0, 2.0]);
let columns = SprsCsc::try_new(x.view()).unwrap();
let fit = Lasso::new(0.1).fit(&columns, &[1.0, 3.0, 5.0])?;
let predictions = fit.predict(&columns)?;
```

For CSR input, explicitly convert with `to_csc()` before wrapping. ndarray supports row-major, column-major, and strided arrays; column-major storage keeps each column contiguous for coordinate descent. 

[`lazymatrix::IndexedRows`](<https://docs.rs/lazymatrix/0.11.0/lazymatrix/indexed_rows/struct.IndexedRows.html>) borrows a raw row selection. Normalization uses only those observations, and predictions retain the original feature scale: 

```rust
use ndarray::array;
use shrinkage::{Lasso, lazymatrix::IndexedRows};

let x = array![[0.0], [100.0], [2.0]];
let rows = [0, 2];
let training = IndexedRows::new(&x, &rows);
let fit = Lasso::new(0.1).fit(&training, &[1.0, 5.0])?;
assert_eq!(fit.preprocessing().centers(), Some([1.0].as_slice()));
let predictions = fit.predict(&x)?;
```

Indexed columns filter the source on each traversal. Built-in cross-validation instead copies selected entries into owned fold matrices once, keeping repeated solver sweeps on that storage.
