Implementation:Avhz RustQuant LinearRegression
| Knowledge Sources | |
|---|---|
| Domains | Machine_Learning, Regression, Quantitative_Finance |
| Last Updated | 2026-02-07 19:00 GMT |
Overview
Concrete tool for ordinary least squares (OLS) linear regression provided by the RustQuant library.
Description
The linear regression module provides a standard OLS implementation with support for three different matrix decomposition strategies via the Decomposition enum:
- None -- Naive normal equations approach: computes
(X^T X)^{-1} X^T ydirectly by inverting the Gram matrix. - QR -- QR decomposition: factors X = Q*R, then solves via
R^{-1} Q^T y. Generally the fastest option. - SVD -- Singular Value Decomposition: factors X = U*S*V^T, then computes the pseudo-inverse
V S^{-1} U^T y. Generally the most numerically stable option.
A column of ones is automatically prepended to the design matrix to fit the intercept term. The intercept is stored as the first element of the coefficients vector.
Key structs:
LinearRegressionInput-- Holds the design matrixxand response vectory.LinearRegressionOutput-- Contains the fitted intercept and coefficient vector.
Usage
Use this implementation for standard linear regression tasks. Choose the decomposition method based on your needs: QR for speed, SVD for stability with ill-conditioned matrices, or None for simplicity. Linear regression is widely used in quantitative finance for factor modeling, hedging ratios, and trend estimation.
Code Reference
Source Location
- Repository: RustQuant
- File: crates/RustQuant_ml/src/linear_regression.rs
- Lines: 1-285
Signature
#[derive(Clone, Debug)]
pub struct LinearRegressionInput<T> {
pub x: DMatrix<T>,
pub y: DVector<T>,
}
#[derive(Clone, Debug)]
pub struct LinearRegressionOutput<T> {
pub intercept: T,
pub coefficients: DVector<T>,
}
pub enum Decomposition {
None,
QR,
SVD,
}
impl LinearRegressionInput<f64> {
pub fn new(x: DMatrix<f64>, y: DVector<f64>) -> Self;
pub fn fit(
&self,
method: Decomposition,
) -> Result<LinearRegressionOutput<f64>, RustQuantError>;
}
impl LinearRegressionOutput<f64> {
pub fn predict(&self, input: DMatrix<f64>) -> Result<DVector<f64>, RustQuantError>;
}
Import
use RustQuant::ml::{LinearRegressionInput, LinearRegressionOutput, Decomposition};
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| x | DMatrix<f64> |
Yes | Design matrix (features). Rows are samples; columns are features. No intercept column needed. |
| y | DVector<f64> |
Yes | Response vector (continuous target values). |
| method | Decomposition |
Yes (fit) | Matrix decomposition strategy: None, QR, or SVD. |
Outputs
| Name | Type | Description |
|---|---|---|
| intercept | f64 |
The fitted intercept (b0). |
| coefficients | DVector<f64> |
Full coefficient vector with intercept at index 0. |
| predictions | DVector<f64> |
Predicted values Y = X * beta + intercept from predict.
|
Usage Examples
use nalgebra::{DMatrix, DVector};
use RustQuant::ml::{LinearRegressionInput, Decomposition};
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 = LinearRegressionInput::new(x_train, y_train);
// Fit using QR decomposition (fastest)
let output = input.fit(Decomposition::QR).expect("Failed to fit");
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();