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

Uploaded CPython 3.10+Windows x86-64

gamfit-0.1.69-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.69-cp310-abi3-musllinux_1_2_aarch64.whl (10.8 MB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ ARM64

gamfit-0.1.69-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.69-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.69-cp310-abi3-macosx_11_0_arm64.whl (10.4 MB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

gamfit-0.1.69-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.69.tar.gz.

File metadata

  • Download URL: gamfit-0.1.69.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.69.tar.gz
Algorithm Hash digest
SHA256 988acf0fd50bc8d8fc7a3a97233639c2e9894f2f3b61c5001f989cc7c6b252d3
MD5 78d08b0082b3ade4c7dd4ceb4ff958f8
BLAKE2b-256 3f0c04b2d766e9e8af53699e6cfaff4337031aff84e81bb982a1e482f7bc71c8

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gamfit-0.1.69-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.69-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 b24d37e22ceaba9430e35eb05374b2f7f604641997d11de11cb113211ecba51d
MD5 b9e73b43e5502e114dbb7e99541a8afa
BLAKE2b-256 4f77f15306777e5f798b27c34cfd87e61a856e7ade8ecf8f2c176142b638477d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.69-cp310-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 59062664f82db96233fec32000b480050ace6e5a71f2ecbf9687be7cc1aece91
MD5 e292b09651add2e2af0000d49477bd4b
BLAKE2b-256 2849d78060cad57650267ebc66dd788b07f2fc63af4fbed2e85f74fa1c5a8f6b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.69-cp310-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 a11e51cd4e884a5a36cffbfa8c2d6345951fb31ab8ec0ebf7b9de0e544334196
MD5 51d8df260684f0357b1fd4a2f4f1d6e2
BLAKE2b-256 1565f1f478bb16309f58c48905e35fc68bb603fa88eb017f196afb4f43bb4679

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.69-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 073beb2ac510598631134ddf3049b1313d25d23e7eed3671a6a09ff71d64b7bf
MD5 93fe24a999e3debf00a907caab10e231
BLAKE2b-256 5b53e68c1e0b8a98b507732c897475619d4cc111b3b0f2bbb1efc8f09df87bc5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.69-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 f799667273804e5969b38123b3e488f7eb09970cf5e9952850b837321e64be57
MD5 c4b98785a5a54416007fd1521b066981
BLAKE2b-256 bc2f2aa652498a0b8faa52d584f0dea0f3b778f00ca5b8aee2aa3510f281bdfa

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.69-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 73aea54876ef664712ec95d8e6deae56b15e06b6dce89f410cae1569ff21fe9e
MD5 2637f3a9860359c21e1590ad8b3cc2e7
BLAKE2b-256 6e95b9319ab0edb340368fa5db4e2b483020b351ec6ef640af049f778c6c1648

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.69-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 c117d53e74498f3e9a4c77106147748016ed882ca1d72945a9fafee789046646
MD5 2835d53823261c4b38c463c1df2ee196
BLAKE2b-256 4c65979ef30f466a4e42080fd122551283934595e604240f2069d3f7240f053b

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