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

Uploaded CPython 3.14Windows x86-64

gamrs-0.11.2-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.2-cp314-cp314-macosx_11_0_arm64.whl (951.0 kB view details)

Uploaded CPython 3.14macOS 11.0+ ARM64

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

Uploaded CPython 3.13Windows x86-64

gamrs-0.11.2-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.2-cp313-cp313-macosx_11_0_arm64.whl (948.8 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

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

Uploaded CPython 3.12Windows x86-64

gamrs-0.11.2-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.2-cp312-cp312-macosx_11_0_arm64.whl (948.5 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

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

Uploaded CPython 3.11Windows x86-64

gamrs-0.11.2-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.2-cp311-cp311-macosx_11_0_arm64.whl (953.1 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

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

Uploaded CPython 3.10Windows x86-64

gamrs-0.11.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.2 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

gamrs-0.11.2-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.2.tar.gz.

File metadata

  • Download URL: gamrs-0.11.2.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.2.tar.gz
Algorithm Hash digest
SHA256 d39b10be49f28acca9f23fa5252a1cf73176e6c449dd1e4782ec0b76f857ca71
MD5 848b522c357faf1cc34896739cde66f2
BLAKE2b-256 2184e45057b11eadd7959dd0c6b9516c7a6a2ab3df441d92400e039813de4916

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.11.2-pp311-pypy311_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 4f55b682cd540a261433b25aa7c7ec2e60502135ff617960b99bf0ee3ac129ae
MD5 86e1e467a39da5a01936414c4ec78c30
BLAKE2b-256 83a15509a29ac3637a134b2cc9b786f78f3ecf8862f4a4d4065da9d6838038b6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gamrs-0.11.2-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.2-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 530f9e7ea169eff833f4558062ea387ce1fca873164a617704a0bcb9c26430f7
MD5 2866a8fe2e4392718f7140ef7d12565c
BLAKE2b-256 3cd0aa1aec56ddfa4500d7029090d006b9c70886400b2e8e8ae607f3d5b4de74

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.11.2-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 4f169ecfd47efd51672cfbd5f782656556a774e42c40933a635a443a3c8fbca2
MD5 93bcb2ffa4d14021812daf9625677ba3
BLAKE2b-256 5537fa2bb7f24f3ab469d225095718e7cba5c77c3902d9073ad0d1e214f6f6e5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.11.2-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 dc820ca1c4915c0a4358a4cb6818d81838d89b734e498676e859e14d2170f3f4
MD5 c71232cf03f87c2e4fe2cc7f0e175105
BLAKE2b-256 9cc4fa3395f60df06a1aa038aca259c0051ce1ce6da4fb00e0840c0996123933

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gamrs-0.11.2-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.2-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 6fb56ba19455021f8632754104b231a07c12e989d24a10589f46170ff13851d9
MD5 560de2a0f81073950cd480df9cf7ac64
BLAKE2b-256 7ea736f7c0854ddc76307d85f04adf01d7406ec4ccc0548727878752e0c28cca

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.11.2-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 a9b9e8df114280817254c5d4c07096b39895452f55cbc22cedf584784fec4d37
MD5 39756d905eb8a551b4cf5ea044799d87
BLAKE2b-256 e606e49e962fd26e216d7ae32d4def3be72ba11a388e1486fd0b521e98f794ad

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.11.2-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 bb878316a308f44728876d31a280095d4f6fca694bd0892428cf20fd46864711
MD5 865d75f0bbccdbbc20a0c9e8d2b36684
BLAKE2b-256 18f31e756a58ea7d1706a85b12c46579b16588d6c93bd3b9e91300594c3b7a28

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gamrs-0.11.2-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.2-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 aff803e1721626c18a527f278131d898fa1fca3a69ad9613203fe604fc92abaa
MD5 98eeaf92ecb4619e95d06d1bf6fff339
BLAKE2b-256 72698713793fec48867557518066a991a5f23141876541f26f4ce722552cf416

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.11.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 65edc959a1489dcd5108211f7e9716cfb3b45982dcd3c4c8b5ed91e42f7ab46e
MD5 667f9625924466afee48ead5e9677f95
BLAKE2b-256 d3ca938da098d251f2499593492687633f491c667e8323590251ca9578866955

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.11.2-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b4ffe65ce9bd8b51aca29c47b044177ee8db77d6f3a98051b43d74dcd40edf4c
MD5 5e80f1aca141b691f4fc747dd87f7cbe
BLAKE2b-256 a5f1c6eb868846c01d38d55e59ef9894cc4502206aa320b3fb075f729841698f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gamrs-0.11.2-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.2-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 080798f5b19a5de708e2e8d93c06ead15cc05fb9f4b398f188fbefe719fdee0d
MD5 259f56c717afcb7ccc711b3b9d084b39
BLAKE2b-256 5eebe757297d5f2edee30c366e7065478e84e4107061d76aa7889ddf5dd2adc3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.11.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 7fd3ccc389a644559e171841cbd9618b1c230c81f4164e21a2498b267b7ea0d4
MD5 2cf24f047920cd48ce33ff5e6106e657
BLAKE2b-256 d3e37198366f069ee9d2d74af7ca4d0d0aa71da3af75167eac271c1545220170

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.11.2-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 9ebd1871fd99defc5ad12902869f17a6d822d88cc71a8d81730cac80b081f4f1
MD5 bf71ea797862ca427948807797aad12a
BLAKE2b-256 be071451656173c352621e97bd43f3097e84c3af0fccf43e3e423d2d66237151

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gamrs-0.11.2-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.2-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 c5c91332c33146d7f2bdfd62919c9a20d35a7b4c3db06737ba0301e548257064
MD5 c9c2dbebd6b3723827e4a149ee7c36ff
BLAKE2b-256 f8ff3120edba7b2626880f03ee78173a07f99900c5305ed4816f49c59b73469c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.11.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 1c36c1d4f01817550f4a09f133580380e6055c5a53f0202d8f348482c96e75f1
MD5 3d1448a1486f9ba36eff98711fc4af16
BLAKE2b-256 03bace86a570a29132a8cba1bd56c988ec0c6f4e1131b0773d44e40fdb52d92a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.11.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 05957ec1a31c6a8814be0c834fdd35914edf5841d2eba88d6f2b5da15fb32479
MD5 d4f977df133ec20930835fb18f20828d
BLAKE2b-256 09306eb3429580ec5ebf75843602122e5de2b77f167d23435b4aca0fb3b7df54

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