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.

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.
  • 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.62.tar.gz (2.7 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.62-cp310-abi3-win_amd64.whl (12.0 MB view details)

Uploaded CPython 3.10+Windows x86-64

gamfit-0.1.62-cp310-abi3-musllinux_1_2_x86_64.whl (11.5 MB view details)

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

gamfit-0.1.62-cp310-abi3-musllinux_1_2_aarch64.whl (10.0 MB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ ARM64

gamfit-0.1.62-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (11.4 MB view details)

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

gamfit-0.1.62-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (9.8 MB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARM64

gamfit-0.1.62-cp310-abi3-macosx_11_0_arm64.whl (9.7 MB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

gamfit-0.1.62-cp310-abi3-macosx_10_12_x86_64.whl (10.9 MB view details)

Uploaded CPython 3.10+macOS 10.12+ x86-64

File details

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

File metadata

  • Download URL: gamfit-0.1.62.tar.gz
  • Upload date:
  • Size: 2.7 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.62.tar.gz
Algorithm Hash digest
SHA256 6f40181bb4348705b9be94d78107293a150e580619ca8b4a4a9ed5bf7aeeb318
MD5 cd7229724ecaa42ce607c0482be237aa
BLAKE2b-256 0f14a17f85c5d0e110506bcad29262360aefd48098852d7f651d8b29ea5ad306

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gamfit-0.1.62-cp310-abi3-win_amd64.whl
  • Upload date:
  • Size: 12.0 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.62-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 c16353036f6b39bcfe9b7ec5a1965f98ea7cd57931db1fc2da84a0614d7defa1
MD5 754435251bb1664e6584353ef14ad12a
BLAKE2b-256 fa47a0c8e988887b63d803b69714d7fe2d31cd6705b1d05335bfa9464a3e6f3f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.62-cp310-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 a6fe1e5b02e673e230755d60ae36b592742354db7447706a3ced15ea0fbe1c28
MD5 c44ad3901121fa05564ec9b94952f607
BLAKE2b-256 71f62faf6a77445830a804345b7a3a56f10fa6cf704b2db402d5659c8ffe4ace

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.62-cp310-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 fb7a7d11a622b72972520002c972a66e4e0d4b19b7c4a666329d95f095a7134f
MD5 7db755632dfc553cd1d60b776330737f
BLAKE2b-256 f2cf16e688aaf50e63d200d4381ab3e8cc8e073de840b65f227be74e80dad8a4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.62-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 6dbd36696db038b51ee64fe03e3752b3c95af28b3b4da3c17041fa9312c81443
MD5 30417fb9807e42f261a1868fe927a24b
BLAKE2b-256 a0acea4de8caf09a615e276a7ad633795464c5b1f0c22c7ef9d09e217aa434c2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.62-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 b73d8a48b5d05d0c631c393dd79523581c2c0056b3e490f719fddebed3083c82
MD5 4b25a57a6fcefeadef9358efede8adfc
BLAKE2b-256 756854dacf5988d9787be09658ce350668a9b16bd1b938dec2291bfe2c2d53a9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.62-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 823898440b863ee3d3c6f3a87cf4beb4b5e9e3374f0a6b5f0763922d7a228081
MD5 f3228336aef16964913858068f853053
BLAKE2b-256 fe2ce40e88734dbc814335020173096d6c76bcef9134001ea07f7ea958878df4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.62-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 e3edf974a91c9a12bcd57afb1e8ea2c5b63b603eda228ec98303792ad93e904f
MD5 9056132c5c177cce5e27b906380e17a9
BLAKE2b-256 2f0850351d8dbc3816426c0b30ece381581c3b2e194e46b99621bcdc22571cd2

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