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 (k=20), best-of-3 wall time. Numbers >1× mean gamrs is faster. See scripts/bench_large_n.py.

family n=10K n=100K n=1M
Gaussian 0.98× 1.46× 1.62×
Poisson 1.50× 1.20× 1.27×
Bernoulli 1.06× 1.42× 1.49×

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 already 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.10.0.tar.gz (3.6 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.10.0-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.10.0-cp314-cp314-win_amd64.whl (3.6 MB view details)

Uploaded CPython 3.14Windows x86-64

gamrs-0.10.0-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.10.0-cp314-cp314-macosx_11_0_arm64.whl (910.2 kB view details)

Uploaded CPython 3.14macOS 11.0+ ARM64

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

Uploaded CPython 3.13Windows x86-64

gamrs-0.10.0-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.10.0-cp313-cp313-macosx_11_0_arm64.whl (907.6 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

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

Uploaded CPython 3.12Windows x86-64

gamrs-0.10.0-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.10.0-cp312-cp312-macosx_11_0_arm64.whl (907.7 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

gamrs-0.10.0-cp311-cp311-win_amd64.whl (3.6 MB view details)

Uploaded CPython 3.11Windows x86-64

gamrs-0.10.0-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.10.0-cp311-cp311-macosx_11_0_arm64.whl (911.8 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

gamrs-0.10.0-cp310-cp310-win_amd64.whl (3.6 MB view details)

Uploaded CPython 3.10Windows x86-64

gamrs-0.10.0-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.10.0-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.10.0.tar.gz.

File metadata

  • Download URL: gamrs-0.10.0.tar.gz
  • Upload date:
  • Size: 3.6 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.13.3

File hashes

Hashes for gamrs-0.10.0.tar.gz
Algorithm Hash digest
SHA256 a1f241d6daac6d8f7ef18167a41477fc777bdaf3317a41d005423c005d65e14f
MD5 9ad68b47c74a1974020d1d30d693ac6f
BLAKE2b-256 7a0d746b81e3eca329a38a9d6e57c3896f9fb6d1c5b0babf39f1aa28aa47a1e4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.10.0-pp311-pypy311_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 6430db316d1f4807db5694f237c202e92bd19e994ab9461ca4fd71885d3a60aa
MD5 3ab71de91119e8da7e02c8fe0b239f17
BLAKE2b-256 21369ed2732a8d460c595cda89e95d22b69a8f67a88a349feae9fa89c0b981db

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gamrs-0.10.0-cp314-cp314-win_amd64.whl
  • Upload date:
  • Size: 3.6 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.10.0-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 310f6b6a3492f0db3bb73a9633432132dcf949f8522f06ccfbcbd866ab96cc47
MD5 7cf8b97a4986d46a5b526db3d921099f
BLAKE2b-256 69ddd4e0bf1abfbc7637357b89275e07e81df529b8f171ebbb25cb69690b421b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.10.0-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 0cdd3ff485c1fab30201176ede1e1b591f6ff6258fce6c85a1f6d5f9967603e9
MD5 151354ce410aaff1eb569622a8461c05
BLAKE2b-256 04c6579558730d6fdacb93b96446dbd70e2f295c2775fac4e90fbb8c42584fff

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.10.0-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 75596c189abdd12f252bab547059767ea7704ead443c1fa531ea060b31a10cc0
MD5 7d81b1ca9d3fd75716c06adfb5b3ae52
BLAKE2b-256 33b0a5758911a8dbc87a767df45de22f1fe5d0d00074ac69a54495ac7db2efbe

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gamrs-0.10.0-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.10.0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 2330195ef6a893cb77e73912e9f82c3606a236b79fc9621e63c407dd0c70193b
MD5 9b36e58f83b53f589a989089a3559ecc
BLAKE2b-256 9e0727a78aca33b03660bdf58bd1e1248834d9bd201aa54c11d7e3b6a72df450

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.10.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 24384c29227eb660ee94279d3d85a2ab7f6bdfc525532b52af204ae16d79dd90
MD5 145d6681cd56bc9a08adfcad452fbd4c
BLAKE2b-256 95876b315b57af367a4658ce0f0c6d5794111f8037fdc4bedc982a44c87cf0eb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.10.0-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 371a00760d9fc420dc74cfc6b90dd6e977b6ee6bfd20fae0f94e54b524276ad0
MD5 684f420495651f9b38bb2634583b3a13
BLAKE2b-256 8a4c04cc206669881201500b9d646f7d35f0aa652c4a8e047c6daadf954d4489

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gamrs-0.10.0-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.10.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 a857414aa251c6e095a8a4905f21d9af4ff306d5fb9ddd5bd15bd87405f8c23e
MD5 431e3e7ff80b82cce61baf5f25b13fbb
BLAKE2b-256 4e1edf3e3519f9abb6950b894375b086a1fbf1f0bb614a67bb4c86354b155c4d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.10.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 86110f6cce42af7e08ecd05e523ed330f211b52d69fb94238455b456d3c3e07e
MD5 60e08c0470b418ec7bce839102c9dbff
BLAKE2b-256 bd9ff73360d9f200467a73f98b8318b170affd388bd748b7ae180a6296c0ab83

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.10.0-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 498194629373e4fa103953731f380a21b6f9953c74e2261c5c5f3cd5dcad1f14
MD5 0faafa86ef24023dc411c2975d5d918c
BLAKE2b-256 0de744068f72acde2242946e7d6b7885b211f3a83f143ae97aa10a2e0ede9460

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gamrs-0.10.0-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 3.6 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.10.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 ef6404bab9e26ab09dbe0b512037a86ae92e41ac0ca7fd75770201dc39d5aeb7
MD5 b24dac564fdf43cc864286ffb9d932ea
BLAKE2b-256 79b6927f63de95e2c1055514651f5e51b0baa6f208eab666b0fcd7605596d028

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.10.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 17eeb68f7b64a4247cea1c18f706b51809db597ab4a09100941e9084ad61e462
MD5 0398bff148ff22f4037fe2d5cbe4bae5
BLAKE2b-256 f3abd748237f486e8d7d2f5945e464ea9ce95da93b63fe58fe3cc3f4af411b66

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.10.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 0c9a6ccb61f27df8bc1aa51051b8a416038c3d50e380cff7e5816f194eea61fa
MD5 aaada550c699a1c88202239b5e629894
BLAKE2b-256 706bcfd5cbccac53271343d762a8dabe3b679441c5cb1170e9ccf7a99ac39e1e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gamrs-0.10.0-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 3.6 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.10.0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 4d32591f3ab32400de445a97011a0bfc48f4d57d47e082c0c04d1ef8ab87cd30
MD5 fe9586be4abbb8df1a55ae5229897064
BLAKE2b-256 4b6fc2b1091c432efbc74eb55a19e8cae364e32cec8862735ebde8e13d1978da

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.10.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 96e9dbd0706859c67a047dab1293237f99d5442bb5accbcc43ce9c51cfc15a42
MD5 c99fbba76e9afda2eaca234507320624
BLAKE2b-256 da4ba8512dff23e73ff333fd170b43d82c5126409592e9928af5098d5781b0dc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.10.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 10dd42fb6f7e8ee73d12b10f6b5e6df3a73d1e37dc1137a76eb0cb0951ea1e3e
MD5 5520bae452234ae9301629bf6429f055
BLAKE2b-256 d01195ccd2720c6a3e0adece6fa366a3d19ed5733448ee9b0bf5a42f93fdb1cf

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