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

Uploaded CPython 3.10+Windows x86-64

gamfit-0.1.43-cp310-abi3-musllinux_1_2_x86_64.whl (11.2 MB view details)

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

gamfit-0.1.43-cp310-abi3-musllinux_1_2_aarch64.whl (9.7 MB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ ARM64

gamfit-0.1.43-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (11.0 MB view details)

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

gamfit-0.1.43-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (9.6 MB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARM64

gamfit-0.1.43-cp310-abi3-macosx_11_0_arm64.whl (9.4 MB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

gamfit-0.1.43-cp310-abi3-macosx_10_12_x86_64.whl (10.5 MB view details)

Uploaded CPython 3.10+macOS 10.12+ x86-64

File details

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

File metadata

  • Download URL: gamfit-0.1.43.tar.gz
  • Upload date:
  • Size: 2.7 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for gamfit-0.1.43.tar.gz
Algorithm Hash digest
SHA256 6a0b471d539d1d943ba530d34fed336d4eacc2999a1d5f60ea4bcaf94919fcc1
MD5 179dcf11a9b813d7a9b8400da4bddd39
BLAKE2b-256 1be9b48c77d420313b2c7c53f5fb2e57d24d7c4eb0e719913ef2d1d71926d91c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gamfit-0.1.43-cp310-abi3-win_amd64.whl
  • Upload date:
  • Size: 11.4 MB
  • Tags: CPython 3.10+, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for gamfit-0.1.43-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 8a7e328d508a3945bc85e3b2d5c2d68a3c21845eebd8c1f8e92fdc5fb14f866b
MD5 5a9b85de83b4cbcb635be6aa32d466fe
BLAKE2b-256 f55aafaf436a7a0269e083caa7428b10a4529dd43fd506fe7305b5c735eab43c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.43-cp310-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 5e4b2a11dbb5737bd5ca7be9d5d05befc9b26fc3e8fc4e8f51f22eb100ccb363
MD5 bead9d05c79c506b0fa7f82d11ce3f8a
BLAKE2b-256 8f358518b2a27690290aa0a20bf7f931f62892129983e2677f073f65fac56f6a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.43-cp310-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 13d9b9adab83799b6d7d876f22ab1df3fde1087682af538ae05972d8d4c9a9cf
MD5 6d022f82f25ffcc23a054f6d99254dcd
BLAKE2b-256 c6b18048648f96bef32eab079e96df4b412908c9936cea13e41fe5af42b0dfc5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.43-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 41e61d14b76767b50cd03bae844e7c3ff3fecadc7398c3f7f607c5ad27e622e0
MD5 ed618f07a564b315369099237ddc38b4
BLAKE2b-256 bc80045847f2d9a916190ae4d873974bb3c0b925fb6b54741e6596ae85dad562

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.43-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 59349a039c3ed7422e50a461788b2fd5160a9f3bde66933c98baa9a40a199a45
MD5 3ef471c1d26496d116e3d2a0c20414ff
BLAKE2b-256 76b4c47aaed06bca5329850114ba32e396424ff05d0ac3a2faf73fb39b87cfcf

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.43-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 2cfa007218040157cf5e73d492b1527e86bae40ca91ff75bfe29df48388388f3
MD5 8972a695f771469ce510b3e1eb696792
BLAKE2b-256 56b9b009c48d89a7bfad90214cc386670d16ac9fc18b3886eea29e1f340775e1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.43-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 caaa87ef5215941766a76a6711cfe2abe9dfbdfb2d1070313a24d3a8af38a1b0
MD5 7c77987b93e8e493d069a69b3c71bcb1
BLAKE2b-256 81f644982d063ca1ccceb0a02e6b9d2392ebb4f3229c9abbd6386889b3f7f9f9

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