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

Uploaded CPython 3.10+Windows x86-64

gamfit-0.1.76-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.76-cp310-abi3-musllinux_1_2_aarch64.whl (10.9 MB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ ARM64

gamfit-0.1.76-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.76-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.76-cp310-abi3-macosx_11_0_arm64.whl (10.6 MB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

gamfit-0.1.76-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.76.tar.gz.

File metadata

  • Download URL: gamfit-0.1.76.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.76.tar.gz
Algorithm Hash digest
SHA256 003bfd2d5a323baf91e591d931677d69d3b361d4a5c969a07e455c3b1d00ad1a
MD5 b45b1be5f19c9cda61f8e8569afd1572
BLAKE2b-256 a2c08e65f644ad16243e5be99dc6bb169c05359787b507b935a0f942e7967081

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gamfit-0.1.76-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.76-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 c0a7340ef30d84bbe18bd410e73fdb666553293b7df1e4b2e6cec60abe8ceee4
MD5 bfbe58cf17774267298e9357c69a1fb3
BLAKE2b-256 ba5226423f1b84c3a4d76edd298f8ae4eeda455c05ea53326baf646da7bf3631

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.76-cp310-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 021e2de1a7b7404f5ceb1696448f27c6866dfb3b154b4b67538f670d94ab5eef
MD5 bc81db0f228700805f5145e807507dbc
BLAKE2b-256 0c3306c9c817c538ed4b41c75d44dceadd6d2c730257a098461e67b1c6c55547

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.76-cp310-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 4464bd5c521712f7f5c2a9d695aa65645df53ff9b3479dfbdf7d2f8aab4e87a7
MD5 1da445535e5aa9b598ec08eb29130364
BLAKE2b-256 b1b07626b0bce39976b7ebbc1c52f5a6064d81d761557110d236198ccb713a5c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.76-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ad41949bb8a3454b84629da901f737238a33429e11e2bd9ffeec24f6c4491a55
MD5 2849547b5d8c12652aab4d94b5fac026
BLAKE2b-256 409cfd6af0dcb4e1cda8fe17a2584da5231618dc44f8ca0b20227b1942a56df3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.76-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 cd903a4a8f0e080d2b204d2d61505b4244fde9e0df333c09438325a83d9049f4
MD5 4aeafb5c5a70f273e53b76b62860d097
BLAKE2b-256 7b77cab0957248cc22f78f6106d8e02bdf791a4f5679831e9afd39cbe7158963

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.76-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 7d55e630c88fa92c7b4ab114577131b829e1e572c7a382777a23e97e642d6ece
MD5 c4454eb8fb517d6a206ecb8727aa0930
BLAKE2b-256 41a2c5597b8cf08d7c2937bb9576597a562ee50610eed6d53f549d316791a3f2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.76-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 d576d003c63803f5577878d6a0059fdd1662db70c35500c24ac33cd22fa6cde0
MD5 d7c042e9390bd4f3c321589b7628a36c
BLAKE2b-256 ad00172a5d022078120b45364c6761911a9839d5082e1295bdda388f64f7473b

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