Skip to main content

GAM core (Rust) with sklearn-style Python bindings — alpha

Project description

gamrs

Generalised Additive Models in Rust — a clean-room reimplementation built on six composable trait layers (Basis, BasisTransform, Loss/Link/VarianceFn, InnerSolver, ScoreDerivatives, OuterSolver). Designed for parity with R's mgcv and the sibling mgcv_rust crate.

Status: alpha. Multi-smooth additive (y ~ s(x0) + s(x1)) and tensor products (te(x0, x1)) both ship in this release — the latter leap-frogs mgcv_rust. Shape-aware families (scat, NegBin, Tweedie, Ocat, ELF) are still single-smooth-only; production users with multi-smooth shape-aware models should stay on mgcv-rust for now.

What's in this alpha

Families (all 1-D parity, 10 families)

Family Link Inner solver Outer Newton 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 ~2e-1
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

Bases

  • Cr — cubic regression splines (default for 1-D smooths)
  • Re — random effects (bs="re")
  • Tensor<A, B> — anisotropic tensor product (te(x0, x1))

Smooth strategies

  • Single 1-D smoothy ~ s(x0)
  • Additive multi-smoothy ~ s(x0) + s(x1) + s(x2) (parity with mgcv_rust on Gaussian/Bernoulli/Poisson/Gamma/InvGauss/QuasiPoisson/QuasiBinomial)
  • Tensor producty ~ te(x0, x1) (leap-frogs mgcv_rust; v0.x doesn't have this)

Python API

PyO3 bindings + numpy. sklearn-like with vcov, predict_ci, predict_diff, serialize/deserialize, GamPredictor for inference-only deployment.

Not in this alpha (follow-ups)

  • Multi-smooth shape-aware families — scat/TDist, NegBin, Tweedie, Ocat, ELF/quantile gated at single-smooth via runtime error. Lifting θ packing from [ρ, shape…] to [ρ_0, …, ρ_{T-1}, shape…] is tracked.
  • s(x0, x1) TPRS — isotropic thin-plate; separate basis kind from te().
  • 3+ margin tensor products te(x0, x1, x2) — mechanical generalization.
  • ti(x0, x1) centred tensor interaction — needs main-effect orthogonalisation.

Use (Rust)

use gamrs::{TermSpec, MarginKind, DesignStrategy};
use ndarray::Array2;

let x: Array2<f64> = /* (n, n_input_dims) */;
let y = /* Array1<f64> */;

// Single 1-D smooth
let fit = gamrs::fit(gamrs::family::gaussian_identity(), x.view(), y.view(), None, 10)?;

// Multi-smooth additive
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,
)?;

// Tensor product
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())?;

Use (Python)

from gamrs import Gam, CrTerm, ReTerm, TeTerm

# Additive multi-smooth
g = Gam(terms=[CrTerm("x0", k=10), CrTerm("x1", k=15)])
g.fit(df, "y")
mu = g.predict(df)

# Tensor product
g = Gam(terms=[TeTerm(cols=("x0", "x1"), k=(5, 5))])
g.fit(df, "y")

Architecture

See architecture-assumptions.md in the repo root and the v2 plan note in ~/ObsidianVault/Projects/mgcv_rust/plans/mgcv_rust - v2 Architecture Plan 2026-05-22.md.

The 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 (ρ-dim generic)
Layer 6   FittedGam          ←  predict, predict_ci, predict_diff, vcov, serialize

Versioning

0.1.0-alpha.x indicates pre-stable. Breaking changes are expected on every minor bump until shape-aware multi-smooth lands.

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.1.0a6.tar.gz (831.3 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

gamrs-0.1.0a6-pp311-pypy311_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (11.2 MB view details)

Uploaded PyPymanylinux: glibc 2.17+ x86-64

gamrs-0.1.0a6-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (11.2 MB view details)

Uploaded CPython 3.14manylinux: glibc 2.17+ x86-64

gamrs-0.1.0a6-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (11.2 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

gamrs-0.1.0a6-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (11.2 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

gamrs-0.1.0a6-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (11.2 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

gamrs-0.1.0a6-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (11.2 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

gamrs-0.1.0a6-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (11.2 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64

File details

Details for the file gamrs-0.1.0a6.tar.gz.

File metadata

  • Download URL: gamrs-0.1.0a6.tar.gz
  • Upload date:
  • Size: 831.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.13.3

File hashes

Hashes for gamrs-0.1.0a6.tar.gz
Algorithm Hash digest
SHA256 eb103cb1622d0cd561635929e2e9eae400a514da43054ec30b6ba8d27f66ddf5
MD5 4b27c119b46f0c1236364f839cbbb2b4
BLAKE2b-256 77f8b47de5b8b11fb4704937797f19e60b852edab6e3702642d867cab4b7c739

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gamrs-0.1.0a6-pp311-pypy311_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 64b7a478369ba2e94ef8d8843a091523f6e11c47349b3f3e12454ecd0774b392
MD5 c4c6023bcbed93ed313a4a3c71d43943
BLAKE2b-256 bbc72165b24b3f0948f057cef5964283366013066b42929c18a40fdff256d11b

See more details on using hashes here.

File details

Details for the file gamrs-0.1.0a6-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for gamrs-0.1.0a6-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 083a36d6f839e6b8bfe7e35aaf622e27159254f81acfe047e440db0f83e61ca3
MD5 82b9fd2dae7dd7804a626a941a3c3e78
BLAKE2b-256 10d05e98be26ad02224b6b0fd0bcb6406e17e9ff0f4231f275bb6defcbcb1345

See more details on using hashes here.

File details

Details for the file gamrs-0.1.0a6-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for gamrs-0.1.0a6-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 8a9f53f4af01ee50e1c6c025844bf6c5ed5cdfd32163a8aac3a2aefa95c2ddeb
MD5 1e749d14f7cc334ff8fbf32367699758
BLAKE2b-256 a8afece31ba57149c5635c586c8ff5da0026d4b533dbc320f51ec603e2775ff8

See more details on using hashes here.

File details

Details for the file gamrs-0.1.0a6-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for gamrs-0.1.0a6-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 45210d5384e910a4a9dd31789d52469a26a42de6a0e1374c0e2a353de6dc5d36
MD5 cfbf357d6224e3f3435e37929af67c58
BLAKE2b-256 5bc5b92a23b12577648df4b4a600731243136a59203f4a6140aa4c2de9734c5b

See more details on using hashes here.

File details

Details for the file gamrs-0.1.0a6-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for gamrs-0.1.0a6-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 81e0ade33858286b0b36ea3e0e16b5e654a89eed8c263d28c12d3135cccbb49c
MD5 f88022b699b5a221305f629cdddf1565
BLAKE2b-256 e3e925eb22649aac89ed0f53361b58aa64153d3f6ba73c074d06787a77aef30e

See more details on using hashes here.

File details

Details for the file gamrs-0.1.0a6-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for gamrs-0.1.0a6-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 5bbcc630555c66aef55ea5a36abf557c8999ef9be5126add5660354b5c902f69
MD5 34d3ddcce8f173a33a544208b4ee912b
BLAKE2b-256 7f3bc3c1219a1804f777553f4c1b2c57c69485646e6887e27e8295b48957e2bf

See more details on using hashes here.

File details

Details for the file gamrs-0.1.0a6-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for gamrs-0.1.0a6-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 a26adf544cbe585c8ce4adf10a1edbd0e508091da8fa5551008ab1644eec0109
MD5 a91f336b2a7f7b3570ad5a85a2df0be6
BLAKE2b-256 4d551f0432d02acabd8cf782a40e92f68e03a6bbe435336a63dc4bf23bb4facc

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