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

Uploaded CPython 3.10+Windows x86-64

gamfit-0.1.98-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.98-cp310-abi3-musllinux_1_2_aarch64.whl (11.6 MB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ ARM64

gamfit-0.1.98-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.98-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (11.4 MB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARM64

gamfit-0.1.98-cp310-abi3-macosx_11_0_arm64.whl (11.2 MB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

gamfit-0.1.98-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.98.tar.gz.

File metadata

  • Download URL: gamfit-0.1.98.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.98.tar.gz
Algorithm Hash digest
SHA256 e0b7582f38d01453281b55750410f4d2938c3172304bfca3029b06d24ce338d4
MD5 892ab8b0c37895466babfdc07c202dbd
BLAKE2b-256 198410ad9decda6e2262ef7b2b6784a6f02f83a5d902e067a6fcba9f509f2031

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gamfit-0.1.98-cp310-abi3-win_amd64.whl
  • Upload date:
  • Size: 13.9 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.98-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 675e5d91c64bf78caec742227ad5e87880920527dd1014e29f4e9fe7f64063b9
MD5 55b49d82ca5aaaba09e1c54d9f9c6f85
BLAKE2b-256 40dcd2e1d386d19fc405633c0b7732706998107823fc72abeff029a6d146204e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.98-cp310-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 9ca382315b3083d979583291a7fd5bd1ec7e228fec16ea5fac747db63765ce77
MD5 309f8c98a2251a5a484c19e5275e073d
BLAKE2b-256 9caa1093a5b253bb44f05878874dafb24cbaaa60124fc12d71c14436b385fe10

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.98-cp310-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 329e1123dc70eff297efbd3b7b0830ebcf47cb318a3cfdc948882919497e29c6
MD5 553be7cc3d5477252e9bb33a5d76e926
BLAKE2b-256 47412bbf2fc55ff76a276e7b507191a86d6593571a59bdf0050ac401d8db22d8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.98-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 f83c6d2e9493b5d42919b8d9c739073e272c79482e55cb4ba06dff2efe63e4b2
MD5 f2f6e321b80c1fa5a9a88db7df992dc5
BLAKE2b-256 fd30f8b5fa9428973e8cdefdc227f59ae291c79c652d73c8b41d18cfb9d31849

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.98-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 afca05ac8984a7652eecdae56d4fce0ce07b45801d95a6cb33a3965ce72da60b
MD5 3b7c234ccb67f3da52ee7d1b0d9e4811
BLAKE2b-256 d0ee4117eca1c1c60487e42cb6282317e63f3ec652da4576484b27cedc89774d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.98-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 01a5332ce6b2adf10c2ba0177f97f69bb448ecb088469d253448f63c81c511a7
MD5 1cb32ffee71b6243c864488550d3d193
BLAKE2b-256 60ddb89ed0b50b75a9a5bbac13baabab8babe77db899eef9fa910cb50895464d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.98-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 71c87ef279f0154974da2c0f587d5f3c1e8bad6cc8ec44e9b6d40e1bc993b9fb
MD5 d413bcf86b242843d5ade6447f484208
BLAKE2b-256 0af22ef4e0bd6be153a5f3c01b64fc33242ab69211da8860b7f13db23fb7b8f4

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