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

Uploaded CPython 3.10+Windows x86-64

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

Uploaded CPython 3.10+musllinux: musl 1.2+ ARM64

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

Uploaded CPython 3.10+macOS 11.0+ ARM64

gamfit-0.1.71-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.71.tar.gz.

File metadata

  • Download URL: gamfit-0.1.71.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.71.tar.gz
Algorithm Hash digest
SHA256 0d2e42b9fc46d148526f5bc3ae7d14fe18291d31e97ad9539e13f0b8312212d8
MD5 6754502b4aa1ebfc3cbc49e22ab8b03b
BLAKE2b-256 958f0e35d541637ef93b4974f6aeb77b9ce01c6af0caa952d247981c14d5dd1d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gamfit-0.1.71-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.71-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 a6099b916349e46a12074228bbf9dc995c50b41d32a5b36e8aed16f3bc03b8c3
MD5 fb22be87315fbef6998922d096a7fcad
BLAKE2b-256 0af4732476f20c81e3e5092253850e2e3083258194e0a3c2e8e0b1542f394888

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.71-cp310-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 586b74ad63015d82e01b1788b54fdb9183449b22afec3bc2fec4417ec7c1c85f
MD5 e7e373e6d678a38555e79c074ccc7aa2
BLAKE2b-256 88b4514e8cd007b3f93629df852d64972a4bdd3c0d0d01adc8db00b7d21531ee

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.71-cp310-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 0fb925d11e0ba6ac24860214a7981090841ad6e3db8f8bc28c2cff6574558b1d
MD5 5abae0ba5127bac45fa4a9799c4e42a4
BLAKE2b-256 344677685d3c551b5185542b198adcf2e663f8fe54ce53001382c07eadc1b204

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.71-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 c43433b4d96ca8bf5fd8a19209f66777914c52ee6a65211012b8e7b83bca4e74
MD5 55e9816a19849943362d8673d387a074
BLAKE2b-256 f0671d209da8aea6ed694807a10c4c7e70ab7828d00bfc8765b041b2a799efed

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.71-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 15bde4995f8035c768e6814312e57266eb8edd02b98b3f0fc353ffc2ed541132
MD5 3fe6c2d9473bfbb38336d5a02676410b
BLAKE2b-256 10a45e6c6f3f790465851c1af8935de2d3928632fb1b55d786e0d1120167edcc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.71-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 2e290293d4521c382f938321e59d85a84314401b1844c1eef9ebad35ef6b5044
MD5 e41cd20678a66ca6b10570f0cfe1c29c
BLAKE2b-256 9cd3615ad19d7942f52069d00edd769b6bab5ec9a56593d34151ea09eb0658bf

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamfit-0.1.71-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 6ac4c802f0ac674f2d8b501b3d95575473fd80ad940f1335cb50f72dc33a9113
MD5 31c462bac4e287c4fd5a6022521ce968
BLAKE2b-256 f6b5b623ede3aaa1c49f049e8f0338463e121d6822c7ecf3c8e5f03b82d1fca0

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