use crate::{CoordinateDescent, Gaussian, L1, Normalization, Problem, WorkingSetPolicy};
pub use crate::{
error::FitError as LassoError,
fit::{LassoFit, Preprocessing, Termination},
solver::StoppingCriterion,
};
use lazymatrix::{ColumnStats, RawColumns};
#[derive(Clone, Debug)]
#[must_use]
pub struct Lasso {
lambda: f64,
fit_intercept: bool,
normalization: Normalization,
supplied_centers: Option<Vec<f64>>,
supplied_scales: Option<Vec<f64>>,
stopping_criterion: Option<StoppingCriterion>,
max_iterations: usize,
working_set_policy: WorkingSetPolicy,
}
impl Lasso {
pub fn new(lambda: f64) -> Self {
Self {
lambda,
fit_intercept: true,
normalization: Normalization::Auto,
supplied_centers: None,
supplied_scales: None,
stopping_criterion: None,
max_iterations: 10_000,
working_set_policy: WorkingSetPolicy::Auto,
}
}
pub fn fit_intercept(mut self, enabled: bool) -> Self {
self.fit_intercept = enabled;
self
}
pub fn normalize(mut self, normalization: Normalization) -> Self {
self.normalization = normalization;
self
}
pub fn with_centers(mut self, centers: Vec<f64>) -> Self {
self.supplied_centers = Some(centers);
self
}
pub fn with_scales(mut self, scales: Vec<f64>) -> Self {
self.supplied_scales = Some(scales);
self
}
pub fn tolerance(mut self, tolerance: f64) -> Self {
self.stopping_criterion = Some(StoppingCriterion::kkt_violation(tolerance));
self
}
pub fn terminate_on(mut self, criterion: StoppingCriterion) -> Self {
self.stopping_criterion = Some(criterion);
self
}
pub fn max_iterations(mut self, max_iterations: usize) -> Self {
self.max_iterations = max_iterations;
self
}
pub fn working_sets(mut self, enabled: bool) -> Self {
self.working_set_policy = WorkingSetPolicy::enabled(enabled);
self
}
pub fn working_set_policy(mut self, policy: WorkingSetPolicy) -> Self {
self.working_set_policy = policy;
self
}
pub fn fit<M>(&self, x: &M, y: &[f64]) -> Result<LassoFit, LassoError<M::Error>>
where
M: RawColumns<f64> + ColumnStats<f64> + ?Sized,
{
self.problem(x, y).fit_with(&self.solver())
}
pub fn fit_path<M>(
&self,
x: &M,
y: &[f64],
lambdas: &[f64],
) -> Result<crate::PathFit<LassoFit, L1>, LassoError<M::Error>>
where
M: RawColumns<f64> + ColumnStats<f64> + ?Sized,
{
Ok(self
.problem(x, y)
.fit_path_with(
&self.solver(),
lambdas.iter().map(|&v| L1::new(v)).collect(),
)?
.with_lambdas(lambdas.to_vec()))
}
pub fn fit_regularization_path<M>(
&self,
x: &M,
y: &[f64],
options: &crate::PathOptions,
) -> Result<crate::PathFit<LassoFit, L1>, LassoError<M::Error>>
where
M: RawColumns<f64> + ColumnStats<f64> + ?Sized,
{
self.problem(x, y)
.fit_regularization_path_with(&self.solver(), options)
}
pub fn lambda_max<M>(&self, x: &M, y: &[f64]) -> Result<f64, LassoError<M::Error>>
where
M: RawColumns<f64> + ColumnStats<f64> + ?Sized,
{
match self.problem(x, y).maximum_penalty_with(&self.solver())? {
crate::PenaltyMaximum::Finite(value) => Ok(value),
crate::PenaltyMaximum::Unbounded => unreachable!("L1 has a finite threshold"),
}
}
pub fn cross_validate_path<M>(
&self,
x: &M,
y: &[f64],
lambdas: &[f64],
cv: &crate::CrossValidation,
) -> Result<crate::CrossValidationFit<LassoFit, L1>, crate::CrossValidationError<M::Error>>
where
M: crate::RowSubset + ?Sized,
{
self.problem(x, y).cross_validate_path_with(
&self.solver(),
lambdas.iter().copied().map(L1::new).collect(),
cv,
)
}
pub fn cross_validate_regularization_path<M>(
&self,
x: &M,
y: &[f64],
options: &crate::PathOptions,
cv: &crate::CrossValidation,
) -> Result<crate::CrossValidationFit<LassoFit, L1>, crate::CrossValidationError<M::Error>>
where
M: crate::RowSubset + ?Sized,
{
self.problem(x, y)
.cross_validate_regularization_path_with(&self.solver(), options, cv)
}
fn solver(&self) -> CoordinateDescent {
let mut solver = CoordinateDescent::new()
.max_iterations(self.max_iterations)
.working_set_policy(self.working_set_policy);
if let Some(criterion) = self.stopping_criterion {
solver = solver.terminate_on(criterion);
}
solver
}
fn problem<'a, M: ?Sized>(&self, x: &'a M, y: &'a [f64]) -> Problem<&'a M, Gaussian<'a>, L1> {
let mut problem = Problem::new(
x,
Gaussian::new(y).fit_intercept(self.fit_intercept),
L1::new(self.lambda),
)
.normalize(self.normalization);
if let Some(centers) = &self.supplied_centers {
problem = problem.with_centers(centers.clone());
}
if let Some(scales) = &self.supplied_scales {
problem = problem.with_scales(scales.clone());
}
problem
}
}