Linear, GLM & Linear SVM
GammaRegressor
Generalized Linear Model with a Gamma distribution and log link for strictly positive, right-skewed continuous targets.
Overview
GammaRegressor fits an L2-regularized Generalized Linear Model (GLM) with a Gamma target distribution and log link function, predicting strictly positive expectations y_hat = exp(X @ w + b). It is equivalent to TweedieRegressor(power=2.0, link="log") and minimizes L2-penalized Gamma deviance via hardware-accelerated L-BFGS.
Target values y must be strictly positive (y > 0).
When to Use
- Strictly positive, right-skewed continuous outcomes where target variance grows quadratically with the mean (such as claim severity, waiting times, latency, or pricing).
- Multiplicative feature effects where log-link predictions naturally guarantee positive outputs.
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 GammaRegressor
# also available via:
from ochreml.linear_model import GammaRegressor
from ochreml import GammaRegressor
model = GammaRegressor(
alpha: float = 1.0,
fit_intercept: bool = True,
max_iter: int = 100,
tol: float = 1e-4,
device: str | None = None,
)Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
alpha | float | 1.0 | Constant multiplying the L2 regularization penalty. Must be >= 0.0. |
fit_intercept | bool | True | Whether to add an unpenalized intercept to the linear predictor. |
max_iter | int | 100 | Maximum number of L-BFGS solver iterations. |
tol | float | 1e-4 | Stopping 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 coefficients for the linear predictor X @ w + b. |
intercept_ | float | Intercept added to the linear predictor. |
n_iter_ | int | Actual number of L-BFGS iterations used. |
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 Gamma GLM on strictly positive targets y > 0. |
predict | predict(X) -> ndarray | Predict positive expected values exp(X @ coef_ + intercept_). |
score | score(X, y, sample_weight=None) -> float | Return the D^2 fraction of Gamma deviance explained. |
to | to(device: str) -> self | Move the estimator execution target to another hardware device. |
save / load | model.save(path) / GammaRegressor.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 GammaRegressor
rng = np.random.default_rng(11)
X = rng.standard_normal((400, 3))
mu = np.exp(0.5 * X[:, 0] - 0.3 * X[:, 1] + 0.4 * X[:, 2] + 1.0)
shape = 4.0
y = rng.gamma(shape=shape, scale=mu / shape)
model = GammaRegressor(alpha=0.1, max_iter=200, device="auto")
model.fit(X, y)
preds = model.predict(X[:5])
print(f"Gamma D^2 score: {model.score(X, y):.4f}, coef_: {model.coef_.round(3)}")
# Persist to .dit and reload
model.save("gamma_glm.dit")
loaded = ochreml.load("gamma_glm.dit", device="cpu")
assert np.allclose(preds, loaded.predict(X[:5]))