Shrinkage

shrinkage::RuntimeProblem

Shrinkage for Rust · rust · 0.3.0

pub type RuntimeProblem<'a> = shrinkage::Problem<Box<dyn RuntimeDesign + 'a>, Box<dyn SmoothDatafit + 'a>, Box<dyn ProximalPenalty + 'a>>;

A problem whose design, smooth datafit, and complete proximal term are selected independently before fitting.

The shared iteration calls object-safe oracles for complete products, loss and gradient evaluations, and proximal operations. It does not erase an already composed typed problem. Use MatrixDesign to adapt each concrete backend once, independently of the chosen datafit and penalty.

ProximalGradient<Vec<f64>> fits this problem, while adapters retain each backend's native vector type. Normalization and supplied vectors use the same builders and semantics as the typed Problem. Selecting CoordinateDescent returns FitError::UnsupportedCombination before preparing the design: these oracles do not expose its Gaussian/L1 coordinate updates and dual certificate. Use proximal gradient here, or a typed Gaussian/L1 problem with coordinate descent. A value-only datafit or penalty cannot be boxed into the required capability traits.

use shrinkage::{FitError, Penalty, ProximalPenalty};
fn select_penalty<P: Penalty<Error = FitError> + 'static>(penalty: P)
    -> Box<dyn ProximalPenalty>
{
    Box::new(penalty)
}
use shrinkage::{Datafit, FitError, SmoothDatafit};
fn select_datafit<D: Datafit<Error = FitError> + 'static>(datafit: D)
    -> Box<dyn SmoothDatafit>
{
    Box::new(datafit)
}
use faer::{Col, Mat};
use shrinkage::{Gaussian, MatrixDesign, ProximalGradient, Ridge, RuntimeProblem};
let x = Mat::from_fn(3, 1, |i, _| i as f64);
let y = [1.0, 3.0, 5.0];
let problem = RuntimeProblem::new(
    Box::new(MatrixDesign::<_, Col<f64>>::new(&x)),
    Box::new(Gaussian::new(&y)),
    Box::new(Ridge::new(0.1)),
);
let fit = problem.fit_with(&ProximalGradient::new())?;
let predictions = fit.predict(&x)?;