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 high-performance 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.

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))), plus blended mixture links and SAS / beta-logistic learnable shapes.
  • Surface smooths in arbitrary dimension. Thin-plate, Duchon (with triple-operator regularization), 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. Mix and match a spline kernel with a length-scale behaviour (e.g. Duchon kernel with Matérn-style global κ).
  • Marginal-slope models. Decouple baseline risk from a calibrated score's effect — for both Bernoulli and survival outcomes.
  • Posterior sampling built in. model.sample(...) runs NUTS where supported and 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.41.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.41-cp310-abi3-win_amd64.whl (11.5 MB view details)

Uploaded CPython 3.10+Windows x86-64

gamfit-0.1.41-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.41-cp310-abi3-musllinux_1_2_aarch64.whl (9.7 MB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ ARM64

gamfit-0.1.41-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.41-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (9.5 MB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARM64

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

Uploaded CPython 3.10+macOS 11.0+ ARM64

gamfit-0.1.41-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.41.tar.gz.

File metadata

  • Download URL: gamfit-0.1.41.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.41.tar.gz
Algorithm Hash digest
SHA256 c35c56d5888a1662c83f0f5e44e8e4ba2856b0ce57bfcdc5527f0b03dcc66b24
MD5 d0094b61fe7ae429973178fbfc0ab8cc
BLAKE2b-256 e014ebb20603947dc119366f90b82ff12caa8354c1e353107b41870f5315fc8e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gamfit-0.1.41-cp310-abi3-win_amd64.whl
  • Upload date:
  • Size: 11.5 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.41-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 33387dc072d6ff872bdfb88600a83fcbc0922510cd384ff4cb8ec4e0f88fae0a
MD5 75d6f66bf108ffd9f0a1dc5b034e51ce
BLAKE2b-256 de0201b0ddd7235f5c50063c5654d3c15cbe1d6f12ba2adde6b9867e2557c872

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.41-cp310-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 0242f43e709653fd79484b93bd4e6615234efe8f5b500296e924b2a426050233
MD5 fbebc16a2387ee4151fef5021ef39825
BLAKE2b-256 a554d862114325ed50eba0c9c76e6e83c6509465eb9d626547d5f9d3a4fd21ac

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.41-cp310-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 bbefd05d445c45e74e9b11a8af9655be6403f4f7a9936dcbc88a9d2129f93a04
MD5 7fe1ca0aa34ddd9e51811a2490df35ee
BLAKE2b-256 16ace1478ebe7cf55108af45dc5a8ac2327cbb72fa0d565eec34a7ca079c2525

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.41-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 3263c755ad102195c9175b245b5599a51fcf6b2997d474a34052cca4f964562d
MD5 5b7ecd4b02b0afaf72295fdb8f943c34
BLAKE2b-256 b7109ff2d2249fd19fd1c0abb6c80d53d1bc0e3865aa1edcfdaa9319b18d3204

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.41-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 57b01f42e452c0e9a13d187687786fd8c96cb1679246165a3002c995531fdbf2
MD5 3864394a65b7e479e187474ccd7ea43b
BLAKE2b-256 32ef8ca2c2b7c8969785cc21f68f3eb27456e5bc69d2f7c4484330e6c3689b9b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.41-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 2716e2cb48f5746b504f5e797cec9b3a64e754f4f80725ce372757e6c92c66ba
MD5 7e38e3310f765cfe18ff7ab96967cf56
BLAKE2b-256 0d0c2371e3967ead0b05f42b1ca3744fb66bb81a82d556689a907d2856ca092b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.41-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 3d1d3d4df0dc362176e05f5bb2a914b053a981d57d37d30101577191ac8a62e4
MD5 cca49e2213d40d49d97eb9fd4096006b
BLAKE2b-256 197ad6cc9ef06b9fc153ee66e61ff6ca87eefcbce098265b16dd8c4829121d54

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