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-20 wall time after long 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.77×
ocat 14× (†)

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

scat closed substantially in v0.11.1 — from v0.10.0's 0.07× to 0.77× of mgcv_rust. Two structural wins drove it: (a) analytic gradient + Level-2 analytic Hessian + observed-W PIRLS + warm-start (v0.11.0), and (b) broadcast-expression conversion + batched-h_diag matmul across the IFT chain (v0.11.1). The remaining ~25 % gap is per-pair work in the Hessian assembly that's already broadcast-friendly — closing it would need either algorithmic shortcuts or fundamentally different BLAS dispatch at small p.

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.6.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.6-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.6-cp314-cp314-win_amd64.whl (3.7 MB view details)

Uploaded CPython 3.14Windows x86-64

gamrs-0.11.6-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.6-cp314-cp314-macosx_11_0_arm64.whl (5.1 MB view details)

Uploaded CPython 3.14macOS 11.0+ ARM64

gamrs-0.11.6-cp313-cp313-win_amd64.whl (3.7 MB view details)

Uploaded CPython 3.13Windows x86-64

gamrs-0.11.6-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.6-cp313-cp313-macosx_11_0_arm64.whl (5.1 MB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

gamrs-0.11.6-cp312-cp312-win_amd64.whl (3.7 MB view details)

Uploaded CPython 3.12Windows x86-64

gamrs-0.11.6-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.6-cp312-cp312-macosx_11_0_arm64.whl (5.1 MB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

gamrs-0.11.6-cp311-cp311-win_amd64.whl (3.7 MB view details)

Uploaded CPython 3.11Windows x86-64

gamrs-0.11.6-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.6-cp311-cp311-macosx_11_0_arm64.whl (5.1 MB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

gamrs-0.11.6-cp310-cp310-win_amd64.whl (3.7 MB view details)

Uploaded CPython 3.10Windows x86-64

gamrs-0.11.6-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.6-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.6.tar.gz.

File metadata

  • Download URL: gamrs-0.11.6.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.6.tar.gz
Algorithm Hash digest
SHA256 2dd7c194d4219f23235857484be66a9486e8c028db05e17fe6dac2867627c9c3
MD5 583b8ef5f9c47257528962dc95206fe1
BLAKE2b-256 cfda462e97923469f2a78a028cccde7cff05e1a8e6df5fba6e22eabcfc0d1326

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.11.6-pp311-pypy311_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 54259d456d7f223a38bb4ea63305eb7390570e633a8a14ae1bc992ea808019e2
MD5 c87406de30c1df7c7dad0d2fdaa28863
BLAKE2b-256 62419e367bb721abe9fd3ec3ee5a43d293a1ecdc89c46d52b7c02f858189db4e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gamrs-0.11.6-cp314-cp314-win_amd64.whl
  • Upload date:
  • Size: 3.7 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.6-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 c54173b9ae1f6a122fd3ae137794a4a9f7b7915e8be230e2695861bd1db4373c
MD5 8e3b632f532502b2623b0e1fdba960b5
BLAKE2b-256 bc7bc16c5c3b0e9891d53044c7d19ad0c8a930394bb4aface75e81418ad07e69

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.11.6-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 d9aac03246a68a3b279768dbf833b4fd5eee0add5a05695976abfd0e64e437ad
MD5 be27af43590344416407448edb78e09f
BLAKE2b-256 88016bdf57fe8b7c0b71cedbabba50bb24a8b0672186607cad7942c3ebf12986

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.11.6-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 e2c836d85ce05d86747b3cc60fdb43a17aef6a641c6e4b79e8c18fa386ba2cdb
MD5 40a4f01c13ed6a3e951c286c8487964b
BLAKE2b-256 f42b5b81679182d629be6dac40316ac8ebfd664c9c21a49dd36efd0d4e71ed2b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gamrs-0.11.6-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 3.7 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.6-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 c8a66fe72400db69fd9debfc3d2ba21ad9328d090b2260ccf53366fde516ca25
MD5 3a93c20d531a1aeef0f3ff839159e99a
BLAKE2b-256 7680afbd957d5dd74693f98637c7318ab1258898778e9e331d1dbc0d847f33ef

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.11.6-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 4705cd65c5f3f18e0c529444e7c078afe21c40edd1083759fd7abc13f828a9df
MD5 798a5574caea18aecd2348a5af5e7738
BLAKE2b-256 a63523b8d0a697becafcb22123e41734b8e90130aab4878c4d3050038cfadc84

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.11.6-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 f83ab9d170b3861505d368e957b7779194b1e61068842ec1d0bc7869629e7af2
MD5 fd75570de133b7e704c91b046dc41fa7
BLAKE2b-256 8fafa052c7f5692cf4cddcbc53c43ca84811c983d4c628629047a68ab94ea824

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gamrs-0.11.6-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 3.7 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.6-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 28b57f350c44d75c0e88956621bed58556f5a182268ed554ec240461df2b8a70
MD5 58bf3e548c4a2d74f3a7e0536843c09f
BLAKE2b-256 21ed21391218bc332bf0b533bc845ad759c50231ded38c30399f93228c1d665c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.11.6-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 7ef50a53195f5ebdb207551cf69953fa42205e4510b104876bb9b47fd4f80272
MD5 42cbf7d4ff701c7e68e34144939d73f9
BLAKE2b-256 38584caabc345b1edd7a6dcac8f59b35514406e7fd0fa0510d993ec1affc0454

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.11.6-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 546d03c865437014f8dd2f7b7c7e3fe175610738b42376ef398d64527b461562
MD5 f6c830d4a176c7bb9af9fdef80187034
BLAKE2b-256 cc5a11fb69dc64c2a1e236ae55e0e6241a6c7af7ff212e01f3ba0c94797da4a5

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gamrs-0.11.6-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 3.7 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.6-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 166be13e24de3bc52af2082b8d83600eb0d4cb50d5d6401cd584db1534b8f4f1
MD5 0db32b141c049ac85ca3e5f57a62505c
BLAKE2b-256 893682e117a79ebaba0788818874b007b9afdc76d6b72e00dcfa8b59ba215a0f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.11.6-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 7fd57d09ae95f58efe05eba70d62020d2a785651168c1bf42861a12c6a2284f8
MD5 31da799e299ad8376f173a8870787aa1
BLAKE2b-256 ca64b733c60a85943e501bf80bbe126036ebf7b076f974ed28e204c2052abe92

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.11.6-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 048397ee29a1d06f70cdec2acc2736f352e3a777f0631bf1c1cfc2807d9cfe16
MD5 3081c83bc5b4bd1dbd1cfa30e3f54256
BLAKE2b-256 cb0bce8465f997cd9db75d6ddefc2da5b8cbe48b65bc98b6e28d84ddb0c77e9f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gamrs-0.11.6-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 3.7 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.6-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 83565294317171900196b0b7034bd2f5c4e9156e97f97d81df36d8140f2d12d3
MD5 7c488317fe9a9ce9d46abefaf1cb0f80
BLAKE2b-256 ab0d7dc1a3fd7ab40fc8ecdc2cf9c6fb524cca963a56d6afc6b254f9dfeb79b2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.11.6-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 e1bd37e8ec2f02a9403bc364412b6165ed600a66ea008f50d98cf0832d15bc69
MD5 c295e7b4d15b964c943126695a95f64e
BLAKE2b-256 c08e02936620a16df6473ca7117fd28e615d6ae777ca75a03f40887499504604

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.11.6-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 5b918f4df077930b4d30284bf74a066654c34ee5cb37fd89c8ef085f176e03b3
MD5 5ffda8a3c615c7e7efc1db418fe32b70
BLAKE2b-256 ce84f708e21516abb3d24089ebb725ec023076ed219bf055ab99b8b3196ec8eb

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