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

Uploaded CPython 3.14Windows x86-64

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

Uploaded CPython 3.14macOS 11.0+ ARM64

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

Uploaded CPython 3.13Windows x86-64

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

Uploaded CPython 3.13macOS 11.0+ ARM64

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

Uploaded CPython 3.12Windows x86-64

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

Uploaded CPython 3.12macOS 11.0+ ARM64

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

Uploaded CPython 3.11Windows x86-64

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

Uploaded CPython 3.11macOS 11.0+ ARM64

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

Uploaded CPython 3.10Windows x86-64

gamrs-0.11.4-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.4-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.2 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64

File details

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

File metadata

  • Download URL: gamrs-0.11.4.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.4.tar.gz
Algorithm Hash digest
SHA256 c13f814757faedff69a078925d271e6e4322de43d9fd86b2fd403d7d1b470fe2
MD5 bd064aef0e54c4ba5eccc04bc4f5b71a
BLAKE2b-256 f36b1410a54ca500fd8546215f167840097cae2588fcc7e5f0b024dd9295e87e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.11.4-pp311-pypy311_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ebbf8aa18dbbcfc317f6ef33b7bbd8dfcf882c8b97b9d817ff4c9932bc858f95
MD5 6154c218fbabdf81d54ebd647aafd7c4
BLAKE2b-256 0c1dd1385aa978db609b5431210a467d29d8e07c874acde531c5135857965ec1

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gamrs-0.11.4-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.4-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 a99a069a5cd78210597923b76773381ecaa9f560baa79fc352a00aa2b7c05b9c
MD5 2fdfab81f2cf695324683141c66195e8
BLAKE2b-256 facc0eaa3c1ab1b1774c1cca7411ac57345b14f50697555260ade3580e103ab6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.11.4-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 cfae7aa0bdc6df67d80747f81a4367709a4a8e91fe76fcd568f48b5cdaebbd86
MD5 a7b300a85f7642d12d3b3b70a1eae474
BLAKE2b-256 a9872aeb3dde73fc6584c26546dd7104a3ad16ca9a69a6fdc9ee8e5d0325a578

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.11.4-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 345e3b09a25ca692315938a7ad3a1f9abd6c4e4d1e6f05347e6a63f27f3ac033
MD5 ac73b5f3e8d86f01c4a4615ea2537e6c
BLAKE2b-256 fe72ee7652c388d5ddeb628df2b7ea1da4d3d0970636af3b939383969fba12b6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gamrs-0.11.4-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.4-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 82cbf8947dffc130aa4c97c7837c164abf05daf966f72e401807fb7d17f3fb60
MD5 31717c4dde337b839ade8e2247d2e352
BLAKE2b-256 bd904829e49cf9b68b3b41d6644e542b1c7a788be450dc13dfc443cd1c899939

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.11.4-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 6e97715065415c397494c0e2e8812f7fbf17140415fd1afffc0446732e56b349
MD5 e4b8c70911ebd0b1d78e2cc599f21052
BLAKE2b-256 79032bf2759ed486891313de56d114a73753ae2bfc6b93fd574e3a25a6982770

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.11.4-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 e36ea75dfea64810b5a38834e2cc30b870c6233afac80156dbed333e426dc4df
MD5 b596e989c9e5cd1d16905aca65a468e3
BLAKE2b-256 2d66aee76d66a43b236e413f575b2a098ae762835164511d0f4bba029ddd3e49

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gamrs-0.11.4-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.4-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 c4de0ec18b7d3f9f51b810bbb59f114a78fb8ca3f826ac66112ab627b4558a13
MD5 56c3541e2c2e51886f951a438271c2b9
BLAKE2b-256 71f70b14616ab4592980202fd09d9eb9adc9a82bb7df9134c8ed987ee9b288c9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.11.4-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 b84cd5383e60448879570ff754dff35fc1626dc5423ceffa3b5bf996e0bb9679
MD5 92d70167f3c64d18484ce9c69cfcafb5
BLAKE2b-256 f2e1701bd71f30b153c74e4a75a894058fca52400b4b4eaae7e7d1a4628efb0b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.11.4-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 eb3b7130ffc3fe07d67567b3a31114ab4be4f11f697dcb3241115c93c756b447
MD5 62cc87551b0503e116e66c44274f5ee7
BLAKE2b-256 3c2a4e842a0d2cd39917159a96ef8b0871822492dd8a1d17d8047d4a9e10b1ee

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gamrs-0.11.4-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.4-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 ac59f187223fdbca3d8ce6b2f77221632ffde2b2dcd85e102eae1e13a65f77ea
MD5 70f51b136e25fb5d6b8ab3ae481cb97b
BLAKE2b-256 fd7a67d464b8df5369c96caf3bc036b1e1a91ceac6a54595f679d26d4146c3c9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.11.4-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 01c5d8e66265f251da977353828d0b71b87aae4a7c02ba5b6d6cffd042a42c5c
MD5 0a3267f17070dd15a23a61c73b53c46e
BLAKE2b-256 848a35ee5bd2cc69af2074355eb1a6840602663f00de96a66bee5fff60e6d8f7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.11.4-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 184f5987ca8aec29adfe65e3cb5966bbbba83de43a73de02fe2586b86909ddde
MD5 2f0cb35b3ba137c59a5cda30e1879064
BLAKE2b-256 585fafee2be88e094d0af7d1d80a9c06b4a128e30897c62d04b5a4cc83b37711

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gamrs-0.11.4-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.4-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 19b95efc085586612c6c6ae99aef553804c117f88d31e6c0f7660e98f766028f
MD5 854649e840fd84a4cb87f8510a394186
BLAKE2b-256 fd63b78449cf3ff69ac7db01c7555ce837ac13177f5abb78f396e3801c8fdc79

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.11.4-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 05b899f04f410a8029f746055820e4c412a6c7196faaf4bf92bb2130cb5a4da6
MD5 e306d4bbf4dadd2bedeb3ec428632570
BLAKE2b-256 cc432163c124fe62db91ea3374b41ff82f38955777eee875607781f40b34d0dd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.11.4-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 730028bb2e7ad0262f64248dd0064b3b2a8aad49a63cc9a27ae7276ae390e818
MD5 dc160d1c0558dc53dfb242d6099e466e
BLAKE2b-256 6474a0f5d797cd0abf3ee53d03e0f9cd66a1ab26d1eb89405b00fa964f2394eb

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