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

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

ParameterTypeDefaultDescription
alphafloat1.0Constant multiplying the L2 regularization penalty. Must be >= 0.0.
fit_interceptboolTrueWhether to add an unpenalized intercept to the linear predictor.
max_iterint100Maximum number of L-BFGS solver iterations.
tolfloat1e-4Stopping 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 coefficients for the linear predictor X @ w + b.
intercept_floatIntercept added to the linear predictor.
n_iter_intActual number of L-BFGS iterations used.
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 Gamma GLM on strictly positive targets y > 0.
predictpredict(X) -> ndarrayPredict positive expected values exp(X @ coef_ + intercept_).
scorescore(X, y, sample_weight=None) -> floatReturn the D^2 fraction of Gamma deviance explained.
toto(device: str) -> selfMove the estimator execution target to another hardware device.
save / loadmodel.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]))

On this page