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

Uploaded CPython 3.14Windows x86-64

gamrs-0.11.1-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.1-cp314-cp314-macosx_11_0_arm64.whl (948.0 kB view details)

Uploaded CPython 3.14macOS 11.0+ ARM64

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

Uploaded CPython 3.13Windows x86-64

gamrs-0.11.1-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.1-cp313-cp313-macosx_11_0_arm64.whl (945.4 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

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

Uploaded CPython 3.12Windows x86-64

gamrs-0.11.1-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.1-cp312-cp312-macosx_11_0_arm64.whl (945.4 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

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

Uploaded CPython 3.11Windows x86-64

gamrs-0.11.1-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.1-cp311-cp311-macosx_11_0_arm64.whl (949.4 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

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

Uploaded CPython 3.10Windows x86-64

gamrs-0.11.1-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.1-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.1.tar.gz.

File metadata

  • Download URL: gamrs-0.11.1.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.1.tar.gz
Algorithm Hash digest
SHA256 c24666eaf472e11541365658e6b5847d14f10f5fd21b1d177bc630466d2bb558
MD5 ea5c2cfddea340816e75fed808ce098a
BLAKE2b-256 d850a515b34a414e106458e288ea3f9e8f05578c3bb52a64ba311c2edb89ae5d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.11.1-pp311-pypy311_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 b89848c55b21d220f33d6546a9db60d47603b97c58f72e25b1cfe679890b3a0b
MD5 a1f80d9caf9c9770422da55552d9b6ac
BLAKE2b-256 a0d43fea1a120028091bf0d983185481435514b2db7c44fa348f0da648abec9c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gamrs-0.11.1-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.1-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 e0e0ba6951f6933aec289759333fa13edb29f55611d7dab35f8873320bd39528
MD5 6af553a8d401820da41257309ec6776e
BLAKE2b-256 c92a6293c87ad6fb6e579580543e8f8441eb78b8180319cebab1a0dbdcb6a607

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.11.1-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 341691624f901e54fded7b7e1eb5a2b9f74ae089952608450ebfaa9b56bb6fc2
MD5 0f28647731dcea4addbe3dcb5394322e
BLAKE2b-256 1d1e5ad83a92c416b4fe6eaf8bb649d0f7ef7bfc507765a71c7d3a25e27f3af5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.11.1-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 5d3e8977131dfed1bc88cc4ebf8c07ae3ba537b90238906faeb10f6048ec52e9
MD5 b1028fd3399167113ca1dbbe5bee41ec
BLAKE2b-256 e45bd9a90f8ab76e537941a11019dfa11601bff98f68973e0593b22fbe199e92

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gamrs-0.11.1-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.1-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 b0c632755f5e7d663cb3210f0601dde3709df5b5e698f8b98cc916644e763406
MD5 68b9c9717d02ab4e0d6b5019c7dee5e5
BLAKE2b-256 16ad986072e41a8a594a793d0ab6100a0818c183d767f4d1cf400f831e2f9010

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.11.1-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 49fac78e707e243646b97a3a0e55e42a8fc6d2e7972cb01af4d074655812473b
MD5 f5a30e1c6a22728b82fa6836613acc8c
BLAKE2b-256 d352db221db3e252db4d848822ba21b13742acff0ef473fdd96643c81cb3fba6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.11.1-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 5d54a1e41e5894d9f99c365c7a0d9e139795f6203c2dac051420440e3b5320cf
MD5 e47a1e590554aa66132c0576caa6ee1b
BLAKE2b-256 37bef7bb6d0fab62ba02e8a633fc7bd08e064388414ec52cf0a8039618c93e43

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gamrs-0.11.1-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.1-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 d3293ab607610871c2763596de21749a2fec1d6878d37b2202eb33610310b4a7
MD5 d50c7286aaab2844f642c87639533b36
BLAKE2b-256 a3c648228907b3cbb46b65281c5c0803cef1275445a0747175bbf23bbdc31984

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.11.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 e1acf25470753fb2d66f920210f8f63bbb18bc7d261386c27371d05445b0d3ca
MD5 5c2718c8306c725e4069219117b9e02a
BLAKE2b-256 60e064ca9cd1d1a95687bf2bb88772b1549339f388e89e48c15f2109edb9d28a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.11.1-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 13f8d07ae0cbd5e5340611a3c25885d96dfe2b0505558101888f76db49b5cd29
MD5 aa84e1da4d30a6c4cf1857dba2150142
BLAKE2b-256 75daed568f08e96d8a1f2275b3e95e9519a01dddbf3c02d4e24db3c452e9df3e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gamrs-0.11.1-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.1-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 1083d10153d94e5784f86b7d52af19816d39eb6cf37d970136514e0a8c587c15
MD5 a091ec1fc5fd2f7178697d173e899f67
BLAKE2b-256 b81644d732b5bf03e74878ea3c7bfa50922b3b9d3eb648d5e87966cb832664ab

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.11.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 3992ce023d9312c0e0922c5ecf60576c51fe2d7526f752c4bd645658f2b11ace
MD5 24b0645f539a34517d8d5f771b814153
BLAKE2b-256 e95b12afc27e1166312dc5dc17d47c755ca2a789fe1d5f182aa3ebc3822254aa

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.11.1-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 512e612fb956c745c4aaaa63377ab8092ef904d9f28d0aae136f96443bc58fb0
MD5 8fd2455c4bce9bf075bfa9a2fe94081e
BLAKE2b-256 58f8aa1c940791e30f6f68c39ac555df8a5920466f3a3ccfb14a4d6994cc51d5

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gamrs-0.11.1-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.1-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 87c0ea1966404e107193374880c4ad1646c94f185637d334e09cbb6c7709c87a
MD5 4e02c019a331eff9ef41427e872875e1
BLAKE2b-256 72789751d0884a58b68cd56232cfd9d2b8e506d8f3483b28c47b0bab82e7bf02

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.11.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 c9a754810d44d29b57bf1b93411d2ef1f23de2696511e3f59f50364051665f36
MD5 29024ef44fb17211f5e30fcbbc844e3e
BLAKE2b-256 c0f3e6d5e7bd656a2d1606e2c5f5c9c1be20cb50381337ec86fa5d068ae45ba2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.11.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 65cc1a0f5e0f307d22d3055059c7fa424f87f2af5f682484557ec8ee1f1909b3
MD5 e72962576f488e6e436d667078fc607a
BLAKE2b-256 a86cd534c8f8790e7ae4cd6f3e65f10ea57b4e1ae413c40de390dce3935f0e2a

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