Skip to Content
OchreML
Linear, GLM & Linear SVM

TweedieRegressor

Generalized Linear Model with a flexible Tweedie power variance family (Normal, Poisson, Compound Poisson-Gamma, Gamma, Inverse Gaussian).

Overview

TweedieRegressor fits an L2-regularized Generalized Linear Model (GLM) with a Tweedie distribution parameterized by power (p), where target variance is proportional to mean^p, using either an "identity" or "log" link function and hardware-accelerated L-BFGS:

  • power = 0.0: Normal distribution (link="auto" resolves to "identity").
  • power = 1.0: Poisson distribution (link="auto" resolves to "log").
  • 1.0 < power < 2.0: Compound Poisson-Gamma distribution for zero-inflated positive continuous targets (link="auto" resolves to "log").
  • power = 2.0: Gamma distribution (link="auto" resolves to "log").
  • power = 3.0: Inverse Gaussian distribution (link="auto" resolves to "log").

Values in (0.0, 1.0) are not valid Tweedie distributions and raise an error.

When to Use

  • Zero-inflated continuous targets such as total insurance loss (pure premium) or customer spend using power=1.5.
  • Unified GLM experimentation across Normal, Poisson, Compound Poisson-Gamma, and Gamma families with a single estimator class.

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 TweedieRegressor
# also available via:
from ochreml.linear_model import TweedieRegressor
from ochreml import TweedieRegressor

model = TweedieRegressor(
    power: float = 0.0,
    alpha: float = 1.0,
    fit_intercept: bool = True,
    link: str = "auto",
    max_iter: int = 100,
    tol: float = 1e-4,
    device: str | None = None,
)

Parameters

ParameterTypeDefaultDescription
powerfloat0.0Tweedie variance power parameter (0 Normal, 1 Poisson, (1, 2) Compound Poisson-Gamma, 2 Gamma, >= 2 heavier-tailed). Must not lie in (0, 1).
alphafloat1.0Constant multiplying the L2 regularization penalty. Must be >= 0.0.
fit_interceptboolTrueWhether to add an unpenalized intercept to the linear predictor.
link"auto" | "identity" | "log""auto"Link function mapping linear predictor to target mean ("auto" selects "identity" for power <= 0 and "log" for power > 0).
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.
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 Tweedie GLM on (X, y) with optional sample weights.
predictpredict(X) -> ndarrayPredict expected target values via the inverse link function.
scorescore(X, y, sample_weight=None) -> floatReturn the D^2 fraction of Tweedie deviance explained.
toto(device: str) -> selfMove the estimator execution target to another hardware device.
save / loadmodel.save(path) / TweedieRegressor.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 TweedieRegressor

rng = np.random.default_rng(23)
X = rng.standard_normal((400, 3))
# Compound Poisson-Gamma (zero-inflated non-negative target)
counts = rng.poisson(np.exp(0.4 * X[:, 0] - 0.3 * X[:, 1]))
y = np.array([rng.gamma(2.0, 1.5, size=c).sum() if c > 0 else 0.0 for c in counts])

model = TweedieRegressor(power=1.5, alpha=0.1, link="log", max_iter=200, device="auto")
model.fit(X, y)

preds = model.predict(X[:5])
print(f"Tweedie D^2 score: {model.score(X, y):.4f}, coef_: {model.coef_.round(3)}")

# Persist to .dit and reload
model.save("tweedie_glm.dit")
loaded = ochreml.load("tweedie_glm.dit", device="cpu")
assert np.allclose(preds, loaded.predict(X[:5]))

On this page