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.8). 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 but are experimental: the joint outer optimiser doesn't always pin down the model scale on two or more smooths (converged_=False is common; predicted η correlates with truth but its magnitude can drift). predict_proba for ocat also currently returns the wrong shape — both are tracked for v0.9.

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

Uploaded CPython 3.14Windows x86-64

gamrs-0.8.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.8.0-cp314-cp314-macosx_11_0_arm64.whl (901.2 kB view details)

Uploaded CPython 3.14macOS 11.0+ ARM64

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

Uploaded CPython 3.13Windows x86-64

gamrs-0.8.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.8.0-cp313-cp313-macosx_11_0_arm64.whl (899.2 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

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

Uploaded CPython 3.12Windows x86-64

gamrs-0.8.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.8.0-cp312-cp312-macosx_11_0_arm64.whl (899.2 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

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

Uploaded CPython 3.11Windows x86-64

gamrs-0.8.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.8.0-cp311-cp311-macosx_11_0_arm64.whl (902.1 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

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

Uploaded CPython 3.10Windows x86-64

gamrs-0.8.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.8.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.8.0.tar.gz.

File metadata

  • Download URL: gamrs-0.8.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.8.0.tar.gz
Algorithm Hash digest
SHA256 ef73a4fc57ed6b338eb5209a22227e5cf2125a2a51c2cf470700a2bf3e792167
MD5 8939b7df82f278e019b7f96b2edab10d
BLAKE2b-256 8e50add277dc9c565b835ebaa5e60d3015f763635c5f78c1270bc79c1e290a45

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.8.0-pp311-pypy311_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ca0d98d800b45fd33bcb8e31396ae070d9bc8dffcf969e0e5a5b055bdd77ad0f
MD5 7d5e65ad77c896b1642d8fb442849b45
BLAKE2b-256 d9b4729bd596a8d6c98a631dc6489d3a78f6136e9f34bcad6cade05565a71b17

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gamrs-0.8.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.8.0-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 a1547ae0f9541f44d9aeeffb057557821ae8283e96fd1b4f0abed6c0b01d8b60
MD5 f4a493c1cb5e6659ac5f50c6600681cc
BLAKE2b-256 cdb96bd2bdabd09d286152b920583ca7e6786bee61e5384ee87db64aa1ea5d4b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.8.0-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 8dcea1282bfcc5b9bf3101fd1a22c2531b676a4972ff55fefc098c40386906bd
MD5 3352fe0c6a59050e4c230fda755123cd
BLAKE2b-256 80f66e4b5363af1b42ff91730988caea436b338ad7ba7f4dcafbca059cf26964

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.8.0-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 f206243822f7d87f6f63395d388b028eed192bec1fe7419a04779a1df7fcaac8
MD5 27975cf9ae650549441d038f6b4f2874
BLAKE2b-256 339266963dba9ca1db727a9ad34a6cf766d6e3fa96fe789af11d47b36ee0e73e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gamrs-0.8.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.8.0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 140bf9c14722b8646c2dc6fd2039ed9d5f2836ddb5784e8da4c1dfb514319717
MD5 487156b7aa5c80974aa7b7a25b9863d6
BLAKE2b-256 c526e924a29e5c1dae91c9a7ca9845e866717d77e7b2dbe32fdaeea36fe6b34d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.8.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 81ad86f1cd8cd0fe053995447a34c74539244185853d267e42f1602b5b3db02a
MD5 0b387ebb6de8dec93598f43f0d17eec4
BLAKE2b-256 08d435e6f15e7339fb6f6333acc36cb877db651ce54c4ddf9808df9dca8b22b4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.8.0-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 f56a76946c00e0f3b8329a1e0d8dffc4cbdcbc09d808258fa2c18ce96fd0b645
MD5 6ae7291c0e0f021bbe4aaf753ab345ad
BLAKE2b-256 fe8b0c30c16d84f0c0766a0fd3edb237abcb62e64c33123317f84cece69d4670

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gamrs-0.8.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.8.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 d9b0f02ede6f236aff750ed7e99d3148d935ca42569227e5305cecd9d4ca2c6e
MD5 af061cd2bb47fa59ef34ce4f655d89f4
BLAKE2b-256 ee9e42cb10be0e42ff6649d6f1bedfcacb3fe992ae9858c3491fcd6b0898785d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.8.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 c2a98881523ddc97d43b2b25114c91f9724c0ce485e913f07d7ab4ba9c8b86ad
MD5 33e627f34d0a3da5d37985b97f388a62
BLAKE2b-256 fe26f6c46a062e986da00b09bc3dd52d76ffc7243f9b7abad292bb74156df168

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.8.0-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 bff084f6b666823b35f41af39d36f730d3f68a1dcf8265997dd3c9b6489bb823
MD5 1c91b8bfd647e8414f2618608f292c60
BLAKE2b-256 5a6216012fa8d88c9e6ba96b845b58d333243259a3bfcdd6a2b2bc5369c95e28

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gamrs-0.8.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.8.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 59e1cf4fc6acbef05522cdb77d55a6fcc4df29ce224e490462aa639356c3c723
MD5 2f1afef9c5cd53912a6f67ce105f7cb6
BLAKE2b-256 401513e83e10ddecb1c5c9ccd39ff3ce2673831a6f7065ea9aa4d47fab7030ca

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.8.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 144a0b19bbcf9a64ebc8c5bc9de374653e0ff44268ad12129def8b6a3024747c
MD5 59079a4f19bb87ded7ea55edf335c43f
BLAKE2b-256 af12749cd792ad5abc4767f052fa17b25bbf91cae1c59efc51bec828d1554fc7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.8.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 71adeedbfd7692b3bca6e03e6dbd968a2865452b05688bc7bc1a1fa84ed22d58
MD5 2c3c454d08737358d155b35e23d08b7e
BLAKE2b-256 07039fccdd43c31b6419ed9cd49395bb1f4544ec71adc1e0226a7e52fb870c58

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gamrs-0.8.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.8.0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 657a4e9d2724e3744d033f789ce295350d97288e501bb2c73356e667f9bbffdf
MD5 a36815d6e5501dded0239dd2bfa977d6
BLAKE2b-256 562a64da5e4d24028418d86b2d1565dad69c2c7f50bbadd3dc6f62a651819775

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.8.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ba7d435dac52735bc6dc02b946864b2a970b2bc893dda7761174bc350ad89120
MD5 f4780b9e0e18a5b3daec41ee09d875a1
BLAKE2b-256 ca6aac37dbad44f573157fb76963081f6ba857565178aec638596509c6bed108

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.8.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 7c869b3e3ff597b5a14d9710f68d76e40c489d39e517453ca0bf3b70e9734984
MD5 4f0f3bc6747576313d1bac3d7654905e
BLAKE2b-256 577b6ec37a2378447c82b3d6f35b7e2b826dc5f345d8dbd224901b3f827c36a1

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