Implementation:Avhz RustQuant LogisticRegression
| Knowledge Sources | |
|---|---|
| Domains | Machine_Learning, Classification, Quantitative_Finance |
| Last Updated | 2026-02-07 19:00 GMT |
Overview
Concrete tool for logistic regression (binary classification) provided by the RustQuant library.
Description
The logistic regression module provides structs and methods for fitting a binary logistic regression model and making predictions. The implementation centers on two primary structs:
LogisticRegressionInput-- Holds the design matrixxand the binary response vectory(values must be 0 or 1). A column of ones is automatically prepended to account for the intercept.LogisticRegressionOutput-- Stores the fitted coefficients (including intercept as the first element) and the number of iterations required for convergence.
Two fitting algorithms are defined in the LogisticRegressionAlgorithm enum:
- IRLS (Iteratively Reweighted Least Squares) -- Fully implemented. Equivalent to Newton's method for maximizing the Bernoulli log-likelihood. The implementation constructs the Hessian matrix and gradient at each step, solves via LU decomposition, and iterates until the norm of the coefficient change falls below the specified tolerance. It also detects linearly separable data and terminates early.
- MLE (Maximum Likelihood via Algorithmic Adjoint Differentiation) -- Declared but not yet implemented (
unimplemented!()).
The initial coefficient guess is computed geometrically: the algorithm finds the direction between class-conditional means and places the separating hyperplane at the weighted midpoint.
The output struct provides methods for:
predict-- Returns binary class predictions (0 or 1) using a 0.5 probability threshold.predict_proba-- Returns the raw probability P(Y=1|X) via the logistic (sigmoid) function.score_misclassification-- Computes the misclassification rate between true and predicted labels.score_cross_entropy-- Computes the average binary cross-entropy loss.
Usage
Use this implementation for binary classification tasks where you need a probabilistic model with interpretable coefficients. Logistic regression is appropriate when the relationship between features and the log-odds of the target class is approximately linear. It is commonly used in credit scoring, default prediction, and other quantitative finance classification problems.
Code Reference
Source Location
- Repository: RustQuant
- File: crates/RustQuant_ml/src/logistic_regression.rs
- Lines: 1-479
Signature
#[derive(Clone, Debug)]
pub struct LogisticRegressionInput<T> {
pub x: DMatrix<T>,
pub y: DVector<T>,
}
#[derive(Clone, Debug)]
pub struct LogisticRegressionOutput<T> {
pub coefficients: DVector<T>,
pub iterations: usize,
}
pub enum LogisticRegressionAlgorithm {
MLE,
IRLS,
}
impl LogisticRegressionInput<f64> {
pub fn new(x: DMatrix<f64>, y: DVector<f64>) -> Self;
pub fn fit(
&self,
method: LogisticRegressionAlgorithm,
tolerance: f64,
) -> Result<LogisticRegressionOutput<f64>, &'static str>;
}
impl LogisticRegressionOutput<f64> {
pub fn predict(&self, input: &DMatrix<f64>) -> DVector<f64>;
pub fn predict_proba(&self, input: &DMatrix<f64>) -> DVector<f64>;
pub fn score_misclassification(&self, y: &DVector<f64>, y_hat: &DVector<f64>) -> f64;
pub fn score_cross_entropy(&self, y: &DVector<f64>, p_hat: &DVector<f64>) -> f64;
}
Import
use RustQuant::ml::{LogisticRegressionInput, LogisticRegressionOutput, LogisticRegressionAlgorithm};
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| x | DMatrix<f64> |
Yes | Design matrix (features). Rows are samples; columns are features. No need to add an intercept column. |
| y | DVector<f64> |
Yes | Binary response vector. Values must be 0.0 or 1.0. |
| method | LogisticRegressionAlgorithm |
Yes (fit) | Algorithm to use: currently only IRLS is implemented. |
| tolerance | f64 |
Yes (fit) | Convergence tolerance for the norm of coefficient changes between iterations. |
Outputs
| Name | Type | Description |
|---|---|---|
| coefficients | DVector<f64> |
Fitted coefficients. The first element is the intercept (b0). |
| iterations | usize |
Number of IRLS iterations performed until convergence. |
| predictions | DVector<f64> |
Binary predictions (0.0 or 1.0) from predict.
|
| probabilities | DVector<f64> |
X) from predict_proba.
|
Usage Examples
use nalgebra::{DMatrix, DVector};
use RustQuant::ml::{LogisticRegressionInput, LogisticRegressionAlgorithm};
let x_train = DMatrix::from_row_slice(4, 3, &[
-1.207, 0.429, -0.564,
0.277, 0.506, -0.890,
1.084, -0.575, -0.477,
-2.346, -0.547, -0.998,
]);
let y_train = DVector::from_row_slice(&[0., 1., 1., 1.]);
let input = LogisticRegressionInput::new(x_train, y_train);
// Fit with IRLS algorithm
let output = input.fit(
LogisticRegressionAlgorithm::IRLS,
f64::EPSILON.sqrt(),
).expect("Failed to fit logistic regression");
println!("Coefficients: {:?}", output.coefficients);
println!("Iterations: {}", output.iterations);
// Predict on new data
let x_test = DMatrix::from_row_slice(2, 3, &[
-0.776, -0.511, 0.134,
0.064, -0.911, -0.491,
]);
let predictions = output.predict(&x_test);
let probabilities = output.predict_proba(&x_test);