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

Uploaded CPython 3.10+Windows x86-64

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

Uploaded CPython 3.10+musllinux: musl 1.2+ ARM64

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

Uploaded CPython 3.10+macOS 11.0+ ARM64

gamfit-0.1.72-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.72.tar.gz.

File metadata

  • Download URL: gamfit-0.1.72.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.72.tar.gz
Algorithm Hash digest
SHA256 1a5bbf62a5b080c753e0c5ffa27cbaedec9772da2ef3a9f1c77171641840504d
MD5 c7b5d40bbe3fadd653d99154b693c284
BLAKE2b-256 6b2de5533b19431941bdee6f6c67926965110760694f9e2ec282205aaf6d9758

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gamfit-0.1.72-cp310-abi3-win_amd64.whl
  • Upload date:
  • Size: 13.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.72-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 985e305731fd091d206b4dab13d366481308b1b0737be40b78b6628f5a60489b
MD5 147424e3625a0284f7c1b51feba622f0
BLAKE2b-256 8abb49307d28cef28c9a56cb242f9cd068d569ebf694c5c40ff2a92a843ef6d7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.72-cp310-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 3c975ccdd7a73051b2b325502df7c2531625e0884de5578e9a0a0ffe152a00df
MD5 a189c772795f3ddcf06bd4567f26bee9
BLAKE2b-256 9d977832f02cb33ad46ee7db4b3271861fa64f394c2663322a97e46be12fbb75

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.72-cp310-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 890609a5a9aac1a34afd9c1c0da593d97f17bfcc0ecd747df1fc58f0e9b8b135
MD5 57f611e29495bc9764e6f093374335b1
BLAKE2b-256 cd240db96f0be858506d016f23107e5a7e6e4331d963e3d22208e7fc55770e32

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.72-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 1cfb4f4dc6418e64fa235b1173ee28fb6702f35acf5445fa6f8e883e10d023de
MD5 9a8806bfc0cbb765367dea5b5f6d077d
BLAKE2b-256 c42ecc33c61cedd5f030ab96703be6c2381d8696a002145a0fb52bad2138c018

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.72-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 f617e7be75f346cb21fe38f8f3a514b3ff9145149d014f30439cc2fca657e7bc
MD5 1e31b40473bd43c0eeff4b5ce1451d5f
BLAKE2b-256 b60de34b2ae27383ad81bc23667a4e98439ead777bb618b4e9bfeae616bba71e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.72-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 cea76225437a68fce49e18baa2466f3b27fe9ce38f613dcd53fd5a8dbf774788
MD5 b7760441538b1ba9908ce4a8bf7beb50
BLAKE2b-256 9e1a888e43c311c0c80def7f9a63cfa77c28471d3858ff129332fdcf110825be

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.72-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 a19ba6f7f4a9effa0df9a8835cc81a22fb2490194ab2b17f4168bca46084c803
MD5 918809dff6606023c01c54ed5e1e3410
BLAKE2b-256 a7f332a8fec78d6467db3893cca553e47c855a2c102475808b48d35d7343bb3e

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