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 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, 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.87.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.87-cp310-abi3-win_amd64.whl (13.2 MB view details)

Uploaded CPython 3.10+Windows x86-64

gamfit-0.1.87-cp310-abi3-musllinux_1_2_x86_64.whl (12.6 MB view details)

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

gamfit-0.1.87-cp310-abi3-musllinux_1_2_aarch64.whl (11.0 MB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ ARM64

gamfit-0.1.87-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (12.5 MB view details)

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

gamfit-0.1.87-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (10.8 MB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARM64

gamfit-0.1.87-cp310-abi3-macosx_11_0_arm64.whl (10.6 MB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

gamfit-0.1.87-cp310-abi3-macosx_10_12_x86_64.whl (11.9 MB view details)

Uploaded CPython 3.10+macOS 10.12+ x86-64

File details

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

File metadata

  • Download URL: gamfit-0.1.87.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.87.tar.gz
Algorithm Hash digest
SHA256 a008daca0e527fd85be32cfa466f3704bd35737d8a07a0edf804300a6ae36794
MD5 3b1563b008b364027332c914bad51ec0
BLAKE2b-256 fb3966b63d91f4b30349e2f5f0aded97b30b9f306533f9e649eb92c6c0de9d4e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gamfit-0.1.87-cp310-abi3-win_amd64.whl
  • Upload date:
  • Size: 13.2 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.87-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 db12392137ff9a7acd096fc7b5f2190acfc2dc3d3c8b7f70b3bb70f89bf09f11
MD5 7b87de0e956201500bbc4b826f0c2d43
BLAKE2b-256 28ce920e5de17bdf3bd82a6d2513feeff1f0681ad88cc1999726ff1afdbdf2ac

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.87-cp310-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 4352b94d2622aa4d82c40db015ccfcd96e92ee67dffbaa22a9fec427aa32c586
MD5 6d6679f28fc68ec1292d6c77fc6d8314
BLAKE2b-256 0729ecc1d028efe0ac88de901799dbb07ec64550a367aabd855c639825e75987

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.87-cp310-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 8d2f607525eba294c9ee4ee12b4c758ef3fc188db485dced294a591e665b7b85
MD5 cf6246bbc63643fc852ec94517a5c8e2
BLAKE2b-256 6929d53756a841bc7e14d9b476565d53162bd36872daac53c60322b14d8ad805

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.87-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 51e925f8d8564eae4902a7527c576edb6cbd81d87efd390eabfdb74cacd2b631
MD5 ead1c4caebab847815e698faee20d4e3
BLAKE2b-256 0ed75f54ce3165807fdec1027d5faa0c544af26c490d7c190bd93e777172eaf6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.87-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 118f80564291eaeab540c24482a7a5499de8e194897eb7b2610d7c3716baa942
MD5 e66a171b4330d5093ada72bff484bc8a
BLAKE2b-256 e0505c0255c39106f52dda57e4690cbc1e76e6f04c04d869dc3a28621a255ac0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.87-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 572c57e4c930ccd6e1e83b9e546d30440bc007c22635be14c52e1f45dfb73b6a
MD5 9988ca6469a22b659e468cf219afb97d
BLAKE2b-256 c437ce0b5a6a8c59616ad24cae8adddbfc4cdac25d22ec15c292a3fb833c2538

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.87-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 36d196eeaaa01ca0db4a0d4ec9f304a92371f8c520699e62f177a38c09353c52
MD5 9055d04328f403ef2bd1fd0eeb3a6200
BLAKE2b-256 d9da458dab649fad2eb013cce00a77c634a90baccf6c3720216d085eefbbebb1

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