Skip to content

gamfit

Formula-based generalized additive models for Python, backed by a Rust engine. REML / LAML smoothing, NUTS or Gaussian Laplace posteriors, survival, location-scale, and flexible links in one API.

gamfit fits Gaussian, binomial (including Bernoulli marginal-slope), 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 a Gaussian Laplace approximation otherwise.

This site is the Python documentation. The source repository is at https://github.com/SauersML/gam; the PyPI page is at https://pypi.org/project/gamfit/.

Table of contents

Starting out

  • Getting started — install, fit a first model, understand the return value.
  • Data input formats — pandas, polars, pyarrow, numpy, dict of columns, list of records.

Building a model

Using a fitted model

Reference

Example

import gamfit
import pandas as pd

train = pd.DataFrame({
    "y": [1.2, 1.9, 3.1, 4.5, 5.2, 6.3, 7.1, 8.4],
    "x": [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0],
    "site": ["A", "A", "A", "B", "B", "B", "C", "C"],
})

# 1. Validate the formula against the data (no fit).
gamfit.validate_formula(train, "y ~ s(x) + group(site)")

# 2. Fit.
model = gamfit.fit(train, "y ~ s(x) + group(site)")

# 3. Predict with pointwise Wald intervals.
test = pd.DataFrame({"x": [1.5, 3.5], "site": ["A", "B"]})
print(model.predict(test, interval=0.95))

# 4. Inspect.
print(model.summary())
print(model.diagnose(train).metrics)

# 5. Posterior draws and bands.
posterior = model.sample(train, seed=42)
print(posterior)                              # convergence summary
print(posterior.predict(test, level=0.95))    # posterior mean bands

# 6. Persist.
model.save("model.gam")
posterior.save("posterior.npz")

License

AGPL-3.0-or-later. See LICENSE on GitHub.