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

Uploaded CPython 3.14Windows x86-64

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

Uploaded CPython 3.14macOS 11.0+ ARM64

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

Uploaded CPython 3.13Windows x86-64

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

Uploaded CPython 3.13macOS 11.0+ ARM64

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

Uploaded CPython 3.12Windows x86-64

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

Uploaded CPython 3.12macOS 11.0+ ARM64

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

Uploaded CPython 3.11Windows x86-64

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

Uploaded CPython 3.11macOS 11.0+ ARM64

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

Uploaded CPython 3.10Windows x86-64

gamrs-0.11.3-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.3-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.3.tar.gz.

File metadata

  • Download URL: gamrs-0.11.3.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.3.tar.gz
Algorithm Hash digest
SHA256 294ac8b8ff575d6826934f7e15040739f4a510376e7ef383f8eb4972b08e359f
MD5 ce52521594a09c78c82423ea19067a84
BLAKE2b-256 eb76e2d6ac57d8514c76cc0e8795e5d6392f71811f2cb426dc8c59bbbdf9c484

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.11.3-pp311-pypy311_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 f5f4d26be3f255f8ccc1dde3679094a366df9066590cde917281a6f98c93c7d6
MD5 038e2d5aaafbfd60a58e71ec84953069
BLAKE2b-256 d71c3a30541e4a22c35930f9ae50f161e0a74b695bd3b65704317e8c3e65f055

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gamrs-0.11.3-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.3-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 ccab069cfd6b06d460515c690a4641866888daa36b9cb5fd58472c839e8c8abd
MD5 321bee54e0bc46dd06ec746dcca4d7dd
BLAKE2b-256 eb772dd0bdded0c9a01a96d57309547fc8a45f9d5468d1f4cdee67b20852f144

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.11.3-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 cae044813089fb125063924135719249e0fd5645a2091bd4616be65e97b05873
MD5 98defac7b253531cc7c0d8b678ef4ca1
BLAKE2b-256 829b9b2b5913767fdc2a31a6d79bc192c793690c6a9d3ce661564d94a9761959

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.11.3-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 816f6fda2ea14f367a784effd66fee1ec5b9127591d5b8490e8602e91d54b989
MD5 ced9cc76122a5b32e0ae3cfbb0b80ac8
BLAKE2b-256 9127ab7123d444280263cb0b1d869e09d8ac0fa17862098e4a053d177212bffd

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gamrs-0.11.3-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.3-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 d4b5163745d7d5e556fc9ad376f76efacc70a0df64779090e2bac7440cfbd568
MD5 2b858aedb64d715a6b49679a8ed26cb8
BLAKE2b-256 0dc5637cd8bea50fb509fa51c9b0994ac0bb2779d8e212172c9b012b9a8a7217

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.11.3-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 509a82206e54411581a07bff84c7dcba7471f11fc96396844b56afed20c722e3
MD5 63b0af8d8511402a4fbb33fb5834e4bd
BLAKE2b-256 ffae5f48d559b59cf1b86d42291e8b3b49c86b78242881dca80e5d6fa096f138

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.11.3-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 e3b9be96d4bfb12641cfb4038577537f032c86cb9921aa1d74d6f8df8e6910d3
MD5 4aadb2b40a884d143fbe05357f34ccba
BLAKE2b-256 d9d61646ae38b4d8d1d96ad2c7a602c4ab5929a3f80e4520da0f73eb72e26cd0

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gamrs-0.11.3-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.3-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 465dd30609a25b3b632330f7f634e4e1ac3af2e1841ee9b5de5268215cfbdbfe
MD5 a28d0aadfc7280ac88f15d5be314e216
BLAKE2b-256 b4eefda7a20a110048a099375f8d62dca3246c2838f763dd241512fffa67112a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.11.3-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 165c6f97f9e70b85d39fb7c96d449b0d07216b640e03fe6b8b9f0a4070891716
MD5 37fdec13e08cb22c29fcfe97d6f38e87
BLAKE2b-256 1a33800edefcd147ceefaf5bdda7658d45c56b11e3ddc5875d5c2b7603fc436a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.11.3-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d87cd17b5faa61b9d5d5a59c9ec31986a21a0c88c432b723eccdf9f9055183ae
MD5 89d6e317c2104aa5ed781b4ed316f308
BLAKE2b-256 0f80fbc54e904cde54d81fef83f284205ad4e102acb633ffe408f130c01f3370

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gamrs-0.11.3-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.3-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 96af50dbd61b5d5e0d4eff4215dd8b5bf50c9beda16acbce975858d5412051e6
MD5 6b030b189b08b9096a077ce36701eb1a
BLAKE2b-256 7434d63ec34ea6b6ce19ec461d0a0f9074779d5fe5f28cf4e10ed9ed35ebfd7a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.11.3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 1007f18fb85784e4bfbf7c11ca2e866053fbfb003dad58881d0d1f280f5a78b1
MD5 11d5268bf859d8dda07c1dc0fd1a659a
BLAKE2b-256 c0c193ca0ef74bf6ac88df896b157d7a155cfa727ac1dbeda27dac4916b160c4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.11.3-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 ca5f880379402c4503d9d8a1c99d440a7ea9517805037a9f1ad4a0dacb18a8a7
MD5 a245e8b0a7edb6ace0a57b85cb7fd313
BLAKE2b-256 f0641ecb465c4f76080bda5860d69138423f6ff49f104ceb88ad7eb44f713f8d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gamrs-0.11.3-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.3-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 1a9f5ddad8b64bcdfb7b284e8da968d032412b2f7a5378b9d9113d87a6c05135
MD5 8c2bf9e7441f2e26ec16932301ef6b26
BLAKE2b-256 51a4d340d798c870ebf73403080264366a920f5eb1d721abfa9f63de4fac2c13

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.11.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 6db0c452e2444e90dae28a37f447ad1a17f435148be423f30bbe0b51536fe5b2
MD5 5e47ac7722cc309640a4d26e60fcdec5
BLAKE2b-256 8f5d5b7f19b336c5b21771eefc599eb6f8e74d0d635af01d136ea711732ea7ff

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.11.3-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 c4b646789f328a047272be9262987a32aefc06a96534837ad5692fdb79364b46
MD5 d40d85893b1a3155d07acd69eb5568f6
BLAKE2b-256 09df7a86cc967551ac6c989d68d3b70fe1496c4593fe7e5fc7d221c5143f8e2a

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