use lazymatrix::RawColumns;
use super::{Preprocessing, Termination, back_transform, predict};
use crate::{LassoError, StoppingCriterion};
#[derive(Clone, Debug)]
#[must_use]
pub struct LassoFit {
working_set: crate::WorkingSetDiagnostics,
coefficients: Vec<f64>,
intercept: f64,
preprocessing: Preprocessing,
termination: Termination,
iterations: usize,
objective: f64,
kkt_violation: f64,
dual_certificate: Vec<f64>,
dual_objective: f64,
duality_gap: f64,
reference_loss: f64,
stopping_criterion: StoppingCriterion,
stopping_value: f64,
stopping_threshold: f64,
}
impl LassoFit {
pub fn working_set_diagnostics(&self) -> &crate::WorkingSetDiagnostics {
&self.working_set
}
pub fn coefficients(&self) -> &[f64] {
&self.coefficients
}
pub fn intercept(&self) -> f64 {
self.intercept
}
pub fn preprocessing(&self) -> &Preprocessing {
&self.preprocessing
}
pub fn termination(&self) -> Termination {
self.termination
}
pub fn iterations(&self) -> usize {
self.iterations
}
pub fn objective(&self) -> f64 {
self.objective
}
pub fn kkt_violation(&self) -> f64 {
self.kkt_violation
}
pub fn dual_certificate(&self) -> &[f64] {
&self.dual_certificate
}
pub fn dual_objective(&self) -> f64 {
self.dual_objective
}
pub fn duality_gap(&self) -> f64 {
self.duality_gap
}
pub fn reference_loss(&self) -> f64 {
self.reference_loss
}
pub fn stopping_criterion(&self) -> StoppingCriterion {
self.stopping_criterion
}
pub fn stopping_value(&self) -> f64 {
self.stopping_value
}
pub fn stopping_threshold(&self) -> f64 {
self.stopping_threshold
}
pub fn predict<M: RawColumns<f64> + ?Sized>(&self, x: &M) -> Result<Vec<f64>, LassoError> {
predict(&self.coefficients, self.intercept, self.iterations, x)
}
}
impl LassoFit {
pub(crate) fn from_solution<E>(
solution: crate::solver::coordinate::Solution,
preprocessing: Preprocessing,
) -> Result<Self, LassoError<E>> {
let (coefficients, intercept) = back_transform(
solution.coefficients,
solution.intercept,
&preprocessing,
solution.iterations,
)?;
Ok(LassoFit {
working_set: solution.working_set,
coefficients,
intercept,
preprocessing,
termination: solution.termination,
iterations: solution.iterations,
objective: solution.objective,
kkt_violation: solution.kkt_violation,
dual_certificate: solution.dual_certificate,
dual_objective: solution.dual_objective,
duality_gap: solution.duality_gap,
reference_loss: solution.reference_loss,
stopping_criterion: solution.stopping_criterion,
stopping_value: solution.stopping_value,
stopping_threshold: solution.stopping_threshold,
})
}
}