Skip to main content

ridge

Function ridge 

Source
pub fn ridge(
    x: &[Vec<f64>],
    y: &[f64],
    lambda: f64,
) -> Result<LinearModel, RegressionError>
Expand description

Fits a ridge (L2-regularised) linear model to x and y with penalty lambda.

Equivalent to scikit-learn’s Ridge(alpha=lambda, fit_intercept=True): the design and target are centred, the penalised normal equations (X_cᵀX_c + λI)β = X_cᵀy_c are solved for the centred slopes (so the penalty shrinks the slopes but never the intercept), and the intercept is recovered from the column means. With lambda == 0.0 this reduces to ols.

§Arguments

  • x — design matrix, one inner slice per observation; rows must share width.
  • y — target values, one per row of x.
  • lambda — L2 penalty strength λ ≥ 0; larger values shrink the slopes more.

§Returns

A LinearModel with the fitted intercept and shrunk per-column slopes.

§Errors

Returns RegressionError::InvalidPenalty if lambda is negative or non-finite, plus the same shape/singularity errors as ols.

§Examples

use stats_claw::algorithms::regression::ridge;

// A positive penalty shrinks the slope toward zero relative to OLS.
let x = vec![vec![0.0], vec![1.0], vec![2.0], vec![3.0]];
let y = vec![2.0, 5.0, 8.0, 11.0];
let model = ridge(&x, &y, 1.0)?;
let slope = model.coefficients().first().copied().unwrap_or(f64::NAN);
assert!(slope < 3.0 && slope > 0.0, "shrunk slope {slope}");