Skip to main content

Formula-first generalized additive models with a high-performance Rust core

Project description

gamfit

PyPI Python Docs License

Formula-first generalized additive models for Python, backed by a Rust engine.

gamfit fits Gaussian, binomial, Poisson, and Gamma GLMs with smooth terms, random effects, bounded/constrained coefficients, location-scale extensions, survival likelihoods, and flexible/learnable links. Smoothing parameters are selected by REML or LAML. Posterior sampling uses NUTS where supported, and Gaussian Laplace for model classes without an exact NUTS path.

Geometric / manifold smooths handle predictor spaces that wrap, including circles, cylinders, tori, the sphere (intrinsic Wahba / spherical harmonic kernels), and periodic tensor products, with no seams or pole artefacts. The gallery's Möbius-looking example is a 4π-periodic double-cover parameterization of a Möbius embedding, not a twisted Möbius-strip predictor basis:

rotating recovery of a trefoil knot, latent-free loop, wobbly cylinder, lumpy sphere, bumpy torus, and Möbius double-cover from noisy 3-D point clouds

Docs: https://gamfit.readthedocs.io/

Install

uv add gamfit

Wheels for Linux (x86_64, aarch64), macOS (x86_64, Apple silicon), and Windows. No Rust toolchain required.

30-second tour

import gamfit

train = [
    {"y": 1.2, "x": 0.0},
    {"y": 1.9, "x": 1.0},
    {"y": 3.1, "x": 2.0},
    {"y": 4.5, "x": 3.0},
]

model = gamfit.fit(train, "y ~ s(x)")
print(model.predict([{"x": 1.5}, {"x": 2.5}], interval=0.95))
print(model.summary())
model.save("model.gam")

pandas, polars, pyarrow, numpy, dict-of-columns, and list-of-records inputs all work without conversion.

What's different

  • Three-part penalty structure. Each smooth gets separate penalties for magnitude, gradient, and curvature. Most GAM libraries use one or two.
  • Flexible link functions. Spline offsets from a base link (link(type=flexible(probit))), blended mixture links, and SAS / beta-logistic learnable shapes.
  • Surface smooths in arbitrary dimension. Thin-plate, Duchon (with triple-operator regularization), and Matérn covariance, with automatic knot placement.
  • Geometric / manifold smooths. Cyclic 1-D, cylinder / torus tensor, intrinsic sphere (Wahba + spherical harmonics), Möbius double-cover demo, boundary-conditioned B-splines. Predictor spaces that wrap or close are first-class — no seams, no pole artefacts.
  • Adaptive anisotropy. Per-axis spatial anisotropy shrinks or stretches each feature axis independently inside a single joint smooth.
  • Composable basis/kernel. Combine a spline kernel with a length-scale behaviour (e.g. Duchon kernel with Matérn-style global κ).
  • Marginal-slope models. Separate baseline risk from a calibrated score's effect, for both Bernoulli and survival outcomes.
  • Posterior sampling. model.sample(...) runs NUTS where supported, and Gaussian Laplace for model classes without an exact NUTS path, behind one API.

Highlights from the API

import gamfit
from gamfit.sklearn import GAMRegressor, GAMClassifier

# Validate before you fit
gamfit.validate_formula(train, "y ~ s(x) + group(site)")

# Posterior sampling and mean bands
posterior = model.sample(train, seed=42)
bands = posterior.predict(test, level=0.95)

# Survival
gamfit.fit(df,
    "Surv(entry, exit, event) ~ s(age) + bmi + timewiggle(internal_knots=6)",
    survival_likelihood="transformation",
    baseline_target="weibull",
)

# scikit-learn
est = GAMRegressor(formula="y ~ s(x)")
est.fit(X, y)

# Diagnose, plot, report
model.diagnose(train).metrics
model.plot(train, x="x", kind="prediction")
model.report("report.html")

Public API

Symbol Purpose
gamfit.fit(data, formula, **kwargs) Fit a model.
gamfit.load(path) / gamfit.loads(bytes) Reload a saved model.
gamfit.validate_formula(data, formula, ...) Type-check a formula without fitting.
gamfit.build_info() Native extension build metadata.
gamfit.explain_error(exc) Human-readable hint for a gamfit exception.
gamfit.Model Fitted-model handle: predict, summary, check, diagnose, plot, report, sample, save.
gamfit.SurvivalPrediction Per-row hazard / survival surface; on-demand evaluation.
gamfit.SamplingConfig, PosteriorSamples, PosteriorPredictive NUTS / posterior interface.
gamfit.sklearn.GAMRegressor / GAMClassifier scikit-learn estimators.

Full reference at https://gamfit.readthedocs.io/en/latest/api-reference/.

Optional extras

uv add "gamfit[pandas]"     # pandas + pyarrow input/output
uv add "gamfit[plot]"       # matplotlib-based plotting
uv add "gamfit[sklearn]"    # scikit-learn integration
uv add "gamfit[torch]"      # PyTorch bridge
uv add "gamfit[all]"        # everything

License

AGPL-3.0-or-later. See LICENSE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gamfit-0.1.92.tar.gz (3.1 MB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

gamfit-0.1.92-cp310-abi3-win_amd64.whl (13.7 MB view details)

Uploaded CPython 3.10+Windows x86-64

gamfit-0.1.92-cp310-abi3-musllinux_1_2_x86_64.whl (13.1 MB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ x86-64

gamfit-0.1.92-cp310-abi3-musllinux_1_2_aarch64.whl (11.4 MB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ ARM64

gamfit-0.1.92-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (13.0 MB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ x86-64

gamfit-0.1.92-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (11.3 MB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARM64

gamfit-0.1.92-cp310-abi3-macosx_11_0_arm64.whl (11.1 MB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

gamfit-0.1.92-cp310-abi3-macosx_10_12_x86_64.whl (12.4 MB view details)

Uploaded CPython 3.10+macOS 10.12+ x86-64

File details

Details for the file gamfit-0.1.92.tar.gz.

File metadata

  • Download URL: gamfit-0.1.92.tar.gz
  • Upload date:
  • Size: 3.1 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.5

File hashes

Hashes for gamfit-0.1.92.tar.gz
Algorithm Hash digest
SHA256 8994e7d927c09bbd6c11d21eeff2b90c3c34df8cc5d226ce7addc80a3abb12f7
MD5 0bf5429ff70734b4d7e73ffbc902af89
BLAKE2b-256 905ba67845614b9eefc697a707f6150c77d2ae6948809dbe73aec8c3d79ba509

See more details on using hashes here.

File details

Details for the file gamfit-0.1.92-cp310-abi3-win_amd64.whl.

File metadata

  • Download URL: gamfit-0.1.92-cp310-abi3-win_amd64.whl
  • Upload date:
  • Size: 13.7 MB
  • Tags: CPython 3.10+, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.5

File hashes

Hashes for gamfit-0.1.92-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 71c037c038e43c6c32475fa3a3d475acba492fc92cb420fab0a4e97b5a9fd6f0
MD5 8a880aa3ff285f0535c66c38b2f9fe48
BLAKE2b-256 c7ef6650e32031f431d02fc599d32e0a2bb25ecd4a584a053c34223e5b15c717

See more details on using hashes here.

File details

Details for the file gamfit-0.1.92-cp310-abi3-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for gamfit-0.1.92-cp310-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 2081b5262eb40818a995d823265713e099464e0419985c87bcebe3a422b3ce12
MD5 e54b25bda97f2e371c01e5580b576fe6
BLAKE2b-256 3334653f28e9c894c47ecc123cbcc0c238ac87d6952e6d3da288b160882fcc97

See more details on using hashes here.

File details

Details for the file gamfit-0.1.92-cp310-abi3-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for gamfit-0.1.92-cp310-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 1b1e8de034b0ebbc7a965f661d01b0396063c269e4e967513ee1f9b2fe11e76f
MD5 cc6c6fbdb2cfeb91dfdb099724e9f082
BLAKE2b-256 a794dcb7787856f9fdd729b10edd588071d15f234612985fff1b373e548cb54b

See more details on using hashes here.

File details

Details for the file gamfit-0.1.92-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for gamfit-0.1.92-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 65fab5e746113ab267a0a3d300135c30e46a2b1649e99b223c1eca8b0c408033
MD5 197f600275a62656ecb40f71574354af
BLAKE2b-256 252f3e7262a246d709965a40d962ae87ed5a499de1f1dde1bc60b48150b559a9

See more details on using hashes here.

File details

Details for the file gamfit-0.1.92-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for gamfit-0.1.92-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 0153b3946dbd5a13d28583d6d4a3c4831c4c85a47cacab80850eb06658989342
MD5 d8832867b79e3816a2ac983859f1bacc
BLAKE2b-256 cb1ccd7173237ffb95b36b1ade45223df83bd3b0a7acfd6a193a5878b02b72dc

See more details on using hashes here.

File details

Details for the file gamfit-0.1.92-cp310-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for gamfit-0.1.92-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 8a0569908ba079110dc7da00794a8320f134951bee21445ffe3d5ae0eb49a6c6
MD5 c90c6744d82b4da85a6db5a5b4af10de
BLAKE2b-256 b8a0fddfc47711d8ba1dd5c692bae15d8fb852f834a26ff4826a35dc333e48f0

See more details on using hashes here.

File details

Details for the file gamfit-0.1.92-cp310-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for gamfit-0.1.92-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 fc3f4fff6532119597242101ff68f06fa276271d65b27764488f64c645190a6d
MD5 b42c5f6ecc155b65e87fba639ee9a99b
BLAKE2b-256 7cfa42323ce1b07f30f5965b9db2afe6af07d9a864605018daeae55166f1eef0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page