Implementation:Avhz RustQuant LassoRegression
| Knowledge Sources | |
|---|---|
| Domains | Machine_Learning, Regression, Quantitative_Finance |
| Last Updated | 2026-02-07 19:00 GMT |
Overview
Concrete tool for Lasso (L1-regularized) regression provided by the RustQuant library.
Description
The Lasso regression module implements the Least Absolute Shrinkage and Selection Operator, a linear regression variant that applies L1 regularization to the coefficients. This encourages sparsity in the fitted model, effectively performing feature selection by driving some coefficients to exactly zero.
The implementation uses coordinate descent optimization. For each feature j, the algorithm computes the partial residual correlation rho and applies the soft-thresholding operator:
- If
rho < -lambda: coefficient =(rho + lambda) / (col_norm / n) - If
rho > lambda: coefficient =(rho - lambda) / (col_norm / n) - Otherwise: coefficient = 0 (feature is eliminated)
The algorithm iterates over all features in each pass and repeats until the maximum absolute change in any coefficient falls below the specified tolerance, or the maximum number of iterations is reached.
When fit_intercept is true, the features are mean-centered before fitting, and the intercept is recovered afterwards as y_mean - feature_means . coefficients.
Key structs:
LassoInput-- Holds the features matrix, response vector, regularization parameter lambda, intercept flag, max iterations, and convergence tolerance.LassoOutput-- Contains the fitted intercept and coefficient vector (intercept prepended at index 0).
Usage
Use Lasso regression when you need a linear model with built-in feature selection, particularly when many features may be irrelevant or when you want a sparse, interpretable model. It is commonly applied in high-dimensional settings such as factor model construction in quantitative finance.
Code Reference
Source Location
- Repository: RustQuant
- File: crates/RustQuant_ml/src/lasso.rs
- Lines: 1-290
Signature
#[derive(Clone, Debug)]
pub struct LassoInput<T> {
pub x: DMatrix<T>,
pub y: DVector<T>,
pub lambda: T,
pub fit_intercept: bool,
pub max_iter: usize,
pub tolerance: T,
}
#[derive(Clone, Debug)]
pub struct LassoOutput<T> {
pub intercept: T,
pub coefficients: DVector<T>,
}
impl LassoInput<f64> {
pub fn new(
x: DMatrix<f64>, y: DVector<f64>, lambda: f64,
fit_intercept: bool, max_iter: usize, tolerance: f64,
) -> Self;
pub fn fit(&self) -> Result<LassoOutput<f64>, RustQuantError>;
}
impl LassoOutput<f64> {
pub fn predict(&self, input: DMatrix<f64>) -> Result<DVector<f64>, RustQuantError>;
}
Import
use RustQuant::ml::{LassoInput, LassoOutput};
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| x | DMatrix<f64> |
Yes | Features matrix. Rows are samples; columns are features. |
| y | DVector<f64> |
Yes | Response vector (continuous target values). |
| lambda | f64 |
Yes | L1 regularization parameter. Larger values increase sparsity. |
| fit_intercept | bool |
Yes | Whether to fit an intercept term by mean-centering. |
| max_iter | usize |
Yes | Maximum number of coordinate descent iterations. |
| tolerance | f64 |
Yes | Convergence tolerance on max absolute coefficient change. |
Outputs
| Name | Type | Description |
|---|---|---|
| intercept | f64 |
The fitted intercept value. |
| coefficients | DVector<f64> |
Fitted coefficients with intercept at index 0. |
| predictions | DVector<f64> |
Predicted values from predict.
|
Usage Examples
use nalgebra::{DMatrix, DVector};
use RustQuant::ml::{LassoInput, LassoOutput};
let x_train = DMatrix::from_row_slice(4, 3, &[
-0.084, -0.633, -0.399,
-0.983, 1.091, -0.468,
-1.875, -0.914, 0.327,
-0.186, 1.002, -0.413,
]);
let y_train = DVector::from_row_slice(&[-0.445, -1.848, -0.629, -0.861]);
let input = LassoInput {
x: x_train,
y: y_train,
lambda: 0.01,
fit_intercept: true,
max_iter: 1000,
tolerance: 1e-4,
};
let output = input.fit().expect("Failed to fit Lasso model");
println!("Intercept: {}", output.intercept);
println!("Coefficients: {:?}", output.coefficients);
// Predict on new data
let x_test = DMatrix::from_row_slice(2, 3, &[
0.562, 0.596, -0.412,
0.663, 0.452, -0.294,
]);
let predictions = output.predict(x_test).unwrap();