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

Uploaded CPython 3.10+Windows x86-64

gamfit-0.1.84-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.84-cp310-abi3-musllinux_1_2_aarch64.whl (11.0 MB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ ARM64

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

Uploaded CPython 3.10+macOS 11.0+ ARM64

gamfit-0.1.84-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.84.tar.gz.

File metadata

  • Download URL: gamfit-0.1.84.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.84.tar.gz
Algorithm Hash digest
SHA256 51463b4f10781301772b6e81a52a650b74bd88f32b600b91ccea2f2e891ba252
MD5 df471664e13e8952cf5a7b36f61aab94
BLAKE2b-256 816a6e6d9bc6f1d69317681f42fe6420b4cf16979758006fbe90e2d58c0f7bda

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gamfit-0.1.84-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.84-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 8d266161ada4ac577bfc7ba220eee98b45eb985d35d206bce9b4a65039bf716d
MD5 5e6c87bbec795e489204a44727e77e71
BLAKE2b-256 def67ef3027c2e6de4ea21df1734761f19e88c7e66cf9cc6fd43d24181e84ec9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.84-cp310-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 5dffe42d283c2edea6576bf9f8aa14cb279852656e06a37c86192c89fbf6a80e
MD5 9f8fce8d9096c900085fde3b646ee034
BLAKE2b-256 4da96c7491bb42ac9a620f8c06215ee8847a7847b930ab01be908321eda06b68

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.84-cp310-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 7f2f6bbf60969dabddb4a3945111815f723e1ddf464d0c404fdb0d573fb10974
MD5 48ccd44633fe58212c805ee9dc6bbd4f
BLAKE2b-256 257c3b38fe2cb543dc36ee7a7b9ef0e3b7229df670794f61e31020d4cba95411

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.84-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 7dd0c7962d9360290d225ed5cc6c7640cb16383452b1c2ebfc529b4987377f1d
MD5 92b8da55c8aee161c9368497dc0b278a
BLAKE2b-256 4d388b33fb103d79aedb0d86e3ec1dde6584474c35728fb8353a5d406f47a6cc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.84-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 52e0a34476cc5e309925354334485aad21a392f4cb07b07bfca960e7cb65f86a
MD5 e5fe60c9f8bcfbf3bdf96d6f0272a36b
BLAKE2b-256 c8694c672a064fadf17c296749d7409c9cd4d031b16bcce91ce7602ea3364346

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.84-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d9dea95e64c367aa779f0ba4cd62bf6a85e2d938c78fb6b781442d8c34957434
MD5 ad2e6b34cd3b770c169b617b36980116
BLAKE2b-256 6072191c3c307c46d70ac5be8fc97c5e6663337cfe8cc76109f362416c260f51

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.84-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 1af16229830423729983ec0c604bb2c3daf17669ef803eff039f3b4326912460
MD5 fa07e4c833be65248d2cdd0b94e75e7f
BLAKE2b-256 487b0b74701a9a8545054736f27f63fbed2dbc1aa7ef50348459d134df8761e6

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