Implementation:Avhz RustQuant RidgeRegression
| Knowledge Sources | |
|---|---|
| Domains | Machine_Learning, Regression, Quantitative_Finance |
| Last Updated | 2026-02-07 19:00 GMT |
Overview
Concrete tool for Ridge (L2-regularized) regression provided by the RustQuant library.
Description
The Ridge regression module implements Tikhonov regularization (L2 penalty) for linear regression. It adds a penalty term lambda * ||beta||^2 to the ordinary least squares objective, which shrinks coefficients toward zero and helps prevent overfitting, especially when features are correlated (multicollinearity).
The closed-form solution is computed as:
beta = (X^T X + lambda * I)^{-1} X^T y
When fit_intercept is true, a column of ones is prepended to the design matrix, and the regularization matrix is modified so that the intercept term is not penalized (the (0,0) entry of the identity matrix is set to 0). When fit_intercept is false, the intercept is set to 0 and all coefficients are penalized equally.
Key structs:
RidgeRegressionInput-- Holds the features matrix, response vector, regularization parameter lambda, intercept flag, max iterations, and tolerance.RidgeRegressionOutput-- Contains the fitted intercept and coefficient vector (intercept at index 0).
Usage
Use Ridge regression when you have multicollinear features or when you want to regularize a linear model to improve out-of-sample prediction. Unlike Lasso, Ridge regression does not produce sparse models but shrinks all coefficients proportionally. It is commonly used in quantitative finance for regularized portfolio optimization and factor model estimation.
Code Reference
Source Location
- Repository: RustQuant
- File: crates/RustQuant_ml/src/ridge_regression.rs
- Lines: 1-256
Signature
#[derive(Clone, Debug)]
pub struct RidgeRegressionInput<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 RidgeRegressionOutput<T> {
pub intercept: T,
pub coefficients: DVector<T>,
}
impl RidgeRegressionInput<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<RidgeRegressionOutput<f64>, RustQuantError>;
}
impl RidgeRegressionOutput<f64> {
pub fn predict(&self, input: DMatrix<f64>) -> Result<DVector<f64>, RustQuantError>;
}
Import
use RustQuant::ml::{RidgeRegressionInput, RidgeRegressionOutput};
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 | L2 regularization parameter. Larger values increase shrinkage. |
| fit_intercept | bool |
Yes | Whether to fit an intercept (unpenalized). |
| max_iter | usize |
Yes | Maximum number of iterations (reserved for iterative solvers). |
| tolerance | f64 |
Yes | Convergence tolerance (reserved for iterative solvers). |
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::{RidgeRegressionInput, RidgeRegressionOutput};
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 = RidgeRegressionInput {
x: x_train,
y: y_train,
lambda: 1.0,
fit_intercept: true,
max_iter: 1000,
tolerance: 1e-4,
};
let output = input.fit().expect("Failed to fit Ridge 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();