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.7). 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 for everything except scat/TDist, Ocat, and Quantile/ELF.

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.

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

Uploaded CPython 3.14Windows x86-64

gamrs-0.7.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.7.0-cp314-cp314-macosx_11_0_arm64.whl (894.9 kB view details)

Uploaded CPython 3.14macOS 11.0+ ARM64

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

Uploaded CPython 3.13Windows x86-64

gamrs-0.7.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.7.0-cp313-cp313-macosx_11_0_arm64.whl (892.8 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

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

Uploaded CPython 3.12Windows x86-64

gamrs-0.7.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.7.0-cp312-cp312-macosx_11_0_arm64.whl (892.7 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

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

Uploaded CPython 3.11Windows x86-64

gamrs-0.7.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.7.0-cp311-cp311-macosx_11_0_arm64.whl (896.0 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

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

Uploaded CPython 3.10Windows x86-64

gamrs-0.7.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.7.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.7.0.tar.gz.

File metadata

  • Download URL: gamrs-0.7.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.7.0.tar.gz
Algorithm Hash digest
SHA256 b64ce190af273257f56e9f735ce24ea5e05bf2bfb905d5b219887ce15bff04a4
MD5 95e05ef7830fb273102ae824473f9045
BLAKE2b-256 5ed865087f101200b1fc4144a66fa185060163299f67de5e6b21d889f6a57cac

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.7.0-pp311-pypy311_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ca91d58106233ca0e4aedc9e8ff333e2db902d3d582d3ea82c822bc4e2db9085
MD5 1cdcc7fe33aa4208648f705849d2dda4
BLAKE2b-256 384f1a00a8964321c2ae2709bd0d7f5c137dcadf5895bb6864084cb2e974d5e8

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gamrs-0.7.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.7.0-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 36366aa04b6db5f1747a61f4de918deaa948b6a54257f5186a636cf292a75722
MD5 046e449150c7cf64c0fcd0aa44afa8eb
BLAKE2b-256 ef5eb38b798747953a03efcdca0f80a6fef20a2e8ef25f3bf688603adbafe329

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.7.0-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 d8b6991969a20b1c9b5383bd10f5f4e6603f747b20faf0762b53a1535f9286ec
MD5 52b295ab391eed22d73b2e01dcc959d6
BLAKE2b-256 cb2ce022791fc62dece27f5d8a9e395db1fcd95e855b555634d5ac0d194ce030

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.7.0-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 1023870e88f06706bb8d6e7c7f0b5b72292ac15654402d49ff42e68152abf335
MD5 5de08d951f26d0b0e00f8134de1ae8dd
BLAKE2b-256 4900901c502c92aef143a1822dbfc4c62f47c64f8c80aa650020d62fec643f7d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gamrs-0.7.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.7.0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 cb41abaefa9e4b97bc1d603a721717624ea7bbe39c3ffe36dcbb3ab6b79b5543
MD5 568616c3a34792d9edb81285f1f92523
BLAKE2b-256 b08ba5e4fc156ee4f4c919bf00b07a6a2e0c91671b32318909051171655261f1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.7.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 f1ee1985458b98475b463f7a6225292477efb3e4ff74aff115e40843fffc2bf4
MD5 ba5acec6234b563eb70084d70c6f5e5c
BLAKE2b-256 e14ddb94e6de52a13659ff2515b222a8419d2b792140b4714d2eac41ffdd604a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.7.0-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b4b56f5107f24ba0536839be5382fdccec92c7c2c0ceebbfdc2a1d0fb54f58f2
MD5 2b24fe22f7a03f1413a4b08fc7069401
BLAKE2b-256 5f19688b9225567822693caf0d1fa23bf19052be2923c327b4fa83c88b2deafe

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gamrs-0.7.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.7.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 9de63b37fed19e9ec52a44289aa56983f9229b8cb3d0c9dcf9fe12dff0214ae5
MD5 fe1d7bf5e66c85ef6d8feb7c767d7674
BLAKE2b-256 c6d98d9d9327523c8018af0748ed006581c83edcbb7e5ca675e016da6ec8302d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.7.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 0caed4311168c57aca1cb90a7389ca8361d9038ca0f677b4c9ca8ded5d7c67f8
MD5 0cbfd75a2a103711208a6f48c947dcb7
BLAKE2b-256 29616adb2aad1478eaf6c94909b1cb55858d82292a47ec8f8cf80c48ca51177f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.7.0-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 3cadbcbdeaefb9763ba02ba1654d050d583166ecb8149acae550de0dba733ebf
MD5 e194902c8aa212bc4b687f039b3c4ce3
BLAKE2b-256 23e060eea3dfcaeaf01de9de9ddf24295f6f4a0d828a6c03231c4b1b8fbca8fe

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gamrs-0.7.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.7.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 048015ddf7b7b0dd31efac1b2edc34004ff175de8608c4e6a10065504f872437
MD5 61a9728ec745f4cd4b93a75f2a5df6da
BLAKE2b-256 d3b04a41e3fcf8d4469c9805bad57cf3a6233ba3c8cc28b60cecc598c24e5621

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.7.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 1e615ba20ba3be43b63d668b0b468c46e26085bf2b57728d66712948d4d7abfb
MD5 ceb1a6e1361f47872effc7dbd6e1349b
BLAKE2b-256 4f8c1ff40792c6cc46439859d35fd8c8e7beea21fdb5eb1971d754422d0837c8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.7.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 10e8c1dc00c64ff021ee4f1a1eefdd8bf5422227999787ded575bc3e42ead72f
MD5 f48b956f4fe2e0a0512203dcb74565dd
BLAKE2b-256 b067ce85fec64fcb20466af11415fbbb15a370487b22084904d4d3b2bf97ed9b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gamrs-0.7.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.7.0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 ed55c82e6ecec497d482cb2f50c5cf8c937f3c8dbc92d0844fada4abde15d15c
MD5 7f3c526fbbdca4421efce307cde47018
BLAKE2b-256 bedd97b6875e33c59b58f832e988f095ff917752e4b942dbecbee9edfe4cf08a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.7.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 09adb0a75189b9f36c0fec5f4ee21eeec703c1fba32cce6d4ab7b1bfc7d23fd7
MD5 6b9f2d3e7fdfb693f474b8cc2c86a861
BLAKE2b-256 029d62d4efced032be420ac0fd1dd1d3b14f0bc08ea8ad646e3d870fc5f4a69f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.7.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 237ca064e8e760b3eff6f52c4a0348596868fc6ac8c40167c94f4f2a4de78d5e
MD5 4c23b6a4c3798966f7bf282cd7ca49b8
BLAKE2b-256 27301c6be2dc2c43d05859bbd9325baf3cc7cbf6ffcf53243f93f05a7661aeae

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