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.67.tar.gz (2.9 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.67-cp310-abi3-win_amd64.whl (12.9 MB view details)

Uploaded CPython 3.10+Windows x86-64

gamfit-0.1.67-cp310-abi3-musllinux_1_2_x86_64.whl (12.4 MB view details)

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

gamfit-0.1.67-cp310-abi3-musllinux_1_2_aarch64.whl (10.8 MB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ ARM64

gamfit-0.1.67-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (12.3 MB view details)

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

gamfit-0.1.67-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (10.6 MB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARM64

gamfit-0.1.67-cp310-abi3-macosx_11_0_arm64.whl (10.4 MB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

gamfit-0.1.67-cp310-abi3-macosx_10_12_x86_64.whl (11.7 MB view details)

Uploaded CPython 3.10+macOS 10.12+ x86-64

File details

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

File metadata

  • Download URL: gamfit-0.1.67.tar.gz
  • Upload date:
  • Size: 2.9 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.67.tar.gz
Algorithm Hash digest
SHA256 1b107b19e543ec10a8f67efbcf2e382d9a5139eb960a9a6a4eedae4e2c224c5d
MD5 e61572a51e340e4c2709ac41053313f3
BLAKE2b-256 17baabc1ec6da4c2a51d3b7343014d2f56b9d5224988d125f304e837ad41fd57

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gamfit-0.1.67-cp310-abi3-win_amd64.whl
  • Upload date:
  • Size: 12.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.67-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 7e7d77d3a9b8598fa9eb97859fb136f76ac67943752af4288312b8ee7cf0b21e
MD5 f16032113bf29324d6cd4c4f8b883821
BLAKE2b-256 6f566448eee38e5d3f4f1366ba64ceb5aa5596a724df4b1261c02c61a27dd8cf

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.67-cp310-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 05f27c756a260eff9c42429ca3f4072ea27db0582dceb30cbf481125dc3fb1fa
MD5 8cfb00c78a2f655deafbebcf88e3d190
BLAKE2b-256 c31f1a4c95e0c68554fb6c7ffeb2c08f6d6c660ac894b8a250829f72125b01c9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.67-cp310-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 c54b6c11e4e953a292e0e9d5f81395d03dd76027290e7d0f2b031419908c80a9
MD5 c9e9639cc8e834010aea768b276220f1
BLAKE2b-256 40a7bdc57035b5aff38967c3f67fe4578a63e26eb60fd6d9e0aa8166302aa2ea

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.67-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 cc5b5fe0859de88de7b22889a56b08a0213fdd33f26a90595df87c01811f3998
MD5 97f37dda477b53a3e14abe2b01654b5b
BLAKE2b-256 440ad40d2fefdda613ea2e6ad35f2c2cd80cdf8f0aad35e0fa4bb6348ad7b861

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.67-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 9653dc790ae368ef2b44c6c877ecd390deb3848e75f1457a9bedae46bea75322
MD5 1f3bc7f019b4a389e729d328943eba49
BLAKE2b-256 d80e57347060696d37bf7a4116d464cdc6b315d01879cce3b81360f5c21a516c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.67-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 5b62a8cdd6d4fff167867af9e4e561df9567bf348fbe78bd162965c4135526da
MD5 b6d044683c8b9da2e6c3f9b03af0f1a7
BLAKE2b-256 ad55b7453d03e0c6d44545318af8366aa9be116e4ca511cee3bed35bfde2954e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.67-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 46580783c68a5251f1aee970461ac2d231d03fefda59de0f9e0a209abbb4663e
MD5 c39696a6951fca733eed817918087e77
BLAKE2b-256 76718301f41fb825369e98c165b9e9452e5092f376e8acb5f457472a2b48571b

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