linreg_core/regularized/
path.rs

1//! Lambda path generation for regularized regression.
2//!
3//! This module provides utilities for generating a sequence of lambda values
4//! for regularization paths, matching glmnet's approach.
5//!
6//! # Lambda Path Construction
7//!
8//! glmnet generates a lambda path from `lambda_max` down to `lambda_min`:
9//!
10//! - `lambda_max`: The smallest lambda for which all penalized coefficients are zero
11//! - `lambda_min`: `lambda_min_ratio * lambda_max`
12//!
13//! For pure ridge (`alpha=0`), `lambda_max` is theoretically infinite, so we use
14//! a small `alpha` value to compute a finite starting point.
15
16use crate::linalg::Matrix;
17
18/// Options for generating a lambda path.
19///
20/// # Fields
21///
22/// * `nlambda` - Number of lambda values (default: 100)
23/// * `lambda_min_ratio` - Ratio for smallest lambda (default: 0.0001 if n < p, else 0.01)
24/// * `alpha` - Elastic net mixing parameter (0 = ridge, 1 = lasso)
25/// * `eps_for_ridge` - Small alpha to use for computing lambda_max when alpha=0 (default: 0.001)
26#[derive(Clone, Debug)]
27pub struct LambdaPathOptions {
28    /// Number of lambda values to generate
29    pub nlambda: usize,
30    /// Minimum lambda as a fraction of maximum
31    pub lambda_min_ratio: Option<f64>,
32    /// Elastic net mixing parameter (0 = ridge, 1 = lasso)
33    pub alpha: f64,
34    /// Small alpha to use for ridge lambda_max computation
35    pub eps_for_ridge: f64,
36}
37
38impl Default for LambdaPathOptions {
39    fn default() -> Self {
40        LambdaPathOptions {
41            nlambda: 100,
42            lambda_min_ratio: None,
43            alpha: 1.0,
44            eps_for_ridge: 0.001,
45        }
46    }
47}
48
49/// Computes `lambda_max`: the smallest lambda for which all penalized coefficients are zero.
50///
51/// For lasso (alpha > 0), lambda_max is the smallest value such that the soft-thresholding
52/// operation zeros out all coefficients.
53///
54/// For standardized X and centered y:
55/// ```text
56/// lambda_max = max_j |x_j^T y| / (n * alpha)
57/// ```
58///
59/// # Arguments
60///
61/// * `x` - Standardized design matrix (n × p), first column is intercept if present
62/// * `y` - Centered response vector (n elements)
63/// * `alpha` - Elastic net mixing parameter
64/// * `penalty_factor` - Per-feature penalty factors (optional, defaults to all 1.0)
65/// * `intercept_col` - Index of intercept column (typically 0, or None if no intercept)
66///
67/// # Returns
68///
69/// The maximum lambda value for the path.
70///
71/// # Note
72///
73/// If `alpha = 0` (pure ridge), this returns `f64::INFINITY` since ridge never produces
74/// exact zero coefficients. Use [`make_lambda_path`] which handles this case by using
75/// a small alpha value.
76pub fn compute_lambda_max(
77    x: &Matrix,
78    y: &[f64],
79    alpha: f64,
80    penalty_factor: Option<&[f64]>,
81    intercept_col: Option<usize>,
82) -> f64 {
83    if alpha <= 0.0 {
84        return f64::INFINITY;
85    }
86
87    let n = x.rows as f64;
88    let p = x.cols;
89
90    let mut max_corr: f64 = 0.0;
91
92    for j in 0..p {
93        // Skip intercept column
94        if let Some(ic) = intercept_col {
95            if j == ic {
96                continue;
97            }
98        }
99
100        // Compute absolute correlation: |x_j^T y|
101        // Matrix is row-major, so we iterate through rows for each column
102        let mut corr = 0.0;
103        for i in 0..x.rows {
104            corr += x.get(i, j) * y[i];
105        }
106        let corr = corr.abs();
107
108        // Apply penalty factor if provided
109        let effective_corr = if let Some(pf) = penalty_factor {
110            if j < pf.len() && pf[j] > 0.0 {
111                corr / pf[j]
112            } else {
113                corr
114            }
115        } else {
116            corr
117        };
118
119        max_corr = max_corr.max(effective_corr);
120    }
121
122    max_corr / (n * alpha)
123}
124
125/// Generates a lambda path from lambda_max down to lambda_min.
126///
127/// This creates a logarithmically-spaced sequence of lambda values, matching
128/// glmnet's approach for regularization paths.
129///
130/// # Arguments
131///
132/// * `x` - Standardized design matrix (n × p)
133/// * `y` - Centered response vector (n elements)
134/// * `options` - Lambda path generation options
135/// * `penalty_factor` - Optional per-feature penalty factors
136/// * `intercept_col` - Index of intercept column (typically 0)
137///
138/// # Returns
139///
140/// A vector of lambda values in **decreasing** order (largest to smallest).
141///
142/// # Lambda Sequence
143///
144/// The lambda values are logarithmically spaced:
145/// ```text
146/// lambda[k] = lambda_max * exp(log(lambda_min_ratio) * k / (nlambda - 1))
147/// ```
148///
149/// For ridge (`alpha ≈ 0`), we use a small alpha value to compute a finite
150/// `lambda_max`, then use that lambda sequence for the actual ridge fit.
151///
152/// # Default lambda_min_ratio
153///
154/// Following glmnet:
155/// - If `n >= p`: `lambda_min_ratio = 0.0001`
156/// - If `n < p`: `lambda_min_ratio = 0.01`
157pub fn make_lambda_path(
158    x: &Matrix,
159    y: &[f64],
160    options: &LambdaPathOptions,
161    penalty_factor: Option<&[f64]>,
162    intercept_col: Option<usize>,
163) -> Vec<f64> {
164    let n = x.rows;
165    let p = x.cols;
166
167    // Determine default lambda_min_ratio
168    let default_min_ratio = if n >= p { 0.0001 } else { 0.01 };
169    let lambda_min_ratio = options.lambda_min_ratio.unwrap_or(default_min_ratio);
170
171    // For pure ridge (alpha = 0), use a small alpha to compute lambda_max
172    let alpha_for_lambda_max = if options.alpha <= 0.0 {
173        options.eps_for_ridge
174    } else {
175        options.alpha
176    };
177
178    // Compute lambda_max
179    let lambda_max = compute_lambda_max(x, y, alpha_for_lambda_max, penalty_factor, intercept_col);
180
181    // Handle the case where lambda_max is infinite or very small
182    if !lambda_max.is_finite() || lambda_max <= 0.0 {
183        // For ridge with no good lambda_max, return a default path
184        return (0..options.nlambda)
185            .map(|k| {
186                let t = k as f64 / (options.nlambda - 1) as f64;
187                10.0_f64.powf(2.0 * (1.0 - t)) // 10^2 down to 10^0
188            })
189            .collect();
190    }
191
192    let _lambda_min = lambda_min_ratio * lambda_max;
193
194    // Generate logarithmically spaced lambdas (decreasing)
195    (0..options.nlambda)
196        .map(|k| {
197            let t = k as f64 / (options.nlambda - 1) as f64;
198            lambda_max * (lambda_min_ratio.powf(t))
199        })
200        .collect()
201}
202
203/// Extracts a specific set of lambdas from a path.
204///
205/// This is useful when you want to evaluate at specific lambda values
206/// rather than using the full path.
207///
208/// # Arguments
209///
210/// * `full_path` - The complete lambda path (must be in decreasing order)
211/// * `indices` - Indices of lambdas to extract
212///
213/// # Returns
214///
215/// A new vector containing the specified lambda values.
216pub fn extract_lambdas(full_path: &[f64], indices: &[usize]) -> Vec<f64> {
217    indices.iter().map(|&i| full_path[i]).collect()
218}
219
220#[cfg(test)]
221mod tests {
222    use super::*;
223
224    #[test]
225    fn test_compute_lambda_max() {
226        // Simple test: X = [1, x], y = [1, 2, 3]
227        let x_data = vec![1.0, -1.0, 1.0, 0.0, 1.0, 1.0];
228        let x = Matrix::new(3, 2, x_data);
229        let y = vec![1.0, 2.0, 3.0];
230
231        // Center y
232        let y_mean: f64 = y.iter().sum::<f64>() / 3.0;
233        let y_centered: Vec<f64> = y.iter().map(|&yi| yi - y_mean).collect();
234
235        let lambda_max = compute_lambda_max(&x, &y_centered, 1.0, None, Some(0));
236
237        // x^T y for column 1: -1*0 + 0*0 + 1*0 = 0 (since y is centered)
238        // Actually y_centered = [-1, 0, 1], so x^T y = 1*(-1) + 0*0 + 1*1 = 0
239        // y = [1,2,3], y_mean = 2, y_centered = [-1, 0, 1]
240        // back checks...
241        // Column 1 of X: [-1, 0, 1]
242        // dot = (-1)*(-1) + 0*0 + 1*1 = 1 + 0 + 1 = 2
243        // lambda_max = 2 / (3 * 1) = 2/3
244        assert!((lambda_max - 2.0 / 3.0).abs() < 1e-10);
245    }
246
247    #[test]
248    fn test_make_lambda_path_decreasing() {
249        let x_data = vec![1.0, -1.0, 1.0, 0.0, 1.0, 1.0];
250        let x = Matrix::new(3, 2, x_data);
251        let y_centered = vec![-1.0, 0.0, 1.0];
252
253        let options = LambdaPathOptions {
254            nlambda: 10,
255            lambda_min_ratio: Some(0.01),
256            alpha: 1.0,
257            eps_for_ridge: 0.001,
258        };
259
260        let path = make_lambda_path(&x, &y_centered, &options, None, Some(0));
261
262        assert_eq!(path.len(), 10);
263
264        // Check that path is decreasing
265        for i in 1..path.len() {
266            assert!(path[i] < path[i - 1]);
267        }
268
269        // First value should be lambda_max, last should be lambda_max * 0.01
270        let lambda_max = 2.0 / 3.0;
271        assert!((path[0] - lambda_max).abs() < 1e-10);
272        assert!((path[9] - lambda_max * 0.01).abs() < 1e-10);
273    }
274
275    #[test]
276    fn test_lambda_max_ridge() {
277        let x_data = vec![1.0, -1.0, 1.0, 0.0, 1.0, 1.0];
278        let x = Matrix::new(3, 2, x_data);
279        let y = vec![-1.0, 0.0, 1.0];
280
281        // For alpha = 0 (ridge), lambda_max should be infinite
282        let lambda_max = compute_lambda_max(&x, &y, 0.0, None, Some(0));
283        assert!(lambda_max.is_infinite());
284    }
285
286    #[test]
287    fn test_extract_lambdas() {
288        let path = vec![10.0, 5.0, 2.5, 1.25, 0.625];
289        let indices = vec![0, 2, 4];
290        let extracted = extract_lambdas(&path, &indices);
291
292        assert_eq!(extracted, vec![10.0, 2.5, 0.625]);
293    }
294}