pub struct PenalizedLogisticFit { /* private fields */ }Expand description
A fitted ridge-penalized logistic regression — a penalized GLM — and its shrinkage-aware diagnostics.
Maximizes the L2-penalized log-likelihood
ℓ(β) − ½λ‖β_pen‖²
(a detected intercept column is left unpenalized), fit by penalized IRLS:
each Newton step solves (XᵀWX + λP) Δ = Xᵀ(y − p) − λPβ with
W = diag(pᵢ(1 − pᵢ)) and P the penalty selector. The penalty is the GLM
analogue of ridge — it tames separation and multicollinearity in logistic
regression, at the cost of biased-but-lower-variance coefficients.
§The diagnostic that changes: effective degrees of freedom
As in ridge OLS, the penalty means the model no longer spends p degrees of
freedom. The effective df is the trace of the penalized hat matrix,
df = tr[ (XᵀWX + λP)⁻¹ XᵀWX ],
which falls from p toward the unpenalized/intercept count as λ grows. It
replaces the raw parameter count in aic / bic,
and the coefficient covariance is the sandwich
(XᵀWX + λP)⁻¹ (XᵀWX) (XᵀWX + λP)⁻¹, not the naïve inverse information — so
the reported standard errors account for the shrinkage.
Implementations§
Source§impl PenalizedLogisticFit
impl PenalizedLogisticFit
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 ridge-penalized logistic regression of binary y on X with penalty
lambda ≥ 0 (default: up to 100 IRLS iterations, tolerance 1e-10).
At lambda = 0 this reproduces the ordinary
LogisticFit.
§Errors
RegressionError::EmptyInput/RegressionError::ShapeMismatch.RegressionError::InvalidParameteriflambda < 0.RegressionError::InvalidResponseifyis not0/1or is one class.RegressionError::RankDeficientif the penalized information is singular.RegressionError::NotConvergedif IRLS fails to converge.
Sourcepub fn with_options(
x: Array2<f64>,
y: Array1<f64>,
lambda: f64,
max_iter: usize,
tol: f64,
) -> Result<Self>
pub fn with_options( x: Array2<f64>, y: Array1<f64>, lambda: f64, max_iter: usize, tol: f64, ) -> Result<Self>
Like PenalizedLogisticFit::new with an explicit iteration cap and
tolerance.
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
IRLS iterations taken to converge.
Sourcepub fn design_matrix(&self) -> ArrayView2<'_, f64>
pub fn design_matrix(&self) -> ArrayView2<'_, f64>
The design matrix as fitted.
Sourcepub fn response(&self) -> ArrayView1<'_, f64>
pub fn response(&self) -> ArrayView1<'_, f64>
The binary response.
Sourcepub fn coefficients(&self) -> ArrayView1<'_, f64>
pub fn coefficients(&self) -> ArrayView1<'_, f64>
Penalized coefficients (log-odds scale), aligned to the design columns.
Sourcepub fn fitted_probabilities(&self) -> ArrayView1<'_, f64>
pub fn fitted_probabilities(&self) -> ArrayView1<'_, f64>
Fitted probabilities pᵢ = P(yᵢ = 1).
Sourcepub fn covariance(&self) -> ArrayView2<'_, f64>
pub fn covariance(&self) -> ArrayView2<'_, f64>
Sandwich coefficient covariance (XᵀWX + λP)⁻¹ (XᵀWX) (XᵀWX + λP)⁻¹.
Sourcepub fn log_likelihood(&self) -> f64
pub fn log_likelihood(&self) -> f64
Unpenalized log-likelihood at the penalized estimate.
Sourcepub fn effective_df(&self) -> f64
pub fn effective_df(&self) -> f64
Effective degrees of freedom tr[(XᵀWX + λP)⁻¹ XᵀWX] — falls from p
toward the intercept count as λ grows.
Sourcepub fn coefficient_standard_errors(&self) -> Array1<f64>
pub fn coefficient_standard_errors(&self) -> Array1<f64>
Coefficient standard errors from the sandwich covariance.
Sourcepub fn residual_deviance(&self) -> f64
pub fn residual_deviance(&self) -> f64
Residual deviance −2ℓ (unpenalized log-likelihood).
Trait Implementations§
Source§impl Clone for PenalizedLogisticFit
impl Clone for PenalizedLogisticFit
Source§fn clone(&self) -> PenalizedLogisticFit
fn clone(&self) -> PenalizedLogisticFit
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for PenalizedLogisticFit
impl RefUnwindSafe for PenalizedLogisticFit
impl Send for PenalizedLogisticFit
impl Sync for PenalizedLogisticFit
impl Unpin for PenalizedLogisticFit
impl UnsafeUnpin for PenalizedLogisticFit
impl UnwindSafe for PenalizedLogisticFit
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.