pub struct LassoFit { /* private fields */ }Expand description
A fitted lasso-regression model and its diagnostics.
Lasso solves min (1/2n)‖y − Xβ‖² + λ‖β_pen‖₁. Unlike ridge there is no
closed form, so this fits by cyclic coordinate descent with
soft-thresholding — the standard, well-conditioned algorithm (glmnet-style).
§Intercept and scaling
A detected constant column is an unpenalized intercept. Predictors are
standardized internally (centered and scaled to unit variance) before the
penalty is applied, then coefficients are transformed back to the original
scale; the intercept is recovered from the means. Because standardization is
internal, λ is on the standardized (1/2n)-objective scale — not comparable
to ridge’s λ.
§The natural diagnostic: the active set
Lasso’s defining behavior is that it drives coefficients exactly to zero.
The size of the surviving active_set is an unbiased
estimate of the model’s degrees of freedom (Zou, Hastie & Tibshirani, 2007),
which is what the information criteria here use as the parameter count.
Implementations§
Source§impl LassoFit
impl LassoFit
Sourcepub fn new(x: Array2<f64>, y: Array1<f64>, lambda: f64) -> Result<Self>
pub fn new(x: Array2<f64>, y: Array1<f64>, lambda: f64) -> Result<Self>
Fit lasso regression of y on X with penalty lambda ≥ 0 using
coordinate descent (default tolerance 1e-7, up to 10_000 sweeps).
§Errors
RegressionError::EmptyInput/RegressionError::ShapeMismatch.RegressionError::InvalidParameteriflambda < 0.RegressionError::NotConvergedif coordinate descent does not converge within the iteration budget.
Sourcepub fn with_options(
x: Array2<f64>,
y: Array1<f64>,
lambda: f64,
tol: f64,
max_iter: usize,
) -> Result<Self>
pub fn with_options( x: Array2<f64>, y: Array1<f64>, lambda: f64, tol: f64, max_iter: usize, ) -> Result<Self>
Like LassoFit::new but with an explicit convergence tolerance and
maximum number of coordinate-descent sweeps.
Sourcepub fn n_observations(&self) -> usize
pub fn n_observations(&self) -> usize
Number of observations.
Sourcepub fn n_parameters(&self) -> usize
pub fn n_parameters(&self) -> usize
Number of coefficients (design columns, intercept included).
Sourcepub fn has_intercept(&self) -> bool
pub fn has_intercept(&self) -> bool
Whether an unpenalized intercept is present.
Sourcepub fn iterations(&self) -> usize
pub fn iterations(&self) -> usize
Coordinate-descent sweeps taken to converge.
Sourcepub fn design_matrix(&self) -> ArrayView2<'_, f64>
pub fn design_matrix(&self) -> ArrayView2<'_, f64>
The design matrix as fitted.
Sourcepub fn coefficients(&self) -> ArrayView1<'_, f64>
pub fn coefficients(&self) -> ArrayView1<'_, f64>
Lasso coefficients, aligned to the design columns. Penalized coefficients
that were shrunk out are exactly 0.0.
Sourcepub fn fitted_values(&self) -> ArrayView1<'_, f64>
pub fn fitted_values(&self) -> ArrayView1<'_, f64>
Fitted values ŷ = Xβ.
Sourcepub fn residuals(&self) -> ArrayView1<'_, f64>
pub fn residuals(&self) -> ArrayView1<'_, f64>
Residuals y − ŷ.
Sourcepub fn residual_sum_of_squares(&self) -> f64
pub fn residual_sum_of_squares(&self) -> f64
Residual sum of squares.
Sourcepub fn response(&self) -> ArrayView1<'_, f64>
pub fn response(&self) -> ArrayView1<'_, f64>
Response vector.
Sourcepub fn active_set(&self) -> Vec<usize>
pub fn active_set(&self) -> Vec<usize>
Indices of the design columns with non-zero (surviving) coefficients — the active set. Excludes the intercept.
Sourcepub fn n_nonzero(&self) -> usize
pub fn n_nonzero(&self) -> usize
Number of non-zero penalized coefficients — the active-set size, which is lasso’s degrees-of-freedom estimate (Zou–Hastie–Tibshirani).
Sourcepub fn effective_df(&self) -> f64
pub fn effective_df(&self) -> f64
Effective degrees of freedom: the active-set size plus one for the intercept if present.
Sourcepub fn log_likelihood(&self) -> f64
pub fn log_likelihood(&self) -> f64
Gaussian log-likelihood at the fitted residual variance.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for LassoFit
impl RefUnwindSafe for LassoFit
impl Send for LassoFit
impl Sync for LassoFit
impl Unpin for LassoFit
impl UnsafeUnpin for LassoFit
impl UnwindSafe for LassoFit
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.