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

Uploaded CPython 3.14Windows x86-64

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

Uploaded CPython 3.14macOS 11.0+ ARM64

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

Uploaded CPython 3.13Windows x86-64

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

Uploaded CPython 3.13macOS 11.0+ ARM64

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

Uploaded CPython 3.12Windows x86-64

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

Uploaded CPython 3.12macOS 11.0+ ARM64

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

Uploaded CPython 3.11Windows x86-64

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

Uploaded CPython 3.11macOS 11.0+ ARM64

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

Uploaded CPython 3.10Windows x86-64

gamrs-0.11.5-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.5-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.5.tar.gz.

File metadata

  • Download URL: gamrs-0.11.5.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.5.tar.gz
Algorithm Hash digest
SHA256 0189e46ce08d664f799f650e8a12347ab7990a979f2eb74b9a800a37ad62fd22
MD5 433f182301c7dcbe3f23911008378428
BLAKE2b-256 a773ea3e1ed9876293c79a5cbbabefd3bce796a7db848ba92ef5a00f60351afa

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.11.5-pp311-pypy311_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 64aaf571619f431d5553a12318a660734a386b125ae284b34a4aace07cd38f76
MD5 9001799f902fd75ecf033fc12632b12f
BLAKE2b-256 5313b038ce8a99fb1ff71ea505f0be3ae280a6a4f2914038bbcdc93b994f7e8c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gamrs-0.11.5-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.5-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 fb4f1bb3c03f368b96c3488767167041e32c916b529ea872983d81d21796b056
MD5 0be61258c99478b7beac414ef43dcec5
BLAKE2b-256 99c2f7eddbedf3a204a2642d808482b6de31cc3561ae7cf337d1a282b83a607e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.11.5-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ee9e48344cc2a8b5d703aec2c609e30894a466d3603b926e7310f4db4e74b105
MD5 21b0a13faa126b4d911f85050a722386
BLAKE2b-256 b99a86aa088643e03451f24ec588a0e1b642014c27aa10a36222df31ffce9619

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.11.5-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 2cb6fd80a0db36dbfa8e65db8e18594afb3e6778d56081436b4ce9b4ab03e3e4
MD5 33c057f1fd0b54131a48a9c650e00010
BLAKE2b-256 81454107fc0aa5463f31ef45fadb83d91d3f1a8f8963fb792de07287dd2db2f7

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gamrs-0.11.5-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.5-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 f803dc521359d66092a95725bef221c85f3d408e8d7ea99f0c0ee85dd503357e
MD5 f547b510a3f61dada1dcc68abdd06899
BLAKE2b-256 ac1b8c27eba2bdc813151ff0bee040cbcfd632a578e1d3bba72fb45a676b5477

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.11.5-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 0ca767aff0456ad7e83809bc87df58dd9287e536e5d5f28e592947478b17ad5a
MD5 1702feebfa519d3bd52839d44585f798
BLAKE2b-256 b9abb319985051c00c56e9ba8801f7538126fb84834a891033fc8073d2f06565

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.11.5-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 bdc21a793d2e492566eaced9e95f3440f2a83c05d52f9d7e7b37c3150cde5c75
MD5 081b0510224b59197b42a33916031cfc
BLAKE2b-256 84399f8a6107c5cdb4e15f513d34f3abf4dc8981e756e7b1dcced12e2beeee01

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gamrs-0.11.5-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.5-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 502ead71b342e1b3ece2a2b70b72cce3766948ad4965c2aff2a05ab4c2fb36b4
MD5 cc36e38cd9434d1b8e8f62f36d0c8b1a
BLAKE2b-256 c1733e30520c4dba40d340344a6878919ee22bfbc9fa0a14d2a14671d4101268

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.11.5-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 cb970c231179197487708d54f9e23b86a47d47dbf64ca0b3c6ff2888d2df5a0f
MD5 9215f479269a6bfafe98d692bf0f99cc
BLAKE2b-256 382f40eb4c04ad90bd116720db45291f524f6ce3b404b6eba366f4dcad72aa4b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.11.5-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 8116076fd96cf20779bd293c86556b9fd2b617ee321b0cbb3c06498dd673ebc5
MD5 0babbcaeddeb80be1f821cfa72384d8e
BLAKE2b-256 1d6154e1f7dbd5457c278dee195431ab3010b545a2d6cd8e4659b42222aa0666

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gamrs-0.11.5-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.5-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 d7998d5daec34d3206666cad329b6c657ad8e76a6eab13b7c7bec20f935d676a
MD5 ee5ce1eb0b0bcc090773cf02f8b13bc8
BLAKE2b-256 9d4ae683885f0e32c3d560b5aec89bc9c65c0aad10e78136be116c62e2f661c1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.11.5-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 993a045b26305dc33dc687a8cf8db7e77eb7f6d4a0e24d3c79b755c5513e8e72
MD5 c9ba497df6d6888a27af8bcd3c480a32
BLAKE2b-256 4aad96932d6026457b66dc1d0b9de778a3695e66cc316065194016fa34824646

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.11.5-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 e1078320d1009ec1ebc2f954b278924b9b725b4d98570b5dd1e72b6d3f42f9d9
MD5 566eb4780bd63632550f059e1b548335
BLAKE2b-256 26e045e67d8f8d2fbab1253af2343bea3d00ff9197d96fa3ef17f709540ef8e0

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gamrs-0.11.5-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.5-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 7757692192536273980939ac384f0b2221f173b16820d754e07f212c4cff4003
MD5 e284bdf462f02b720c6fd24d780c9db1
BLAKE2b-256 acc6d0eab11e586744e26335a78218e885a3ee5142a4aad26d425c3f9e2637ae

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.11.5-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 5e536354e426a39521e6397dedf59f7d65e5906061ab4b435d3e40e9ed5c089b
MD5 39b77d05f843adfaf419f2c4cdb4a379
BLAKE2b-256 31274163dc0e623a56dba9c0650847e9fa6b8ab2941e78c5f2e31d9db775e0de

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.11.5-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 4e51e3713d8720192c8f5cd4e034eff6e3e88fdfe12e23b0497ce91454cc1bba
MD5 b8bd226673afdd1684b55aba74f2366d
BLAKE2b-256 4206e8893a26dc008ed55d631115da28d78fbb26a720f6fd762ae06221bf7e88

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