use lazymatrix::RawColumns;
use super::{Preprocessing, Termination, back_transform, predict};
use crate::{LassoError, StoppingCriterion};
#[derive(Clone, Debug)]
#[must_use]
pub struct ProximalFit {
coefficients: Vec<f64>,
intercept: f64,
preprocessing: Preprocessing,
termination: Termination,
iterations: usize,
objective: f64,
stopping_criterion: StoppingCriterion,
stopping_value: f64,
stopping_threshold: f64,
step: f64,
certificate: Option<crate::DualCertificate>,
screening: crate::ScreeningDiagnostics,
}
impl ProximalFit {
pub fn dual_certificate(&self) -> Option<&[f64]> {
self.certificate
.as_ref()
.map(crate::DualCertificate::dual_vector)
}
pub fn dual_objective(&self) -> Option<f64> {
self.certificate
.as_ref()
.map(crate::DualCertificate::dual_objective)
}
pub fn duality_gap(&self) -> Option<f64> {
self.certificate
.as_ref()
.map(crate::DualCertificate::duality_gap)
}
pub fn screening_diagnostics(&self) -> &crate::ScreeningDiagnostics {
&self.screening
}
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 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 step_size(&self) -> f64 {
self.step
}
pub fn predict<M: RawColumns<f64> + ?Sized>(&self, x: &M) -> Result<Vec<f64>, LassoError> {
predict(&self.coefficients, self.intercept, self.iterations, x)
}
pub(crate) fn from_solution<E>(
solution: crate::solver::proximal::Solution,
preprocessing: Preprocessing,
) -> Result<Self, LassoError<E>> {
let (coefficients, intercept) = back_transform(
solution.coefficients,
solution.intercept,
&preprocessing,
solution.iterations,
)?;
Ok(Self {
coefficients,
intercept,
preprocessing,
termination: solution.termination,
iterations: solution.iterations,
objective: solution.objective,
stopping_criterion: solution.stopping_criterion,
stopping_value: solution.stopping_value,
stopping_threshold: solution.stopping_threshold,
step: solution.step,
certificate: solution.certificate,
screening: solution.screening,
})
}
}