use lazymatrix::RawColumns;
use crate::{FitError, Preprocessing, StoppingCriterion, Termination};
#[derive(Clone, Debug)]
#[must_use]
pub struct IrlsFit {
coefficients: Vec<f64>,
intercept: f64,
preprocessing: Preprocessing,
solution: Diagnostics,
}
#[derive(Clone, Debug)]
struct Diagnostics {
working_set: crate::WorkingSetDiagnostics,
iterations: usize,
coordinate_sweeps: usize,
objective: f64,
kkt_violation: f64,
threshold: f64,
termination: Termination,
}
impl IrlsFit {
pub fn working_set_diagnostics(&self) -> &crate::WorkingSetDiagnostics {
&self.solution.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.solution.termination
}
pub fn iterations(&self) -> usize {
self.solution.iterations
}
pub fn coordinate_sweeps(&self) -> usize {
self.solution.coordinate_sweeps
}
pub fn objective(&self) -> f64 {
self.solution.objective
}
pub fn kkt_violation(&self) -> f64 {
self.solution.kkt_violation
}
pub fn stopping_criterion(&self) -> StoppingCriterion {
StoppingCriterion::kkt_violation(self.solution.threshold)
}
pub fn stopping_value(&self) -> f64 {
self.solution.kkt_violation
}
pub fn stopping_threshold(&self) -> f64 {
self.solution.threshold
}
pub fn predict<M: RawColumns<f64> + ?Sized>(&self, x: &M) -> Result<Vec<f64>, FitError> {
super::predict(&self.coefficients, self.intercept, self.iterations(), x)
}
pub(crate) fn from_solution<E>(
solution: crate::solver::irls::Solution,
preprocessing: Preprocessing,
) -> Result<Self, FitError<E>> {
let (coefficients, intercept) = super::back_transform(
solution.coefficients,
solution.intercept,
&preprocessing,
solution.iterations,
)?;
Ok(Self {
coefficients,
intercept,
preprocessing,
solution: Diagnostics {
working_set: solution.working_set,
iterations: solution.iterations,
coordinate_sweeps: solution.coordinate_sweeps,
objective: solution.objective,
kkt_violation: solution.kkt_violation,
threshold: solution.threshold,
termination: solution.termination,
},
})
}
}