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, with a Gaussian Laplace fallback elsewhere.

Geometric / manifold smooths handle predictor spaces that wrap, including circles, cylinders, tori, the sphere (intrinsic Wahba / spherical harmonic kernels), and one-sided strips, with no seams or pole artefacts:

rotating recovery of a trefoil knot, latent-free loop, wobbly cylinder, lumpy sphere, bumpy torus, and Möbius strip 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 strip, 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, with a Gaussian Laplace fallback elsewhere, 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 predictive 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",
)

# 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[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.74.tar.gz (3.0 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.74-cp310-abi3-win_amd64.whl (13.1 MB view details)

Uploaded CPython 3.10+Windows x86-64

gamfit-0.1.74-cp310-abi3-musllinux_1_2_x86_64.whl (12.5 MB view details)

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

gamfit-0.1.74-cp310-abi3-musllinux_1_2_aarch64.whl (10.9 MB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ ARM64

gamfit-0.1.74-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (12.4 MB view details)

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

gamfit-0.1.74-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (10.7 MB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARM64

gamfit-0.1.74-cp310-abi3-macosx_11_0_arm64.whl (10.5 MB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

gamfit-0.1.74-cp310-abi3-macosx_10_12_x86_64.whl (11.8 MB view details)

Uploaded CPython 3.10+macOS 10.12+ x86-64

File details

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

File metadata

  • Download URL: gamfit-0.1.74.tar.gz
  • Upload date:
  • Size: 3.0 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.74.tar.gz
Algorithm Hash digest
SHA256 d3cdeb13e05a7d41a9bb457b113cf8ba59d35c9ec4f8a2d0d957da868ea401f4
MD5 90e23c94be61f6ed2e3771cf8dbec4e8
BLAKE2b-256 ac5677b0348cba6b1c6a41c6844daccc70383000829410b98b0c67893311add7

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gamfit-0.1.74-cp310-abi3-win_amd64.whl
  • Upload date:
  • Size: 13.1 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.74-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 d7546b9ede4d91cf6e9604ded32396e6b3417e86653668cebc5bf92ee03bcca5
MD5 d9bdf11d5f8450ef1002076144e1a5ca
BLAKE2b-256 61c8aefc920411697c23ab96822e5bc39233c9fc80fff5973dd375a2f4b399b7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.74-cp310-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 753053a15e5654a387db53cfccec9452f748ea823d3dabf5b773730c41a0dde4
MD5 548f21e77cc6876fc7d2ddcaeee5ed5a
BLAKE2b-256 07ac2d9feb59a3b492ddbf5e384397f52ae3471b028131e66f26d6e4771005a2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.74-cp310-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 07d9bd81dba4ad51109dc59b79de7ebc5e2e17b0e5d95040334d022c19e6eca4
MD5 b61a050521f7ed84635fd5239d6833bc
BLAKE2b-256 124207e0d5534b779729fcc7c85636ca34b72fb6b4fee62bd5300138af264d65

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.74-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 f61ce511f08f573fd62e9ce9ed7eb509522349ba973c69ce5a6006144afa199e
MD5 03e2b22a93f1265fcd392813c70e7ded
BLAKE2b-256 55d0c7940247e534da5ec96fefbd57b2d251b755a80fcfee1fce7c497b923897

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.74-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 bfddb78364834ea26b2eb07ec495f5ee90dee3ae65f873150c693c9d4f418ab2
MD5 0d215f220dd69c7c860077ef6dd92eff
BLAKE2b-256 7043fa6c4525c69ba7f49ef0824158f4ce0f09553d8bec1c5fd6b25eca814769

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.74-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 4cb01d3142cbe7307dc69bdf9d829a620823dcf80652a0637fee94d82f18266f
MD5 62cd8f8d522749abb823ea1823ea1b51
BLAKE2b-256 c730d1f14454bb463911d562ad8b2675359462c6f90693f70f565b6d0f09df9f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.74-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 3263a291676562b278a37a97e2bc7b2960cb450202c059f6f4042b8d35d7ecff
MD5 13dd9e8219ba33bde114bf5c2c5776cd
BLAKE2b-256 6148d72b46b4e6f5d391b3aa9392f64821499461d6a277bb0fa6d76ab8f2f326

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