Skip to main content

linreg_core/regularized/
lasso.rs

1//! Lasso regression (L1-regularized linear regression).
2//!
3//! This module provides a wrapper around the elastic net implementation with `alpha=1.0`.
4
5use crate::error::Result;
6use crate::linalg::Matrix;
7use crate::regularized::elastic_net::{elastic_net_fit, ElasticNetOptions};
8use crate::regularized::preprocess::predict;
9use crate::serialization::types::ModelType;
10use crate::impl_serialization;
11use serde::{Deserialize, Serialize};
12
13pub use crate::regularized::elastic_net::soft_threshold;
14
15/// Options for lasso regression fitting.
16///
17/// Configuration options for lasso regression (L1-regularized linear regression).
18///
19/// # Fields
20///
21/// - `lambda` - Regularization strength (≥ 0, higher = more sparsity)
22/// - `intercept` - Whether to include an intercept term
23/// - `standardize` - Whether to standardize predictors to unit variance
24/// - `max_iter` - Maximum coordinate descent iterations
25/// - `tol` - Convergence tolerance on coefficient changes
26/// - `penalty_factor` - Optional per-feature penalty multipliers
27/// - `warm_start` - Optional initial coefficient values for warm starts
28/// - `weights` - Optional observation weights
29///
30/// # Example
31///
32/// ```
33/// # use linreg_core::regularized::lasso::LassoFitOptions;
34/// let options = LassoFitOptions {
35///     lambda: 0.1,
36///     intercept: true,
37///     standardize: true,
38///     ..Default::default()
39/// };
40/// ```
41#[derive(Clone, Debug)]
42pub struct LassoFitOptions {
43    pub lambda: f64,
44    pub intercept: bool,
45    pub standardize: bool,
46    pub max_iter: usize,
47    pub tol: f64,
48    pub penalty_factor: Option<Vec<f64>>,
49    pub warm_start: Option<Vec<f64>>,
50    pub weights: Option<Vec<f64>>, // Observation weights
51}
52
53impl Default for LassoFitOptions {
54    fn default() -> Self {
55        LassoFitOptions {
56            lambda: 1.0,
57            intercept: true,
58            standardize: true,
59            max_iter: 100000,
60            tol: 1e-7, // Match ElasticNetOptions default
61            penalty_factor: None,
62            warm_start: None,
63            weights: None,
64        }
65    }
66}
67
68/// Result of a lasso regression fit.
69///
70/// Contains the fitted model coefficients, convergence information, and diagnostic metrics.
71///
72/// # Fields
73///
74/// - `lambda` - The regularization strength used
75/// - `intercept` - Intercept coefficient (never penalized)
76/// - `coefficients` - Slope coefficients (some may be exactly zero due to L1 penalty)
77/// - `fitted_values` - Predicted values on training data
78/// - `residuals` - Residuals (y - fitted_values)
79/// - `n_nonzero` - Number of non-zero coefficients (excluding intercept)
80/// - `iterations` - Number of coordinate descent iterations performed
81/// - `converged` - Whether the algorithm converged
82/// - `r_squared` - Coefficient of determination
83/// - `adj_r_squared` - Adjusted R²
84/// - `mse` - Mean squared error
85/// - `rmse` - Root mean squared error
86/// - `mae` - Mean absolute error
87/// - `log_likelihood` - Log-likelihood of the model (for model comparison)
88/// - `aic` - Akaike Information Criterion (lower = better)
89/// - `bic` - Bayesian Information Criterion (lower = better)
90///
91/// # Example
92///
93/// ```
94/// # use linreg_core::regularized::lasso::{lasso_fit, LassoFitOptions};
95/// # use linreg_core::linalg::Matrix;
96/// # let y = vec![2.0, 4.0, 6.0, 8.0];
97/// # let x = Matrix::new(4, 2, vec![1.0, 1.0, 1.0, 2.0, 1.0, 3.0, 1.0, 4.0]);
98/// # let options = LassoFitOptions { lambda: 0.01, intercept: true, standardize: true, ..Default::default() };
99/// let fit = lasso_fit(&x, &y, &options).unwrap();
100///
101/// // Check convergence and sparsity
102/// println!("Converged: {}", fit.converged);
103/// println!("Non-zero coefficients: {}", fit.n_nonzero);
104/// println!("Iterations: {}", fit.iterations);
105///
106/// // Access model coefficients
107/// println!("Intercept: {}", fit.intercept);
108/// println!("Slopes: {:?}", fit.coefficients);
109/// println!("AIC: {}", fit.aic);
110/// # Ok::<(), linreg_core::Error>(())
111/// ```
112#[derive(Clone, Debug, Serialize, Deserialize)]
113pub struct LassoFit {
114    pub lambda: f64,
115    pub intercept: f64,
116    pub coefficients: Vec<f64>,
117    pub fitted_values: Vec<f64>,
118    pub residuals: Vec<f64>,
119    pub n_nonzero: usize,
120    pub iterations: usize,
121    pub converged: bool,
122    pub r_squared: f64,
123    pub adj_r_squared: f64,
124    pub mse: f64,
125    pub rmse: f64,
126    pub mae: f64,
127    pub log_likelihood: f64,
128    pub aic: f64,
129    pub bic: f64,
130}
131
132/// Fits lasso regression for a single lambda value.
133///
134/// Lasso regression adds an L1 penalty to the coefficients, which performs
135/// automatic variable selection by shrinking some coefficients to exactly zero.
136/// The intercept is never penalized.
137///
138/// # Arguments
139///
140/// * `x` - Design matrix (n rows × p columns including intercept)
141/// * `y` - Response variable (n observations)
142/// * `options` - Configuration options for lasso regression
143///
144/// # Returns
145///
146/// A `LassoFit` containing coefficients, convergence info, and metrics.
147///
148/// # Example
149///
150/// ```
151/// # use linreg_core::regularized::lasso::{lasso_fit, LassoFitOptions};
152/// # use linreg_core::linalg::Matrix;
153/// let y = vec![2.0, 4.0, 6.0, 8.0];
154/// let x = Matrix::new(4, 2, vec![1.0, 1.0, 1.0, 2.0, 1.0, 3.0, 1.0, 4.0]);
155///
156/// let options = LassoFitOptions {
157///     lambda: 0.01,
158///     intercept: true,
159///     standardize: true,
160///     ..Default::default()
161/// };
162///
163/// let fit = lasso_fit(&x, &y, &options).unwrap();
164/// assert!(fit.converged);
165/// assert!(fit.n_nonzero <= 1); // At most 1 non-zero coefficient
166/// # Ok::<(), linreg_core::Error>(())
167/// ```
168pub fn lasso_fit(x: &Matrix, y: &[f64], options: &LassoFitOptions) -> Result<LassoFit> {
169    let en_options = ElasticNetOptions {
170        lambda: options.lambda,
171        alpha: 1.0, // Lasso
172        intercept: options.intercept,
173        standardize: options.standardize,
174        max_iter: options.max_iter,
175        tol: options.tol,
176        penalty_factor: options.penalty_factor.clone(),
177        warm_start: options.warm_start.clone(),
178        weights: options.weights.clone(),
179        coefficient_bounds: None,
180    };
181
182    let fit = elastic_net_fit(x, y, &en_options)?;
183
184    Ok(LassoFit {
185        lambda: fit.lambda,
186        intercept: fit.intercept,
187        coefficients: fit.coefficients,
188        fitted_values: fit.fitted_values,
189        residuals: fit.residuals,
190        n_nonzero: fit.n_nonzero,
191        iterations: fit.iterations,
192        converged: fit.converged,
193        r_squared: fit.r_squared,
194        adj_r_squared: fit.adj_r_squared,
195        mse: fit.mse,
196        rmse: fit.rmse,
197        mae: fit.mae,
198        log_likelihood: fit.log_likelihood,
199        aic: fit.aic,
200        bic: fit.bic,
201    })
202}
203
204/// Makes predictions using a lasso regression fit.
205///
206/// Computes predictions for new observations using the fitted lasso regression model.
207///
208/// # Arguments
209///
210/// * `fit` - Fitted lasso regression model
211/// * `x_new` - New design matrix (same number of columns as training data)
212///
213/// # Returns
214///
215/// Vector of predicted values.
216///
217/// # Example
218///
219/// ```
220/// # use linreg_core::regularized::lasso::{lasso_fit, predict_lasso, LassoFitOptions};
221/// # use linreg_core::linalg::Matrix;
222/// // Training data
223/// let y = vec![2.0, 4.0, 6.0, 8.0];
224/// let x = Matrix::new(4, 2, vec![1.0, 1.0, 1.0, 2.0, 1.0, 3.0, 1.0, 4.0]);
225///
226/// let options = LassoFitOptions {
227///     lambda: 0.01,
228///     intercept: true,
229///     standardize: true,
230///     ..Default::default()
231/// };
232/// let fit = lasso_fit(&x, &y, &options).unwrap();
233///
234/// // Predict on new data
235/// let x_new = Matrix::new(2, 2, vec![1.0, 5.0, 1.0, 6.0]);
236/// let predictions = predict_lasso(&fit, &x_new);
237///
238/// assert_eq!(predictions.len(), 2);
239/// // Predictions should be close to [10.0, 12.0] for the linear relationship y = 2*x
240/// # Ok::<(), linreg_core::Error>(())
241/// ```
242pub fn predict_lasso(fit: &LassoFit, x_new: &Matrix) -> Vec<f64> {
243    predict(x_new, fit.intercept, &fit.coefficients)
244}
245
246// ============================================================================
247// Model Serialization Traits
248// ============================================================================
249
250// Generate ModelSave and ModelLoad implementations using macro
251impl_serialization!(LassoFit, ModelType::Lasso, "Lasso");
252
253#[cfg(test)]
254mod tests {
255    use super::*;
256
257    #[test]
258    fn test_soft_threshold() {
259        assert_eq!(soft_threshold(5.0, 2.0), 3.0);
260        assert_eq!(soft_threshold(-5.0, 2.0), -3.0);
261        assert_eq!(soft_threshold(1.0, 2.0), 0.0);
262    }
263
264    #[test]
265    fn test_lasso_fit_simple() {
266        // Simple test: y = 2*x with perfect linear relationship
267        let x_data = vec![1.0, 1.0, 1.0, 2.0, 1.0, 3.0, 1.0, 4.0];
268        let x = Matrix::new(4, 2, x_data);
269        let y = vec![2.0, 4.0, 6.0, 8.0];
270
271        let options = LassoFitOptions {
272            lambda: 0.01,
273            intercept: true,
274            standardize: true,
275            ..Default::default()
276        };
277
278        let fit = lasso_fit(&x, &y, &options).unwrap();
279
280        assert!(fit.converged);
281        // Predictions should be close to actual values
282        for i in 0..4 {
283            assert!((fit.fitted_values[i] - y[i]).abs() < 0.5);
284        }
285    }
286}