Linear, GLM & Linear SVM
HuberRegressor
Outlier-robust L2-regularized linear regression jointly optimizing weights, intercept, and scale via L-BFGS.
Overview
HuberRegressor fits an L2-regularized linear regression model that is robust to outliers in the target variable. It optimizes Owen's Huber loss jointly over weights w, intercept b, and residual scale sigma using hardware-accelerated L-BFGS:
- Samples with small scaled residuals (
|y - y_hat| / sigma <= epsilon) incur quadratic (L2) loss. - Samples with large scaled residuals (
|y - y_hat| / sigma > epsilon) incur linear (L1) loss, limiting outlier influence and flagging them inoutliers_.
When to Use
- Continuous regression datasets containing heavy-tailed noise or corrupted target outliers where
LinearRegressionorRidgegets pulled off the true trend. - Workflows that also benefit from an explicit boolean outlier mask (
outliers_) and estimated residual scale (scale_).
Hardware Backend Support
| Backend | Primitives Used | Support |
|---|---|---|
| CPU (Rayon) | Dense E1-E5, N2 (f64) | Full (default fallback) |
CUDA (cuda:0, cuda:all) | cuBLAS FP64 matvec & L-BFGS gradients (E1-E5, N2) | Full |
wgpu (wgpu) | WGSL compute shaders (E1-E5, N2) | Full |
TPU (tpu:0) | OpenXLA PJRT (E1-E5, N2) | Full |
Import & Constructor
from ochreml.glm import HuberRegressor
# also available via:
from ochreml.linear_model import HuberRegressor
from ochreml import HuberRegressor
model = HuberRegressor(
epsilon: float = 1.35,
max_iter: int = 100,
alpha: float = 0.0001,
fit_intercept: bool = True,
tol: float = 1e-5,
device: str | None = None,
)Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
epsilon | float | 1.35 | Outlier threshold controlling transition from quadratic to linear loss. Must be >= 1.0. Smaller values increase robustness; 1.35 gives 95% statistical efficiency on Gaussian noise. |
max_iter | int | 100 | Maximum number of L-BFGS iterations. |
alpha | float | 0.0001 | Strength of the squared L2 regularization term. Must be >= 0.0. |
fit_intercept | bool | True | Whether to fit an unpenalized intercept term. |
tol | float | 1e-5 | Convergence tolerance for the L-BFGS solver. |
device | str | None | None | Target compute device ("auto", "cpu", "cuda:0", "wgpu", "tpu:0"). Defaults to ochreml.get_default_device(). |
Attributes
| Attribute | Type | Description |
|---|---|---|
coef_ | ndarray of shape (n_features,) | Estimated feature weights. |
intercept_ | float | Fitted bias term. |
scale_ | float | Estimated residual scale factor sigma. |
outliers_ | ndarray of shape (n_samples,) of bool | Boolean mask indicating which training samples were classified as outliers. |
n_iter_ | int | Number of L-BFGS iterations executed. |
n_features_in_ | int | Number of features seen during fit. |
device_ | str | Resolved hardware device string used by the estimator. |
Methods
| Method | Signature | Description |
|---|---|---|
fit | fit(X, y, sample_weight=None) -> self | Fit robust Huber regression model on (X, y). |
predict | predict(X) -> ndarray | Predict continuous target values for input matrix X. |
score | score(X, y, sample_weight=None) -> float | Return weighted coefficient of determination R^2. |
to | to(device: str) -> self | Move the estimator execution target to another hardware device. |
save / load | model.save(path) / HuberRegressor.load(path, device=None) | Persist or restore the model in OchreML's binary .dit format. |
Usage Example
import numpy as np
import ochreml
from ochreml.glm import HuberRegressor
rng = np.random.default_rng(42)
X = rng.standard_normal((250, 3))
y = X @ np.array([2.0, -1.5, 0.8]) + 1.0 + rng.normal(0.0, 0.1, size=250)
# Inject 10 severe target outliers
y[:10] += 50.0
model = HuberRegressor(epsilon=1.35, alpha=1e-4, device="auto")
model.fit(X, y)
preds = model.predict(X[10:15])
print(f"Detected outliers: {int(model.outliers_.sum())}, scale_: {model.scale_:.4f}")
print(f"Inlier R^2: {model.score(X[10:], y[10:]):.4f}")
# Persist to .dit and reload
model.save("huber.dit")
loaded = ochreml.load("huber.dit", device="cpu")
assert np.allclose(preds, loaded.predict(X[10:15]))