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.66.tar.gz (2.9 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.66-cp310-abi3-win_amd64.whl (12.8 MB view details)

Uploaded CPython 3.10+Windows x86-64

gamfit-0.1.66-cp310-abi3-musllinux_1_2_x86_64.whl (12.2 MB view details)

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

gamfit-0.1.66-cp310-abi3-musllinux_1_2_aarch64.whl (10.6 MB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ ARM64

gamfit-0.1.66-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (12.1 MB view details)

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

gamfit-0.1.66-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (10.5 MB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARM64

gamfit-0.1.66-cp310-abi3-macosx_11_0_arm64.whl (10.3 MB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

gamfit-0.1.66-cp310-abi3-macosx_10_12_x86_64.whl (11.6 MB view details)

Uploaded CPython 3.10+macOS 10.12+ x86-64

File details

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

File metadata

  • Download URL: gamfit-0.1.66.tar.gz
  • Upload date:
  • Size: 2.9 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.66.tar.gz
Algorithm Hash digest
SHA256 2a92d3a4a9e1f6236a7140e7c17206814314ab6da9863d7470f2e112a5293721
MD5 1af3bae7551bbc19afb582d2749eb280
BLAKE2b-256 5e562cefd3182ff63702d503c1355e5dae9a0120a7fbbf26e81e31239c45bcb9

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gamfit-0.1.66-cp310-abi3-win_amd64.whl
  • Upload date:
  • Size: 12.8 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.66-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 eada4e6db7fef08a41986b175757c291926119788c9759301ebeed169fc81c44
MD5 9872ce349430c37329a484eb52984479
BLAKE2b-256 b24d72314546834bbf2e69dd0f981fdd0a8a243eebf388816718204097b842f7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.66-cp310-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 8a8c01892865e21d0c2fe6c086b023bcf1ef3182ca701ddf4fe195bc82d968d6
MD5 51feba7fc59465c01e6e03377b96e907
BLAKE2b-256 5cfbf8cd4121be5fbb2e484b043da1e6e3fe41d1b9ebcc58dc7df9f22dafc55a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.66-cp310-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 aa1c799b9b07f4afbf0189dcd5121e8d95546a04be18ef2a62e2bf56ef19464f
MD5 68565038bf63d26ad9c57e971a728829
BLAKE2b-256 4a8fd6ff4d4740cad780c194469fa34030405bf2d2db3b6a4189095e80d28c37

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.66-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 25872c2d4273b8a1ddba4ef57efae29ce2ed20815af7dfe07881ee52085dfc7c
MD5 4448fbd199db0b11a9a8dce3e5fd5a0e
BLAKE2b-256 7f55f58a9c615f43d38e4aa6a269cfa823a1a6df2aba4b7494a19f47ec9a444a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.66-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 a21a37206eec33a1dee7bcf841dbe8ede77330245e0ab09d199c38d914bc8254
MD5 2bebd936b80d8ae86040148bd4151935
BLAKE2b-256 f779001aa9de1ca41c6ad0c09c1868dda2dd37e4ec81c94bfaeb3140ea736b36

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.66-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 db622ae460b9a112097205d4b959f9be2ae752c71a2b82ab52f6d62a2cbf9dd2
MD5 246bff5303563ca07ba7e918d391d4c2
BLAKE2b-256 f4cbbf0e88bdcbbc0d16b55fd763146852eb82255e8dd8788f90d83b61704caf

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.66-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 be8980d105b253324918c9698f07e16202a3ccb7f61effd768b5caa24cdcb45c
MD5 4b7970312297ff0d2e7900766ff520ca
BLAKE2b-256 88d364fba13a0213c31bedc2b06262b5d4988ca6909a44819e6270fb95b17270

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