Skip to Content
OchreML
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 in outliers_.

When to Use

  • Continuous regression datasets containing heavy-tailed noise or corrupted target outliers where LinearRegression or Ridge gets pulled off the true trend.
  • Workflows that also benefit from an explicit boolean outlier mask (outliers_) and estimated residual scale (scale_).

Hardware Backend Support

BackendPrimitives UsedSupport
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

ParameterTypeDefaultDescription
epsilonfloat1.35Outlier 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_iterint100Maximum number of L-BFGS iterations.
alphafloat0.0001Strength of the squared L2 regularization term. Must be >= 0.0.
fit_interceptboolTrueWhether to fit an unpenalized intercept term.
tolfloat1e-5Convergence tolerance for the L-BFGS solver.
devicestr | NoneNoneTarget compute device ("auto", "cpu", "cuda:0", "wgpu", "tpu:0"). Defaults to ochreml.get_default_device().

Attributes

AttributeTypeDescription
coef_ndarray of shape (n_features,)Estimated feature weights.
intercept_floatFitted bias term.
scale_floatEstimated residual scale factor sigma.
outliers_ndarray of shape (n_samples,) of boolBoolean mask indicating which training samples were classified as outliers.
n_iter_intNumber of L-BFGS iterations executed.
n_features_in_intNumber of features seen during fit.
device_strResolved hardware device string used by the estimator.

Methods

MethodSignatureDescription
fitfit(X, y, sample_weight=None) -> selfFit robust Huber regression model on (X, y).
predictpredict(X) -> ndarrayPredict continuous target values for input matrix X.
scorescore(X, y, sample_weight=None) -> floatReturn weighted coefficient of determination R^2.
toto(device: str) -> selfMove the estimator execution target to another hardware device.
save / loadmodel.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]))

On this page