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

Uploaded CPython 3.10+Windows x86-64

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

Uploaded CPython 3.10+musllinux: musl 1.2+ ARM64

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

Uploaded CPython 3.10+macOS 11.0+ ARM64

gamfit-0.1.88-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.88.tar.gz.

File metadata

  • Download URL: gamfit-0.1.88.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.88.tar.gz
Algorithm Hash digest
SHA256 76f793cb95f80b5f35c4344c9bbda2617abadbaee762cb8c91d58e7f572715d9
MD5 ec282fec9abe64b89f45809a54810ca3
BLAKE2b-256 269ab32ab2d560f8e8c26cffd1200e3d173823c6a85debea49a3ae12e5556fb3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gamfit-0.1.88-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.88-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 2e4d251ef5aafd7b570bd60892ddb3f1fa68d48b9321a0b1015fcae1597b0ec6
MD5 dea83784f5691621c40826cf756e88f2
BLAKE2b-256 e776104bffe1bc45bda8ea89031a409ec8944676f15cb78fa9df66ab29dcddce

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.88-cp310-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 6ed68a83954ab04132a6aeb576925d460aedc0df50a111535f53e7ce9ef129f1
MD5 90632a84532dc3b7b2acfff4178825a0
BLAKE2b-256 debaba35fe49dc74db36565c23529b5e4e429fe73bd824725533836e01cdd868

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.88-cp310-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 8c493e9789ef9fb53092712139a8ee1dba669b00af29c536991c0a94fe6ee32e
MD5 d98b0bebf2fe91cf7aa579b1387db90b
BLAKE2b-256 89e6a4b40da2b2e33f0ce0e1d6d0124bfb1886b1ec8e6e1362cd1ec30992bd42

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.88-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 2a1adb61d9b6d6e6f3b7798d9f82a2dfd7fd8aa63c1171245276d7c1281312a9
MD5 eafaf8c15b62349d796c8c7426f0a083
BLAKE2b-256 878106e42ee0ca0420fbdbc380dd420ead6aafe553ab6ec2ddd163ebbe9ca80b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.88-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 f895eea10646c9959ecae8e16f180c103a66ba513b92fa4d7c8a767d3085f657
MD5 5642798ec8b3d7c202adb1ce39c60b70
BLAKE2b-256 5705507b43ab12a3e8abe70538fd2c5a3cc4a1328e1b422af117818227ee6471

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.88-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 e986e3358a862bb09289ddaf653e9913baa4f5b25ebb6f3c959cb9e50d941aed
MD5 938bebf0b2e82b58599cf51591023495
BLAKE2b-256 61c1dda5c96499a9d930522d943bbf1deb08d5278998cde13149152389592a81

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.88-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 909ec67394834adc0a07ba28fe8081bda482020f950ba67d0eeb6f6a0e184412
MD5 e71149f749377ec0bda0f30e7895b2d4
BLAKE2b-256 e22e00d4d30d0432a95a6d3689f496fd6ecd473713d4cd8ade11ce15fa2862f6

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