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, and Gaussian Laplace for model classes without an exact NUTS path.

Geometric / manifold smooths handle predictor spaces that wrap, including circles, cylinders, tori, the sphere (intrinsic Wahba / spherical harmonic kernels), and periodic tensor products, with no seams or pole artefacts. The gallery's Möbius-looking example is a 4π-periodic double-cover parameterization of a Möbius embedding, not a twisted Möbius-strip predictor basis:

rotating recovery of a trefoil knot, latent-free loop, wobbly cylinder, lumpy sphere, bumpy torus, and Möbius double-cover 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 double-cover demo, 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, and Gaussian Laplace for model classes without an exact NUTS path, 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 mean 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",
    baseline_target="weibull",
)

# 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[torch]"      # PyTorch bridge
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.99.tar.gz (3.1 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.99-cp310-abi3-win_amd64.whl (14.0 MB view details)

Uploaded CPython 3.10+Windows x86-64

gamfit-0.1.99-cp310-abi3-musllinux_1_2_x86_64.whl (13.3 MB view details)

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

gamfit-0.1.99-cp310-abi3-musllinux_1_2_aarch64.whl (11.6 MB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ ARM64

gamfit-0.1.99-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (13.2 MB view details)

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

gamfit-0.1.99-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (11.5 MB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARM64

gamfit-0.1.99-cp310-abi3-macosx_11_0_arm64.whl (11.3 MB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

gamfit-0.1.99-cp310-abi3-macosx_10_12_x86_64.whl (12.6 MB view details)

Uploaded CPython 3.10+macOS 10.12+ x86-64

File details

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

File metadata

  • Download URL: gamfit-0.1.99.tar.gz
  • Upload date:
  • Size: 3.1 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.99.tar.gz
Algorithm Hash digest
SHA256 e95f0c83239a1e6ad42e9e802cf1e899b3124e9ff88f63f1dff3e40782f02acf
MD5 dd5d2626ba48d3becabadd16228c323d
BLAKE2b-256 52119bae2c789f89ec0d3bd9aaaeb260e7407c1a4ef75f65a20df4597ab0fd32

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gamfit-0.1.99-cp310-abi3-win_amd64.whl
  • Upload date:
  • Size: 14.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.99-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 8a8c67f5ee15a58d2c5b997ab1b53a528f3071e05c49ef57c9006110c1150acf
MD5 5ca9eb5e1f39082483a6a0024998854f
BLAKE2b-256 07e22c0c9a452aa7c82817919e9301d1f62074e5a2d3dee2a944936ad23b9334

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.99-cp310-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 8738bf0d07219e9e9993e99f96ae73178fe6bfc7ac6d6e6cec581c1f54f2524e
MD5 6be79cc5ba61877fd7a494b6c6ea5ddf
BLAKE2b-256 1251f51fe3bba6a974615cdb5c54981bc2f42f722a4ddabc9c3dbbfc6b3f5384

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.99-cp310-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 634732934ee2a9e52e7f4eb104cbbe63d8e7b26f19a7aa4f5d46020c320cdf87
MD5 f6383df88c3d071a87f3219c42199a19
BLAKE2b-256 c4a03c1eefb0d36a970d7ab62721a1e7160f2df01ded4e664d43db7060d78768

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.99-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 bee2bdbccaf3b35bb6135356d8d258f1a7cc9b0a02d9108c2afa61db14eb4dc8
MD5 7e9e93f81e77a1a27dc8bc192e4d2c12
BLAKE2b-256 bf8ed4b94e4cce8bfc2f54c4ec62b71604b9179320ea21bdcaa9ffb6abec4d18

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.99-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 8d78a4b8e04fc45d6dd71fbf5e1ef88c29e5b9756bfab3aa66a45b20e430f027
MD5 fb7038240261dd82c94ad51119c05eae
BLAKE2b-256 f6bb68c0c7673d5e3a1729c32d41024fc367b0b2dfaa69b07e7789ac1e8a6af3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.99-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 e6512d11092146e882d61e8a5ff65c66dc277c23d2c0f2f9cfd99ffd41e8c89c
MD5 92a4007d1462cbb4d25c14db51d3f397
BLAKE2b-256 255c3bab053013a95eb6f2184e249fa830108f864aeb4e9f1a097450d5de8881

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.99-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 9e614f69299bf85e91cfd5150e0a3c0cffc905372a9ef6a2bced4b6af23843f9
MD5 92577357dff3ce3d933d26b3db6fd511
BLAKE2b-256 ccc384745a9cac8a75f010f480627110d0aaf341335e023423ebb15f049a6783

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