Skip to main content

GAM core (Rust) with sklearn-style Python bindings — beta

Project description

gamrs

Generalised Additive Models in Rust — clean-room reimplementation built on six composable trait layers (Basis, BasisTransform, Loss/Link/VarianceFn, InnerSolver, ScoreDerivatives, OuterSolver). Designed for parity with R's mgcv.

Status: beta (v0.10). Faster than mgcv_rust 0.23 at every tested fixture and scale (see Performance), and mgcv R-parity on µ across all ten families. Multi-smooth additive (y ~ s(x0) + s(x1)), n-margin tensor products (te(x0, x1, …) / ti(…)) and thin-plate splines (s(x0, x1, bs="tp")) all ship. NegBin and Tweedie fit multi-smooth, with Tweedie offering both profile-p (tw()) and fixed-p (Tweedie(p)).

Install

pip install gamrs            # base wheel
pip install gamrs[quantile]  # + scipy for SHASH-calibrated quantile fits

Quickstart

from gamrs import Gam, CrTerm, TeTerm

# Single 1-D smooth, Gaussian
g = Gam(family="gaussian").fit(X, y)
mu  = g.predict(X)
mu, lo, hi = g.predict_ci(X, level=0.95)

# Multi-smooth additive
g = Gam(terms=[CrTerm("x0", k=10), CrTerm("x1", k=15)]).fit(df, df["y"])

# Tensor product
g = Gam(terms=[TeTerm(cols=("x0", "x1"), k=(5, 5))]).fit(df, df["y"])

# Large-n GLM — switch to the bam()-style fREML optimiser
g = Gam(family="poisson", method="fREML").fit(X_big, y_big)

Full walkthrough: docs/quickstart.md. Optimiser & large-n notes: docs/perf.md.

Families

All ten families land 1-D parity against mgcv:

Family Link Inner solver Outer optimiser Parity (µ rel-err)
Gaussian identity one-Cholesky 1-D Newton ~3e-6
Bernoulli logit PIRLS 1-D Newton ~1e-3
Poisson log PIRLS 1-D Newton ~8e-5
QuasiPoisson log PIRLS 1-D Newton (prof φ) ~2e-4
QuasiBinomial logit PIRLS 1-D Newton (prof φ) ~7e-5
Gamma log PIRLS 1-D Newton (prof φ) ~2e-2
InverseGaussian log PIRLS 1-D Newton (prof φ) ~3e-4
NegBin log PIRLS 2-D joint Newton ~9e-7
Tweedie log PIRLS 3-D joint Newton ~5e-3
TDist (scat) identity PIRLS 3-D joint Newton ~2e-2
Ocat logit gam.fit5 joint β + threshold smoke
Quantile (ELF) identity Armijo BT 1-D Newton smoke

Multi-smooth (s(x0) + s(x1) + …) ships with mgcv R parity tests for Gaussian / Bernoulli / Poisson / QuasiPoisson / QuasiBinomial / Gamma / InvGauss / NegBin / Tweedie. scat / TDist multi-smooth fits run and converge; reference parity tests are pending. Quantile/ELF is single-smooth-only.

Multi-smooth Ocat fits run, produce well-defined predict_proba, and reach 99%+ classification accuracy on synthetic fixtures.

v0.10 ports the full mgcv R outer-Newton stabilisation stack (smart θ-init from category frequencies, diagonal Hessian preconditioning, Gill-Murray-Wright eigen-fix, subset Newton, rank-deficient KKT convergence check). After the ports, single-smooth ocat converges cleanly on every tested seed. Multi-smooth still hits a flat coordinated-shift ridge on the most pathological synthetic fixtures (both θ axes against the bound) where converged_=False lingers despite correct predictions — same regime mgcv R itself bails out of with a "did not converge after 200 iterations" warning. See ~/ObsidianVault/Projects/gamrs/gamrs - mgcv outer-Newton stabilisation techniques (port catalogue) 2026-06-03.md for the full port story.

Smooths

  • Single 1-Ds(x0) via CrTerm (cubic regression spline default).
  • Additive multis(x0) + s(x1) + s(x2).
  • Tensor productte(x0, x1, …) via TeTerm, ti(…) via TiTerm, any n-margin.
  • Thin-plates(x0, x1, bs="tp") via TpsTerm.
  • Random effectss(g, bs="re") via ReTerm.
  • Parametric (linear) — unsmoothed raw column via ParametricTerm or predictor_basis_map={"x": "parametric"} (alias "linear"). Use for 0/1 indicators, counts, or anything you want unpenalised. mgcv R's "pterms" block.

Performance

gamrs vs mgcv_rust 0.23.2, single-smooth, best-of-8 wall time after warm-up. Numbers >1× mean gamrs is faster. Measured on a single 12th-gen Intel core. See scripts/bench_matters.py.

Single-smooth at gamrs-default k=10, n=2K:

family speedup vs mgcv_rust
Gaussian 1.6×
Poisson 1.9×
Bernoulli 2.1×
Tweedie 2.6×
NegBin 1.0×
scat 0.5×
ocat 14× (†)

(†) ocat speedup is fixture-dependent; mgcv_rust's ocat path is slow on multi-category outputs by construction.

scat is the remaining gap: per-outer-iter overhead from v0.10's outer Newton stabilisation stack (GMW-modified Hessian, subset Newton, RD-KKT step-halving) costs more than the simpler Newton mgcv_rust uses. The analytic-Hessian plumbing scat exercises is now complete in v0.11 (Level-2 derivatives + observed-W PIRLS + warm-start across outer iters), moving scat from v0.10's 0.07× to v0.11's 0.5×; closing the remaining 2× gap is per-iter constants work (caching θ-state across score/grad/hess, dropping the per-PIRLS x_design.clone(), lightening the outer Newton guards on benign fits).

For GLM families at large n, set method="fREML" (mgcv R's bam() equivalent — Wood & Fasiolo 2017 Fellner-Schall multiplicative updates with single-step IRLS per outer iteration). The defaults are sensible at small/medium n; the perf guide covers when to switch.

Rust API

use gamrs::{TermSpec, MarginKind, DesignStrategy};
use ndarray::Array2;

let x: Array2<f64> = /* (n, n_input_dims) */;
let y = /* Array1<f64> */;

let fit = gamrs::fit(gamrs::family::gaussian_identity(), x.view(), y.view(), None, 10)?;

let fit = gamrs::fit_with_design(
    gamrs::family::gaussian_identity(),
    DesignStrategy::Additive { terms: vec![
        TermSpec::Cr { col: 0, k: 10 },
        TermSpec::Cr { col: 1, k: 15 },
    ]},
    x.view(), y.view(), None,
)?;

let fit = gamrs::fit_with_design(
    gamrs::family::gaussian_identity(),
    DesignStrategy::Additive { terms: vec![
        TermSpec::Tensor { col_a: 0, col_b: 1, k_a: 5, k_b: 5,
                           bs_a: MarginKind::Cr, bs_b: MarginKind::Cr },
    ]},
    x.view(), y.view(), None,
)?;

let mu = fit.predict(x.view())?;

Python API

PyO3 bindings + numpy. sklearn-like surface: fit / predict / predict_ci / predict_diff / vcov_ / coef_ / lambda_ / edf_ / fit_stats_, plus serialize / deserialize and GamPredictor for inference-only deployment.

Architecture

Trait layering (src/traits.rs):

Layer 1   Basis              ←  CrBasis, RandomEffectsBasis, TensorProductBasis<A, B>
Layer 1.5 BasisTransform     ←  SumToZero, StableReparam
Layer 2   Loss/Link/Variance ←  10 families (see table above)
Layer 3   InnerSolver        ←  GaussianClosedFormInner, PirlsInner, GamFit5Inner, ArmijoInner
Layer 4   ScoreDerivatives   ←  EnvelopeScore, ShapeAwareEnvelopeScore
Layer 5   OuterSolver        ←  NewtonWithHalving, FellnerSchall
Layer 6   FittedGam          ←  predict, predict_ci, predict_diff, vcov, serialize

Outer optimisers (Newton, Fellner-Schall) and per-family tolerances are selected through Loss::outer_tuning() and Loss::allows_no_refresh(), so adding a family is a Loss impl, not a fork of the optimiser.

Versioning

Beta (0.10.x). The API is stabilising; minor bumps may carry breaking changes until the remaining shape-aware families (scat/Ocat/ELF) gain multi-smooth support and the 1.0 surface is locked.

License

MIT.

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

gamrs-0.11.0.tar.gz (3.7 MB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

gamrs-0.11.0-pp311-pypy311_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.1 MB view details)

Uploaded PyPymanylinux: glibc 2.17+ x86-64

gamrs-0.11.0-cp314-cp314-win_amd64.whl (3.6 MB view details)

Uploaded CPython 3.14Windows x86-64

gamrs-0.11.0-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.1 MB view details)

Uploaded CPython 3.14manylinux: glibc 2.17+ x86-64

gamrs-0.11.0-cp314-cp314-macosx_11_0_arm64.whl (925.6 kB view details)

Uploaded CPython 3.14macOS 11.0+ ARM64

gamrs-0.11.0-cp313-cp313-win_amd64.whl (3.6 MB view details)

Uploaded CPython 3.13Windows x86-64

gamrs-0.11.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.1 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

gamrs-0.11.0-cp313-cp313-macosx_11_0_arm64.whl (923.7 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

gamrs-0.11.0-cp312-cp312-win_amd64.whl (3.6 MB view details)

Uploaded CPython 3.12Windows x86-64

gamrs-0.11.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.1 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

gamrs-0.11.0-cp312-cp312-macosx_11_0_arm64.whl (923.3 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

gamrs-0.11.0-cp311-cp311-win_amd64.whl (3.6 MB view details)

Uploaded CPython 3.11Windows x86-64

gamrs-0.11.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.1 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

gamrs-0.11.0-cp311-cp311-macosx_11_0_arm64.whl (927.6 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

gamrs-0.11.0-cp310-cp310-win_amd64.whl (3.6 MB view details)

Uploaded CPython 3.10Windows x86-64

gamrs-0.11.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.1 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

gamrs-0.11.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.1 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64

File details

Details for the file gamrs-0.11.0.tar.gz.

File metadata

  • Download URL: gamrs-0.11.0.tar.gz
  • Upload date:
  • Size: 3.7 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.13.3

File hashes

Hashes for gamrs-0.11.0.tar.gz
Algorithm Hash digest
SHA256 3a595ef2890af89b75f04083a765f23b24e244e03b07c39fd14ffe2b30f43a98
MD5 a5906ed106af6347e677181d46d8daf3
BLAKE2b-256 73ed2c7eaf03b33adc3436fae139915debf891416bbf2c11c27afa533870f490

See more details on using hashes here.

File details

Details for the file gamrs-0.11.0-pp311-pypy311_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for gamrs-0.11.0-pp311-pypy311_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 64e7360799e8e25f4d743d8e7acac46b8c0bba2d0e7b107c68eff4efae05de32
MD5 3b73db41016481bd5c35875c67ab514f
BLAKE2b-256 a9e9e1cddd825f9849b8792f4a60f5575c955f12ca37d46109671250e2bd15b2

See more details on using hashes here.

File details

Details for the file gamrs-0.11.0-cp314-cp314-win_amd64.whl.

File metadata

  • Download URL: gamrs-0.11.0-cp314-cp314-win_amd64.whl
  • Upload date:
  • Size: 3.6 MB
  • Tags: CPython 3.14, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.13.3

File hashes

Hashes for gamrs-0.11.0-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 9712d9fb1ec15ad8584d215c567c24aab6d9c7e9513b6a1065a49e77ccbe5ce2
MD5 94c42ef23b04c0c86c6ebe49acd60f1e
BLAKE2b-256 6f59ac17d958e815450da974411ea72645c9638e33c7cc65dca1c202a0fc15f1

See more details on using hashes here.

File details

Details for the file gamrs-0.11.0-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for gamrs-0.11.0-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 a9b58f93fa9f2e5d64d1754ef0e9452d2b2f38522ab9c189ac839e86e5775e94
MD5 a9caee88be0a2f02aff77e6d56c7ae80
BLAKE2b-256 159994bd5063df5a4f36158409e9dd57e7ccde42f2072f2b3a300fdf2435509c

See more details on using hashes here.

File details

Details for the file gamrs-0.11.0-cp314-cp314-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for gamrs-0.11.0-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 f0f8e19b5e05f15b6439c76b0ff52ce3a59a9e84e4e0ab312a4b336ff05cbda9
MD5 0a077e80f1e14d63baff19b8db95ae60
BLAKE2b-256 1545c1494a29e0923c7439b3e29d6b0f8b45a616e07dd9fd3dd4381036f24326

See more details on using hashes here.

File details

Details for the file gamrs-0.11.0-cp313-cp313-win_amd64.whl.

File metadata

  • Download URL: gamrs-0.11.0-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 3.6 MB
  • Tags: CPython 3.13, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.13.3

File hashes

Hashes for gamrs-0.11.0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 f1c896ccd96b5fc004db9a0ca0141077a5cef5876efe294f83d8e09f7e378869
MD5 982a40de50392f7349d81099aea4c30a
BLAKE2b-256 0b7a77ff54c15a4c981dc28609fe200809e0274292db6e887b3ace943f3273fa

See more details on using hashes here.

File details

Details for the file gamrs-0.11.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for gamrs-0.11.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ab5371e0bedb382abd27e70522a2755b8c3df5a8c055b7d4caa5aa0fffc38d43
MD5 fec645bcb023e431042261961a029bf6
BLAKE2b-256 3272569deff95abc83d08773b2b973dbedeab37439dde7d7016fbdec3675f1ba

See more details on using hashes here.

File details

Details for the file gamrs-0.11.0-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for gamrs-0.11.0-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 8947ef942564137e01915dc8e9a9cfcbba260d45eaa0e41651d09260a2b78be0
MD5 c974dfab25237a233037378d9de6fd94
BLAKE2b-256 9cb89348a3c1eba181c26542cdf962dd75405c8b74f8ce7296c73a5cb7c665a0

See more details on using hashes here.

File details

Details for the file gamrs-0.11.0-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: gamrs-0.11.0-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 3.6 MB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.13.3

File hashes

Hashes for gamrs-0.11.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 6cdc93f5b0b0ed7b30d73cfc6d2126e9ccaab167d300b93f13275de6b240f632
MD5 322faa712c183cc363e1149c6a7f9e04
BLAKE2b-256 0d2248118045917b90d17e92429fcef791473cae3bca848d3ca8bdf4e0628fd3

See more details on using hashes here.

File details

Details for the file gamrs-0.11.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for gamrs-0.11.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 d0d1a95cb11473715e11fdc382fdc584ed7acf9799bcc1b8d1fda4a968ba11f1
MD5 3ba3cfd26db535b97e7e333a19e8bc8f
BLAKE2b-256 d208045bd0c69c890000dd384b9b2096c353ce1abd9a2f3385fcd1bc77fa267e

See more details on using hashes here.

File details

Details for the file gamrs-0.11.0-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for gamrs-0.11.0-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a802f4d05ffe137c7cd57501d4836bd904529fb05c3c4572dc4fe1f7263eee16
MD5 95bbd62d6bb36469976ad4061b1d2124
BLAKE2b-256 a59f098612c3868fecbdb3dc4aacae8f66ff424c23e8120095829fad8af79898

See more details on using hashes here.

File details

Details for the file gamrs-0.11.0-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: gamrs-0.11.0-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 3.6 MB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.13.3

File hashes

Hashes for gamrs-0.11.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 697c18068d6931d9562c41df5ca40a189af1576a1070804849175a3ed8b4c531
MD5 12b7396182ddaef9c122f324e6ac3a32
BLAKE2b-256 ef75fb3a06b6b7f6b0b7707d5b9995bfd297594ec2320c474043a1b3d6267ba1

See more details on using hashes here.

File details

Details for the file gamrs-0.11.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for gamrs-0.11.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 5523e0ae9b50fd7c4d0f5d49b5390bb53fbb31b446ed9866d8f0ddf18284d717
MD5 71b252f0b77745e1b0fb7896d7777df1
BLAKE2b-256 eb7d5b383a48aaef6edc50e25e66890b658116014b0a47085bac870bf1b95435

See more details on using hashes here.

File details

Details for the file gamrs-0.11.0-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for gamrs-0.11.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 401b3716556e331ad1579a9faedb7513e4882e2973e4eae6893e488e0f66744c
MD5 02095d32a3c0d76bdc325f09a8ceff28
BLAKE2b-256 ee11860a708c4e3b9ec7f98c996b0592a0648939e64c9480445ecc9748859104

See more details on using hashes here.

File details

Details for the file gamrs-0.11.0-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: gamrs-0.11.0-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 3.6 MB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.13.3

File hashes

Hashes for gamrs-0.11.0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 5c86e9f428beacdea721e266b81573f8daea8bf8a08bfe737d36fb0b1e856262
MD5 b201ee0c11b0cbe64f808b321b4f91c2
BLAKE2b-256 251574fa6eec028752d965ef0a2914f80a8491db56fbe88d5ad55fb7637bc2e2

See more details on using hashes here.

File details

Details for the file gamrs-0.11.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for gamrs-0.11.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 159abe99fa26a73a2dec0ead8ef102977e265db06c9a7c8147d981d3f0b29849
MD5 2882c09c67829e470204899f3df0d392
BLAKE2b-256 6493b146a220dcf6973c759168321fa25b3d12441e3456597aeb96e588e166d8

See more details on using hashes here.

File details

Details for the file gamrs-0.11.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for gamrs-0.11.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 d0e2262f5e754e0ec32e417872d2a5f55ef93fb9576c56ec2cd5a2311c89040c
MD5 ce4f72732d28503d18a24e2ec3df0da9
BLAKE2b-256 0472897687195ecf9d5b4ee8cac10640243b4398e6cff09e2cc7698fed2f53d4

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